File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1427: download - view: text, annotated - select for diffs
Tue Oct 6 17:26:15 2020 UTC (3 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6518.
  More comprehensive access to symb for a requested URL to determine when
  access should be blocked and why, if content blocking in use.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1427 2020/10/06 17:26:15 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 LONCAPA qw(:DEFAULT :match);
  100: use LONCAPA::Configuration;
  101: use LONCAPA::lonmetadata;
  102: use LONCAPA::Lond;
  103: use LONCAPA::LWPReq;
  104: use LONCAPA::transliterate;
  105: 
  106: use File::Copy;
  107: 
  108: my $readit;
  109: my $max_connection_retries = 20;     # Or some such value.
  110: 
  111: require Exporter;
  112: 
  113: our @ISA = qw (Exporter);
  114: our @EXPORT = qw(%env);
  115: 
  116: 
  117: # ------------------------------------ Logging (parameters, docs, slots, roles)
  118: {
  119:     my $logid;
  120:     sub write_log {
  121: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  122:         if ($context eq 'course') {
  123:             if (($cnum eq '') || ($cdom eq '')) {
  124:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  125:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  126:             }
  127:         }
  128: 	$logid ++;
  129:         my $now = time();
  130: 	my $id=$now.'00000'.$$.'00000'.$logid;
  131:         my $logentry = { 
  132:                           $id => {
  133:                                    'exe_uname' => $env{'user.name'},
  134:                                    'exe_udom'  => $env{'user.domain'},
  135:                                    'exe_time'  => $now,
  136:                                    'exe_ip'    => $ENV{'REMOTE_ADDR'},
  137:                                    'delflag'   => $delflag,
  138:                                    'logentry'  => $storehash,
  139:                                    'uname'     => $uname,
  140:                                    'udom'      => $udom,
  141:                                   }
  142:                        };
  143: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  144:     }
  145: }
  146: 
  147: sub logtouch {
  148:     my $execdir=$perlvar{'lonDaemons'};
  149:     unless (-e "$execdir/logs/lonnet.log") {	
  150: 	open(my $fh,">>","$execdir/logs/lonnet.log");
  151: 	close $fh;
  152:     }
  153:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  154:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  155: }
  156: 
  157: sub logthis {
  158:     my $message=shift;
  159:     my $execdir=$perlvar{'lonDaemons'};
  160:     my $now=time;
  161:     my $local=localtime($now);
  162:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
  163: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  164: 	print $fh $logstring;
  165: 	close($fh);
  166:     }
  167:     return 1;
  168: }
  169: 
  170: sub logperm {
  171:     my $message=shift;
  172:     my $execdir=$perlvar{'lonDaemons'};
  173:     my $now=time;
  174:     my $local=localtime($now);
  175:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
  176: 	print $fh "$now:$message:$local\n";
  177: 	close($fh);
  178:     }
  179:     return 1;
  180: }
  181: 
  182: sub create_connection {
  183:     my ($hostname,$lonid) = @_;
  184:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  185: 				     Type    => SOCK_STREAM,
  186: 				     Timeout => 10);
  187:     return 0 if (!$client);
  188:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
  189:     my $result = <$client>;
  190:     chomp($result);
  191:     return 1 if ($result eq 'done');
  192:     return 0;
  193: }
  194: 
  195: sub get_server_timezone {
  196:     my ($cnum,$cdom) = @_;
  197:     my $home=&homeserver($cnum,$cdom);
  198:     if ($home ne 'no_host') {
  199:         my $cachetime = 24*3600;
  200:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  201:         if (defined($cached)) {
  202:             return $timezone;
  203:         } else {
  204:             my $timezone = &reply('servertimezone',$home);
  205:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  206:         }
  207:     }
  208: }
  209: 
  210: sub get_server_distarch {
  211:     my ($lonhost,$ignore_cache) = @_;
  212:     if (defined($lonhost)) {
  213:         if (!defined(&hostname($lonhost))) {
  214:             return;
  215:         }
  216:         my $cachetime = 12*3600;
  217:         if (!$ignore_cache) {
  218:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  219:             if (defined($cached)) {
  220:                 return $distarch;
  221:             }
  222:         }
  223:         my $rep = &reply('serverdistarch',$lonhost);
  224:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  225:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  226:                 $rep eq '') {
  227:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  228:         }
  229:     }
  230:     return;
  231: }
  232: 
  233: sub get_servercerts_info {
  234:     my ($lonhost,$hostname,$context) = @_;
  235:     return if ($lonhost eq '');
  236:     if ($hostname eq '') {
  237:         $hostname = &hostname($lonhost);
  238:     }
  239:     return if ($hostname eq '');
  240:     my ($rep,$uselocal);
  241:     if ($context eq 'install') {
  242:         $uselocal = 1;
  243:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  244:         $uselocal = 1;
  245:     }
  246:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  247:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  248:         if ($distro eq '') {
  249:             $uselocal = 0;
  250:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  251:             if ($1 < 6) {
  252:                 $uselocal = 0;
  253:             }
  254:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  255:             if ($1 < 12) {
  256:                 $uselocal = 0;
  257:             }
  258:         }
  259:     }
  260:     if ($uselocal) {
  261:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  262:     } else {
  263:         $rep=&reply('servercerts',$lonhost);
  264:     }
  265:     my ($result,%returnhash);
  266:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  267:         ($rep eq 'unknown_cmd')) {
  268:         $result = $rep;
  269:     } else {
  270:         $result = 'ok';
  271:         my @pairs=split(/\&/,$rep);
  272:         foreach my $item (@pairs) {
  273:             my ($key,$value)=split(/=/,$item,2);
  274:             my $what = &unescape($key);
  275:             $returnhash{$what}=&thaw_unescape($value);
  276:         }
  277:     }
  278:     return ($result,\%returnhash);
  279: }
  280: 
  281: sub get_server_loncaparev {
  282:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  283:     if (defined($lonhost)) {
  284:         if (!defined(&hostname($lonhost))) {
  285:             undef($lonhost);
  286:         }
  287:     }
  288:     if (!defined($lonhost)) {
  289:         if (defined(&domain($dom,'primary'))) {
  290:             $lonhost=&domain($dom,'primary');
  291:             if ($lonhost eq 'no_host') {
  292:                 undef($lonhost);
  293:             }
  294:         }
  295:     }
  296:     if (defined($lonhost)) {
  297:         my $cachetime = 12*3600;
  298:         if (!$ignore_cache) {
  299:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  300:             if (defined($cached)) {
  301:                 return $loncaparev;
  302:             }
  303:         }
  304:         my ($answer,$loncaparev);
  305:         my @ids=&current_machine_ids();
  306:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  307:             $answer = $perlvar{'lonVersion'};
  308:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  309:                 $loncaparev = $1;
  310:             }
  311:         } else {
  312:             $answer = &reply('serverloncaparev',$lonhost);
  313:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  314:                 if ($caller eq 'loncron') {
  315:                     my $hostname = &hostname($lonhost);
  316:                     my $protocol = $protocol{$lonhost};
  317:                     $protocol = 'http' if ($protocol ne 'https');
  318:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  319:                     my $request=new HTTP::Request('GET',$url);
  320:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  321:                     unless ($response->is_error()) {
  322:                         my $content = $response->content;
  323:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  324:                             $loncaparev = $1;
  325:                         }
  326:                     }
  327:                 } else {
  328:                     $loncaparev = $loncaparevs{$lonhost};
  329:                 }
  330:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  331:                 $loncaparev = $1;
  332:             }
  333:         }
  334:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  335:     }
  336: }
  337: 
  338: sub get_server_homeID {
  339:     my ($hostname,$ignore_cache,$caller) = @_;
  340:     unless ($ignore_cache) {
  341:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  342:         if (defined($cached)) {
  343:             return $serverhomeID;
  344:         }
  345:     }
  346:     my $cachetime = 12*3600;
  347:     my $serverhomeID;
  348:     if ($caller eq 'loncron') { 
  349:         my @machine_ids = &machine_ids($hostname);
  350:         foreach my $id (@machine_ids) {
  351:             my $response = &reply('serverhomeID',$id);
  352:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  353:                 $serverhomeID = $response;
  354:                 last;
  355:             }
  356:         }
  357:         if ($serverhomeID eq '') {
  358:             $serverhomeID = $machine_ids[-1];
  359:         }
  360:     } else {
  361:         $serverhomeID = $serverhomeIDs{$hostname};
  362:     }
  363:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  364: }
  365: 
  366: sub get_remote_globals {
  367:     my ($lonhost,$whathash,$ignore_cache) = @_;
  368:     my ($result,%returnhash,%whatneeded);
  369:     if (ref($whathash) eq 'HASH') {
  370:         foreach my $what (sort(keys(%{$whathash}))) {
  371:             my $hashid = $lonhost.'-'.$what;
  372:             my ($response,$cached);
  373:             unless ($ignore_cache) {
  374:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  375:             }
  376:             if (defined($cached)) {
  377:                 $returnhash{$what} = $response;
  378:             } else {
  379:                 $whatneeded{$what} = 1;
  380:             }
  381:         }
  382:         if (keys(%whatneeded) == 0) {
  383:             $result = 'ok';
  384:         } else {
  385:             my $requested = &freeze_escape(\%whatneeded);
  386:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  387:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  388:                 ($rep eq 'unknown_cmd')) {
  389:                 $result = $rep;
  390:             } else {
  391:                 $result = 'ok';
  392:                 my @pairs=split(/\&/,$rep);
  393:                 foreach my $item (@pairs) {
  394:                     my ($key,$value)=split(/=/,$item,2);
  395:                     my $what = &unescape($key);
  396:                     my $hashid = $lonhost.'-'.$what;
  397:                     $returnhash{$what}=&thaw_unescape($value);
  398:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  399:                 }
  400:             }
  401:         }
  402:     }
  403:     return ($result,\%returnhash);
  404: }
  405: 
  406: sub remote_devalidate_cache {
  407:     my ($lonhost,$cachekeys) = @_;
  408:     my $items;
  409:     return unless (ref($cachekeys) eq 'ARRAY');
  410:     my $cachestr = join('&',@{$cachekeys});
  411:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  412:     return $response;
  413: }
  414: 
  415: # -------------------------------------------------- Non-critical communication
  416: sub subreply {
  417:     my ($cmd,$server)=@_;
  418:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  419:     #
  420:     #  With loncnew process trimming, there's a timing hole between lonc server
  421:     #  process exit and the master server picking up the listen on the AF_UNIX
  422:     #  socket.  In that time interval, a lock file will exist:
  423: 
  424:     my $lockfile=$peerfile.".lock";
  425:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  426: 	sleep(0.1);
  427:     }
  428:     # At this point, either a loncnew parent is listening or an old lonc
  429:     # or loncnew child is listening so we can connect or everything's dead.
  430:     #
  431:     #   We'll give the connection a few tries before abandoning it.  If
  432:     #   connection is not possible, we'll con_lost back to the client.
  433:     #   
  434:     my $client;
  435:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  436: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  437: 				      Type    => SOCK_STREAM,
  438: 				      Timeout => 10);
  439: 	if ($client) {
  440: 	    last;		# Connected!
  441: 	} else {
  442: 	    &create_connection(&hostname($server),$server);
  443: 	}
  444:         sleep(0.1);	# Try again later if failed connection.
  445:     }
  446:     my $answer;
  447:     if ($client) {
  448: 	print $client "sethost:$server:$cmd\n";
  449: 	$answer=<$client>;
  450: 	if (!$answer) { $answer="con_lost"; }
  451: 	chomp($answer);
  452:     } else {
  453: 	$answer = 'con_lost';	# Failed connection.
  454:     }
  455:     return $answer;
  456: }
  457: 
  458: sub reply {
  459:     my ($cmd,$server)=@_;
  460:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  461:     my $answer=subreply($cmd,$server);
  462:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  463:         my $logged = $cmd;
  464:         if ($cmd =~ /^encrypt:([^:]+):/) {
  465:             my $subcmd = $1;
  466:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  467:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  468:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
  469:                 (undef,undef,my @rest) = split(/:/,$cmd);
  470:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  471:                     splice(@rest,2,1,'Hidden');
  472:                 } elsif ($subcmd eq 'passwd') {
  473:                     splice(@rest,2,2,('Hidden','Hidden'));
  474:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  475:                          ($subcmd eq 'autoexportgrades')) {
  476:                     splice(@rest,3,1,'Hidden');
  477:                 }
  478:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  479:             }
  480:         }
  481:         &logthis("<font color=\"blue\">WARNING:".
  482:                  " $logged to $server returned $answer</font>");
  483:     }
  484:     return $answer;
  485: }
  486: 
  487: # ----------------------------------------------------------- Send USR1 to lonc
  488: 
  489: sub reconlonc {
  490:     my ($lonid) = @_;
  491:     if ($lonid) {
  492:         my $hostname = &hostname($lonid);
  493: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  494: 	if ($hostname && -e $peerfile) {
  495: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  496: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  497: 					     Type    => SOCK_STREAM,
  498: 					     Timeout => 10);
  499: 	    if ($client) {
  500: 		print $client ("reset_retries\n");
  501: 		my $answer=<$client>;
  502: 		#reset just this one.
  503: 	    }
  504: 	}
  505: 	return;
  506:     }
  507: 
  508:     &logthis("Trying to reconnect lonc");
  509:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  510:     if (open(my $fh,"<",$loncfile)) {
  511: 	my $loncpid=<$fh>;
  512:         chomp($loncpid);
  513:         if (kill 0 => $loncpid) {
  514: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  515:             kill USR1 => $loncpid;
  516:             sleep 1;
  517:         } else {
  518: 	    &logthis(
  519:                "<font color=\"blue\">WARNING:".
  520:                " lonc at pid $loncpid not responding, giving up</font>");
  521:         }
  522:     } else {
  523: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  524:     }
  525: }
  526: 
  527: # ------------------------------------------------------ Critical communication
  528: 
  529: sub critical {
  530:     my ($cmd,$server)=@_;
  531:     unless (&hostname($server)) {
  532:         &logthis("<font color=\"blue\">WARNING:".
  533:                " Critical message to unknown server ($server)</font>");
  534:         return 'no_such_host';
  535:     }
  536:     my $answer=reply($cmd,$server);
  537:     if ($answer eq 'con_lost') {
  538: 	&reconlonc($server);
  539: 	my $answer=reply($cmd,$server);
  540:         if ($answer eq 'con_lost') {
  541:             my $now=time;
  542:             my $middlename=$cmd;
  543:             $middlename=substr($middlename,0,16);
  544:             $middlename=~s/\W//g;
  545:             my $dfilename=
  546:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  547:             $dumpcount++;
  548:             {
  549: 		my $dfh;
  550: 		if (open($dfh,">",$dfilename)) {
  551: 		    print $dfh "$cmd\n"; 
  552: 		    close($dfh);
  553: 		}
  554:             }
  555:             sleep 1;
  556:             my $wcmd='';
  557:             {
  558: 		my $dfh;
  559: 		if (open($dfh,"<",$dfilename)) {
  560: 		    $wcmd=<$dfh>; 
  561: 		    close($dfh);
  562: 		}
  563:             }
  564:             chomp($wcmd);
  565:             if ($wcmd eq $cmd) {
  566: 		&logthis("<font color=\"blue\">WARNING: ".
  567:                          "Connection buffer $dfilename: $cmd</font>");
  568:                 &logperm("D:$server:$cmd");
  569: 	        return 'con_delayed';
  570:             } else {
  571:                 &logthis("<font color=\"red\">CRITICAL:"
  572:                         ." Critical connection failed: $server $cmd</font>");
  573:                 &logperm("F:$server:$cmd");
  574:                 return 'con_failed';
  575:             }
  576:         }
  577:     }
  578:     return $answer;
  579: }
  580: 
  581: # ------------------------------------------- check if return value is an error
  582: 
  583: sub error {
  584:     my ($result) = @_;
  585:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  586: 	if ($2 == 2) { return undef; }
  587: 	return $1;
  588:     }
  589:     return undef;
  590: }
  591: 
  592: sub convert_and_load_session_env {
  593:     my ($lonidsdir,$handle)=@_;
  594:     my @profile;
  595:     {
  596: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  597: 	if (!$opened) {
  598: 	    return 0;
  599: 	}
  600: 	flock($idf,LOCK_SH);
  601: 	@profile=<$idf>;
  602: 	close($idf);
  603:     }
  604:     my %temp_env;
  605:     foreach my $line (@profile) {
  606: 	if ($line !~ m/=/) {
  607: 	    return 0;
  608: 	}
  609: 	chomp($line);
  610: 	my ($envname,$envvalue)=split(/=/,$line,2);
  611: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  612:     }
  613:     unlink("$lonidsdir/$handle.id");
  614:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  615: 	    0640)) {
  616: 	%disk_env = %temp_env;
  617: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  618: 	untie(%disk_env);
  619:     }
  620:     return 1;
  621: }
  622: 
  623: # ------------------------------------------- Transfer profile into environment
  624: my $env_loaded;
  625: sub transfer_profile_to_env {
  626:     my ($lonidsdir,$handle,$force_transfer) = @_;
  627:     if (!$force_transfer && $env_loaded) { return; } 
  628: 
  629:     if (!defined($lonidsdir)) {
  630: 	$lonidsdir = $perlvar{'lonIDsDir'};
  631:     }
  632:     if (!defined($handle)) {
  633:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  634:     }
  635: 
  636:     my $convert;
  637:     {
  638:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  639: 	if (!$opened) {
  640: 	    return;
  641: 	}
  642: 	flock($idf,LOCK_SH);
  643: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  644: 		&GDBM_READER(),0640)) {
  645: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  646: 	    untie(%disk_env);
  647: 	} else {
  648: 	    $convert = 1;
  649: 	}
  650:     }
  651:     if ($convert) {
  652: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  653: 	    &logthis("Failed to load session, or convert session.");
  654: 	}
  655:     }
  656: 
  657:     my %remove;
  658:     while ( my $envname = each(%env) ) {
  659:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  660:             if ($time < time-300) {
  661:                 $remove{$key}++;
  662:             }
  663:         }
  664:     }
  665: 
  666:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  667:     $env_loaded=1;
  668:     foreach my $expired_key (keys(%remove)) {
  669:         &delenv($expired_key);
  670:     }
  671: }
  672: 
  673: # ---------------------------------------------------- Check for valid session 
  674: sub check_for_valid_session {
  675:     my ($r,$name,$userhashref,$domref) = @_;
  676:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  677:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  678:     if ($name eq 'lonDAV') {
  679:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  680:     } else {
  681:         $lonidsdir=$r->dir_config('lonIDsDir');
  682:         if ($name eq '') {
  683:             $name = 'lonID';
  684:         }
  685:     }
  686:     if ($name eq 'lonID') {
  687:         $secure = 'lonSID';
  688:         $linkname = 'lonLinkID';
  689:         $pubname = 'lonPubID';
  690:         if (exists($cookies{$secure})) {
  691:             $lonid=$cookies{$secure};
  692:         } elsif (exists($cookies{$name})) {
  693:             $lonid=$cookies{$name};
  694:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  695:             $lonid=$cookies{$linkname};
  696:         } elsif (exists($cookies{$pubname})) {
  697:             $lonid=$cookies{$pubname};
  698:         }
  699:     } else {
  700:         $lonid=$cookies{$name};
  701:     }
  702:     return undef if (!$lonid);
  703: 
  704:     my $handle=&LONCAPA::clean_handle($lonid->value);
  705:     if (-l "$lonidsdir/$handle.id") {
  706:         my $link = readlink("$lonidsdir/$handle.id");
  707:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  708:             $handle = $1;
  709:         }
  710:     }
  711:     if (!-e "$lonidsdir/$handle.id") {
  712:         if ((ref($domref)) && ($name eq 'lonID') && 
  713:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  714:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  715:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  716:                 $$domref = $possudom;
  717:             }
  718:         }
  719:         return undef;
  720:     }
  721: 
  722:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  723:     return undef if (!$opened);
  724: 
  725:     flock($idf,LOCK_SH);
  726:     my %disk_env;
  727:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  728: 	    &GDBM_READER(),0640)) {
  729: 	return undef;	
  730:     }
  731: 
  732:     if (!defined($disk_env{'user.name'})
  733: 	|| !defined($disk_env{'user.domain'})) {
  734:         untie(%disk_env);
  735: 	return undef;
  736:     }
  737: 
  738:     if (ref($userhashref) eq 'HASH') {
  739:         $userhashref->{'name'} = $disk_env{'user.name'};
  740:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  741:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  742:         if ($userhashref->{'lti'}) {
  743:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  744:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  745:         }
  746:     }
  747:     untie(%disk_env);
  748: 
  749:     return $handle;
  750: }
  751: 
  752: sub timed_flock {
  753:     my ($file,$lock_type) = @_;
  754:     my $failed=0;
  755:     eval {
  756: 	local $SIG{__DIE__}='DEFAULT';
  757: 	local $SIG{ALRM}=sub {
  758: 	    $failed=1;
  759: 	    die("failed lock");
  760: 	};
  761: 	alarm(13);
  762: 	flock($file,$lock_type);
  763: 	alarm(0);
  764:     };
  765:     if ($failed) {
  766: 	return undef;
  767:     } else {
  768: 	return 1;
  769:     }
  770: }
  771: 
  772: sub get_sessionfile_vars {
  773:     my ($handle,$lonidsdir,$storearr) = @_;
  774:     my %returnhash;
  775:     unless (ref($storearr) eq 'ARRAY') {
  776:         return %returnhash;
  777:     }
  778:     if (-l "$lonidsdir/$handle.id") {
  779:         my $link = readlink("$lonidsdir/$handle.id");
  780:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  781:             $handle = $1;
  782:         }
  783:     }
  784:     if ((-e "$lonidsdir/$handle.id") &&
  785:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  786:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  787:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  788:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  789:                 flock($idf,LOCK_SH);
  790:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  791:                         &GDBM_READER(),0640)) {
  792:                     foreach my $item (@{$storearr}) {
  793:                         $returnhash{$item} = $disk_env{$item};
  794:                     }
  795:                     untie(%disk_env);
  796:                 }
  797:             }
  798:         }
  799:     }
  800:     return %returnhash;
  801: }
  802: 
  803: # ---------------------------------------------------------- Append Environment
  804: 
  805: sub appenv {
  806:     my ($newenv,$roles) = @_;
  807:     if (ref($newenv) eq 'HASH') {
  808:         foreach my $key (keys(%{$newenv})) {
  809:             my $refused = 0;
  810: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  811:                 $refused = 1;
  812:                 if (ref($roles) eq 'ARRAY') {
  813:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  814:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  815:                         $refused = 0;
  816:                     }
  817:                 }
  818:             }
  819:             if ($refused) {
  820:                 &logthis("<font color=\"blue\">WARNING: ".
  821:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  822:                          .'</font>');
  823: 	        delete($newenv->{$key});
  824:             } else {
  825:                 $env{$key}=$newenv->{$key};
  826:             }
  827:         }
  828:         my $lonids = $perlvar{'lonIDsDir'};
  829:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  830:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  831:             if ($opened
  832: 	        && &timed_flock($env_file,LOCK_EX)
  833: 	        &&
  834: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  835: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  836: 	        while (my ($key,$value) = each(%{$newenv})) {
  837: 	            $disk_env{$key} = $value;
  838: 	        }
  839: 	        untie(%disk_env);
  840:             }
  841:         }
  842:     }
  843:     return 'ok';
  844: }
  845: # ----------------------------------------------------- Delete from Environment
  846: 
  847: sub delenv {
  848:     my ($delthis,$regexp,$roles) = @_;
  849:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  850:         my $refused = 1;
  851:         if (ref($roles) eq 'ARRAY') {
  852:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  853:             if (grep(/^\Q$role\E$/,@{$roles})) {
  854:                 $refused = 0;
  855:             }
  856:         }
  857:         if ($refused) {
  858:             &logthis("<font color=\"blue\">WARNING: ".
  859:                      "Attempt to delete from environment ".$delthis);
  860:             return 'error';
  861:         }
  862:     }
  863:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  864:     if ($opened
  865: 	&& &timed_flock($env_file,LOCK_EX)
  866: 	&&
  867: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  868: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  869: 	foreach my $key (keys(%disk_env)) {
  870: 	    if ($regexp) {
  871:                 if ($key=~/^$delthis/) {
  872:                     delete($env{$key});
  873:                     delete($disk_env{$key});
  874:                 } 
  875:             } else {
  876:                 if ($key=~/^\Q$delthis\E/) {
  877: 		    delete($env{$key});
  878: 		    delete($disk_env{$key});
  879: 	        }
  880:             }
  881: 	}
  882: 	untie(%disk_env);
  883:     }
  884:     return 'ok';
  885: }
  886: 
  887: sub get_env_multiple {
  888:     my ($name) = @_;
  889:     my @values;
  890:     if (defined($env{$name})) {
  891:         # exists is it an array
  892:         if (ref($env{$name})) {
  893:             @values=@{ $env{$name} };
  894:         } else {
  895:             $values[0]=$env{$name};
  896:         }
  897:     }
  898:     return(@values);
  899: }
  900: 
  901: # ------------------------------------------------------------------- Locking
  902: 
  903: sub set_lock {
  904:     my ($text)=@_;
  905:     $locknum++;
  906:     my $id=$$.'-'.$locknum;
  907:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  908:              'session.lock.'.$id => $text});
  909:     return $id;
  910: }
  911: 
  912: sub get_locks {
  913:     my $num=0;
  914:     my %texts=();
  915:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  916:        if ($lock=~/\w/) {
  917:           $num++;
  918:           $texts{$lock}=$env{'session.lock.'.$lock};
  919:        }
  920:    }
  921:    return ($num,%texts);
  922: }
  923: 
  924: sub remove_lock {
  925:     my ($id)=@_;
  926:     my $newlocks='';
  927:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  928:        if (($lock=~/\w/) && ($lock ne $id)) {
  929:           $newlocks.=','.$lock;
  930:        }
  931:     }
  932:     &appenv({'session.locks' => $newlocks});
  933:     &delenv('session.lock.'.$id);
  934: }
  935: 
  936: sub remove_all_locks {
  937:     my $activelocks=$env{'session.locks'};
  938:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  939:        if ($lock=~/\w/) {
  940:           &remove_lock($lock);
  941:        }
  942:     }
  943: }
  944: 
  945: 
  946: # ------------------------------------------ Find out current server userload
  947: sub userload {
  948:     my $numusers=0;
  949:     {
  950: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  951: 	my $filename;
  952: 	my $curtime=time;
  953: 	while ($filename=readdir(LONIDS)) {
  954: 	    next if ($filename eq '.' || $filename eq '..');
  955: 	    next if ($filename =~ /publicuser_\d+\.id/);
  956:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
  957: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  958: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  959: 	}
  960: 	closedir(LONIDS);
  961:     }
  962:     my $userloadpercent=0;
  963:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  964:     if ($maxuserload) {
  965: 	$userloadpercent=100*$numusers/$maxuserload;
  966:     }
  967:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  968:     return $userloadpercent;
  969: }
  970: 
  971: # ------------------------------ Find server with least workload from spare.tab
  972: 
  973: sub spareserver {
  974:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  975:     my $spare_server;
  976:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  977:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  978:                                                      :  $userloadpercent;
  979:     my ($uint_dom,$remotesessions);
  980:     if (($udom ne '') && (&domain($udom) ne '')) {
  981:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  982:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  983:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  984:         $remotesessions = $udomdefaults{'remotesessions'};
  985:     }
  986:     my $spareshash = &this_host_spares($udom);
  987:     if (ref($spareshash) eq 'HASH') {
  988:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  989:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  990:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  991:                                              $try_server));
  992: 	        ($spare_server, $lowest_load) =
  993: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
  994:             }
  995:         }
  996: 
  997:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
  998: 
  999:         if (!$found_server) {
 1000:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1001: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1002:                     next unless (&spare_can_host($udom,$uint_dom,
 1003:                                                  $remotesessions,$try_server));
 1004: 	            ($spare_server, $lowest_load) =
 1005: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1006:                 }
 1007: 	    }
 1008:         }
 1009:     }
 1010: 
 1011:     if (!$want_server_name) {
 1012:         if (defined($spare_server)) {
 1013:             my $hostname = &hostname($spare_server);
 1014:             if (defined($hostname)) {
 1015:                 my $protocol = 'http';
 1016:                 if ($protocol{$spare_server} eq 'https') {
 1017:                     $protocol = $protocol{$spare_server};
 1018:                 }
 1019: 	        $spare_server = $protocol.'://'.$hostname;
 1020:             }
 1021:         }
 1022:     }
 1023:     return $spare_server;
 1024: }
 1025: 
 1026: sub compare_server_load {
 1027:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1028: 
 1029:     if ($required) {
 1030:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1031:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1032:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1033:         if (($major eq '' && $minor eq '') ||
 1034:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1035:             return ($spare_server,$lowest_load);
 1036:         }
 1037:     }
 1038: 
 1039:     my $loadans     = &reply('load',    $try_server);
 1040:     my $userloadans = &reply('userload',$try_server);
 1041: 
 1042:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1043: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1044:     }
 1045: 
 1046:     my $load;
 1047:     if ($loadans =~ /\d/) {
 1048: 	if ($userloadans =~ /\d/) {
 1049: 	    #both are numbers, pick the bigger one
 1050: 	    $load = ($loadans > $userloadans) ? $loadans 
 1051: 		                              : $userloadans;
 1052: 	} else {
 1053: 	    $load = $loadans;
 1054: 	}
 1055:     } else {
 1056: 	$load = $userloadans;
 1057:     }
 1058: 
 1059:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1060: 	$spare_server = $try_server;
 1061: 	$lowest_load  = $load;
 1062:     }
 1063:     return ($spare_server,$lowest_load);
 1064: }
 1065: 
 1066: # --------------------------- ask offload servers if user already has a session
 1067: sub find_existing_session {
 1068:     my ($udom,$uname) = @_;
 1069:     my $spareshash = &this_host_spares($udom);
 1070:     if (ref($spareshash) eq 'HASH') {
 1071:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1072:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1073:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1074:             }
 1075:         }
 1076:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1077:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1078:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1079:             }
 1080:         }
 1081:     }
 1082:     return;
 1083: }
 1084: 
 1085: sub delusersession {
 1086:     my ($lonid,$udom,$uname) = @_;
 1087:     my $uprimary_id = &domain($udom,'primary');
 1088:     my $uintdom = &internet_dom($uprimary_id);
 1089:     my $intdom = &internet_dom($lonid);
 1090:     my $serverhomedom = &host_domain($lonid);
 1091:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1092:         return &reply(join(':','delusersession',
 1093:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1094:     }
 1095:     return;
 1096: }
 1097: 
 1098: # check if user's browser sent load balancer cookie and server still has session
 1099: # and is not overloaded.
 1100: sub check_for_balancer_cookie {
 1101:     my ($r,$update_mtime) = @_;
 1102:     my ($otherserver,$cookie);
 1103:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1104:     if (exists($cookies{'balanceID'})) {
 1105:         my $balid = $cookies{'balanceID'};
 1106:         $cookie=&LONCAPA::clean_handle($balid->value);
 1107:         my $balancedir=$r->dir_config('lonBalanceDir');
 1108:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1109:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1110:                 my ($possudom,$possuname) = ($1,$2);
 1111:                 my $has_session = 0;
 1112:                 if ((&domain($possudom) ne '') &&
 1113:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1114:                     my $try_server;
 1115:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1116:                     if ($opened) {
 1117:                         flock($idf,LOCK_SH);
 1118:                         while (my $line = <$idf>) {
 1119:                             chomp($line);
 1120:                             if (&hostname($line) ne '') {
 1121:                                 $try_server = $line;
 1122:                                 last;
 1123:                             }
 1124:                         }
 1125:                         close($idf);
 1126:                         if (($try_server) &&
 1127:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1128:                             my $lowest_load = 30000;
 1129:                             ($otherserver,$lowest_load) =
 1130:                                 &compare_server_load($try_server,undef,$lowest_load);
 1131:                             if ($otherserver ne '' && $lowest_load < 100) {
 1132:                                 $has_session = 1;
 1133:                             } else {
 1134:                                 undef($otherserver);
 1135:                             }
 1136:                         }
 1137:                     }
 1138:                 }
 1139:                 if ($has_session) {
 1140:                     if ($update_mtime) {
 1141:                         my $atime = my $mtime = time;
 1142:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1143:                     }
 1144:                 } else {
 1145:                     unlink("$balancedir/$cookie.id");
 1146:                 }
 1147:             }
 1148:         }
 1149:     }
 1150:     return ($otherserver,$cookie);
 1151: }
 1152: 
 1153: sub delbalcookie {
 1154:     my ($cookie,$balancer) =@_;
 1155:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1156:         my ($udom,$uname) = ($1,$2);
 1157:         my $uprimary_id = &domain($udom,'primary');
 1158:         my $uintdom = &internet_dom($uprimary_id);
 1159:         my $intdom = &internet_dom($balancer);
 1160:         my $serverhomedom = &host_domain($balancer);
 1161:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1162:             return &reply("delbalcookie:$cookie",$balancer);
 1163:         }
 1164:     }
 1165: }
 1166: 
 1167: # -------------------------------- ask if server already has a session for user
 1168: sub has_user_session {
 1169:     my ($lonid,$udom,$uname) = @_;
 1170:     my $result = &reply(join(':','userhassession',
 1171: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1172:     return 1 if ($result eq 'ok');
 1173: 
 1174:     return 0;
 1175: }
 1176: 
 1177: # --------- determine least loaded server in a user's domain which allows login
 1178: 
 1179: sub choose_server {
 1180:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1181:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1182:     my %servers = &get_servers($udom);
 1183:     my $lowest_load = 30000;
 1184:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1185:     if ($skiploadbal) {
 1186:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1187:         unless (defined($cached)) {
 1188:             my $cachetime = 60*60*24;
 1189:             my %domconfig =
 1190:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1191:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1192:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1193:                                            $cachetime);
 1194:             }
 1195:         }
 1196:     }
 1197:     foreach my $lonhost (keys(%servers)) {
 1198:         if ($skiploadbal) {
 1199:             if (ref($balancers) eq 'HASH') {
 1200:                 next if (exists($balancers->{$lonhost}));
 1201:             }
 1202:         }
 1203:         my $loginvia;
 1204:         if ($checkloginvia) {
 1205:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1206:             if ($loginvia) {
 1207:                 my ($server,$path) = split(/:/,$loginvia);
 1208:                 ($login_host, $lowest_load) =
 1209:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1210:                 if ($login_host eq $server) {
 1211:                     $portal_path = $path;
 1212:                     $isredirect = 1;
 1213:                 }
 1214:             } else {
 1215:                 ($login_host, $lowest_load) =
 1216:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1217:                 if ($login_host eq $lonhost) {
 1218:                     $portal_path = '';
 1219:                     $isredirect = ''; 
 1220:                 }
 1221:             }
 1222:         } else {
 1223:             ($login_host, $lowest_load) =
 1224:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1225:         }
 1226:     }
 1227:     if ($login_host ne '') {
 1228:         $hostname = &hostname($login_host);
 1229:     }
 1230:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1231: }
 1232: 
 1233: sub get_course_sessions {
 1234:     my ($cnum,$cdom,$lastactivity) = @_;
 1235:     my %servers = &internet_dom_servers($cdom);
 1236:     my %returnhash;
 1237:     foreach my $server (sort(keys(%servers))) {
 1238:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1239:         my @pairs=split(/\&/,$rep);
 1240:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1241:             foreach my $item (@pairs) {
 1242:                 my ($key,$value)=split(/=/,$item,2);
 1243:                 $key = &unescape($key);
 1244:                 next if ($key =~ /^error: 2 /);
 1245:                 if (exists($returnhash{$key})) {
 1246:                     next if ($value < $returnhash{$key});
 1247:                 }
 1248:                 $returnhash{$key}=$value;
 1249:             }
 1250:         }
 1251:     }
 1252:     return %returnhash;
 1253: }
 1254: 
 1255: # --------------------------------------------- Try to change a user's password
 1256: 
 1257: sub changepass {
 1258:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1259:     $currentpass = &escape($currentpass);
 1260:     $newpass     = &escape($newpass);
 1261:     my $lonhost = $perlvar{'lonHostID'};
 1262:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1263: 		       $server);
 1264:     if (! $answer) {
 1265: 	&logthis("No reply on password change request to $server ".
 1266: 		 "by $uname in domain $udom.");
 1267:     } elsif ($answer =~ "^ok") {
 1268:         &logthis("$uname in $udom successfully changed their password ".
 1269: 		 "on $server.");
 1270:     } elsif ($answer =~ "^pwchange_failure") {
 1271: 	&logthis("$uname in $udom was unable to change their password ".
 1272: 		 "on $server.  The action was blocked by either lcpasswd ".
 1273: 		 "or pwchange");
 1274:     } elsif ($answer =~ "^non_authorized") {
 1275:         &logthis("$uname in $udom did not get their password correct when ".
 1276: 		 "attempting to change it on $server.");
 1277:     } elsif ($answer =~ "^auth_mode_error") {
 1278:         &logthis("$uname in $udom attempted to change their password despite ".
 1279: 		 "not being locally or internally authenticated on $server.");
 1280:     } elsif ($answer =~ "^unknown_user") {
 1281:         &logthis("$uname in $udom attempted to change their password ".
 1282: 		 "on $server but were unable to because $server is not ".
 1283: 		 "their home server.");
 1284:     } elsif ($answer =~ "^refused") {
 1285: 	&logthis("$server refused to change $uname in $udom password because ".
 1286: 		 "it was sent an unencrypted request to change the password.");
 1287:     } elsif ($answer =~ "invalid_client") {
 1288:         &logthis("$server refused to change $uname in $udom password because ".
 1289:                  "it was a reset by e-mail originating from an invalid server.");
 1290:     } elsif ($answer =~ "^prioruse") {
 1291:        &logthis("$server refused to change $uname in $udom password because ".
 1292:                 "the password had been used before");
 1293:     }
 1294:     return $answer;
 1295: }
 1296: 
 1297: # ----------------------- Try to determine user's current authentication scheme
 1298: 
 1299: sub queryauthenticate {
 1300:     my ($uname,$udom)=@_;
 1301:     my $uhome=&homeserver($uname,$udom);
 1302:     if (!$uhome) {
 1303: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1304: 	return 'no_host';
 1305:     }
 1306:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1307:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1308: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1309:     }
 1310:     return $answer;
 1311: }
 1312: 
 1313: # --------- Try to authenticate user from domain's lib servers (first this one)
 1314: 
 1315: sub authenticate {
 1316:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1317:     $upass=&escape($upass);
 1318:     $uname= &LONCAPA::clean_username($uname);
 1319:     my $uhome=&homeserver($uname,$udom,1);
 1320:     my $newhome;
 1321:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1322: # Maybe the machine was offline and only re-appeared again recently?
 1323:         &reconlonc();
 1324: # One more
 1325: 	$uhome=&homeserver($uname,$udom,1);
 1326:         if (($uhome eq 'no_host') && $checkdefauth) {
 1327:             if (defined(&domain($udom,'primary'))) {
 1328:                 $newhome=&domain($udom,'primary');
 1329:             }
 1330:             if ($newhome ne '') {
 1331:                 $uhome = $newhome;
 1332:             }
 1333:         }
 1334: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1335: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1336: 	    return 'no_host';
 1337:         }
 1338:     }
 1339:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1340:     if ($answer eq 'authorized') {
 1341:         if ($newhome) {
 1342:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1343:             return 'no_account_on_host'; 
 1344:         } else {
 1345:             &logthis("User $uname at $udom authorized by $uhome");
 1346:             return $uhome;
 1347:         }
 1348:     }
 1349:     if ($answer eq 'non_authorized') {
 1350: 	&logthis("User $uname at $udom rejected by $uhome");
 1351: 	return 'no_host'; 
 1352:     }
 1353:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1354:     return 'no_host';
 1355: }
 1356: 
 1357: sub can_host_session {
 1358:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1359:     my $canhost = 1;
 1360:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1361:     if (ref($remotesessions) eq 'HASH') {
 1362:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1363:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1364:                 $canhost = 0;
 1365:             } else {
 1366:                 $canhost = 1;
 1367:             }
 1368:         }
 1369:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1370:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1371:                 $canhost = 1;
 1372:             } else {
 1373:                 $canhost = 0;
 1374:             }
 1375:         }
 1376:         if ($canhost) {
 1377:             if ($remotesessions->{'version'} ne '') {
 1378:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1379:                 if ($reqmajor ne '' && $reqminor ne '') {
 1380:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1381:                         my $major = $1;
 1382:                         my $minor = $2;
 1383:                         if (($major < $reqmajor ) ||
 1384:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1385:                             $canhost = 0;
 1386:                         }
 1387:                     } else {
 1388:                         $canhost = 0;
 1389:                     }
 1390:                 }
 1391:             }
 1392:         }
 1393:     }
 1394:     if ($canhost) {
 1395:         if (ref($hostedsessions) eq 'HASH') {
 1396:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1397:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1398:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1399:                 if (($uint_dom ne '') && 
 1400:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1401:                     $canhost = 0;
 1402:                 } else {
 1403:                     $canhost = 1;
 1404:                 }
 1405:             }
 1406:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1407:                 if (($uint_dom ne '') && 
 1408:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1409:                     $canhost = 1;
 1410:                 } else {
 1411:                     $canhost = 0;
 1412:                 }
 1413:             }
 1414:         }
 1415:     }
 1416:     return $canhost;
 1417: }
 1418: 
 1419: sub spare_can_host {
 1420:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1421:     my $canhost=1;
 1422:     my $try_server_hostname = &hostname($try_server);
 1423:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1424:     my $serverhomedom = &host_domain($serverhomeID);
 1425:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1426:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1427:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1428:             $canhost = 0;
 1429:         }
 1430:     }
 1431:     if (($canhost) && ($uint_dom)) {
 1432:         my @intdoms;
 1433:         my $internet_names = &get_internet_names($try_server);
 1434:         if (ref($internet_names) eq 'ARRAY') {
 1435:             @intdoms = @{$internet_names};
 1436:         }
 1437:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1438:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1439:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1440:                                          $remotesessions,
 1441:                                          $defdomdefaults{'hostedsessions'});
 1442:         }
 1443:     }
 1444:     return $canhost;
 1445: }
 1446: 
 1447: sub this_host_spares {
 1448:     my ($dom) = @_;
 1449:     my ($dom_in_use,$lonhost_in_use,$result);
 1450:     my @hosts = &current_machine_ids();
 1451:     foreach my $lonhost (@hosts) {
 1452:         if (&host_domain($lonhost) eq $dom) {
 1453:             $dom_in_use = $dom;
 1454:             $lonhost_in_use = $lonhost;
 1455:             last;
 1456:         }
 1457:     }
 1458:     if ($dom_in_use ne '') {
 1459:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1460:     }
 1461:     if (ref($result) ne 'HASH') {
 1462:         $lonhost_in_use = $perlvar{'lonHostID'};
 1463:         $dom_in_use = &host_domain($lonhost_in_use);
 1464:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1465:         if (ref($result) ne 'HASH') {
 1466:             $result = \%spareid;
 1467:         }
 1468:     }
 1469:     return $result;
 1470: }
 1471: 
 1472: sub spares_for_offload  {
 1473:     my ($dom_in_use,$lonhost_in_use) = @_;
 1474:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1475:     if (defined($cached)) {
 1476:         return $result;
 1477:     } else {
 1478:         my $cachetime = 60*60*24;
 1479:         my %domconfig =
 1480:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1481:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1482:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1483:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1484:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1485:                 }
 1486:             }
 1487:         }
 1488:     }
 1489:     return;
 1490: }
 1491: 
 1492: sub get_lonbalancer_config {
 1493:     my ($servers) = @_;
 1494:     my ($currbalancer,$currtargets);
 1495:     if (ref($servers) eq 'HASH') {
 1496:         foreach my $server (keys(%{$servers})) {
 1497:             my %what = (
 1498:                          spareid => 1,
 1499:                          perlvar => 1,
 1500:                        );
 1501:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1502:             if ($result eq 'ok') {
 1503:                 if (ref($returnhash) eq 'HASH') {
 1504:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1505:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1506:                             $currbalancer = $server;
 1507:                             $currtargets = {};
 1508:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1509:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1510:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1511:                                 }
 1512:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1513:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1514:                                 }
 1515:                             }
 1516:                             last;
 1517:                         }
 1518:                     }
 1519:                 }
 1520:             }
 1521:         }
 1522:     }
 1523:     return ($currbalancer,$currtargets);
 1524: }
 1525: 
 1526: sub check_loadbalancing {
 1527:     my ($uname,$udom,$caller) = @_;
 1528:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1529:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1530:     my $lonhost = $perlvar{'lonHostID'};
 1531:     my @hosts = &current_machine_ids();
 1532:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1533:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1534:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1535:     my $serverhomedom = &host_domain($lonhost);
 1536:     my $domneedscache;
 1537:     my $cachetime = 60*60*24;
 1538: 
 1539:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1540:         $dom_in_use = $udom;
 1541:         $homeintdom = 1;
 1542:     } else {
 1543:         $dom_in_use = $serverhomedom;
 1544:     }
 1545:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1546:     unless (defined($cached)) {
 1547:         my %domconfig =
 1548:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1549:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1550:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1551:         } else {
 1552:             $domneedscache = $dom_in_use;
 1553:         }
 1554:     }
 1555:     if (ref($result) eq 'HASH') {
 1556:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1557:             &check_balancer_result($result,@hosts);
 1558:         if ($is_balancer) {
 1559:             if (ref($currrules) eq 'HASH') {
 1560:                 if ($homeintdom) {
 1561:                     if ($uname ne '') {
 1562:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1563:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1564:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1565:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1566:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1567:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1568:                             }
 1569:                         }
 1570:                         if ($rule_in_effect eq '') {
 1571:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1572:                             if ($userenv{'inststatus'} ne '') {
 1573:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1574:                                 my ($othertitle,$usertypes,$types) =
 1575:                                     &Apache::loncommon::sorted_inst_types($udom);
 1576:                                 if (ref($types) eq 'ARRAY') {
 1577:                                     foreach my $type (@{$types}) {
 1578:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1579:                                             if (exists($currrules->{$type})) {
 1580:                                                 $rule_in_effect = $currrules->{$type};
 1581:                                             }
 1582:                                         }
 1583:                                     }
 1584:                                 }
 1585:                             } else {
 1586:                                 if (exists($currrules->{'default'})) {
 1587:                                     $rule_in_effect = $currrules->{'default'};
 1588:                                 }
 1589:                             }
 1590:                         }
 1591:                     } else {
 1592:                         if (exists($currrules->{'default'})) {
 1593:                             $rule_in_effect = $currrules->{'default'};
 1594:                         }
 1595:                     }
 1596:                 } else {
 1597:                     if ($currrules->{'_LC_external'} ne '') {
 1598:                         $rule_in_effect = $currrules->{'_LC_external'};
 1599:                     }
 1600:                 }
 1601:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1602:                                                        $uname,$udom);
 1603:             }
 1604:         }
 1605:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1606:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1607:         unless (defined($cached)) {
 1608:             my %domconfig =
 1609:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1610:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1611:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1612:             } else {
 1613:                 $domneedscache = $serverhomedom;
 1614:             }
 1615:         }
 1616:         if (ref($result) eq 'HASH') {
 1617:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1618:                 &check_balancer_result($result,@hosts);
 1619:             if ($is_balancer) {
 1620:                 if (ref($currrules) eq 'HASH') {
 1621:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1622:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1623:                     }
 1624:                 }
 1625:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1626:                                                        $uname,$udom);
 1627:             }
 1628:         } else {
 1629:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1630:                 $is_balancer = 1;
 1631:                 $offloadto = &this_host_spares($dom_in_use);
 1632:             }
 1633:             unless (defined($cached)) {
 1634:                 $domneedscache = $serverhomedom;
 1635:             }
 1636:         }
 1637:     } else {
 1638:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1639:             $is_balancer = 1;
 1640:             $offloadto = &this_host_spares($dom_in_use);
 1641:         }
 1642:         unless (defined($cached)) {
 1643:             $domneedscache = $serverhomedom;
 1644:         }
 1645:     }
 1646:     if ($domneedscache) {
 1647:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1648:     }
 1649:     if ($is_balancer) {
 1650:         my $lowest_load = 30000;
 1651:         if (ref($offloadto) eq 'HASH') {
 1652:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1653:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1654:                     ($otherserver,$lowest_load) =
 1655:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1656:                 }
 1657:             }
 1658:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1659: 
 1660:             if (!$found_server) {
 1661:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1662:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1663:                         ($otherserver,$lowest_load) =
 1664:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1665:                     }
 1666:                 }
 1667:             }
 1668:         } elsif (ref($offloadto) eq 'ARRAY') {
 1669:             if (@{$offloadto} == 1) {
 1670:                 $otherserver = $offloadto->[0];
 1671:             } elsif (@{$offloadto} > 1) {
 1672:                 foreach my $try_server (@{$offloadto}) {
 1673:                     ($otherserver,$lowest_load) =
 1674:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1675:                 }
 1676:             }
 1677:         }
 1678:         unless ($caller eq 'login') {
 1679:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1680:                 $is_balancer = 0;
 1681:                 if ($uname ne '' && $udom ne '') {
 1682:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1683:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1684:                                  'user.loadbalcheck.time' => time});
 1685:                     }
 1686:                 }
 1687:             }
 1688:         }
 1689:         unless ($homeintdom) {
 1690:             undef($setcookie);
 1691:         }
 1692:     }
 1693:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1694: }
 1695: 
 1696: sub check_balancer_result {
 1697:     my ($result,@hosts) = @_;
 1698:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1699:     if (ref($result) eq 'HASH') {
 1700:         if ($result->{'lonhost'} ne '') {
 1701:             my $currbalancer = $result->{'lonhost'};
 1702:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1703:                 $is_balancer = 1;
 1704:                 $currtargets = $result->{'targets'};
 1705:                 $currrules = $result->{'rules'};
 1706:             }
 1707:             $dom_balancers = $currbalancer;
 1708:         } else {
 1709:             if (keys(%{$result})) {
 1710:                 foreach my $key (keys(%{$result})) {
 1711:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1712:                         (ref($result->{$key}) eq 'HASH')) {
 1713:                         $is_balancer = 1;
 1714:                         $currrules = $result->{$key}{'rules'};
 1715:                         $currtargets = $result->{$key}{'targets'};
 1716:                         $setcookie = $result->{$key}{'cookie'};
 1717:                         last;
 1718:                     }
 1719:                 }
 1720:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1721:             }
 1722:         }
 1723:     }
 1724:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1725: }
 1726: 
 1727: sub get_loadbalancer_targets {
 1728:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1729:     my $offloadto;
 1730:     if ($rule_in_effect eq 'none') {
 1731:         return [$perlvar{'lonHostID'}];
 1732:     } elsif ($rule_in_effect eq '') {
 1733:         $offloadto = $currtargets;
 1734:     } else {
 1735:         if ($rule_in_effect eq 'homeserver') {
 1736:             my $homeserver = &homeserver($uname,$udom);
 1737:             if ($homeserver ne 'no_host') {
 1738:                 $offloadto = [$homeserver];
 1739:             }
 1740:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1741:             my %domconfig =
 1742:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1743:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1744:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1745:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1746:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1747:                     }
 1748:                 }
 1749:             } else {
 1750:                 my %servers = &internet_dom_servers($udom);
 1751:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1752:                 if (&hostname($remotebalancer) ne '') {
 1753:                     $offloadto = [$remotebalancer];
 1754:                 }
 1755:             }
 1756:         } elsif (&hostname($rule_in_effect) ne '') {
 1757:             $offloadto = [$rule_in_effect];
 1758:         }
 1759:     }
 1760:     return $offloadto;
 1761: }
 1762: 
 1763: sub internet_dom_servers {
 1764:     my ($dom) = @_;
 1765:     my (%uniqservers,%servers);
 1766:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1767:     my @machinedoms = &machine_domains($primaryserver);
 1768:     foreach my $mdom (@machinedoms) {
 1769:         my %currservers = %servers;
 1770:         my %server = &get_servers($mdom);
 1771:         %servers = (%currservers,%server);
 1772:     }
 1773:     my %by_hostname;
 1774:     foreach my $id (keys(%servers)) {
 1775:         push(@{$by_hostname{$servers{$id}}},$id);
 1776:     }
 1777:     foreach my $hostname (sort(keys(%by_hostname))) {
 1778:         if (@{$by_hostname{$hostname}} > 1) {
 1779:             my $match = 0;
 1780:             foreach my $id (@{$by_hostname{$hostname}}) {
 1781:                 if (&host_domain($id) eq $dom) {
 1782:                     $uniqservers{$id} = $hostname;
 1783:                     $match = 1;
 1784:                 }
 1785:             }
 1786:             unless ($match) {
 1787:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1788:             }
 1789:         } else {
 1790:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1791:         }
 1792:     }
 1793:     return %uniqservers;
 1794: }
 1795: 
 1796: sub trusted_domains {
 1797:     my ($cmdtype,$calldom) = @_;
 1798:     my ($trusted,$untrusted);
 1799:     if (&domain($calldom) eq '') {
 1800:         return ($trusted,$untrusted);
 1801:     }
 1802:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1803:         return ($trusted,$untrusted);
 1804:     }
 1805:     my $callprimary = &domain($calldom,'primary');
 1806:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1807:     if ($intcalldom eq '') {
 1808:         return ($trusted,$untrusted);
 1809:     }
 1810: 
 1811:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1812:     unless (defined($cached)) {
 1813:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1814:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1815:         $trustconfig = $domconfig{'trust'};
 1816:     }
 1817:     if (ref($trustconfig)) {
 1818:         my (%possexc,%possinc,@allexc,@allinc); 
 1819:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1820:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1821:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1822:             }
 1823:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1824:                 $possinc{$intcalldom} = 1;
 1825:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1826:             }
 1827:         }
 1828:         if (keys(%possexc)) {
 1829:             if (keys(%possinc)) {
 1830:                 foreach my $key (sort(keys(%possexc))) {
 1831:                     next if ($key eq $intcalldom);
 1832:                     unless ($possinc{$key}) {
 1833:                         push(@allexc,$key);
 1834:                     }
 1835:                 }
 1836:             } else {
 1837:                 @allexc = sort(keys(%possexc));
 1838:             }
 1839:         }
 1840:         if (keys(%possinc)) {
 1841:             $possinc{$intcalldom} = 1;
 1842:             @allinc = sort(keys(%possinc));
 1843:         }
 1844:         if ((@allexc > 0) || (@allinc > 0)) {
 1845:             my %doms_by_intdom;
 1846:             my %allintdoms = &all_host_intdom();
 1847:             my %alldoms = &all_host_domain();
 1848:             foreach my $key (%allintdoms) {
 1849:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1850:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1851:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1852:                     }
 1853:                 } else {
 1854:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1855:                 }
 1856:             }
 1857:             foreach my $exc (@allexc) {
 1858:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1859:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1860:                 }
 1861:             }
 1862:             foreach my $inc (@allinc) {
 1863:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1864:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1865:                 }
 1866:             }
 1867:         }
 1868:     }
 1869:     return ($trusted,$untrusted);
 1870: }
 1871: 
 1872: sub will_trust {
 1873:     my ($cmdtype,$domain,$possdom) = @_;
 1874:     return 1 if ($domain eq $possdom);
 1875:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1876:     my $willtrust; 
 1877:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1878:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1879:             $willtrust = 1;
 1880:         }
 1881:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1882:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1883:             $willtrust = 1;
 1884:         }
 1885:     } else {
 1886:         $willtrust = 1;
 1887:     }
 1888:     return $willtrust;
 1889: }
 1890: 
 1891: # ---------------------- Find the homebase for a user from domain's lib servers
 1892: 
 1893: my %homecache;
 1894: sub homeserver {
 1895:     my ($uname,$udom,$ignoreBadCache)=@_;
 1896:     my $index="$uname:$udom";
 1897: 
 1898:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1899: 
 1900:     my %servers = &get_servers($udom,'library');
 1901:     foreach my $tryserver (keys(%servers)) {
 1902:         next if ($ignoreBadCache ne 'true' && 
 1903: 		 exists($badServerCache{$tryserver}));
 1904: 
 1905: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1906: 	if ($answer eq 'found') {
 1907: 	    delete($badServerCache{$tryserver}); 
 1908: 	    return $homecache{$index}=$tryserver;
 1909: 	} elsif ($answer eq 'no_host') {
 1910: 	    $badServerCache{$tryserver}=1;
 1911: 	}
 1912:     }    
 1913:     return 'no_host';
 1914: }
 1915: 
 1916: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1917: 
 1918: sub idget {
 1919:     my ($udom,$idsref,$namespace)=@_;
 1920:     my %returnhash=();
 1921:     my @ids=(); 
 1922:     if (ref($idsref) eq 'ARRAY') {
 1923:         @ids = @{$idsref};
 1924:     } else {
 1925:         return %returnhash; 
 1926:     }
 1927:     if ($namespace eq '') {
 1928:         $namespace = 'ids';
 1929:     }
 1930:     
 1931:     my %servers = &get_servers($udom,'library');
 1932:     foreach my $tryserver (keys(%servers)) {
 1933: 	my $idlist=join('&', map { &escape($_); } @ids);
 1934: 	if ($namespace eq 'ids') {
 1935: 	    $idlist=~tr/A-Z/a-z/;
 1936: 	}
 1937: 	my $reply;
 1938: 	if ($namespace eq 'ids') {
 1939: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1940: 	} else {
 1941: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1942: 	}
 1943: 	my @answer=();
 1944: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1945: 	    @answer=split(/\&/,$reply);
 1946: 	}                    ;
 1947: 	my $i;
 1948: 	for ($i=0;$i<=$#ids;$i++) {
 1949: 	    if ($answer[$i]) {
 1950: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1951: 	    }
 1952: 	}
 1953:     }
 1954:     return %returnhash;
 1955: }
 1956: 
 1957: # ------------------------------------- Find the IDs behind a list of usernames
 1958: 
 1959: sub idrget {
 1960:     my ($udom,@unames)=@_;
 1961:     my %returnhash=();
 1962:     foreach my $uname (@unames) {
 1963:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1964:     }
 1965:     return %returnhash;
 1966: }
 1967: 
 1968: # Store away a list of names and associated student/employee IDs or clicker IDs
 1969: 
 1970: sub idput {
 1971:     my ($udom,$idsref,$uhom,$namespace)=@_;
 1972:     my %servers=();
 1973:     my %ids=();
 1974:     my %byid = ();
 1975:     if (ref($idsref) eq 'HASH') {
 1976:         %ids=%{$idsref};
 1977:     }
 1978:     if ($namespace eq '') {
 1979:         $namespace = 'ids'; 
 1980:     }
 1981:     foreach my $uname (keys(%ids)) {
 1982: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 1983:         if ($uhom eq '') {
 1984:             $uhom=&homeserver($uname,$udom);
 1985:         }
 1986:         if ($uhom ne 'no_host') {
 1987:             my $esc_unam=&escape($uname);
 1988:             if ($namespace eq 'ids') {
 1989:                 my $id=&escape($ids{$uname});
 1990:                 $id=~tr/A-Z/a-z/;
 1991:                 my $esc_unam=&escape($uname);
 1992:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 1993:             } else {
 1994:                 my @currids = split(/,/,$ids{$uname});
 1995:                 foreach my $id (@currids) {
 1996:                     $byid{$uhom}{$id} .= $uname.',';
 1997:                 }
 1998:             }
 1999:         }
 2000:     }
 2001:     if ($namespace eq 'clickers') {
 2002:         foreach my $server (keys(%byid)) {
 2003:             if (ref($byid{$server}) eq 'HASH') {
 2004:                 foreach my $id (keys(%{$byid{$server}})) {
 2005:                     $byid{$server} =~ s/,$//;
 2006:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2007:                 }
 2008:             }
 2009:         }
 2010:     }
 2011:     foreach my $server (keys(%servers)) {
 2012:         $servers{$server} =~ s/\&$//;
 2013:         if ($namespace eq 'ids') {     
 2014:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2015:         } else {
 2016:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2017:         }
 2018:     }
 2019: }
 2020: 
 2021: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2022: 
 2023: sub iddel {
 2024:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2025:     my %result=();
 2026:     my %ids=();
 2027:     my %byid = ();
 2028:     if (ref($idshashref) eq 'HASH') {
 2029:         %ids=%{$idshashref};
 2030:     } else {
 2031:         return %result;
 2032:     }
 2033:     if ($namespace eq '') {
 2034:         $namespace = 'ids';
 2035:     }
 2036:     my %servers=();
 2037:     while (my ($id,$unamestr) = each(%ids)) {
 2038:         if ($namespace eq 'ids') {
 2039:             my $uhom = $uhome;
 2040:             if ($uhom eq '') { 
 2041:                 $uhom=&homeserver($unamestr,$udom);
 2042:             }
 2043:             if ($uhom ne 'no_host') {
 2044:                 $servers{$uhom}.='&'.&escape($id);
 2045:             }
 2046:          } else {
 2047:             my @curritems = split(/,/,$ids{$id});
 2048:             foreach my $uname (@curritems) {
 2049:                 my $uhom = $uhome;
 2050:                 if ($uhom eq '') {
 2051:                     $uhom=&homeserver($uname,$udom);
 2052:                 }
 2053:                 if ($uhom ne 'no_host') { 
 2054:                     $byid{$uhom}{$id} .= $uname.',';
 2055:                 }
 2056:             }
 2057:         }
 2058:     }
 2059:     if ($namespace eq 'clickers') {
 2060:         foreach my $server (keys(%byid)) {
 2061:             if (ref($byid{$server}) eq 'HASH') {
 2062:                 foreach my $id (keys(%{$byid{$server}})) {
 2063:                     $byid{$server}{$id} =~ s/,$//;
 2064:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2065:                 }
 2066:             }
 2067:         }
 2068:     }
 2069:     foreach my $server (keys(%servers)) {
 2070:         $servers{$server} =~ s/\&$//;
 2071:         if ($namespace eq 'ids') {
 2072:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2073:         } elsif ($namespace eq 'clickers') {
 2074:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2075:         }
 2076:     }
 2077:     return %result;
 2078: }
 2079: 
 2080: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2081: 
 2082: sub updateclickers {
 2083:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2084:     my %clickers;
 2085:     if (ref($idshashref) eq 'HASH') {
 2086:         %clickers=%{$idshashref};
 2087:     } else {
 2088:         return;
 2089:     }
 2090:     my $items='';
 2091:     foreach my $item (keys(%clickers)) {
 2092:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2093:     }
 2094:     $items=~s/\&$//;
 2095:     my $request = "updateclickers:$udom:$action:$items";
 2096:     if ($critical) {
 2097:         return &critical($request,$uhome);
 2098:     } else {
 2099:         return &reply($request,$uhome);
 2100:     }
 2101: }
 2102: 
 2103: # ------------------------------dump from db file owned by domainconfig user
 2104: sub dump_dom {
 2105:     my ($namespace, $udom, $regexp) = @_;
 2106: 
 2107:     $udom ||= $env{'user.domain'};
 2108: 
 2109:     return () unless $udom;
 2110: 
 2111:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2112: }
 2113: 
 2114: # ------------------------------------------ get items from domain db files   
 2115: 
 2116: sub get_dom {
 2117:     my ($namespace,$storearr,$udom,$uhome)=@_;
 2118:     return if ($udom eq 'public');
 2119:     my $items='';
 2120:     foreach my $item (@$storearr) {
 2121:         $items.=&escape($item).'&';
 2122:     }
 2123:     $items=~s/\&$//;
 2124:     if (!$udom) {
 2125:         $udom=$env{'user.domain'};
 2126:         return if ($udom eq 'public');
 2127:         if (defined(&domain($udom,'primary'))) {
 2128:             $uhome=&domain($udom,'primary');
 2129:         } else {
 2130:             undef($uhome);
 2131:         }
 2132:     } else {
 2133:         if (!$uhome) {
 2134:             if (defined(&domain($udom,'primary'))) {
 2135:                 $uhome=&domain($udom,'primary');
 2136:             }
 2137:         }
 2138:     }
 2139:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2140:         my $rep;
 2141:         if ($namespace =~ /^enc/) {
 2142:             $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2143:         } else {
 2144:             $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2145:         }
 2146:         my %returnhash;
 2147:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2148:             return %returnhash;
 2149:         }
 2150:         my @pairs=split(/\&/,$rep);
 2151:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2152:             return @pairs;
 2153:         }
 2154:         my $i=0;
 2155:         foreach my $item (@$storearr) {
 2156:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2157:             $i++;
 2158:         }
 2159:         return %returnhash;
 2160:     } else {
 2161:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2162:     }
 2163: }
 2164: 
 2165: # -------------------------------------------- put items in domain db files 
 2166: 
 2167: sub put_dom {
 2168:     my ($namespace,$storehash,$udom,$uhome)=@_;
 2169:     if (!$udom) {
 2170:         $udom=$env{'user.domain'};
 2171:         if (defined(&domain($udom,'primary'))) {
 2172:             $uhome=&domain($udom,'primary');
 2173:         } else {
 2174:             undef($uhome);
 2175:         }
 2176:     } else {
 2177:         if (!$uhome) {
 2178:             if (defined(&domain($udom,'primary'))) {
 2179:                 $uhome=&domain($udom,'primary');
 2180:             }
 2181:         }
 2182:     } 
 2183:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2184:         my $items='';
 2185:         foreach my $item (keys(%$storehash)) {
 2186:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2187:         }
 2188:         $items=~s/\&$//;
 2189:         if ($namespace =~ /^enc/) {
 2190:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2191:         } else {
 2192:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2193:         }
 2194:     } else {
 2195:         &logthis("put_dom failed - no homeserver and/or domain");
 2196:     }
 2197: }
 2198: 
 2199: # --------------------- newput for items in db file owned by domainconfig user
 2200: sub newput_dom {
 2201:     my ($namespace,$storehash,$udom) = @_;
 2202:     my $result;
 2203:     if (!$udom) {
 2204:         $udom=$env{'user.domain'};
 2205:     }
 2206:     if ($udom) {
 2207:         my $uname = &get_domainconfiguser($udom);
 2208:         $result = &newput($namespace,$storehash,$udom,$uname);
 2209:     }
 2210:     return $result;
 2211: }
 2212: 
 2213: # --------------------- delete for items in db file owned by domainconfig user
 2214: sub del_dom {
 2215:     my ($namespace,$storearr,$udom)=@_;
 2216:     if (ref($storearr) eq 'ARRAY') {
 2217:         if (!$udom) {
 2218:             $udom=$env{'user.domain'};
 2219:         }
 2220:         if ($udom) {
 2221:             my $uname = &get_domainconfiguser($udom); 
 2222:             return &del($namespace,$storearr,$udom,$uname);
 2223:         }
 2224:     }
 2225: }
 2226: 
 2227: # ----------------------------------construct domainconfig user for a domain 
 2228: sub get_domainconfiguser {
 2229:     my ($udom) = @_;
 2230:     return $udom.'-domainconfig';
 2231: }
 2232: 
 2233: sub retrieve_inst_usertypes {
 2234:     my ($udom) = @_;
 2235:     my (%returnhash,@order);
 2236:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2237:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2238:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2239:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2240:     } else {
 2241:         if (defined(&domain($udom,'primary'))) {
 2242:             my $uhome=&domain($udom,'primary');
 2243:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2244:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2245:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2246:                 return (\%returnhash,\@order);
 2247:             }
 2248:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2249:             my @pairs=split(/\&/,$hashitems);
 2250:             foreach my $item (@pairs) {
 2251:                 my ($key,$value)=split(/=/,$item,2);
 2252:                 $key = &unescape($key);
 2253:                 next if ($key =~ /^error: 2 /);
 2254:                 $returnhash{$key}=&thaw_unescape($value);
 2255:             }
 2256:             my @esc_order = split(/\&/,$orderitems);
 2257:             foreach my $item (@esc_order) {
 2258:                 push(@order,&unescape($item));
 2259:             }
 2260:         } else {
 2261:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2262:         }
 2263:         return (\%returnhash,\@order);
 2264:     }
 2265: }
 2266: 
 2267: sub is_domainimage {
 2268:     my ($url) = @_;
 2269:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2270:         if (&domain($1) ne '') {
 2271:             return '1';
 2272:         }
 2273:     }
 2274:     return;
 2275: }
 2276: 
 2277: sub inst_directory_query {
 2278:     my ($srch) = @_;
 2279:     my $udom = $srch->{'srchdomain'};
 2280:     my %results;
 2281:     my $homeserver = &domain($udom,'primary');
 2282:     my $outcome;
 2283:     if ($homeserver ne '') {
 2284:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2285:             if ($srch->{'srchby'} eq 'email') {
 2286:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2287:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2288:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2289:                     (($major == 2) && ($minor < 12))) {
 2290:                     return;
 2291:                 }
 2292:             }
 2293:         }
 2294: 	my $queryid=&reply("querysend:instdirsearch:".
 2295: 			   &escape($srch->{'srchby'}).':'.
 2296: 			   &escape($srch->{'srchterm'}).':'.
 2297: 			   &escape($srch->{'srchtype'}),$homeserver);
 2298: 	my $host=&hostname($homeserver);
 2299: 	if ($queryid !~/^\Q$host\E\_/) {
 2300: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2301: 	    return;
 2302: 	}
 2303: 	my $response = &get_query_reply($queryid);
 2304: 	my $maxtries = 5;
 2305: 	my $tries = 1;
 2306: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2307: 	    $response = &get_query_reply($queryid);
 2308: 	    $tries ++;
 2309: 	}
 2310: 
 2311:         if (!&error($response) && $response ne 'refused') {
 2312:             if ($response eq 'unavailable') {
 2313:                 $outcome = $response;
 2314:             } else {
 2315:                 $outcome = 'ok';
 2316:                 my @matches = split(/\n/,$response);
 2317:                 foreach my $match (@matches) {
 2318:                     my ($key,$value) = split(/=/,$match);
 2319:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2320:                 }
 2321:             }
 2322:         }
 2323:     }
 2324:     return ($outcome,%results);
 2325: }
 2326: 
 2327: sub usersearch {
 2328:     my ($srch) = @_;
 2329:     my $dom = $srch->{'srchdomain'};
 2330:     my %results;
 2331:     my %libserv = &all_library();
 2332:     my $query = 'usersearch';
 2333:     foreach my $tryserver (keys(%libserv)) {
 2334:         if (&host_domain($tryserver) eq $dom) {
 2335:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2336:                 if ($srch->{'srchby'} eq 'email') {
 2337:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2338:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2339:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2340:                              (($major == 2) && ($minor < 12)));
 2341:                 }
 2342:             }
 2343:             my $host=&hostname($tryserver);
 2344:             my $queryid=
 2345:                 &reply("querysend:".&escape($query).':'.
 2346:                        &escape($srch->{'srchby'}).':'.
 2347:                        &escape($srch->{'srchtype'}).':'.
 2348:                        &escape($srch->{'srchterm'}),$tryserver);
 2349:             if ($queryid !~/^\Q$host\E\_/) {
 2350:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2351:                 next;
 2352:             }
 2353:             my $reply = &get_query_reply($queryid);
 2354:             my $maxtries = 1;
 2355:             my $tries = 1;
 2356:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2357:                 $reply = &get_query_reply($queryid);
 2358:                 $tries ++;
 2359:             }
 2360:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2361:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2362:             } else {
 2363:                 my @matches;
 2364:                 if ($reply =~ /\n/) {
 2365:                     @matches = split(/\n/,$reply);
 2366:                 } else {
 2367:                     @matches = split(/\&/,$reply);
 2368:                 }
 2369:                 foreach my $match (@matches) {
 2370:                     my ($uname,$udom,%userhash);
 2371:                     foreach my $entry (split(/:/,$match)) {
 2372:                         my ($key,$value) =
 2373:                             map {&unescape($_);} split(/=/,$entry);
 2374:                         $userhash{$key} = $value;
 2375:                         if ($key eq 'username') {
 2376:                             $uname = $value;
 2377:                         } elsif ($key eq 'domain') {
 2378:                             $udom = $value;
 2379:                         }
 2380:                     }
 2381:                     $results{$uname.':'.$udom} = \%userhash;
 2382:                 }
 2383:             }
 2384:         }
 2385:     }
 2386:     return %results;
 2387: }
 2388: 
 2389: sub get_instuser {
 2390:     my ($udom,$uname,$id) = @_;
 2391:     my $homeserver = &domain($udom,'primary');
 2392:     my ($outcome,%results);
 2393:     if ($homeserver ne '') {
 2394:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2395:                            &escape($id).':'.&escape($udom),$homeserver);
 2396:         my $host=&hostname($homeserver);
 2397:         if ($queryid !~/^\Q$host\E\_/) {
 2398:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2399:             return;
 2400:         }
 2401:         my $response = &get_query_reply($queryid);
 2402:         my $maxtries = 5;
 2403:         my $tries = 1;
 2404:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2405:             $response = &get_query_reply($queryid);
 2406:             $tries ++;
 2407:         }
 2408:         if (!&error($response) && $response ne 'refused') {
 2409:             if ($response eq 'unavailable') {
 2410:                 $outcome = $response;
 2411:             } else {
 2412:                 $outcome = 'ok';
 2413:                 my @matches = split(/\n/,$response);
 2414:                 foreach my $match (@matches) {
 2415:                     my ($key,$value) = split(/=/,$match);
 2416:                     $results{&unescape($key)} = &thaw_unescape($value);
 2417:                 }
 2418:             }
 2419:         }
 2420:     }
 2421:     my %userinfo;
 2422:     if (ref($results{$uname}) eq 'HASH') {
 2423:         %userinfo = %{$results{$uname}};
 2424:     } 
 2425:     return ($outcome,%userinfo);
 2426: }
 2427: 
 2428: sub get_multiple_instusers {
 2429:     my ($udom,$users,$caller) = @_;
 2430:     my ($outcome,$results);
 2431:     if (ref($users) eq 'HASH') {
 2432:         my $count = keys(%{$users}); 
 2433:         my $requested = &freeze_escape($users);
 2434:         my $homeserver = &domain($udom,'primary');
 2435:         if ($homeserver ne '') {
 2436:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2437:             my $host=&hostname($homeserver);
 2438:             if ($queryid !~/^\Q$host\E\_/) {
 2439:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2440:                          ' for host: '.$homeserver.'in domain '.$udom);
 2441:                 return ($outcome,$results);
 2442:             }
 2443:             my $response = &get_query_reply($queryid);
 2444:             my $maxtries = 5;
 2445:             if ($count > 100) {
 2446:                 $maxtries = 1+int($count/20);
 2447:             }
 2448:             my $tries = 1;
 2449:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2450:                 $response = &get_query_reply($queryid);
 2451:                 $tries ++;
 2452:             }
 2453:             if ($response eq '') {
 2454:                 $results = {};
 2455:                 foreach my $key (keys(%{$users})) {
 2456:                     my ($uname,$id);
 2457:                     if ($caller eq 'id') {
 2458:                         $id = $key;
 2459:                     } else {
 2460:                         $uname = $key;
 2461:                     }
 2462:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2463:                     $outcome = $resp;
 2464:                     if ($resp eq 'ok') {
 2465:                         %{$results} = (%{$results}, %info);
 2466:                     } else {
 2467:                         last;
 2468:                     }
 2469:                 }
 2470:             } elsif(!&error($response) && ($response ne 'refused')) {
 2471:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2472:                     $outcome = $response;
 2473:                 } else {
 2474:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2475:                     if ($outcome eq 'ok') {
 2476:                         $results = &thaw_unescape($userdata); 
 2477:                     }
 2478:                 }
 2479:             }
 2480:         }
 2481:     }
 2482:     return ($outcome,$results);
 2483: }
 2484: 
 2485: sub inst_rulecheck {
 2486:     my ($udom,$uname,$id,$item,$rules) = @_;
 2487:     my %returnhash;
 2488:     if ($udom ne '') {
 2489:         if (ref($rules) eq 'ARRAY') {
 2490:             @{$rules} = map {&escape($_);} (@{$rules});
 2491:             my $rulestr = join(':',@{$rules});
 2492:             my $homeserver=&domain($udom,'primary');
 2493:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2494:                 my $response;
 2495:                 if ($item eq 'username') {                
 2496:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2497:                                               ':'.&escape($uname).':'.$rulestr,
 2498:                                               $homeserver));
 2499:                 } elsif ($item eq 'id') {
 2500:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2501:                                               ':'.&escape($id).':'.$rulestr,
 2502:                                               $homeserver));
 2503:                 } elsif ($item eq 'selfcreate') {
 2504:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2505:                                                &escape($udom).':'.&escape($uname).
 2506:                                               ':'.$rulestr,$homeserver));
 2507:                 }
 2508:                 if ($response ne 'refused') {
 2509:                     my @pairs=split(/\&/,$response);
 2510:                     foreach my $item (@pairs) {
 2511:                         my ($key,$value)=split(/=/,$item,2);
 2512:                         $key = &unescape($key);
 2513:                         next if ($key =~ /^error: 2 /);
 2514:                         $returnhash{$key}=&thaw_unescape($value);
 2515:                     }
 2516:                 }
 2517:             }
 2518:         }
 2519:     }
 2520:     return %returnhash;
 2521: }
 2522: 
 2523: sub inst_userrules {
 2524:     my ($udom,$check) = @_;
 2525:     my (%ruleshash,@ruleorder);
 2526:     if ($udom ne '') {
 2527:         my $homeserver=&domain($udom,'primary');
 2528:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2529:             my $response;
 2530:             if ($check eq 'id') {
 2531:                 $response=&reply('instidrules:'.&escape($udom),
 2532:                                  $homeserver);
 2533:             } elsif ($check eq 'email') {
 2534:                 $response=&reply('instemailrules:'.&escape($udom),
 2535:                                  $homeserver);
 2536:             } else {
 2537:                 $response=&reply('instuserrules:'.&escape($udom),
 2538:                                  $homeserver);
 2539:             }
 2540:             if (($response ne 'refused') && ($response ne 'error') && 
 2541:                 ($response ne 'unknown_cmd') && 
 2542:                 ($response ne 'no_such_host')) {
 2543:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2544:                 my @pairs=split(/\&/,$hashitems);
 2545:                 foreach my $item (@pairs) {
 2546:                     my ($key,$value)=split(/=/,$item,2);
 2547:                     $key = &unescape($key);
 2548:                     next if ($key =~ /^error: 2 /);
 2549:                     $ruleshash{$key}=&thaw_unescape($value);
 2550:                 }
 2551:                 my @esc_order = split(/\&/,$orderitems);
 2552:                 foreach my $item (@esc_order) {
 2553:                     push(@ruleorder,&unescape($item));
 2554:                 }
 2555:             }
 2556:         }
 2557:     }
 2558:     return (\%ruleshash,\@ruleorder);
 2559: }
 2560: 
 2561: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2562: 
 2563: sub get_domain_defaults {
 2564:     my ($domain,$ignore_cache) = @_;
 2565:     return if (($domain eq '') || ($domain eq 'public'));
 2566:     my $cachetime = 60*60*24;
 2567:     unless ($ignore_cache) {
 2568:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2569:         if (defined($cached)) {
 2570:             if (ref($result) eq 'HASH') {
 2571:                 return %{$result};
 2572:             }
 2573:         }
 2574:     }
 2575:     my %domdefaults;
 2576:     my %domconfig =
 2577:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2578:                                   'requestcourses','inststatus',
 2579:                                   'coursedefaults','usersessions',
 2580:                                   'requestauthor','selfenrollment',
 2581:                                   'coursecategories','ssl','autoenroll',
 2582:                                   'trust','helpsettings'],$domain);
 2583:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2584:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2585:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2586:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2587:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2588:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2589:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2590:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2591:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2592:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2593:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2594:     } else {
 2595:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2596:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2597:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2598:     }
 2599:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2600:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2601:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2602:         } else {
 2603:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2604:         }
 2605:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2606:         foreach my $item (@usertools) {
 2607:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2608:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2609:             }
 2610:         }
 2611:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2612:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2613:         }
 2614:     }
 2615:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2616:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2617:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2618:         }
 2619:     }
 2620:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2621:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2622:     }
 2623:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2624:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2625:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2626:         }
 2627:     }
 2628:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2629:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2630:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2631:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2632:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2633:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2634:         }
 2635:         foreach my $type (@coursetypes) {
 2636:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2637:                 unless ($type eq 'community') {
 2638:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2639:                 }
 2640:             }
 2641:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2642:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2643:             }
 2644:             if ($domdefaults{'postsubmit'} eq 'on') {
 2645:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2646:                     $domdefaults{$type.'postsubtimeout'} = 
 2647:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2648:                 }
 2649:             }
 2650:         }
 2651:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2652:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2653:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2654:                 if (@clonecodes) {
 2655:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2656:                 }
 2657:             }
 2658:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2659:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2660:         }
 2661:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2662:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2663:         } 
 2664:     }
 2665:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2666:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2667:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2668:         }
 2669:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2670:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2671:         }
 2672:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2673:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2674:         }
 2675:     }
 2676:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2677:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2678:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2679:                             'approval','limit');
 2680:             foreach my $type (@coursetypes) {
 2681:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2682:                     my @mgrdc = ();
 2683:                     foreach my $item (@settings) {
 2684:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2685:                             push(@mgrdc,$item);
 2686:                         }
 2687:                     }
 2688:                     if (@mgrdc) {
 2689:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2690:                     }
 2691:                 }
 2692:             }
 2693:         }
 2694:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2695:             foreach my $type (@coursetypes) {
 2696:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2697:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2698:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2699:                     }
 2700:                 }
 2701:             }
 2702:         }
 2703:     }
 2704:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2705:         $domdefaults{'catauth'} = 'std';
 2706:         $domdefaults{'catunauth'} = 'std';
 2707:         if ($domconfig{'coursecategories'}{'auth'}) {
 2708:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2709:         }
 2710:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2711:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2712:         }
 2713:     }
 2714:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2715:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2716:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2717:         }
 2718:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2719:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2720:         }
 2721:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2722:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2723:         }
 2724:     }
 2725:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2726:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2727:         foreach my $prefix (@prefixes) {
 2728:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2729:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2730:             }
 2731:         }
 2732:     }
 2733:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2734:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2735:     }
 2736:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2737:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2738:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2739:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2740:         }
 2741:     }
 2742:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2743:     return %domdefaults;
 2744: }
 2745: 
 2746: sub get_dom_cats {
 2747:     my ($dom) = @_;
 2748:     return unless (&domain($dom));
 2749:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2750:     unless (defined($cached)) {
 2751:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2752:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2753:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2754:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2755:             } else {
 2756:                 $cats = {};
 2757:             }
 2758:         } else {
 2759:             $cats = {};
 2760:         }
 2761:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
 2762:     }
 2763:     return $cats;
 2764: }
 2765: 
 2766: sub get_dom_instcats {
 2767:     my ($dom) = @_;
 2768:     return unless (&domain($dom));
 2769:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2770:     unless (defined($cached)) {
 2771:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 2772:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 2773:         if ($totcodes > 0) {
 2774:             my $caller = 'global';
 2775:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 2776:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 2777:                 $instcats = {
 2778:                                 codes => \%codes,
 2779:                                 codetitles => \@codetitles,
 2780:                                 cat_titles => \%cat_titles,
 2781:                                 cat_order => \%cat_order,
 2782:                             };
 2783:                 &do_cache_new('instcats',$dom,$instcats,3600);
 2784:             }
 2785:         }
 2786:     }
 2787:     return $instcats;
 2788: }
 2789: 
 2790: sub retrieve_instcodes {
 2791:     my ($coursecodes,$dom) = @_;
 2792:     my $totcodes;
 2793:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 2794:     foreach my $course (keys(%courses)) {
 2795:         if (ref($courses{$course}) eq 'HASH') {
 2796:             if ($courses{$course}{'inst_code'} ne '') {
 2797:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 2798:                 $totcodes ++;
 2799:             }
 2800:         }
 2801:     }
 2802:     return $totcodes;
 2803: }
 2804: 
 2805: sub course_portal_url {
 2806:     my ($cnum,$cdom) = @_;
 2807:     my $chome = &homeserver($cnum,$cdom);
 2808:     my $hostname = &hostname($chome);
 2809:     my $protocol = $protocol{$chome};
 2810:     $protocol = 'http' if ($protocol ne 'https');
 2811:     my %domdefaults = &get_domain_defaults($cdom);
 2812:     my $firsturl;
 2813:     if ($domdefaults{'portal_def'}) {
 2814:         $firsturl = $domdefaults{'portal_def'};
 2815:     } else {
 2816:         $firsturl = $protocol.'://'.$hostname;
 2817:     }
 2818:     return $firsturl;
 2819: }
 2820: 
 2821: # --------------------------------------------- Get domain config for passwords
 2822: 
 2823: sub get_passwdconf {
 2824:     my ($dom) = @_;
 2825:     my (%passwdconf,$gotconf,$lookup);
 2826:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2827:     if (defined($cached)) {
 2828:         if (ref($result) eq 'HASH') {
 2829:             %passwdconf = %{$result};
 2830:             $gotconf = 1;
 2831:         }
 2832:     }
 2833:     unless ($gotconf) {
 2834:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2835:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2836:             %passwdconf = %{$domconfig{'passwords'}};
 2837:         }
 2838:         my $cachetime = 24*60*60;
 2839:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2840:     }
 2841:     return %passwdconf;
 2842: }
 2843: 
 2844: # --------------------------------------------------- Assign a key to a student
 2845: 
 2846: sub assign_access_key {
 2847: #
 2848: # a valid key looks like uname:udom#comments
 2849: # comments are being appended
 2850: #
 2851:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2852:     $kdom=
 2853:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2854:     $knum=
 2855:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2856:     $cdom=
 2857:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2858:     $cnum=
 2859:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2860:     $udom=$env{'user.name'} unless (defined($udom));
 2861:     $uname=$env{'user.domain'} unless (defined($uname));
 2862:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2863:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2864:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2865:                                                   # assigned to this person
 2866:                                                   # - this should not happen,
 2867:                                                   # unless something went wrong
 2868:                                                   # the first time around
 2869: # ready to assign
 2870:         $logentry=$1.'; '.$logentry;
 2871:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2872:                                                  $kdom,$knum) eq 'ok') {
 2873: # key now belongs to user
 2874: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2875:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2876:                 &appenv({'environment.'.$envkey => $ckey});
 2877:                 return 'ok';
 2878:             } else {
 2879:                 return 
 2880:   'error: Count not permanently assign key, will need to be re-entered later.';
 2881: 	    }
 2882:         } else {
 2883:             return 'error: Could not assign key, try again later.';
 2884:         }
 2885:     } elsif (!$existing{$ckey}) {
 2886: # the key does not exist
 2887: 	return 'error: The key does not exist';
 2888:     } else {
 2889: # the key is somebody else's
 2890: 	return 'error: The key is already in use';
 2891:     }
 2892: }
 2893: 
 2894: # ------------------------------------------ put an additional comment on a key
 2895: 
 2896: sub comment_access_key {
 2897: #
 2898: # a valid key looks like uname:udom#comments
 2899: # comments are being appended
 2900: #
 2901:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2902:     $cdom=
 2903:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2904:     $cnum=
 2905:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2906:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2907:     if ($existing{$ckey}) {
 2908:         $existing{$ckey}.='; '.$logentry;
 2909: # ready to assign
 2910:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2911:                                                  $cdom,$cnum) eq 'ok') {
 2912: 	    return 'ok';
 2913:         } else {
 2914: 	    return 'error: Count not store comment.';
 2915:         }
 2916:     } else {
 2917: # the key does not exist
 2918: 	return 'error: The key does not exist';
 2919:     }
 2920: }
 2921: 
 2922: # ------------------------------------------------------ Generate a set of keys
 2923: 
 2924: sub generate_access_keys {
 2925:     my ($number,$cdom,$cnum,$logentry)=@_;
 2926:     $cdom=
 2927:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2928:     $cnum=
 2929:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2930:     unless (&allowed('mky',$cdom)) { return 0; }
 2931:     unless (($cdom) && ($cnum)) { return 0; }
 2932:     if ($number>10000) { return 0; }
 2933:     sleep(2); # make sure don't get same seed twice
 2934:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2935:     my $total=0;
 2936:     for (my $i=1;$i<=$number;$i++) {
 2937:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2938:                   sprintf("%lx",int(100000*rand)).'-'.
 2939:                   sprintf("%lx",int(100000*rand));
 2940:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2941:        $newkey=~s/0/h/g; # and also 0 and O
 2942:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2943:        if ($existing{$newkey}) {
 2944:            $i--;
 2945:        } else {
 2946: 	  if (&put('accesskeys',
 2947:               { $newkey => '# generated '.localtime().
 2948:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 2949:                            '; '.$logentry },
 2950: 		   $cdom,$cnum) eq 'ok') {
 2951:               $total++;
 2952: 	  }
 2953:        }
 2954:     }
 2955:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 2956:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 2957:     return $total;
 2958: }
 2959: 
 2960: # ------------------------------------------------------- Validate an accesskey
 2961: 
 2962: sub validate_access_key {
 2963:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 2964:     $cdom=
 2965:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2966:     $cnum=
 2967:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2968:     $udom=$env{'user.domain'} unless (defined($udom));
 2969:     $uname=$env{'user.name'} unless (defined($uname));
 2970:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2971:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 2972: }
 2973: 
 2974: # ------------------------------------- Find the section of student in a course
 2975: sub devalidate_getsection_cache {
 2976:     my ($udom,$unam,$courseid)=@_;
 2977:     my $hashid="$udom:$unam:$courseid";
 2978:     &devalidate_cache_new('getsection',$hashid);
 2979: }
 2980: 
 2981: sub courseid_to_courseurl {
 2982:     my ($courseid) = @_;
 2983:     #already url style courseid
 2984:     return $courseid if ($courseid =~ m{^/});
 2985: 
 2986:     if (exists($env{'course.'.$courseid.'.num'})) {
 2987: 	my $cnum = $env{'course.'.$courseid.'.num'};
 2988: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 2989: 	return "/$cdom/$cnum";
 2990:     }
 2991: 
 2992:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 2993:     if (exists($courseinfo{'num'})) {
 2994: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 2995:     }
 2996: 
 2997:     return undef;
 2998: }
 2999: 
 3000: sub getsection {
 3001:     my ($udom,$unam,$courseid)=@_;
 3002:     my $cachetime=1800;
 3003: 
 3004:     my $hashid="$udom:$unam:$courseid";
 3005:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3006:     if (defined($cached)) { return $result; }
 3007: 
 3008:     my %Pending; 
 3009:     my %Expired;
 3010:     #
 3011:     # Each role can either have not started yet (pending), be active, 
 3012:     #    or have expired.
 3013:     #
 3014:     # If there is an active role, we are done.
 3015:     #
 3016:     # If there is more than one role which has not started yet, 
 3017:     #     choose the one which will start sooner
 3018:     # If there is one role which has not started yet, return it.
 3019:     #
 3020:     # If there is more than one expired role, choose the one which ended last.
 3021:     # If there is a role which has expired, return it.
 3022:     #
 3023:     $courseid = &courseid_to_courseurl($courseid);
 3024:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3025:     foreach my $key (keys(%roleshash)) {
 3026:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3027:         my $section=$1;
 3028:         if ($key eq $courseid.'_st') { $section=''; }
 3029:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3030:         my $now=time;
 3031:         if (defined($end) && $end && ($now > $end)) {
 3032:             $Expired{$end}=$section;
 3033:             next;
 3034:         }
 3035:         if (defined($start) && $start && ($now < $start)) {
 3036:             $Pending{$start}=$section;
 3037:             next;
 3038:         }
 3039:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3040:     }
 3041:     #
 3042:     # Presumedly there will be few matching roles from the above
 3043:     # loop and the sorting time will be negligible.
 3044:     if (scalar(keys(%Pending))) {
 3045:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3046:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3047:     } 
 3048:     if (scalar(keys(%Expired))) {
 3049:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3050:         my $time = pop(@sorted);
 3051:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3052:     }
 3053:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3054: }
 3055: 
 3056: sub save_cache {
 3057:     &purge_remembered();
 3058:     #&Apache::loncommon::validate_page();
 3059:     undef(%env);
 3060:     undef($env_loaded);
 3061: }
 3062: 
 3063: my $to_remember=-1;
 3064: my %remembered;
 3065: my %accessed;
 3066: my $kicks=0;
 3067: my $hits=0;
 3068: sub make_key {
 3069:     my ($name,$id) = @_;
 3070:     if (length($id) > 65 
 3071: 	&& length(&escape($id)) > 200) {
 3072: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3073:     }
 3074:     return &escape($name.':'.$id);
 3075: }
 3076: 
 3077: sub devalidate_cache_new {
 3078:     my ($name,$id,$debug) = @_;
 3079:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3080:     my $remembered_id=$name.':'.$id;
 3081:     $id=&make_key($name,$id);
 3082:     $memcache->delete($id);
 3083:     delete($remembered{$remembered_id});
 3084:     delete($accessed{$remembered_id});
 3085: }
 3086: 
 3087: sub is_cached_new {
 3088:     my ($name,$id,$debug) = @_;
 3089:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3090:     if (exists($remembered{$remembered_id})) {
 3091: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3092: 	$accessed{$remembered_id}=[&gettimeofday()];
 3093: 	$hits++;
 3094: 	return ($remembered{$remembered_id},1);
 3095:     }
 3096:     $id=&make_key($name,$id);
 3097:     my $value = $memcache->get($id);
 3098:     if (!(defined($value))) {
 3099: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3100: 	return (undef,undef);
 3101:     }
 3102:     if ($value eq '__undef__') {
 3103: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3104: 	$value=undef;
 3105:     }
 3106:     &make_room($remembered_id,$value,$debug);
 3107:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3108:     return ($value,1);
 3109: }
 3110: 
 3111: sub do_cache_new {
 3112:     my ($name,$id,$value,$time,$debug) = @_;
 3113:     my $remembered_id=$name.':'.$id;
 3114:     $id=&make_key($name,$id);
 3115:     my $setvalue=$value;
 3116:     if (!defined($setvalue)) {
 3117: 	$setvalue='__undef__';
 3118:     }
 3119:     if (!defined($time) ) {
 3120: 	$time=600;
 3121:     }
 3122:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3123:     my $result = $memcache->set($id,$setvalue,$time);
 3124:     if (! $result) {
 3125: 	&logthis("caching of id -> $id  failed");
 3126: 	$memcache->disconnect_all();
 3127:     }
 3128:     # need to make a copy of $value
 3129:     &make_room($remembered_id,$value,$debug);
 3130:     return $value;
 3131: }
 3132: 
 3133: sub make_room {
 3134:     my ($remembered_id,$value,$debug)=@_;
 3135: 
 3136:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3137:                                     : $value;
 3138:     if ($to_remember<0) { return; }
 3139:     $accessed{$remembered_id}=[&gettimeofday()];
 3140:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3141:     my $to_kick;
 3142:     my $max_time=0;
 3143:     foreach my $other (keys(%accessed)) {
 3144: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3145: 	    $to_kick=$other;
 3146: 	    $max_time=&tv_interval($accessed{$other});
 3147: 	}
 3148:     }
 3149:     delete($remembered{$to_kick});
 3150:     delete($accessed{$to_kick});
 3151:     $kicks++;
 3152:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3153:     return;
 3154: }
 3155: 
 3156: sub purge_remembered {
 3157:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3158:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3159:     undef(%remembered);
 3160:     undef(%accessed);
 3161: }
 3162: # ------------------------------------- Read an entry from a user's environment
 3163: 
 3164: sub userenvironment {
 3165:     my ($udom,$unam,@what)=@_;
 3166:     my $items;
 3167:     foreach my $item (@what) {
 3168:         $items.=&escape($item).'&';
 3169:     }
 3170:     $items=~s/\&$//;
 3171:     my %returnhash=();
 3172:     my $uhome = &homeserver($unam,$udom);
 3173:     unless ($uhome eq 'no_host') {
 3174:         my @answer=split(/\&/, 
 3175:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3176:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3177:             return %returnhash;
 3178:         }
 3179:         my $i;
 3180:         for ($i=0;$i<=$#what;$i++) {
 3181: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3182:         }
 3183:     }
 3184:     return %returnhash;
 3185: }
 3186: 
 3187: # ---------------------------------------------------------- Get a studentphoto
 3188: sub studentphoto {
 3189:     my ($udom,$unam,$ext) = @_;
 3190:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3191:     if (defined($env{'request.course.id'})) {
 3192:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3193:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3194:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3195:             } else {
 3196:                 my ($result,$perm_reqd)=
 3197: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3198:                 if ($result eq 'ok') {
 3199:                     if (!($perm_reqd eq 'yes')) {
 3200:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3201:                     }
 3202:                 }
 3203:             }
 3204:         }
 3205:     } else {
 3206:         my ($result,$perm_reqd) = 
 3207: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3208:         if ($result eq 'ok') {
 3209:             if (!($perm_reqd eq 'yes')) {
 3210:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3211:             }
 3212:         }
 3213:     }
 3214:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3215: }
 3216: 
 3217: sub retrievestudentphoto {
 3218:     my ($udom,$unam,$ext,$type) = @_;
 3219:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3220:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3221:     if ($ret eq 'ok') {
 3222:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3223:         if ($type eq 'thumbnail') {
 3224:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3225:         }
 3226:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3227:         return $tokenurl;
 3228:     } else {
 3229:         if ($type eq 'thumbnail') {
 3230:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3231:         } else { 
 3232:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3233:         }
 3234:     }
 3235: }
 3236: 
 3237: # -------------------------------------------------------------------- New chat
 3238: 
 3239: sub chatsend {
 3240:     my ($newentry,$anon,$group)=@_;
 3241:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3242:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3243:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3244:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3245: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3246: 		   &escape($newentry)).':'.$group,$chome);
 3247: }
 3248: 
 3249: # ------------------------------------------ Find current version of a resource
 3250: 
 3251: sub getversion {
 3252:     my $fname=&clutter(shift);
 3253:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3254:     return &currentversion(&filelocation('',$fname));
 3255: }
 3256: 
 3257: sub currentversion {
 3258:     my $fname=shift;
 3259:     my $author=$fname;
 3260:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3261:     my ($udom,$uname)=split(/\//,$author);
 3262:     my $home=&homeserver($uname,$udom);
 3263:     if ($home eq 'no_host') { 
 3264:         return -1; 
 3265:     }
 3266:     my $answer=&reply("currentversion:$fname",$home);
 3267:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3268: 	return -1;
 3269:     }
 3270:     return $answer;
 3271: }
 3272: 
 3273: #
 3274: # Return special version number of resource if set by override, empty otherwise
 3275: #
 3276: sub usedversion {
 3277:     my $fname=shift;
 3278:     unless ($fname) { $fname=$env{'request.uri'}; }
 3279:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3280:     if ($urlversion) { return $urlversion; }
 3281:     return '';
 3282: }
 3283: 
 3284: # ----------------------------- Subscribe to a resource, return URL if possible
 3285: 
 3286: sub subscribe {
 3287:     my $fname=shift;
 3288:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3289:     $fname=~s/[\n\r]//g;
 3290:     my $author=$fname;
 3291:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3292:     my ($udom,$uname)=split(/\//,$author);
 3293:     my $home=homeserver($uname,$udom);
 3294:     if ($home eq 'no_host') {
 3295:         return 'not_found';
 3296:     }
 3297:     my $answer=reply("sub:$fname",$home);
 3298:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3299: 	$answer.=' by '.$home;
 3300:     }
 3301:     return $answer;
 3302: }
 3303:     
 3304: # -------------------------------------------------------------- Replicate file
 3305: 
 3306: sub repcopy {
 3307:     my $filename=shift;
 3308:     $filename=~s/\/+/\//g;
 3309:     my $londocroot = $perlvar{'lonDocRoot'};
 3310:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3311:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3312:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3313: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3314: 	return &repcopy_userfile($filename);
 3315:     }
 3316:     $filename=~s/[\n\r]//g;
 3317:     my $transname="$filename.in.transfer";
 3318: # FIXME: this should flock
 3319:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3320:     my $remoteurl=subscribe($filename);
 3321:     if ($remoteurl =~ /^con_lost by/) {
 3322: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3323:            return 'unavailable';
 3324:     } elsif ($remoteurl eq 'not_found') {
 3325: 	   #&logthis("Subscribe returned not_found: $filename");
 3326: 	   return 'not_found';
 3327:     } elsif ($remoteurl =~ /^rejected by/) {
 3328: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3329:            return 'forbidden';
 3330:     } elsif ($remoteurl eq 'directory') {
 3331:            return 'ok';
 3332:     } else {
 3333:         my $author=$filename;
 3334:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3335:         my ($udom,$uname)=split(/\//,$author);
 3336:         my $home=homeserver($uname,$udom);
 3337:         unless ($home eq $perlvar{'lonHostID'}) {
 3338:            my @parts=split(/\//,$filename);
 3339:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3340:            if ($path ne "$londocroot/res") {
 3341:                &logthis("Malconfiguration for replication: $filename");
 3342: 	       return 'bad_request';
 3343:            }
 3344:            my $count;
 3345:            for ($count=5;$count<$#parts;$count++) {
 3346:                $path.="/$parts[$count]";
 3347:                if ((-e $path)!=1) {
 3348: 		   mkdir($path,0777);
 3349:                }
 3350:            }
 3351:            my $request=new HTTP::Request('GET',"$remoteurl");
 3352:            my $response;
 3353:            if ($remoteurl =~ m{/raw/}) {
 3354:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3355:            } else {
 3356:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3357:            }
 3358:            if ($response->is_error()) {
 3359: 	       unlink($transname);
 3360:                my $message=$response->status_line;
 3361:                &logthis("<font color=\"blue\">WARNING:"
 3362:                        ." LWP get: $message: $filename</font>");
 3363:                return 'unavailable';
 3364:            } else {
 3365: 	       if ($remoteurl!~/\.meta$/) {
 3366:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3367:                   my $mresponse;
 3368:                   if ($remoteurl =~ m{/raw/}) {
 3369:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3370:                   } else {
 3371:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3372:                   }
 3373:                   if ($mresponse->is_error()) {
 3374: 		      unlink($filename.'.meta');
 3375:                       &logthis(
 3376:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3377:                   }
 3378: 	       }
 3379:                rename($transname,$filename);
 3380:                return 'ok';
 3381:            }
 3382:        }
 3383:     }
 3384: }
 3385: 
 3386: # ------------------------------------------------- Unsubscribe from a resource
 3387: 
 3388: sub unsubscribe {
 3389:     my ($fname) = @_;
 3390:     my $answer;
 3391:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3392:     $fname=~s/[\n\r]//g;
 3393:     my $author=$fname;
 3394:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3395:     my ($udom,$uname)=split(/\//,$author);
 3396:     my $home=homeserver($uname,$udom);
 3397:     if ($home eq 'no_host') {
 3398:         $answer = 'no_host';
 3399:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3400:         $answer = 'home';
 3401:     } else {
 3402:         my $defdom = $perlvar{'lonDefDomain'};
 3403:         if (&will_trust('content',$defdom,$udom)) {
 3404:             $answer = reply("unsub:$fname",$home);
 3405:         } else {
 3406:             $answer = 'untrusted';
 3407:         }
 3408:     }
 3409:     return $answer;
 3410: }
 3411: 
 3412: # ------------------------------------------------ Get server side include body
 3413: sub ssi_body {
 3414:     my ($filelink,%form)=@_;
 3415:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3416:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3417:     }
 3418:     my $output='';
 3419:     my $response;
 3420:     if ($filelink=~/^https?\:/) {
 3421:        ($output,$response)=&externalssi($filelink);
 3422:     } else {
 3423:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3424:        $filelink .= 'inhibitmenu=yes';
 3425:        ($output,$response)=&ssi($filelink,%form);
 3426:     }
 3427:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3428:     $output=~s/^.*?\<body[^\>]*\>//si;
 3429:     $output=~s/\<\/body\s*\>.*?$//si;
 3430:     if (wantarray) {
 3431:         return ($output, $response);
 3432:     } else {
 3433:         return $output;
 3434:     }
 3435: }
 3436: 
 3437: # --------------------------------------------------------- Server Side Include
 3438: 
 3439: sub absolute_url {
 3440:     my ($host_name) = @_;
 3441:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3442:     if ($host_name eq '') {
 3443: 	$host_name = $ENV{'SERVER_NAME'};
 3444:     }
 3445:     return $protocol.$host_name;
 3446: }
 3447: 
 3448: #
 3449: #   Server side include.
 3450: # Parameters:
 3451: #  fn     Possibly encrypted resource name/id.
 3452: #  form   Hash that describes how the rendering should be done
 3453: #         and other things.
 3454: # Returns:
 3455: #   Scalar context: The content of the response.
 3456: #   Array context:  2 element list of the content and the full response object.
 3457: #     
 3458: sub ssi {
 3459: 
 3460:     my ($fn,%form)=@_;
 3461:     my $request;
 3462: 
 3463:     $form{'no_update_last_known'}=1;
 3464:     &Apache::lonenc::check_encrypt(\$fn);
 3465:     if (%form) {
 3466:       $request=new HTTP::Request('POST',&absolute_url().$fn);
 3467:       $request->content(join('&',map { 
 3468:             my $name = escape($_);
 3469:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3470:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3471:             : &escape($form{$_}) );    
 3472:         } keys(%form)));
 3473:     } else {
 3474:       $request=new HTTP::Request('GET',&absolute_url().$fn);
 3475:     }
 3476: 
 3477:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3478:     my $lonhost = $perlvar{'lonHostID'};
 3479:     my $islocal;
 3480:     if (($env{'request.course.id'}) &&
 3481:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3482:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3483:         ($form{'grade_symb'} ne '') &&
 3484:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3485:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3486:         $islocal = 1;
 3487:     }
 3488:     my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3489:                                                 '','','',$islocal);
 3490: 
 3491:     if (wantarray) {
 3492: 	return ($response->content, $response);
 3493:     } else {
 3494: 	return $response->content;
 3495:     }
 3496: }
 3497: 
 3498: sub externalssi {
 3499:     my ($url)=@_;
 3500:     my $request=new HTTP::Request('GET',$url);
 3501:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3502:     if (wantarray) {
 3503:         return ($response->content, $response);
 3504:     } else {
 3505:         return $response->content;
 3506:     }
 3507: }
 3508: 
 3509: 
 3510: # If the local copy of a replicated resource is outdated, trigger a  
 3511: # connection from the homeserver to flush the delayed queue. If no update 
 3512: # happens, remove local copies of outdated resource (and corresponding
 3513: # metadata file).
 3514: 
 3515: sub remove_stale_resfile {
 3516:     my ($url) = @_;
 3517:     my $removed;
 3518:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3519:         my $audom = $1;
 3520:         my $auname = $2;
 3521:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3522:             my $homeserver = &homeserver($auname,$audom);
 3523:             unless (($homeserver eq 'no_host') ||
 3524:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3525:                 my $fname = &filelocation('',$url);
 3526:                 if (-e $fname) {
 3527:                     my $hostname = &hostname($homeserver);
 3528:                     if ($hostname) {
 3529:                         my $protocol = $protocol{$homeserver};
 3530:                         $protocol = 'http' if ($protocol ne 'https');
 3531:                         my $uri = &declutter($url);
 3532:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3533:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3534:                         if ($response->is_success()) {
 3535:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3536:                             my $locmodtime = (stat($fname))[9];
 3537:                             if ($locmodtime < $remmodtime) {
 3538:                                 my $stale;
 3539:                                 my $answer = &reply('pong',$homeserver);
 3540:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3541:                                     sleep(0.2);
 3542:                                     $locmodtime = (stat($fname))[9];
 3543:                                     if ($locmodtime < $remmodtime) {
 3544:                                         my $posstransfer = $fname.'.in.transfer';
 3545:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3546:                                             $removed = 1;
 3547:                                         } else {
 3548:                                             $stale = 1;
 3549:                                         }
 3550:                                     } else {
 3551:                                         $removed = 1;
 3552:                                     }
 3553:                                 } else {
 3554:                                     $stale = 1;
 3555:                                 }
 3556:                                 if ($stale) {
 3557:                                     if (unlink($fname)) {
 3558:                                         if ($uri!~/\.meta$/) {
 3559:                                             if (-e $fname.'.meta') {
 3560:                                                 unlink($fname.'.meta');
 3561:                                             }
 3562:                                         }
 3563:                                         my $unsubresult = &unsubscribe($fname);
 3564:                                         unless ($unsubresult eq 'ok') {
 3565:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3566:                                         }
 3567:                                         $removed = 1;
 3568:                                     }
 3569:                                 }
 3570:                             }
 3571:                         }
 3572:                     }
 3573:                 }
 3574:             }
 3575:         }
 3576:     }
 3577:     return $removed;
 3578: }
 3579: 
 3580: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3581: 
 3582: sub allowuploaded {
 3583:     my ($srcurl,$url)=@_;
 3584:     $url=&clutter(&declutter($url));
 3585:     my $dir=$url;
 3586:     $dir=~s/\/[^\/]+$//;
 3587:     my %httpref=();
 3588:     my $httpurl=&hreflocation('',$url);
 3589:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3590:     &Apache::lonnet::appenv(\%httpref);
 3591: }
 3592: 
 3593: #
 3594: # Determine if the current user should be able to edit a particular resource,
 3595: # when viewing in course context.
 3596: # (a) When viewing resource used to determine if "Edit" item is included in 
 3597: #     Functions.
 3598: # (b) When displaying folder contents in course editor, used to determine if
 3599: #     "Edit" link will be displayed alongside resource.
 3600: #
 3601: #  input: six args -- filename (decluttered), course number, course domain,
 3602: #                   url, symb (if registered) and group (if this is a group
 3603: #                   item -- e.g., bulletin board, group page etc.).
 3604: #  output: array of five scalars -- 
 3605: #          $cfile -- url for file editing if editable on current server
 3606: #          $home -- homeserver of resource (i.e., for author if published,
 3607: #                                           or course if uploaded.).
 3608: #          $switchserver --  1 if server switch will be needed.
 3609: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3610: #          $forceview -- 1 if icon/link should be to go to view mode
 3611: #
 3612: 
 3613: sub can_edit_resource {
 3614:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3615:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3616: #
 3617: # For aboutme pages user can only edit his/her own.
 3618: #
 3619:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3620:         my ($sdom,$sname) = ($1,$2);
 3621:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3622:             $home = $env{'user.home'};
 3623:             $cfile = $resurl;
 3624:             if ($env{'form.forceedit'}) {
 3625:                 $forceview = 1;
 3626:             } else {
 3627:                 $forceedit = 1;
 3628:             }
 3629:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3630:         } else {
 3631:             return;
 3632:         }
 3633:     }
 3634: 
 3635:     if ($env{'request.course.id'}) {
 3636:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3637:         if ($group ne '') {
 3638: # if this is a group homepage or group bulletin board, check group privs
 3639:             my $allowed = 0;
 3640:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3641:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3642:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3643:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3644:                     $allowed = 1;
 3645:                 }
 3646:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3647:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3648:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3649:                     $allowed = 1;
 3650:                 }
 3651:             }
 3652:             if ($allowed) {
 3653:                 $home=&homeserver($cnum,$cdom);
 3654:                 if ($env{'form.forceedit'}) {
 3655:                     $forceview = 1;
 3656:                 } else {
 3657:                     $forceedit = 1;
 3658:                 }
 3659:                 $cfile = $resurl;
 3660:             } else {
 3661:                 return;
 3662:             }
 3663:         } else {
 3664:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3665:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3666:                     return;
 3667:                 }
 3668:             } elsif (!$crsedit) {
 3669: #
 3670: # No edit allowed where CC has switched to student role.
 3671: #
 3672:                 return;
 3673:             }
 3674:         }
 3675:     }
 3676: 
 3677:     if ($file ne '') {
 3678:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3679:             if (&is_course_upload($file,$cnum,$cdom)) {
 3680:                 $uploaded = 1;
 3681:                 $incourse = 1;
 3682:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3683:                     $cfile = &hreflocation('',$file);
 3684:                     if ($env{'form.forceedit'}) {
 3685:                         $forceview = 1;
 3686:                     } else {
 3687:                         $forceedit = 1;
 3688:                     }
 3689:                 }
 3690:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3691:                 $incourse = 1;
 3692:                 if ($env{'form.forceedit'}) {
 3693:                     $forceview = 1;
 3694:                 } else {
 3695:                     $forceedit = 1;
 3696:                 }
 3697:                 $cfile = $resurl;
 3698:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3699:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3700:                     $incourse = 1;
 3701:                     if ($env{'form.forceedit'}) {
 3702:                         $forceview = 1;
 3703:                     } else {
 3704:                         $forceedit = 1;
 3705:                     }
 3706:                     $cfile = $resurl;
 3707:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3708:                     $incourse = 1;
 3709:                     $cfile = $resurl.'/smpedit';
 3710:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3711:                     $incourse = 1;
 3712:                     if ($env{'form.forceedit'}) {
 3713:                         $forceview = 1;
 3714:                     } else {
 3715:                         $forceedit = 1;
 3716:                     }
 3717:                     $cfile = $resurl;
 3718:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3719:                     my ($map,$id,$res) = &decode_symb($symb);
 3720:                     if ($map =~ /\.page$/) {
 3721:                         $incourse = 1;
 3722:                         if ($env{'form.forceedit'}) {
 3723:                             $forceview = 1;
 3724:                             $cfile = $map;
 3725:                         } else {
 3726:                             $forceedit = 1;
 3727:                             $cfile =  '/adm/wrapper'.$resurl;
 3728:                         }
 3729:                     }
 3730:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3731:                     $incourse = 1;
 3732:                     if ($env{'form.forceedit'}) {
 3733:                         $forceview = 1;
 3734:                     } else {
 3735:                         $forceedit = 1;
 3736:                     }
 3737:                     $cfile = $resurl;
 3738:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3739:                     $incourse = 1;
 3740:                     if ($env{'form.forceedit'}) {
 3741:                         $forceview = 1;
 3742:                     } else {
 3743:                         $forceedit = 1;
 3744:                     }
 3745:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3746:                 }
 3747:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3748:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3749:                 if (&is_on_map($template)) { 
 3750:                     $incourse = 1;
 3751:                     $forceview = 1;
 3752:                     $cfile = $template;
 3753:                 }
 3754:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3755:                 $incourse = 1;
 3756:                 if ($env{'form.forceedit'}) {
 3757:                     $forceview = 1;
 3758:                 } else {
 3759:                     $forceedit = 1;
 3760:                 }
 3761:                 $cfile = $resurl;
 3762:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3763:                 $incourse = 1;
 3764:                 if ($env{'form.forceedit'}) {
 3765:                     $forceview = 1;
 3766:                 } else {
 3767:                     $forceedit = 1;
 3768:                 }
 3769:                 $cfile = $resurl;
 3770:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3771:                 $incourse = 1;
 3772:                 $forceview = 1;
 3773:                 if ($symb) {
 3774:                     my ($map,$id,$res)=&decode_symb($symb);
 3775:                     $env{'request.symb'} = $symb;
 3776:                     $cfile = &clutter($res);
 3777:                 } else {
 3778:                     $cfile = $env{'form.suppurl'};
 3779:                     my $escfile = &unescape($cfile);
 3780:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3781:                         $cfile = '/adm/wrapper'.$escfile;
 3782:                     } else {
 3783:                         $escfile =~ s{^http://}{};
 3784:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3785:                     }
 3786:                 }
 3787:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3788:                 if ($env{'form.forceedit'}) {
 3789:                     $forceview = 1;
 3790:                 } else {
 3791:                     $forceedit = 1;
 3792:                 }
 3793:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3794:             }
 3795:         }
 3796:         if ($uploaded || $incourse) {
 3797:             $home=&homeserver($cnum,$cdom);
 3798:         } elsif ($file !~ m{/$}) {
 3799:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3800:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3801:             # Check that the user has permission to edit this resource
 3802:             my $setpriv = 1;
 3803:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3804:             if (defined($cfudom)) {
 3805:                 $home=&homeserver($cfuname,$cfudom);
 3806:                 $cfile=$file;
 3807:             }
 3808:         }
 3809:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3810:             (($home ne '') && ($home ne 'no_host'))) {
 3811:             my @ids=&current_machine_ids();
 3812:             unless (grep(/^\Q$home\E$/,@ids)) {
 3813:                 $switchserver=1;
 3814:             }
 3815:         }
 3816:     }
 3817:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3818: }
 3819: 
 3820: sub is_course_upload {
 3821:     my ($file,$cnum,$cdom) = @_;
 3822:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3823:     $uploadpath =~ s{^\/}{};
 3824:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3825:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3826:         return 1;
 3827:     }
 3828:     return;
 3829: }
 3830: 
 3831: sub in_course {
 3832:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3833:     if ($hideprivileged) {
 3834:         my $skipuser;
 3835:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3836:         my @possdoms = ($cdom);  
 3837:         if ($coursehash{'checkforpriv'}) { 
 3838:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3839:         }
 3840:         if (&privileged($uname,$udom,\@possdoms)) {
 3841:             $skipuser = 1;
 3842:             if ($coursehash{'nothideprivileged'}) {
 3843:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3844:                     my $user;
 3845:                     if ($item =~ /:/) {
 3846:                         $user = $item;
 3847:                     } else {
 3848:                         $user = join(':',split(/[\@]/,$item));
 3849:                     }
 3850:                     if ($user eq $uname.':'.$udom) {
 3851:                         undef($skipuser);
 3852:                         last;
 3853:                     }
 3854:                 }
 3855:             }
 3856:             if ($skipuser) {
 3857:                 return 0;
 3858:             }
 3859:         }
 3860:     }
 3861:     $type ||= 'any';
 3862:     if (!defined($cdom) || !defined($cnum)) {
 3863:         my $cid  = $env{'request.course.id'};
 3864:         $cdom = $env{'course.'.$cid.'.domain'};
 3865:         $cnum = $env{'course.'.$cid.'.num'};
 3866:     }
 3867:     my $typesref;
 3868:     if (($type eq 'any') || ($type eq 'all')) {
 3869:         $typesref = ['active','previous','future'];
 3870:     } elsif ($type eq 'previous' || $type eq 'future') {
 3871:         $typesref = [$type];
 3872:     }
 3873:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3874:                               $typesref,undef,[$cdom]);
 3875:     my ($tmp) = keys(%roles);
 3876:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3877:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3878:     if (@course_roles > 0) {
 3879:         return 1;
 3880:     }
 3881:     return 0;
 3882: }
 3883: 
 3884: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3885: # input: action, courseID, current domain, intended
 3886: #        path to file, source of file, instruction to parse file for objects,
 3887: #        ref to hash for embedded objects,
 3888: #        ref to hash for codebase of java objects.
 3889: #        reference to scalar to accommodate mime type determined
 3890: #          from File::MMagic if $parser = parse.
 3891: #
 3892: # output: url to file (if action was uploaddoc), 
 3893: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3894: #
 3895: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3896: # course.
 3897: #
 3898: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3899: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3900: #          course's home server.
 3901: #
 3902: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3903: #          be copied from $source (current location) to 
 3904: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3905: #         and will then be copied to
 3906: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3907: #         course's home server.
 3908: #
 3909: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3910: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3911: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3912: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3913: #         in course's home server.
 3914: #
 3915: 
 3916: sub process_coursefile {
 3917:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3918:         $mimetype)=@_;
 3919:     my $fetchresult;
 3920:     my $home=&homeserver($docuname,$docudom);
 3921:     if ($action eq 'propagate') {
 3922:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3923: 			     $home);
 3924:     } else {
 3925:         my $fpath = '';
 3926:         my $fname = $file;
 3927:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3928:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3929:         my $filepath = &build_filepath($fpath);
 3930:         if ($action eq 'copy') {
 3931:             if ($source eq '') {
 3932:                 $fetchresult = 'no source file';
 3933:                 return $fetchresult;
 3934:             } else {
 3935:                 my $destination = $filepath.'/'.$fname;
 3936:                 rename($source,$destination);
 3937:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3938:                                  $home);
 3939:             }
 3940:         } elsif ($action eq 'uploaddoc') {
 3941:             open(my $fh,'>',$filepath.'/'.$fname);
 3942:             print $fh $env{'form.'.$source};
 3943:             close($fh);
 3944:             if ($parser eq 'parse') {
 3945:                 my $mm = new File::MMagic;
 3946:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 3947:                 if ($type eq 'text/html') {
 3948:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 3949:                     unless ($parse_result eq 'ok') {
 3950:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 3951:                     }
 3952:                 }
 3953:                 if (ref($mimetype)) {
 3954:                     $$mimetype = $type;
 3955:                 } 
 3956:             }
 3957:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3958:                                  $home);
 3959:             if ($fetchresult eq 'ok') {
 3960:                 return '/uploaded/'.$fpath.'/'.$fname;
 3961:             } else {
 3962:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3963:                         ' to host '.$home.': '.$fetchresult);
 3964:                 return '/adm/notfound.html';
 3965:             }
 3966:         }
 3967:     }
 3968:     unless ( $fetchresult eq 'ok') {
 3969:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3970:              ' to host '.$home.': '.$fetchresult);
 3971:     }
 3972:     return $fetchresult;
 3973: }
 3974: 
 3975: sub build_filepath {
 3976:     my ($fpath) = @_;
 3977:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 3978:     unless ($fpath eq '') {
 3979:         my @parts=split('/',$fpath);
 3980:         foreach my $part (@parts) {
 3981:             $filepath.= '/'.$part;
 3982:             if ((-e $filepath)!=1) {
 3983:                 mkdir($filepath,0777);
 3984:             }
 3985:         }
 3986:     }
 3987:     return $filepath;
 3988: }
 3989: 
 3990: sub store_edited_file {
 3991:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 3992:     my $file = $primary_url;
 3993:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 3994:     my $fpath = '';
 3995:     my $fname = $file;
 3996:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3997:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3998:     my $filepath = &build_filepath($fpath);
 3999:     open(my $fh,'>',$filepath.'/'.$fname);
 4000:     print $fh $content;
 4001:     close($fh);
 4002:     my $home=&homeserver($docuname,$docudom);
 4003:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4004: 			  $home);
 4005:     if ($$fetchresult eq 'ok') {
 4006:         return '/uploaded/'.$fpath.'/'.$fname;
 4007:     } else {
 4008:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4009: 		 ' to host '.$home.': '.$$fetchresult);
 4010:         return '/adm/notfound.html';
 4011:     }
 4012: }
 4013: 
 4014: sub clean_filename {
 4015:     my ($fname,$args)=@_;
 4016: # Replace Windows backslashes by forward slashes
 4017:     $fname=~s/\\/\//g;
 4018:     if (!$args->{'keep_path'}) {
 4019:         # Get rid of everything but the actual filename
 4020: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4021:     }
 4022: # Replace spaces by underscores
 4023:     $fname=~s/\s+/\_/g;
 4024: # Transliterate non-ascii text to ascii
 4025:     my $lang = &Apache::lonlocal::current_language();
 4026:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4027: # Replace all other weird characters by nothing
 4028:     $fname=~s{[^/\w\.\-]}{}g;
 4029: # Replace all .\d. sequences with _\d. so they no longer look like version
 4030: # numbers
 4031:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4032:     return $fname;
 4033: }
 4034: 
 4035: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4036: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4037: # image with the same aspect ratio as the original, but with dimensions which do 
 4038: # not exceed $resizewidth and $resizeheight.
 4039:  
 4040: sub resizeImage {
 4041:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4042:     my $ima = Image::Magick->new;
 4043:     my $resized;
 4044:     if (-e $img_path) {
 4045:         $ima->Read($img_path);
 4046:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4047:             my $width = $ima->Get('width');
 4048:             my $height = $ima->Get('height');
 4049:             if ($width > $resizewidth) {
 4050: 	        my $factor = $width/$resizewidth;
 4051:                 my $newheight = $height/$factor;
 4052:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4053:                 $resized = 1;
 4054:             }
 4055:         }
 4056:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4057:             my $width = $ima->Get('width');
 4058:             my $height = $ima->Get('height');
 4059:             if ($height > $resizeheight) {
 4060:                 my $factor = $height/$resizeheight;
 4061:                 my $newwidth = $width/$factor;
 4062:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4063:                 $resized = 1;
 4064:             }
 4065:         }
 4066:         if ($resized) {
 4067:             $ima->Write($img_path);
 4068:         }
 4069:     }
 4070:     return;
 4071: }
 4072: 
 4073: # --------------- Take an uploaded file and put it into the userfiles directory
 4074: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4075: #                    the desired filename is in $env{"form.$formname.filename"}
 4076: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4077: #                                    canceloverwrite, scantron or ''.
 4078: #                   if 'coursedoc': upload to the current course
 4079: #                   if 'existingfile': write file to tmp/overwrites directory 
 4080: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4081: #                   $context is passed as argument to &finishuserfileupload
 4082: #        $subdir - directory in userfile to store the file into
 4083: #        $parser - instruction to parse file for objects ($parser = parse) or
 4084: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4085: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4086: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4087: #        $allfiles - reference to hash for embedded objects
 4088: #        $codebase - reference to hash for codebase of java objects
 4089: #        $desuname - username for permanent storage of uploaded file
 4090: #        $dsetudom - domain for permanaent storage of uploaded file
 4091: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4092: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4093: #        $resizewidth - width (pixels) to which to resize uploaded image
 4094: #        $resizeheight - height (pixels) to which to resize uploaded image
 4095: #        $mimetype - reference to scalar to accommodate mime type determined
 4096: #                    from File::MMagic.
 4097: # 
 4098: # output: url of file in userspace, or error: <message> 
 4099: #             or /adm/notfound.html if failure to upload occurse
 4100: 
 4101: sub userfileupload {
 4102:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4103:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4104:     if (!defined($subdir)) { $subdir='unknown'; }
 4105:     my $fname=$env{'form.'.$formname.'.filename'};
 4106:     $fname=&clean_filename($fname);
 4107:     # See if there is anything left
 4108:     unless ($fname) { return 'error: no uploaded file'; }
 4109:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4110:     if ($fname =~ /^\./) {
 4111:         my ($s,$usec) = &gettimeofday();
 4112:         while (length($usec) < 6) {
 4113:             $usec = '0'.$usec;
 4114:         }
 4115:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4116:     }
 4117:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4118:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4119:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4120:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4121:         my $now = time;
 4122:         my $filepath;
 4123:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4124:              $filepath = 'tmp/helprequests/'.$now;
 4125:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4126:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4127:                          '_'.$env{'user.domain'}.'/pending';
 4128:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4129:             my ($docuname,$docudom);
 4130:             if ($destudom =~ /^$match_domain$/) {
 4131:                 $docudom = $destudom;
 4132:             } else {
 4133:                 $docudom = $env{'user.domain'};
 4134:             }
 4135:             if ($destuname =~ /^$match_username$/) {
 4136:                 $docuname = $destuname;
 4137:             } else {
 4138:                 $docuname = $env{'user.name'};
 4139:             }
 4140:             if (exists($env{'form.group'})) {
 4141:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4142:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4143:             }
 4144:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4145:             if ($context eq 'canceloverwrite') {
 4146:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4147:                 if (-e  $tempfile) {
 4148:                     my @info = stat($tempfile);
 4149:                     if ($info[9] eq $env{'form.timestamp'}) {
 4150:                         unlink($tempfile);
 4151:                     }
 4152:                 }
 4153:                 return;
 4154:             }
 4155:         }
 4156:         # Create the directory if not present
 4157:         my @parts=split(/\//,$filepath);
 4158:         my $fullpath = $perlvar{'lonDaemons'};
 4159:         for (my $i=0;$i<@parts;$i++) {
 4160:             $fullpath .= '/'.$parts[$i];
 4161:             if ((-e $fullpath)!=1) {
 4162:                 mkdir($fullpath,0777);
 4163:             }
 4164:         }
 4165:         open(my $fh,'>',$fullpath.'/'.$fname);
 4166:         print $fh $env{'form.'.$formname};
 4167:         close($fh);
 4168:         if ($context eq 'existingfile') {
 4169:             my @info = stat($fullpath.'/'.$fname);
 4170:             return ($fullpath.'/'.$fname,$info[9]);
 4171:         } else {
 4172:             return $fullpath.'/'.$fname;
 4173:         }
 4174:     }
 4175:     if ($subdir eq 'scantron') {
 4176:         $fname = 'scantron_orig_'.$fname;
 4177:     } else {
 4178:         $fname="$subdir/$fname";
 4179:     }
 4180:     if ($context eq 'coursedoc') {
 4181: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4182: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4183:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4184:             return &finishuserfileupload($docuname,$docudom,
 4185: 					 $formname,$fname,$parser,$allfiles,
 4186: 					 $codebase,$thumbwidth,$thumbheight,
 4187:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4188:         } else {
 4189:             if ($env{'form.folder'}) {
 4190:                 $fname=$env{'form.folder'}.'/'.$fname;
 4191:             }
 4192:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4193: 				       $fname,$formname,$parser,
 4194: 				       $allfiles,$codebase,$mimetype);
 4195:         }
 4196:     } elsif (defined($destuname)) {
 4197:         my $docuname=$destuname;
 4198:         my $docudom=$destudom;
 4199: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4200: 				     $parser,$allfiles,$codebase,
 4201:                                      $thumbwidth,$thumbheight,
 4202:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4203:     } else {
 4204:         my $docuname=$env{'user.name'};
 4205:         my $docudom=$env{'user.domain'};
 4206:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4207:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4208:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4209:         }
 4210: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4211: 				     $parser,$allfiles,$codebase,
 4212:                                      $thumbwidth,$thumbheight,
 4213:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4214:     }
 4215: }
 4216: 
 4217: sub finishuserfileupload {
 4218:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4219:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4220:     my $path=$docudom.'/'.$docuname.'/';
 4221:     my $filepath=$perlvar{'lonDocRoot'};
 4222:   
 4223:     my ($fnamepath,$file,$fetchthumb);
 4224:     $file=$fname;
 4225:     if ($fname=~m|/|) {
 4226:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4227: 	$path.=$fnamepath.'/';
 4228:     }
 4229:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4230:     my $count;
 4231:     for ($count=4;$count<=$#parts;$count++) {
 4232:         $filepath.="/$parts[$count]";
 4233:         if ((-e $filepath)!=1) {
 4234: 	    mkdir($filepath,0777);
 4235:         }
 4236:     }
 4237: 
 4238: # Save the file
 4239:     {
 4240: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4241: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4242: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4243: 	    return '/adm/notfound.html';
 4244: 	}
 4245:         if ($context eq 'overwrite') {
 4246:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4247:             my $target = $filepath.'/'.$file;
 4248:             if (-e $source) {
 4249:                 my @info = stat($source);
 4250:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4251:                     unless (&File::Copy::move($source,$target)) {
 4252:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4253:                         return "Moving from $source failed";
 4254:                     }
 4255:                 } else {
 4256:                     return "Temporary file: $source had unexpected date/time for last modification";
 4257:                 }
 4258:             } else {
 4259:                 return "Temporary file: $source missing";
 4260:             }
 4261:         } elsif (!print FH ($env{'form.'.$formname})) {
 4262: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4263: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4264: 	    return '/adm/notfound.html';
 4265: 	}
 4266: 	close(FH);
 4267:         if ($resizewidth && $resizeheight) {
 4268:             my $mm = new File::MMagic;
 4269:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4270:             if ($mime_type =~ m{^image/}) {
 4271: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4272:             }  
 4273: 	}
 4274:     }
 4275:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4276:         if (ref($mimetype)) {
 4277:             if ($$mimetype eq '') {
 4278:                 my $mm = new File::MMagic;
 4279:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4280:                 $$mimetype = $type;
 4281:             }
 4282:         }
 4283:     }
 4284:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4285:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4286:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4287:                                                        $allfiles,$codebase);
 4288:             unless ($parse_result eq 'ok') {
 4289:                 &logthis('Failed to parse '.$filepath.$file.
 4290: 	   	         ' for embedded media: '.$parse_result); 
 4291:             }
 4292:         }
 4293:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4294:         my $format = $env{'form.scantron_format'};
 4295:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4296:     }
 4297:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4298:         my $input = $filepath.'/'.$file;
 4299:         my $output = $filepath.'/'.'tn-'.$file;
 4300:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4301:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4302:         system({$args[0]} @args);
 4303:         if (-e $filepath.'/'.'tn-'.$file) {
 4304:             $fetchthumb  = 1; 
 4305:         }
 4306:     }
 4307:  
 4308: # Notify homeserver to grep it
 4309: #
 4310:     my $docuhome=&homeserver($docuname,$docudom);	
 4311:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4312:     if ($fetchresult eq 'ok') {
 4313:         if ($fetchthumb) {
 4314:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4315:             if ($thumbresult ne 'ok') {
 4316:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4317:                          $docuhome.': '.$thumbresult);
 4318:             }
 4319:         }
 4320: #
 4321: # Return the URL to it
 4322:         return '/uploaded/'.$path.$file;
 4323:     } else {
 4324:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4325: 		 ': '.$fetchresult);
 4326:         return '/adm/notfound.html';
 4327:     }
 4328: }
 4329: 
 4330: sub extract_embedded_items {
 4331:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4332:     my @state = ();
 4333:     my (%lastids,%related,%shockwave,%flashvars);
 4334:     my %javafiles = (
 4335:                       codebase => '',
 4336:                       code => '',
 4337:                       archive => ''
 4338:                     );
 4339:     my %mediafiles = (
 4340:                       src => '',
 4341:                       movie => '',
 4342:                      );
 4343:     my $p;
 4344:     if ($content) {
 4345:         $p = HTML::LCParser->new($content);
 4346:     } else {
 4347:         $p = HTML::LCParser->new($fullpath);
 4348:     }
 4349:     while (my $t=$p->get_token()) {
 4350: 	if ($t->[0] eq 'S') {
 4351: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4352: 	    push(@state, $tagname);
 4353:             if (lc($tagname) eq 'allow') {
 4354:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4355:             }
 4356: 	    if (lc($tagname) eq 'img') {
 4357: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4358: 	    }
 4359: 	    if (lc($tagname) eq 'a') {
 4360:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4361:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4362:                 }
 4363: 	    }
 4364:             if (lc($tagname) eq 'script') {
 4365:                 my $src;
 4366:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4367:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4368:                 } else {
 4369:                     if ($attr->{'src'} ne '') {
 4370:                         $src = $attr->{'src'};
 4371:                         &add_filetype($allfiles,$src,'src');
 4372:                     }
 4373:                 }
 4374:                 my $text = $p->get_trimmed_text();
 4375:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4376:                     my @swfargs = split(/,/,$1);
 4377:                     foreach my $item (@swfargs) {
 4378:                         $item =~ s/["']//g;
 4379:                         $item =~ s/^\s+//;
 4380:                         $item =~ s/\s+$//;
 4381:                     }
 4382:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4383:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4384:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4385:                         } else {
 4386:                             $related{$swfargs[0]} = [$swfargs[2]];
 4387:                         }
 4388:                     }
 4389:                 }
 4390:             }
 4391:             if (lc($tagname) eq 'link') {
 4392:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4393:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4394:                 }
 4395:             }
 4396: 	    if (lc($tagname) eq 'object' ||
 4397: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4398: 		foreach my $item (keys(%javafiles)) {
 4399: 		    $javafiles{$item} = '';
 4400: 		}
 4401:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4402:                     $lastids{lc($tagname)} = $attr->{'id'};
 4403:                 }
 4404: 	    }
 4405: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4406: 		my $name = lc($attr->{'name'});
 4407: 		foreach my $item (keys(%javafiles)) {
 4408: 		    if ($name eq $item) {
 4409: 			$javafiles{$item} = $attr->{'value'};
 4410: 			last;
 4411: 		    }
 4412: 		}
 4413:                 my $pathfrom;
 4414: 		foreach my $item (keys(%mediafiles)) {
 4415: 		    if ($name eq $item) {
 4416:                         $pathfrom = $attr->{'value'};
 4417:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4418: 			&add_filetype($allfiles,$pathfrom,$name);
 4419: 			last;
 4420: 		    }
 4421: 		}
 4422:                 if ($name eq 'flashvars') {
 4423:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4424:                 }
 4425:                 if ($pathfrom ne '') {
 4426:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4427:                                          $pathfrom);
 4428:                 }
 4429: 	    }
 4430: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4431: 		foreach my $item (keys(%javafiles)) {
 4432: 		    if ($attr->{$item}) {
 4433: 			$javafiles{$item} = $attr->{$item};
 4434: 			last;
 4435: 		    }
 4436: 		}
 4437: 		foreach my $item (keys(%mediafiles)) {
 4438: 		    if ($attr->{$item}) {
 4439: 			&add_filetype($allfiles,$attr->{$item},$item);
 4440: 			last;
 4441: 		    }
 4442: 		}
 4443:                 if (lc($tagname) eq 'embed') {
 4444:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4445:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4446:                                              $attr->{'src'});
 4447:                     }
 4448:                 }
 4449: 	    }
 4450:             if (lc($tagname) eq 'iframe') {
 4451:                 my $src = $attr->{'src'} ;
 4452:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4453:                     &add_filetype($allfiles,$src,'src');
 4454:                 } elsif ($src =~ m{^/}) {
 4455:                     if ($env{'request.course.id'}) {
 4456:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4457:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4458:                         my $url = &hreflocation('',$fullpath);
 4459:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4460:                             my $relpath = $1;
 4461:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4462:                                 &add_filetype($allfiles,$1,'src');
 4463:                             }
 4464:                         }
 4465:                     }
 4466:                 }
 4467:             }
 4468:             if ($t->[4] =~ m{/>$}) {
 4469:                 pop(@state);
 4470:             }
 4471: 	} elsif ($t->[0] eq 'E') {
 4472: 	    my ($tagname) = ($t->[1]);
 4473: 	    if ($javafiles{'codebase'} ne '') {
 4474: 		$javafiles{'codebase'} .= '/';
 4475: 	    }  
 4476: 	    if (lc($tagname) eq 'applet' ||
 4477: 		lc($tagname) eq 'object' ||
 4478: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4479: 		) {
 4480: 		foreach my $item (keys(%javafiles)) {
 4481: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4482: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4483: 			&add_filetype($allfiles,$file,$item);
 4484: 		    }
 4485: 		}
 4486: 	    } 
 4487: 	    pop @state;
 4488: 	}
 4489:     }
 4490:     foreach my $id (sort(keys(%flashvars))) {
 4491:         if ($shockwave{$id} ne '') {
 4492:             my @pairs = split(/\&/,$flashvars{$id});
 4493:             foreach my $pair (@pairs) {
 4494:                 my ($key,$value) = split(/\=/,$pair);
 4495:                 if ($key eq 'thumb') {
 4496:                     &add_filetype($allfiles,$value,$key);
 4497:                 } elsif ($key eq 'content') {
 4498:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4499:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4500:                     if ($ext ne '') {
 4501:                         &add_filetype($allfiles,$path.$value,$ext);
 4502:                     }
 4503:                 }
 4504:             }
 4505:         }
 4506:     }
 4507:     return 'ok';
 4508: }
 4509: 
 4510: sub add_filetype {
 4511:     my ($allfiles,$file,$type)=@_;
 4512:     if (exists($allfiles->{$file})) {
 4513: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4514: 	    push(@{$allfiles->{$file}}, &escape($type));
 4515: 	}
 4516:     } else {
 4517: 	@{$allfiles->{$file}} = (&escape($type));
 4518:     }
 4519: }
 4520: 
 4521: sub embedded_dependency {
 4522:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4523:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4524:         if (($identifier ne '') &&
 4525:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4526:             ($pathfrom ne '')) {
 4527:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4528:             foreach my $dep (@{$related->{$identifier}}) {
 4529:                 &add_filetype($allfiles,$path.$dep,'object');
 4530:             }
 4531:         }
 4532:     }
 4533:     return;
 4534: }
 4535: 
 4536: sub bubblesheet_converter {
 4537:     my ($cdom,$fullpath,$config,$format) = @_;
 4538:     if ((&domain($cdom) ne '') &&
 4539:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4540:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4541:         my (%csvcols,%csvoptions);
 4542:         if (ref($config->{'fields'}) eq 'HASH') {  
 4543:             %csvcols = %{$config->{'fields'}};
 4544:         }
 4545:         if (ref($config->{'options'}) eq 'HASH') {
 4546:             %csvoptions = %{$config->{'options'}};
 4547:         }
 4548:         my %csvbynum = reverse(%csvcols);
 4549:         my %scantronconf = &get_scantron_config($format,$cdom);
 4550:         if (keys(%scantronconf)) {
 4551:             my %bynum = (
 4552:                           $scantronconf{CODEstart} => 'CODEstart',
 4553:                           $scantronconf{IDstart}   => 'IDstart',
 4554:                           $scantronconf{PaperID}   => 'PaperID',
 4555:                           $scantronconf{FirstName} => 'FirstName',
 4556:                           $scantronconf{LastName}  => 'LastName',
 4557:                           $scantronconf{Qstart}    => 'Qstart',
 4558:                         );
 4559:             my @ordered;
 4560:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4561:                 push(@ordered,$bynum{$item});
 4562:             }
 4563:             my %mapstart = (
 4564:                               CODEstart => 'CODE',
 4565:                               IDstart   => 'ID',
 4566:                               PaperID   => 'PaperID',
 4567:                               FirstName => 'FirstName',
 4568:                               LastName  => 'LastName',
 4569:                               Qstart    => 'FirstQuestion',
 4570:                            );
 4571:             my %maplength = (
 4572:                               CODEstart => 'CODElength',
 4573:                               IDstart   => 'IDlength',
 4574:                               PaperID   => 'PaperIDlength',
 4575:                               FirstName => 'FirstNamelength',
 4576:                               LastName  => 'LastNamelength',
 4577:             );
 4578:             if (open(my $fh,'<',$fullpath)) {
 4579:                 my $output;
 4580:                 my %lettdig = &letter_to_digits();
 4581:                 my %diglett = reverse(%lettdig);
 4582:                 my $numletts = scalar(keys(%lettdig));
 4583:                 my $num = 0;
 4584:                 while (my $line=<$fh>) {
 4585:                     $num ++;
 4586:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4587:                     $line =~ s{[\r\n]+$}{};
 4588:                     my %found;
 4589:                     my @values = split(/,/,$line);
 4590:                     my ($qstart,$record);
 4591:                     for (my $i=0; $i<@values; $i++) {
 4592:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4593:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4594:                             if ($values[$i] eq '') {
 4595:                                 $values[$i] = $scantronconf{'Qoff'};
 4596:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4597:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4598:                                     $values[$i] = $lettdig{uc($values[$i])};
 4599:                                 }
 4600:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4601:                                 if ($values[$i] =~ /^[0-9]$/) {
 4602:                                     $values[$i] = $diglett{$values[$i]};
 4603:                                 }
 4604:                             } else {
 4605:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4606:                                     my $digit;
 4607:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4608:                                         $digit = $lettdig{uc($values[$i])}-1;
 4609:                                         if ($values[$i] eq 'J') {
 4610:                                             $digit += $numletts;
 4611:                                         }
 4612:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4613:                                         $digit = $values[$i]-1;
 4614:                                         if ($values[$i] eq '0') {
 4615:                                             $digit += $numletts;
 4616:                                         }
 4617:                                     }
 4618:                                     my $qval='';
 4619:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4620:                                         if ($j == $digit) {
 4621:                                             $qval .= $scantronconf{'Qon'};
 4622:                                         } else {
 4623:                                             $qval .= $scantronconf{'Qoff'};
 4624:                                         }
 4625:                                     }
 4626:                                     $values[$i] = $qval;
 4627:                                 }
 4628:                             }
 4629:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4630:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4631:                             }
 4632:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4633:                             if ($numblank > 0) {
 4634:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4635:                             }
 4636:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4637:                                 $qstart = $i;
 4638:                                 $found{$csvbynum{$i}} = $values[$i];
 4639:                             } else {
 4640:                                 $found{'FirstQuestion'} .= $values[$i];
 4641:                             }
 4642:                         } elsif (exists($csvbynum{$i})) {
 4643:                             if ($csvoptions{'rem'}) {
 4644:                                 $values[$i] =~ s/^\s+//;
 4645:                             }
 4646:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4647:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4648:                                     $values[$i] = '0'.$values[$i];
 4649:                                 }
 4650:                             }
 4651:                             $found{$csvbynum{$i}} = $values[$i];
 4652:                         }
 4653:                     }
 4654:                     foreach my $item (@ordered) {
 4655:                         my $currlength = 1+length($record);
 4656:                         my $numspaces = $scantronconf{$item} - $currlength;
 4657:                         if ($numspaces > 0) {
 4658:                             $record .= (' ' x $numspaces);
 4659:                         }
 4660:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4661:                             unless ($item eq 'Qstart') {
 4662:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4663:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4664:                                 }
 4665:                             }
 4666:                             $record .= $found{$mapstart{$item}};
 4667:                         }
 4668:                     }
 4669:                     $output .= "$record\n";
 4670:                 }
 4671:                 close($fh);
 4672:                 if ($output) {
 4673:                     if (open(my $fh,'>',$fullpath)) {
 4674:                         print $fh $output;
 4675:                         close($fh);
 4676:                     }
 4677:                 }
 4678:             }
 4679:         }
 4680:         return;
 4681:     }
 4682: }
 4683: 
 4684: sub letter_to_digits {
 4685:     my %lettdig = (
 4686:                     A => 1,
 4687:                     B => 2,
 4688:                     C => 3,
 4689:                     D => 4,
 4690:                     E => 5,
 4691:                     F => 6,
 4692:                     G => 7,
 4693:                     H => 8,
 4694:                     I => 9,
 4695:                     J => 0,
 4696:                   );
 4697:     return %lettdig;
 4698: }
 4699: 
 4700: sub get_scantron_config {
 4701:     my ($which,$cdom) = @_;
 4702:     my @lines = &get_scantronformat_file($cdom);
 4703:     my %config;
 4704:     #FIXME probably should move to XML it has already gotten a bit much now
 4705:     foreach my $line (@lines) {
 4706:         my ($name,$descrip)=split(/:/,$line);
 4707:         if ($name ne $which ) { next; }
 4708:         chomp($line);
 4709:         my @config=split(/:/,$line);
 4710:         $config{'name'}=$config[0];
 4711:         $config{'description'}=$config[1];
 4712:         $config{'CODElocation'}=$config[2];
 4713:         $config{'CODEstart'}=$config[3];
 4714:         $config{'CODElength'}=$config[4];
 4715:         $config{'IDstart'}=$config[5];
 4716:         $config{'IDlength'}=$config[6];
 4717:         $config{'Qstart'}=$config[7];
 4718:         $config{'Qlength'}=$config[8];
 4719:         $config{'Qoff'}=$config[9];
 4720:         $config{'Qon'}=$config[10];
 4721:         $config{'PaperID'}=$config[11];
 4722:         $config{'PaperIDlength'}=$config[12];
 4723:         $config{'FirstName'}=$config[13];
 4724:         $config{'FirstNamelength'}=$config[14];
 4725:         $config{'LastName'}=$config[15];
 4726:         $config{'LastNamelength'}=$config[16];
 4727:         $config{'BubblesPerRow'}=$config[17];
 4728:         last;
 4729:     }
 4730:     return %config;
 4731: }
 4732: 
 4733: sub get_scantronformat_file {
 4734:     my ($cdom) = @_;
 4735:     if ($cdom eq '') {
 4736:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4737:     }
 4738:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4739:     my $gottab = 0;
 4740:     my @lines;
 4741:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4742:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4743:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4744:             if ($formatfile ne '-1') {
 4745:                 @lines = split("\n",$formatfile,-1);
 4746:                 $gottab = 1;
 4747:             }
 4748:         }
 4749:     }
 4750:     if (!$gottab) {
 4751:         my $confname = $cdom.'-domainconfig';
 4752:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4753:         my $formatfile = &getfile($default);
 4754:         if ($formatfile ne '-1') {
 4755:             @lines = split("\n",$formatfile,-1);
 4756:             $gottab = 1;
 4757:         }
 4758:     }
 4759:     if (!$gottab) {
 4760:         my @domains = &current_machine_domains();
 4761:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4762:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4763:                 @lines = <$fh>;
 4764:                 close($fh);
 4765:             }
 4766:         } else {
 4767:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4768:                 @lines = <$fh>;
 4769:                 close($fh);
 4770:             }
 4771:         }
 4772:     }
 4773:     return @lines;
 4774: }
 4775: 
 4776: sub removeuploadedurl {
 4777:     my ($url)=@_;	
 4778:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4779:     return &removeuserfile($uname,$udom,$fname);
 4780: }
 4781: 
 4782: sub removeuserfile {
 4783:     my ($docuname,$docudom,$fname)=@_;
 4784:     my $home=&homeserver($docuname,$docudom);    
 4785:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4786:     if ($result eq 'ok') {	
 4787:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4788:             my $metafile = $fname.'.meta';
 4789:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4790: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4791:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4792:             my $sqlresult = 
 4793:                 &update_portfolio_table($docuname,$docudom,$file,
 4794:                                         'portfolio_metadata',$group,
 4795:                                         'delete');
 4796:         }
 4797:     }
 4798:     return $result;
 4799: }
 4800: 
 4801: sub mkdiruserfile {
 4802:     my ($docuname,$docudom,$dir)=@_;
 4803:     my $home=&homeserver($docuname,$docudom);
 4804:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4805: }
 4806: 
 4807: sub renameuserfile {
 4808:     my ($docuname,$docudom,$old,$new)=@_;
 4809:     my $home=&homeserver($docuname,$docudom);
 4810:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4811:                         &escape("$old").':'.&escape("$new"),$home);
 4812:     if ($result eq 'ok') {
 4813:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4814:             my $oldmeta = $old.'.meta';
 4815:             my $newmeta = $new.'.meta';
 4816:             my $metaresult = 
 4817:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4818: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4819:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4820:             my $sqlresult = 
 4821:                 &update_portfolio_table($docuname,$docudom,$file,
 4822:                                         'portfolio_metadata',$group,
 4823:                                         'delete');
 4824:         }
 4825:     }
 4826:     return $result;
 4827: }
 4828: 
 4829: # ------------------------------------------------------------------------- Log
 4830: 
 4831: sub log {
 4832:     my ($dom,$nam,$hom,$what)=@_;
 4833:     return critical("log:$dom:$nam:$what",$hom);
 4834: }
 4835: 
 4836: # ------------------------------------------------------------------ Course Log
 4837: #
 4838: # This routine flushes several buffers of non-mission-critical nature
 4839: #
 4840: 
 4841: sub flushcourselogs {
 4842:     &logthis('Flushing log buffers');
 4843: #
 4844: # course logs
 4845: # This is a log of all transactions in a course, which can be used
 4846: # for data mining purposes
 4847: #
 4848: # It also collects the courseid database, which lists last transaction
 4849: # times and course titles for all courseids
 4850: #
 4851:     my %courseidbuffer=();
 4852:     foreach my $crsid (keys(%courselogs)) {
 4853:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4854: 		          &escape($courselogs{$crsid}),
 4855: 		          $coursehombuf{$crsid}) eq 'ok') {
 4856: 	    delete $courselogs{$crsid};
 4857:         } else {
 4858:             &logthis('Failed to flush log buffer for '.$crsid);
 4859:             if (length($courselogs{$crsid})>40000) {
 4860:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4861:                         " exceeded maximum size, deleting.</font>");
 4862:                delete $courselogs{$crsid};
 4863:             }
 4864:         }
 4865:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4866:             'description' => $coursedescrbuf{$crsid},
 4867:             'inst_code'    => $courseinstcodebuf{$crsid},
 4868:             'type'        => $coursetypebuf{$crsid},
 4869:             'owner'       => $courseownerbuf{$crsid},
 4870:         };
 4871:     }
 4872: #
 4873: # Write course id database (reverse lookup) to homeserver of courses 
 4874: # Is used in pickcourse
 4875: #
 4876:     foreach my $crs_home (keys(%courseidbuffer)) {
 4877:         my $response = &courseidput(&host_domain($crs_home),
 4878:                                     $courseidbuffer{$crs_home},
 4879:                                     $crs_home,'timeonly');
 4880:     }
 4881: #
 4882: # File accesses
 4883: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4884: #
 4885:     foreach my $entry (keys(%accesshash)) {
 4886:         if ($entry =~ /___count$/) {
 4887:             my ($dom,$name);
 4888:             ($dom,$name,undef)=
 4889: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4890:             if (! defined($dom) || $dom eq '' || 
 4891:                 ! defined($name) || $name eq '') {
 4892:                 my $cid = $env{'request.course.id'};
 4893:                 $dom  = $env{'request.'.$cid.'.domain'};
 4894:                 $name = $env{'request.'.$cid.'.num'};
 4895:             }
 4896:             my $value = $accesshash{$entry};
 4897:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4898:             my %temphash=($url => $value);
 4899:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4900:             if ($result eq 'ok') {
 4901:                 delete $accesshash{$entry};
 4902:             }
 4903:         } else {
 4904:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4905:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4906:             my %temphash=($entry => $accesshash{$entry});
 4907:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4908:                 delete $accesshash{$entry};
 4909:             }
 4910:         }
 4911:     }
 4912: #
 4913: # Roles
 4914: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4915: #
 4916:     foreach my $entry (keys(%userrolehash)) {
 4917:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4918: 	    split(/\:/,$entry);
 4919:         if (&Apache::lonnet::put('nohist_userroles',
 4920:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4921:                 $rudom,$runame) eq 'ok') {
 4922: 	    delete $userrolehash{$entry};
 4923:         }
 4924:     }
 4925: #
 4926: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 4927: #
 4928:     my %domrolebuffer = ();
 4929:     foreach my $entry (keys(%domainrolehash)) {
 4930:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 4931:         if ($domrolebuffer{$rudom}) {
 4932:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 4933:                       '='.&escape($domainrolehash{$entry});
 4934:         } else {
 4935:             $domrolebuffer{$rudom}.=&escape($entry).
 4936:                       '='.&escape($domainrolehash{$entry});
 4937:         }
 4938:         delete $domainrolehash{$entry};
 4939:     }
 4940:     foreach my $dom (keys(%domrolebuffer)) {
 4941: 	my %servers;
 4942: 	if (defined(&domain($dom,'primary'))) {
 4943: 	    my $primary=&domain($dom,'primary');
 4944: 	    my $hostname=&hostname($primary);
 4945: 	    $servers{$primary} = $hostname;
 4946: 	} else { 
 4947: 	    %servers = &get_servers($dom,'library');
 4948: 	}
 4949: 	foreach my $tryserver (keys(%servers)) {
 4950: 	    if (&reply('domroleput:'.$dom.':'.
 4951: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 4952: 		last;
 4953: 	    } else {  
 4954: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 4955: 	    }
 4956:         }
 4957:     }
 4958:     $dumpcount++;
 4959: }
 4960: 
 4961: sub courselog {
 4962:     my $what=shift;
 4963:     $what=time.':'.$what;
 4964:     unless ($env{'request.course.id'}) { return ''; }
 4965:     $coursedombuf{$env{'request.course.id'}}=
 4966:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 4967:     $coursenumbuf{$env{'request.course.id'}}=
 4968:        $env{'course.'.$env{'request.course.id'}.'.num'};
 4969:     $coursehombuf{$env{'request.course.id'}}=
 4970:        $env{'course.'.$env{'request.course.id'}.'.home'};
 4971:     $coursedescrbuf{$env{'request.course.id'}}=
 4972:        $env{'course.'.$env{'request.course.id'}.'.description'};
 4973:     $courseinstcodebuf{$env{'request.course.id'}}=
 4974:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 4975:     $courseownerbuf{$env{'request.course.id'}}=
 4976:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 4977:     $coursetypebuf{$env{'request.course.id'}}=
 4978:        $env{'course.'.$env{'request.course.id'}.'.type'};
 4979:     if (defined $courselogs{$env{'request.course.id'}}) {
 4980: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 4981:     } else {
 4982: 	$courselogs{$env{'request.course.id'}}.=$what;
 4983:     }
 4984:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 4985: 	&flushcourselogs();
 4986:     }
 4987: }
 4988: 
 4989: sub courseacclog {
 4990:     my $fnsymb=shift;
 4991:     unless ($env{'request.course.id'}) { return ''; }
 4992:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 4993:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 4994:         $what.=':POST';
 4995:         # FIXME: Probably ought to escape things....
 4996: 	foreach my $key (keys(%env)) {
 4997:             if ($key=~/^form\.(.*)/) {
 4998:                 my $formitem = $1;
 4999:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5000:                     $what.=':'.$formitem.'='.$env{$key};
 5001:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5002:                     $what.=':'.$formitem.'='.$env{$key};
 5003:                 }
 5004:             }
 5005:         }
 5006:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5007:         # FIXME: We should not be depending on a form parameter that someone
 5008:         # editing lonsearchcat.pm might change in the future.
 5009:         if ($env{'form.phase'} eq 'course_search') {
 5010:             $what.= ':POST';
 5011:             # FIXME: Probably ought to escape things....
 5012:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5013:                                  'crsdiscuss') {
 5014:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5015:             }
 5016:         }
 5017:     }
 5018:     &courselog($what);
 5019: }
 5020: 
 5021: sub countacc {
 5022:     my $url=&declutter(shift);
 5023:     return if (! defined($url) || $url eq '');
 5024:     unless ($env{'request.course.id'}) { return ''; }
 5025: #
 5026: # Mark that this url was used in this course
 5027: #
 5028:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5029: #
 5030: # Increase the access count for this resource in this child process
 5031: #
 5032:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5033:     $accesshash{$key}++;
 5034: }
 5035: 
 5036: sub linklog {
 5037:     my ($from,$to)=@_;
 5038:     $from=&declutter($from);
 5039:     $to=&declutter($to);
 5040:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5041:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5042: }
 5043: 
 5044: sub statslog {
 5045:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5046:     if ($users<2) { return; }
 5047:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5048:             'course'       => $env{'request.course.id'},
 5049:             'sections'     => '"all"',
 5050:             'num_students' => $users,
 5051:             'part'         => $part,
 5052:             'symb'         => $symb,
 5053:             'mean_tries'   => $av_attempts,
 5054:             'deg_of_diff'  => $degdiff});
 5055:     foreach my $key (keys(%dynstore)) {
 5056:         $accesshash{$key}=$dynstore{$key};
 5057:     }
 5058: }
 5059:   
 5060: sub userrolelog {
 5061:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5062:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5063:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5064:        $userrolehash
 5065:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5066:                     =$tend.':'.$tstart;
 5067:     }
 5068:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5069:        $userrolehash
 5070:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5071:                     =$tend.':'.$tstart;
 5072:     }
 5073:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5074:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5075:        $domainrolehash
 5076:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5077:                     = $tend.':'.$tstart;
 5078:     }
 5079: }
 5080: 
 5081: sub courserolelog {
 5082:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5083:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5084:         my $cdom = $1;
 5085:         my $cnum = $2;
 5086:         my $sec = $3;
 5087:         my $namespace = 'rolelog';
 5088:         my %storehash = (
 5089:                            role    => $trole,
 5090:                            start   => $tstart,
 5091:                            end     => $tend,
 5092:                            selfenroll => $selfenroll,
 5093:                            context    => $context,
 5094:                         );
 5095:         if ($trole eq 'gr') {
 5096:             $namespace = 'groupslog';
 5097:             $storehash{'group'} = $sec;
 5098:         } else {
 5099:             $storehash{'section'} = $sec;
 5100:         }
 5101:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5102:                    $domain,$cnum,$cdom);
 5103:         if (($trole ne 'st') || ($sec ne '')) {
 5104:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5105:         }
 5106:     }
 5107:     return;
 5108: }
 5109: 
 5110: sub domainrolelog {
 5111:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5112:     if ($area =~ m{^/($match_domain)/$}) {
 5113:         my $cdom = $1;
 5114:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 5115:         my $namespace = 'rolelog';
 5116:         my %storehash = (
 5117:                            role    => $trole,
 5118:                            start   => $tstart,
 5119:                            end     => $tend,
 5120:                            context => $context,
 5121:                         );
 5122:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5123:                    $domain,$domconfiguser,$cdom);
 5124:     }
 5125:     return;
 5126: 
 5127: }
 5128: 
 5129: sub coauthorrolelog {
 5130:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5131:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5132:         my $audom = $1;
 5133:         my $auname = $2;
 5134:         my $namespace = 'rolelog';
 5135:         my %storehash = (
 5136:                            role    => $trole,
 5137:                            start   => $tstart,
 5138:                            end     => $tend,
 5139:                            context => $context,
 5140:                         );
 5141:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5142:                    $domain,$auname,$audom);
 5143:     }
 5144:     return;
 5145: }
 5146: 
 5147: sub get_course_adv_roles {
 5148:     my ($cid,$codes) = @_;
 5149:     $cid=$env{'request.course.id'} unless (defined($cid));
 5150:     my %coursehash=&coursedescription($cid);
 5151:     my $crstype = &Apache::loncommon::course_type($cid);
 5152:     my %nothide=();
 5153:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5154:         if ($user !~ /:/) {
 5155: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5156:         } else {
 5157:             $nothide{$user}=1;
 5158:         }
 5159:     }
 5160:     my @possdoms = ($coursehash{'domain'});
 5161:     if ($coursehash{'checkforpriv'}) {
 5162:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5163:     }
 5164:     my %returnhash=();
 5165:     my %dumphash=
 5166:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5167:     my $now=time;
 5168:     my %privileged;
 5169:     foreach my $entry (keys(%dumphash)) {
 5170: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5171:         if (($tstart) && ($tstart<0)) { next; }
 5172:         if (($tend) && ($tend<$now)) { next; }
 5173:         if (($tstart) && ($now<$tstart)) { next; }
 5174:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5175: 	if ($username eq '' || $domain eq '') { next; }
 5176:         if ((&privileged($username,$domain,\@possdoms)) &&
 5177:             (!$nothide{$username.':'.$domain})) { next; }
 5178: 	if ($role eq 'cr') { next; }
 5179:         if ($codes) {
 5180:             if ($section) { $role .= ':'.$section; }
 5181:             if ($returnhash{$role}) {
 5182:                 $returnhash{$role}.=','.$username.':'.$domain;
 5183:             } else {
 5184:                 $returnhash{$role}=$username.':'.$domain;
 5185:             }
 5186:         } else {
 5187:             my $key=&plaintext($role,$crstype);
 5188:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5189:             if ($returnhash{$key}) {
 5190: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5191:             } else {
 5192:                 $returnhash{$key}=$username.':'.$domain;
 5193:             }
 5194:         }
 5195:     }
 5196:     return %returnhash;
 5197: }
 5198: 
 5199: sub get_my_roles {
 5200:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5201:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5202:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5203:     my (%dumphash,%nothide);
 5204:     if ($context eq 'userroles') {
 5205:         %dumphash = &dump('roles',$udom,$uname);
 5206:     } else {
 5207:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5208:         if ($hidepriv) {
 5209:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5210:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5211:                 if ($user !~ /:/) {
 5212:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5213:                 } else {
 5214:                     $nothide{$user} = 1;
 5215:                 }
 5216:             }
 5217:         }
 5218:     }
 5219:     my %returnhash=();
 5220:     my $now=time;
 5221:     my %privileged;
 5222:     foreach my $entry (keys(%dumphash)) {
 5223:         my ($role,$tend,$tstart);
 5224:         if ($context eq 'userroles') {
 5225:             next if ($entry =~ /^rolesdef/);
 5226: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5227:         } else {
 5228:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5229:         }
 5230:         if (($tstart) && ($tstart<0)) { next; }
 5231:         my $status = 'active';
 5232:         if (($tend) && ($tend<=$now)) {
 5233:             $status = 'previous';
 5234:         } 
 5235:         if (($tstart) && ($now<$tstart)) {
 5236:             $status = 'future';
 5237:         }
 5238:         if (ref($types) eq 'ARRAY') {
 5239:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5240:                 next;
 5241:             } 
 5242:         } else {
 5243:             if ($status ne 'active') {
 5244:                 next;
 5245:             }
 5246:         }
 5247:         my ($rolecode,$username,$domain,$section,$area);
 5248:         if ($context eq 'userroles') {
 5249:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5250:             (undef,$domain,$username,$section) = split(/\//,$area);
 5251:         } else {
 5252:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5253:         }
 5254:         if (ref($roledoms) eq 'ARRAY') {
 5255:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5256:                 next;
 5257:             }
 5258:         }
 5259:         if (ref($roles) eq 'ARRAY') {
 5260:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5261:                 if ($role =~ /^cr\//) {
 5262:                     if (!grep(/^cr$/,@{$roles})) {
 5263:                         next;
 5264:                     }
 5265:                 } elsif ($role =~ /^gr\//) {
 5266:                     if (!grep(/^gr$/,@{$roles})) {
 5267:                         next;
 5268:                     }
 5269:                 } else {
 5270:                     next;
 5271:                 }
 5272:             }
 5273:         }
 5274:         if ($hidepriv) {
 5275:             my @privroles = ('dc','su');
 5276:             if ($context eq 'userroles') {
 5277:                 next if (grep(/^\Q$role\E$/,@privroles));
 5278:             } else {
 5279:                 my $possdoms = [$domain];
 5280:                 if (ref($roledoms) eq 'ARRAY') {
 5281:                    push(@{$possdoms},@{$roledoms}); 
 5282:                 }
 5283:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5284:                     if (!$nothide{$username.':'.$domain}) {
 5285:                         next;
 5286:                     }
 5287:                 }
 5288:             }
 5289:         }
 5290:         if ($withsec) {
 5291:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5292:                 $tstart.':'.$tend;
 5293:         } else {
 5294:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5295:         }
 5296:     }
 5297:     return %returnhash;
 5298: }
 5299: 
 5300: sub get_all_adhocroles {
 5301:     my ($dom) = @_;
 5302:     my @roles_by_num = ();
 5303:     my %domdefaults = &get_domain_defaults($dom);
 5304:     my (%description,%access_in_dom,%access_info);
 5305:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5306:         my $count = 0;
 5307:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5308:         my %ordered;
 5309:         foreach my $role (sort(keys(%domcurrent))) {
 5310:             my ($order,$desc,$access_in_dom);
 5311:             if (ref($domcurrent{$role}) eq 'HASH') {
 5312:                 $order = $domcurrent{$role}{'order'};
 5313:                 $desc = $domcurrent{$role}{'desc'};
 5314:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5315:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5316:             }
 5317:             if ($order eq '') {
 5318:                 $order = $count;
 5319:             }
 5320:             $ordered{$order} = $role;
 5321:             if ($desc ne '') {
 5322:                 $description{$role} = $desc;
 5323:             } else {
 5324:                 $description{$role}= $role;
 5325:             }
 5326:             $count++;
 5327:         }
 5328:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5329:             push(@roles_by_num,$ordered{$item});
 5330:         }
 5331:     }
 5332:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5333: }
 5334: 
 5335: sub get_my_adhocroles {
 5336:     my ($cid,$checkreg) = @_;
 5337:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5338:     if ($env{'request.course.id'} eq $cid) {
 5339:         $cdom = $env{'course.'.$cid.'.domain'};
 5340:         $cnum = $env{'course.'.$cid.'.num'};
 5341:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5342:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5343:         $cdom = $1;
 5344:         $cnum = $2;
 5345:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5346:                                      $cdom,$cnum);
 5347:     }
 5348:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5349:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5350:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5351:         if ($rosterhash{$user} ne '') {
 5352:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5353:             return ([],{}) if ($type eq 'auto');
 5354:         }
 5355:     }
 5356:     if (($cdom ne '') && ($cnum ne ''))  {
 5357:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5358:             my $then=$env{'user.login.time'};
 5359:             my $update=$env{'user.update.time'};
 5360:             if (!$update) {
 5361:                 $update = $then;
 5362:             }
 5363:             my @liveroles;
 5364:             foreach my $role ('dh','da') {
 5365:                 if ($env{"user.role.$role./$cdom/"}) {
 5366:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5367:                     my $limit = $update;
 5368:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5369:                         $limit = $then;
 5370:                     }
 5371:                     my $activerole = 1;
 5372:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5373:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5374:                     if ($activerole) {
 5375:                         push(@liveroles,$role);
 5376:                     }
 5377:                 }
 5378:             }
 5379:             if (@liveroles) {
 5380:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5381:                     my ($accessref,$accessinfo,%access_in_dom);
 5382:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5383:                     if (ref($roles_by_num) eq 'ARRAY') {
 5384:                         if (@{$roles_by_num}) {
 5385:                             my %settings;
 5386:                             if ($env{'request.course.id'} eq $cid) {
 5387:                                 foreach my $envkey (keys(%env)) {
 5388:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5389:                                         $settings{$1} = $env{$envkey};
 5390:                                     }
 5391:                                 }
 5392:                             } else {
 5393:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5394:                             }
 5395:                             my %setincrs;
 5396:                             if ($settings{'internal.adhocaccess'}) {
 5397:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5398:                             }
 5399:                             my @statuses;
 5400:                             if ($env{'environment.inststatus'}) {
 5401:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5402:                             }
 5403:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5404:                             if (ref($accessref) eq 'HASH') {
 5405:                                 %access_in_dom = %{$accessref};
 5406:                             }
 5407:                             foreach my $role (@{$roles_by_num}) {
 5408:                                 my ($curraccess,@okstatus,@personnel);
 5409:                                 if ($setincrs{$role}) {
 5410:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5411:                                     if ($curraccess eq 'status') {
 5412:                                         @okstatus = split(/\&/,$rest);
 5413:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5414:                                         @personnel = split(/\&/,$rest);
 5415:                                     }
 5416:                                 } else {
 5417:                                     $curraccess = $access_in_dom{$role};
 5418:                                     if (ref($accessinfo) eq 'HASH') {
 5419:                                         if ($curraccess eq 'status') {
 5420:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5421:                                                 @okstatus = @{$accessinfo->{$role}};
 5422:                                             }
 5423:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5424:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5425:                                                 @personnel = @{$accessinfo->{$role}};
 5426:                                             }
 5427:                                         }
 5428:                                     }
 5429:                                 }
 5430:                                 if ($curraccess eq 'none') {
 5431:                                     next;
 5432:                                 } elsif ($curraccess eq 'all') {
 5433:                                     push(@possroles,$role);
 5434:                                 } elsif ($curraccess eq 'dh') {
 5435:                                     if (grep(/^dh$/,@liveroles)) {
 5436:                                         push(@possroles,$role);
 5437:                                     } else {
 5438:                                         next;
 5439:                                     }
 5440:                                 } elsif ($curraccess eq 'da') {
 5441:                                     if (grep(/^da$/,@liveroles)) {
 5442:                                         push(@possroles,$role);
 5443:                                     } else {
 5444:                                         next;
 5445:                                     }
 5446:                                 } elsif ($curraccess eq 'status') {
 5447:                                     if (@okstatus) {
 5448:                                         if (!@statuses) {
 5449:                                             if (grep(/^default$/,@okstatus)) {
 5450:                                                 push(@possroles,$role);
 5451:                                             }
 5452:                                         } else {
 5453:                                             foreach my $status (@okstatus) {
 5454:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5455:                                                     push(@possroles,$role);
 5456:                                                     last;
 5457:                                                 }
 5458:                                             }
 5459:                                         }
 5460:                                     }
 5461:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5462:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5463:                                         if ($curraccess eq 'exc') {
 5464:                                             push(@possroles,$role);
 5465:                                         }
 5466:                                     } elsif ($curraccess eq 'inc') {
 5467:                                         push(@possroles,$role);
 5468:                                     }
 5469:                                 }
 5470:                             }
 5471:                         }
 5472:                     }
 5473:                 }
 5474:             }
 5475:         }
 5476:     }
 5477:     unless (ref($description) eq 'HASH') {
 5478:         if (ref($roles_by_num) eq 'ARRAY') {
 5479:             my %desc;
 5480:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5481:             $description = \%desc;
 5482:         } else {
 5483:             $description = {};
 5484:         }
 5485:     }
 5486:     return (\@possroles,$description);
 5487: }
 5488: 
 5489: # ----------------------------------------------------- Frontpage Announcements
 5490: #
 5491: #
 5492: 
 5493: sub postannounce {
 5494:     my ($server,$text)=@_;
 5495:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5496:     unless ($text=~/\w/) { $text=''; }
 5497:     return &reply('setannounce:'.&escape($text),$server);
 5498: }
 5499: 
 5500: sub getannounce {
 5501: 
 5502:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5503: 	my $announcement='';
 5504: 	while (my $line = <$fh>) { $announcement .= $line; }
 5505: 	close($fh);
 5506: 	if ($announcement=~/\w/) { 
 5507: 	    return 
 5508:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5509:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5510: 	} else {
 5511: 	    return '';
 5512: 	}
 5513:     } else {
 5514: 	return '';
 5515:     }
 5516: }
 5517: 
 5518: # ---------------------------------------------------------- Course ID routines
 5519: # Deal with domain's nohist_courseid.db files
 5520: #
 5521: 
 5522: sub courseidput {
 5523:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5524:     return unless (ref($storehash) eq 'HASH');
 5525:     my $outcome;
 5526:     if ($caller eq 'timeonly') {
 5527:         my $cids = '';
 5528:         foreach my $item (keys(%$storehash)) {
 5529:             $cids.=&escape($item).'&';
 5530:         }
 5531:         $cids=~s/\&$//;
 5532:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5533:                           $coursehome);       
 5534:     } else {
 5535:         my $items = '';
 5536:         foreach my $item (keys(%$storehash)) {
 5537:             $items.= &escape($item).'='.
 5538:                      &freeze_escape($$storehash{$item}).'&';
 5539:         }
 5540:         $items=~s/\&$//;
 5541:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5542:                           $coursehome);
 5543:     }
 5544:     if ($outcome eq 'unknown_cmd') {
 5545:         my $what;
 5546:         foreach my $cid (keys(%$storehash)) {
 5547:             $what .= &escape($cid).'=';
 5548:             foreach my $item ('description','inst_code','owner','type') {
 5549:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5550:             }
 5551:             $what =~ s/\:$/&/;
 5552:         }
 5553:         $what =~ s/\&$//;  
 5554:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5555:     } else {
 5556:         return $outcome;
 5557:     }
 5558: }
 5559: 
 5560: sub courseiddump {
 5561:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5562:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5563:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5564:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5565:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5566:     my $as_hash = 1;
 5567:     my %returnhash;
 5568:     if (!$domfilter) { $domfilter=''; }
 5569:     my %libserv = &all_library();
 5570:     foreach my $tryserver (keys(%libserv)) {
 5571:         if ( (  $hostidflag == 1 
 5572: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5573: 	     || (!defined($hostidflag)) ) {
 5574: 
 5575: 	    if (($domfilter eq '') ||
 5576: 		(&host_domain($tryserver) eq $domfilter)) {
 5577:                 my $rep;
 5578:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5579:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5580:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5581:                                 &escape($descfilter), &escape($instcodefilter), 
 5582:                                 &escape($ownerfilter), &escape($coursefilter),
 5583:                                 &escape($typefilter), &escape($regexp_ok), 
 5584:                                 $as_hash, &escape($selfenrollonly), 
 5585:                                 &escape($catfilter), $showhidden, $caller, 
 5586:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5587:                                 &escape($createdbefore), &escape($createdafter), 
 5588:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5589:                                 $reqcrsdom,&escape($reqinstcode))));
 5590:                 } else {
 5591:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5592:                              $sincefilter.':'.&escape($descfilter).':'.
 5593:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5594:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5595:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5596:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5597:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5598:                              &escape($cc_clone).':'.$cloneonly.':'.
 5599:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5600:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5601:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5602:                 }
 5603:                      
 5604:                 my @pairs=split(/\&/,$rep);
 5605:                 foreach my $item (@pairs) {
 5606:                     my ($key,$value)=split(/\=/,$item,2);
 5607:                     $key = &unescape($key);
 5608:                     next if ($key =~ /^error: 2 /);
 5609:                     my $result = &thaw_unescape($value);
 5610:                     if (ref($result) eq 'HASH') {
 5611:                         $returnhash{$key}=$result;
 5612:                     } else {
 5613:                         my @responses = split(/:/,$value);
 5614:                         my @items = ('description','inst_code','owner','type');
 5615:                         for (my $i=0; $i<@responses; $i++) {
 5616:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5617:                         }
 5618:                     }
 5619:                 }
 5620:             }
 5621:         }
 5622:     }
 5623:     return %returnhash;
 5624: }
 5625: 
 5626: sub courselastaccess {
 5627:     my ($cdom,$cnum,$hostidref) = @_;
 5628:     my %returnhash;
 5629:     if ($cdom && $cnum) {
 5630:         my $chome = &homeserver($cnum,$cdom);
 5631:         if ($chome ne 'no_host') {
 5632:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5633:             &extract_lastaccess(\%returnhash,$rep);
 5634:         }
 5635:     } else {
 5636:         if (!$cdom) { $cdom=''; }
 5637:         my %libserv = &all_library();
 5638:         foreach my $tryserver (keys(%libserv)) {
 5639:             if (ref($hostidref) eq 'ARRAY') {
 5640:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5641:             } 
 5642:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5643:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5644:                 &extract_lastaccess(\%returnhash,$rep);
 5645:             }
 5646:         }
 5647:     }
 5648:     return %returnhash;
 5649: }
 5650: 
 5651: sub extract_lastaccess {
 5652:     my ($returnhash,$rep) = @_;
 5653:     if (ref($returnhash) eq 'HASH') {
 5654:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5655:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5656:                  $rep eq '') {
 5657:             my @pairs=split(/\&/,$rep);
 5658:             foreach my $item (@pairs) {
 5659:                 my ($key,$value)=split(/\=/,$item,2);
 5660:                 $key = &unescape($key);
 5661:                 next if ($key =~ /^error: 2 /);
 5662:                 $returnhash->{$key} = &thaw_unescape($value);
 5663:             }
 5664:         }
 5665:     }
 5666:     return;
 5667: }
 5668: 
 5669: # ---------------------------------------------------------- DC e-mail
 5670: 
 5671: sub dcmailput {
 5672:     my ($domain,$msgid,$message,$server)=@_;
 5673:     my $status = &Apache::lonnet::critical(
 5674:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5675:        &escape($message),$server);
 5676:     return $status;
 5677: }
 5678: 
 5679: sub dcmaildump {
 5680:     my ($dom,$startdate,$enddate,$senders) = @_;
 5681:     my %returnhash=();
 5682: 
 5683:     if (defined(&domain($dom,'primary'))) {
 5684:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5685:                                                          &escape($enddate).':';
 5686: 	my @esc_senders=map { &escape($_)} @$senders;
 5687: 	$cmd.=&escape(join('&',@esc_senders));
 5688: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5689:             my ($key,$value) = split(/\=/,$line,2);
 5690:             if (($key) && ($value)) {
 5691:                 $returnhash{&unescape($key)} = &unescape($value);
 5692:             }
 5693:         }
 5694:     }
 5695:     return %returnhash;
 5696: }
 5697: # ---------------------------------------------------------- Domain roles
 5698: 
 5699: sub get_domain_roles {
 5700:     my ($dom,$roles,$startdate,$enddate)=@_;
 5701:     if ((!defined($startdate)) || ($startdate eq '')) {
 5702:         $startdate = '.';
 5703:     }
 5704:     if ((!defined($enddate)) || ($enddate eq '')) {
 5705:         $enddate = '.';
 5706:     }
 5707:     my $rolelist;
 5708:     if (ref($roles) eq 'ARRAY') {
 5709:         $rolelist = join('&',@{$roles});
 5710:     }
 5711:     my %personnel = ();
 5712: 
 5713:     my %servers = &get_servers($dom,'library');
 5714:     foreach my $tryserver (keys(%servers)) {
 5715: 	%{$personnel{$tryserver}}=();
 5716: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5717: 					    &escape($startdate).':'.
 5718: 					    &escape($enddate).':'.
 5719: 					    &escape($rolelist), $tryserver))) {
 5720: 	    my ($key,$value) = split(/\=/,$line,2);
 5721: 	    if (($key) && ($value)) {
 5722: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5723: 	    }
 5724: 	}
 5725:     }
 5726:     return %personnel;
 5727: }
 5728: 
 5729: sub get_active_domroles {
 5730:     my ($dom,$roles) = @_;
 5731:     return () unless (ref($roles) eq 'ARRAY');
 5732:     my $now = time;
 5733:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5734:     my %domroles;
 5735:     foreach my $server (keys(%dompersonnel)) {
 5736:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5737:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5738:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5739:         }
 5740:     }
 5741:     return %domroles;
 5742: }
 5743: 
 5744: # ----------------------------------------------------------- Interval timing 
 5745: 
 5746: {
 5747: # Caches needed for speedup of navmaps
 5748: # We don't want to cache this for very long at all (5 seconds at most)
 5749: # 
 5750: # The user for whom we cache
 5751: my $cachedkey='';
 5752: # The cached times for this user
 5753: my %cachedtimes=();
 5754: # When this was last done
 5755: my $cachedtime='';
 5756: 
 5757: sub load_all_first_access {
 5758:     my ($uname,$udom,$ignorecache)=@_;
 5759:     if (($cachedkey eq $uname.':'.$udom) &&
 5760:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5761:         (!$ignorecache)) {
 5762:         return;
 5763:     }
 5764:     $cachedtime=time;
 5765:     $cachedkey=$uname.':'.$udom;
 5766:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5767: }
 5768: 
 5769: sub get_first_access {
 5770:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5771:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5772:     if ($argsymb) { $symb=$argsymb; }
 5773:     my ($map,$id,$res)=&decode_symb($symb);
 5774:     if ($argmap) { $map = $argmap; }
 5775:     if ($type eq 'course') {
 5776: 	$res='course';
 5777:     } elsif ($type eq 'map') {
 5778: 	$res=&symbread($map);
 5779:     } else {
 5780: 	$res=$symb;
 5781:     }
 5782:     &load_all_first_access($uname,$udom,$ignorecache);
 5783:     return $cachedtimes{"$courseid\0$res"};
 5784: }
 5785: 
 5786: sub set_first_access {
 5787:     my ($type,$interval)=@_;
 5788:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5789:     my ($map,$id,$res)=&decode_symb($symb);
 5790:     if ($type eq 'course') {
 5791: 	$res='course';
 5792:     } elsif ($type eq 'map') {
 5793: 	$res=&symbread($map);
 5794:     } else {
 5795: 	$res=$symb;
 5796:     }
 5797:     $cachedkey='';
 5798:     my $firstaccess=&get_first_access($type,$symb,$map);
 5799:     if ($firstaccess) {
 5800:         &logthis("First access time already set ($firstaccess) when attempting ".
 5801:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5802:                  "in $courseid");
 5803:         return 'already_set';
 5804:     } else {
 5805:         my $start = time;
 5806: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5807:                           $udom,$uname);
 5808:         if ($putres eq 'ok') {
 5809:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5810:                  $udom,$uname); 
 5811:             &appenv(
 5812:                      {
 5813:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5814:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5815:                      }
 5816:                   );
 5817:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5818:                 $cachedtimes{"$courseid\0$res"} = $start;
 5819:             }
 5820:         } elsif ($putres ne 'refused') {
 5821:             &logthis("Result: $putres when attempting to set first access time ".
 5822:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5823:         }
 5824:         return $putres;
 5825:     }
 5826:     return 'already_set';
 5827: }
 5828: }
 5829: 
 5830: # --------------------------------------------- Set Expire Date for Spreadsheet
 5831: 
 5832: sub expirespread {
 5833:     my ($uname,$udom,$stype,$usymb)=@_;
 5834:     my $cid=$env{'request.course.id'}; 
 5835:     if ($cid) {
 5836:        my $now=time;
 5837:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5838:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5839:                             $env{'course.'.$cid.'.num'}.
 5840: 	        	    ':nohist_expirationdates:'.
 5841:                             &escape($key).'='.$now,
 5842:                             $env{'course.'.$cid.'.home'})
 5843:     }
 5844:     return 'ok';
 5845: }
 5846: 
 5847: # ----------------------------------------------------- Devalidate Spreadsheets
 5848: 
 5849: sub devalidate {
 5850:     my ($symb,$uname,$udom)=@_;
 5851:     my $cid=$env{'request.course.id'}; 
 5852:     if ($cid) {
 5853:         # delete the stored spreadsheets for
 5854:         # - the student level sheet of this user in course's homespace
 5855:         # - the assessment level sheet for this resource 
 5856:         #   for this user in user's homespace
 5857: 	# - current conditional state info
 5858: 	my $key=$uname.':'.$udom.':';
 5859:         my $status=
 5860: 	    &del('nohist_calculatedsheets',
 5861: 		 [$key.'studentcalc:'],
 5862: 		 $env{'course.'.$cid.'.domain'},
 5863: 		 $env{'course.'.$cid.'.num'})
 5864: 		.' '.
 5865: 	    &del('nohist_calculatedsheets_'.$cid,
 5866: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5867:         unless ($status eq 'ok ok') {
 5868:            &logthis('Could not devalidate spreadsheet '.
 5869:                     $uname.' at '.$udom.' for '.
 5870: 		    $symb.': '.$status);
 5871:         }
 5872: 	&delenv('user.state.'.$cid);
 5873:     }
 5874: }
 5875: 
 5876: sub get_scalar {
 5877:     my ($string,$end) = @_;
 5878:     my $value;
 5879:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5880: 	$value = $1;
 5881:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5882: 	$value = $1;
 5883:     }
 5884:     return &unescape($value);
 5885: }
 5886: 
 5887: sub array2str {
 5888:   my (@array) = @_;
 5889:   my $result=&arrayref2str(\@array);
 5890:   $result=~s/^__ARRAY_REF__//;
 5891:   $result=~s/__END_ARRAY_REF__$//;
 5892:   return $result;
 5893: }
 5894: 
 5895: sub arrayref2str {
 5896:   my ($arrayref) = @_;
 5897:   my $result='__ARRAY_REF__';
 5898:   foreach my $elem (@$arrayref) {
 5899:     if(ref($elem) eq 'ARRAY') {
 5900:       $result.=&arrayref2str($elem).'&';
 5901:     } elsif(ref($elem) eq 'HASH') {
 5902:       $result.=&hashref2str($elem).'&';
 5903:     } elsif(ref($elem)) {
 5904:       #print("Got a ref of ".(ref($elem))." skipping.");
 5905:     } else {
 5906:       $result.=&escape($elem).'&';
 5907:     }
 5908:   }
 5909:   $result=~s/\&$//;
 5910:   $result .= '__END_ARRAY_REF__';
 5911:   return $result;
 5912: }
 5913: 
 5914: sub hash2str {
 5915:   my (%hash) = @_;
 5916:   my $result=&hashref2str(\%hash);
 5917:   $result=~s/^__HASH_REF__//;
 5918:   $result=~s/__END_HASH_REF__$//;
 5919:   return $result;
 5920: }
 5921: 
 5922: sub hashref2str {
 5923:   my ($hashref)=@_;
 5924:   my $result='__HASH_REF__';
 5925:   foreach my $key (sort(keys(%$hashref))) {
 5926:     if (ref($key) eq 'ARRAY') {
 5927:       $result.=&arrayref2str($key).'=';
 5928:     } elsif (ref($key) eq 'HASH') {
 5929:       $result.=&hashref2str($key).'=';
 5930:     } elsif (ref($key)) {
 5931:       $result.='=';
 5932:       #print("Got a ref of ".(ref($key))." skipping.");
 5933:     } else {
 5934: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 5935:     }
 5936: 
 5937:     if(ref($hashref->{$key}) eq 'ARRAY') {
 5938:       $result.=&arrayref2str($hashref->{$key}).'&';
 5939:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 5940:       $result.=&hashref2str($hashref->{$key}).'&';
 5941:     } elsif(ref($hashref->{$key})) {
 5942:        $result.='&';
 5943:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 5944:     } else {
 5945:       $result.=&escape($hashref->{$key}).'&';
 5946:     }
 5947:   }
 5948:   $result=~s/\&$//;
 5949:   $result .= '__END_HASH_REF__';
 5950:   return $result;
 5951: }
 5952: 
 5953: sub str2hash {
 5954:     my ($string)=@_;
 5955:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 5956:     return %$hash;
 5957: }
 5958: 
 5959: sub str2hashref {
 5960:   my ($string) = @_;
 5961: 
 5962:   my %hash;
 5963: 
 5964:   if($string !~ /^__HASH_REF__/) {
 5965:       if (! ($string eq '' || !defined($string))) {
 5966: 	  $hash{'error'}='Not hash reference';
 5967:       }
 5968:       return (\%hash, $string);
 5969:   }
 5970: 
 5971:   $string =~ s/^__HASH_REF__//;
 5972: 
 5973:   while($string !~ /^__END_HASH_REF__/) {
 5974:       #key
 5975:       my $key='';
 5976:       if($string =~ /^__HASH_REF__/) {
 5977:           ($key, $string)=&str2hashref($string);
 5978:           if(defined($key->{'error'})) {
 5979:               $hash{'error'}='Bad data';
 5980:               return (\%hash, $string);
 5981:           }
 5982:       } elsif($string =~ /^__ARRAY_REF__/) {
 5983:           ($key, $string)=&str2arrayref($string);
 5984:           if($key->[0] eq 'Array reference error') {
 5985:               $hash{'error'}='Bad data';
 5986:               return (\%hash, $string);
 5987:           }
 5988:       } else {
 5989:           $string =~ s/^(.*?)=//;
 5990: 	  $key=&unescape($1);
 5991:       }
 5992:       $string =~ s/^=//;
 5993: 
 5994:       #value
 5995:       my $value='';
 5996:       if($string =~ /^__HASH_REF__/) {
 5997:           ($value, $string)=&str2hashref($string);
 5998:           if(defined($value->{'error'})) {
 5999:               $hash{'error'}='Bad data';
 6000:               return (\%hash, $string);
 6001:           }
 6002:       } elsif($string =~ /^__ARRAY_REF__/) {
 6003:           ($value, $string)=&str2arrayref($string);
 6004:           if($value->[0] eq 'Array reference error') {
 6005:               $hash{'error'}='Bad data';
 6006:               return (\%hash, $string);
 6007:           }
 6008:       } else {
 6009: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6010:       }
 6011:       $string =~ s/^&//;
 6012: 
 6013:       $hash{$key}=$value;
 6014:   }
 6015: 
 6016:   $string =~ s/^__END_HASH_REF__//;
 6017: 
 6018:   return (\%hash, $string);
 6019: }
 6020: 
 6021: sub str2array {
 6022:     my ($string)=@_;
 6023:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6024:     return @$array;
 6025: }
 6026: 
 6027: sub str2arrayref {
 6028:   my ($string) = @_;
 6029:   my @array;
 6030: 
 6031:   if($string !~ /^__ARRAY_REF__/) {
 6032:       if (! ($string eq '' || !defined($string))) {
 6033: 	  $array[0]='Array reference error';
 6034:       }
 6035:       return (\@array, $string);
 6036:   }
 6037: 
 6038:   $string =~ s/^__ARRAY_REF__//;
 6039: 
 6040:   while($string !~ /^__END_ARRAY_REF__/) {
 6041:       my $value='';
 6042:       if($string =~ /^__HASH_REF__/) {
 6043:           ($value, $string)=&str2hashref($string);
 6044:           if(defined($value->{'error'})) {
 6045:               $array[0] ='Array reference error';
 6046:               return (\@array, $string);
 6047:           }
 6048:       } elsif($string =~ /^__ARRAY_REF__/) {
 6049:           ($value, $string)=&str2arrayref($string);
 6050:           if($value->[0] eq 'Array reference error') {
 6051:               $array[0] ='Array reference error';
 6052:               return (\@array, $string);
 6053:           }
 6054:       } else {
 6055: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6056:       }
 6057:       $string =~ s/^&//;
 6058: 
 6059:       push(@array, $value);
 6060:   }
 6061: 
 6062:   $string =~ s/^__END_ARRAY_REF__//;
 6063: 
 6064:   return (\@array, $string);
 6065: }
 6066: 
 6067: # -------------------------------------------------------------------Temp Store
 6068: 
 6069: sub tmpreset {
 6070:   my ($symb,$namespace,$domain,$stuname) = @_;
 6071:   if (!$symb) {
 6072:     $symb=&symbread();
 6073:     if (!$symb) { $symb= $env{'request.url'}; }
 6074:   }
 6075:   $symb=escape($symb);
 6076: 
 6077:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6078:   $namespace=~s/\//\_/g;
 6079:   $namespace=~s/\W//g;
 6080: 
 6081:   if (!$domain) { $domain=$env{'user.domain'}; }
 6082:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6083:   if ($domain eq 'public' && $stuname eq 'public') {
 6084:       $stuname=$ENV{'REMOTE_ADDR'};
 6085:   }
 6086:   my $path=LONCAPA::tempdir();
 6087:   my %hash;
 6088:   if (tie(%hash,'GDBM_File',
 6089: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6090: 	  &GDBM_WRCREAT(),0640)) {
 6091:     foreach my $key (keys(%hash)) {
 6092:       if ($key=~ /:$symb/) {
 6093: 	delete($hash{$key});
 6094:       }
 6095:     }
 6096:   }
 6097: }
 6098: 
 6099: sub tmpstore {
 6100:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6101: 
 6102:   if (!$symb) {
 6103:     $symb=&symbread();
 6104:     if (!$symb) { $symb= $env{'request.url'}; }
 6105:   }
 6106:   $symb=escape($symb);
 6107: 
 6108:   if (!$namespace) {
 6109:     # I don't think we would ever want to store this for a course.
 6110:     # it seems this will only be used if we don't have a course.
 6111:     #$namespace=$env{'request.course.id'};
 6112:     #if (!$namespace) {
 6113:       $namespace=$env{'request.state'};
 6114:     #}
 6115:   }
 6116:   $namespace=~s/\//\_/g;
 6117:   $namespace=~s/\W//g;
 6118:   if (!$domain) { $domain=$env{'user.domain'}; }
 6119:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6120:   if ($domain eq 'public' && $stuname eq 'public') {
 6121:       $stuname=$ENV{'REMOTE_ADDR'};
 6122:   }
 6123:   my $now=time;
 6124:   my %hash;
 6125:   my $path=LONCAPA::tempdir();
 6126:   if (tie(%hash,'GDBM_File',
 6127: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6128: 	  &GDBM_WRCREAT(),0640)) {
 6129:     $hash{"version:$symb"}++;
 6130:     my $version=$hash{"version:$symb"};
 6131:     my $allkeys=''; 
 6132:     foreach my $key (keys(%$storehash)) {
 6133:       $allkeys.=$key.':';
 6134:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6135:     }
 6136:     $hash{"$version:$symb:timestamp"}=$now;
 6137:     $allkeys.='timestamp';
 6138:     $hash{"$version:keys:$symb"}=$allkeys;
 6139:     if (untie(%hash)) {
 6140:       return 'ok';
 6141:     } else {
 6142:       return "error:$!";
 6143:     }
 6144:   } else {
 6145:     return "error:$!";
 6146:   }
 6147: }
 6148: 
 6149: # -----------------------------------------------------------------Temp Restore
 6150: 
 6151: sub tmprestore {
 6152:   my ($symb,$namespace,$domain,$stuname) = @_;
 6153: 
 6154:   if (!$symb) {
 6155:     $symb=&symbread();
 6156:     if (!$symb) { $symb= $env{'request.url'}; }
 6157:   }
 6158:   $symb=escape($symb);
 6159: 
 6160:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6161: 
 6162:   if (!$domain) { $domain=$env{'user.domain'}; }
 6163:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6164:   if ($domain eq 'public' && $stuname eq 'public') {
 6165:       $stuname=$ENV{'REMOTE_ADDR'};
 6166:   }
 6167:   my %returnhash;
 6168:   $namespace=~s/\//\_/g;
 6169:   $namespace=~s/\W//g;
 6170:   my %hash;
 6171:   my $path=LONCAPA::tempdir();
 6172:   if (tie(%hash,'GDBM_File',
 6173: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6174: 	  &GDBM_READER(),0640)) {
 6175:     my $version=$hash{"version:$symb"};
 6176:     $returnhash{'version'}=$version;
 6177:     my $scope;
 6178:     for ($scope=1;$scope<=$version;$scope++) {
 6179:       my $vkeys=$hash{"$scope:keys:$symb"};
 6180:       my @keys=split(/:/,$vkeys);
 6181:       my $key;
 6182:       $returnhash{"$scope:keys"}=$vkeys;
 6183:       foreach $key (@keys) {
 6184: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6185: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6186:       }
 6187:     }
 6188:     if (!(untie(%hash))) {
 6189:       return "error:$!";
 6190:     }
 6191:   } else {
 6192:     return "error:$!";
 6193:   }
 6194:   return %returnhash;
 6195: }
 6196: 
 6197: # ----------------------------------------------------------------------- Store
 6198: 
 6199: sub store {
 6200:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6201:     my $home='';
 6202: 
 6203:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6204: 
 6205:     $symb=&symbclean($symb);
 6206:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6207: 
 6208:     if (!$domain) { $domain=$env{'user.domain'}; }
 6209:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6210: 
 6211:     &devalidate($symb,$stuname,$domain);
 6212: 
 6213:     $symb=escape($symb);
 6214:     if (!$namespace) { 
 6215:        unless ($namespace=$env{'request.course.id'}) { 
 6216:           return ''; 
 6217:        } 
 6218:     }
 6219:     if (!$home) { $home=$env{'user.home'}; }
 6220: 
 6221:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 6222:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6223: 
 6224:     my $namevalue='';
 6225:     foreach my $key (keys(%$storehash)) {
 6226:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6227:     }
 6228:     $namevalue=~s/\&$//;
 6229:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6230:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6231: }
 6232: 
 6233: # -------------------------------------------------------------- Critical Store
 6234: 
 6235: sub cstore {
 6236:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6237:     my $home='';
 6238: 
 6239:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6240: 
 6241:     $symb=&symbclean($symb);
 6242:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6243: 
 6244:     if (!$domain) { $domain=$env{'user.domain'}; }
 6245:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6246: 
 6247:     &devalidate($symb,$stuname,$domain);
 6248: 
 6249:     $symb=escape($symb);
 6250:     if (!$namespace) { 
 6251:        unless ($namespace=$env{'request.course.id'}) { 
 6252:           return ''; 
 6253:        } 
 6254:     }
 6255:     if (!$home) { $home=$env{'user.home'}; }
 6256: 
 6257:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 6258:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6259: 
 6260:     my $namevalue='';
 6261:     foreach my $key (keys(%$storehash)) {
 6262:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6263:     }
 6264:     $namevalue=~s/\&$//;
 6265:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6266:     return critical
 6267:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6268: }
 6269: 
 6270: # --------------------------------------------------------------------- Restore
 6271: 
 6272: sub restore {
 6273:     my ($symb,$namespace,$domain,$stuname) = @_;
 6274:     my $home='';
 6275: 
 6276:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6277: 
 6278:     if (!$symb) {
 6279:         return if ($namespace eq 'courserequests');
 6280:         unless ($symb=escape(&symbread())) { return ''; }
 6281:     } else {
 6282:         unless ($namespace eq 'courserequests') {
 6283:             $symb=&escape(&symbclean($symb));
 6284:         }
 6285:     }
 6286:     if (!$namespace) { 
 6287:        unless ($namespace=$env{'request.course.id'}) { 
 6288:           return ''; 
 6289:        } 
 6290:     }
 6291:     if (!$domain) { $domain=$env{'user.domain'}; }
 6292:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6293:     if (!$home) { $home=$env{'user.home'}; }
 6294:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6295: 
 6296:     my %returnhash=();
 6297:     foreach my $line (split(/\&/,$answer)) {
 6298: 	my ($name,$value)=split(/\=/,$line);
 6299:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6300:     }
 6301:     my $version;
 6302:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6303:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6304:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6305:        }
 6306:     }
 6307:     return %returnhash;
 6308: }
 6309: 
 6310: # ---------------------------------------------------------- Course Description
 6311: #
 6312: #  
 6313: 
 6314: sub coursedescription {
 6315:     my ($courseid,$args)=@_;
 6316:     $courseid=~s/^\///;
 6317:     $courseid=~s/\_/\//g;
 6318:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6319:     my $chome=&homeserver($cnum,$cdomain);
 6320:     my $normalid=$cdomain.'_'.$cnum;
 6321:     # need to always cache even if we get errors otherwise we keep 
 6322:     # trying and trying and trying to get the course description.
 6323:     my %envhash=();
 6324:     my %returnhash=();
 6325:     
 6326:     my $expiretime=600;
 6327:     if ($env{'request.course.id'} eq $normalid) {
 6328: 	$expiretime=120;
 6329:     }
 6330: 
 6331:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6332:     if (!$args->{'freshen_cache'}
 6333: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6334: 	foreach my $key (keys(%env)) {
 6335: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6336: 	    my ($setting) = $1;
 6337: 	    $returnhash{$setting} = $env{$key};
 6338: 	}
 6339: 	return %returnhash;
 6340:     }
 6341: 
 6342:     # get the data again
 6343: 
 6344:     if (!$args->{'one_time'}) {
 6345: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6346:     }
 6347: 
 6348:     if ($chome ne 'no_host') {
 6349:        %returnhash=&dump('environment',$cdomain,$cnum);
 6350:        if (!exists($returnhash{'con_lost'})) {
 6351: 	   my $username = $env{'user.name'}; # Defult username
 6352: 	   if(defined $args->{'user'}) {
 6353: 	       $username = $args->{'user'};
 6354: 	   }
 6355:            $returnhash{'home'}= $chome;
 6356: 	   $returnhash{'domain'} = $cdomain;
 6357: 	   $returnhash{'num'} = $cnum;
 6358:            if (!defined($returnhash{'type'})) {
 6359:                $returnhash{'type'} = 'Course';
 6360:            }
 6361:            while (my ($name,$value) = each %returnhash) {
 6362:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6363:            }
 6364:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6365:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6366: 	       $username.'_'.$cdomain.'_'.$cnum;
 6367:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6368:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6369:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6370:        }
 6371:     }
 6372:     if (!$args->{'one_time'}) {
 6373: 	&appenv(\%envhash);
 6374:     }
 6375:     return %returnhash;
 6376: }
 6377: 
 6378: sub update_released_required {
 6379:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6380:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6381:         $cid = $env{'request.course.id'};
 6382:         $cdom = $env{'course.'.$cid.'.domain'};
 6383:         $cnum = $env{'course.'.$cid.'.num'};
 6384:         $chome = $env{'course.'.$cid.'.home'};
 6385:     }
 6386:     if ($needsrelease) {
 6387:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6388:         my $needsupdate;
 6389:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6390:             $needsupdate = 1;
 6391:         } else {
 6392:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6393:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6394:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6395:                 $needsupdate = 1;
 6396:             }
 6397:         }
 6398:         if ($needsupdate) {
 6399:             my %needshash = (
 6400:                              'internal.releaserequired' => $needsrelease,
 6401:                             );
 6402:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6403:             if ($putresult eq 'ok') {
 6404:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6405:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6406:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6407:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6408:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6409:                 }
 6410:             }
 6411:         }
 6412:     }
 6413:     return;
 6414: }
 6415: 
 6416: # -------------------------------------------------See if a user is privileged
 6417: 
 6418: sub privileged {
 6419:     my ($username,$domain,$possdomains,$possroles)=@_;
 6420:     my $now = time;
 6421:     my $roles;
 6422:     if (ref($possroles) eq 'ARRAY') {
 6423:         $roles = $possroles; 
 6424:     } else {
 6425:         $roles = ['dc','su'];
 6426:     }
 6427:     if (ref($possdomains) eq 'ARRAY') {
 6428:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6429:         foreach my $dom (@{$possdomains}) {
 6430:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6431:                 (ref($privileged{$dom}) eq 'HASH')) {
 6432:                 foreach my $role (@{$roles}) {
 6433:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6434:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6435:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6436:                             return 1 unless (($end && $end < $now) ||
 6437:                                              ($start && $start > $now));
 6438:                         }
 6439:                     }
 6440:                 }
 6441:             }
 6442:         }
 6443:     } else {
 6444:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6445:         my $now = time;
 6446: 
 6447:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6448:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6449:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6450:                 return 1 unless ($tend && $tend < $now) 
 6451:                         or ($tstart && $tstart > $now);
 6452:             }
 6453:         }
 6454:     }
 6455:     return 0;
 6456: }
 6457: 
 6458: sub privileged_by_domain {
 6459:     my ($domains,$roles) = @_;
 6460:     my %privileged = ();
 6461:     my $cachetime = 60*60*24;
 6462:     my $now = time;
 6463:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6464:         return %privileged;
 6465:     }
 6466:     foreach my $dom (@{$domains}) {
 6467:         next if (ref($privileged{$dom}) eq 'HASH');
 6468:         my $needroles;
 6469:         foreach my $role (@{$roles}) {
 6470:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6471:             if (defined($cached)) {
 6472:                 if (ref($result) eq 'HASH') {
 6473:                     $privileged{$dom}{$role} = $result;
 6474:                 }
 6475:             } else {
 6476:                 $needroles = 1;
 6477:             }
 6478:         }
 6479:         if ($needroles) {
 6480:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6481:             $privileged{$dom} = {};
 6482:             foreach my $server (keys(%dompersonnel)) {
 6483:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6484:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6485:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6486:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6487:                         next if ($end && $end < $now);
 6488:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6489:                             $dompersonnel{$server}{$item};
 6490:                     }
 6491:                 }
 6492:             }
 6493:             if (ref($privileged{$dom}) eq 'HASH') {
 6494:                 foreach my $role (@{$roles}) {
 6495:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6496:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6497:                     } else {
 6498:                         my %hash = ();
 6499:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6500:                     }
 6501:                 }
 6502:             }
 6503:         }
 6504:     }
 6505:     return %privileged;
 6506: }
 6507: 
 6508: # -------------------------------------------------------- Get user privileges
 6509: 
 6510: sub rolesinit {
 6511:     my ($domain, $username) = @_;
 6512:     my %userroles = ('user.login.time' => time);
 6513:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6514: 
 6515:     # firstaccess and timerinterval are related to timed maps/resources. 
 6516:     # also, blocking can be triggered by an activating timer
 6517:     # it's saved in the user's %env.
 6518:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6519:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6520:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6521:         %timerintchk, %timerintenv);
 6522: 
 6523:     foreach my $key (keys(%firstaccess)) {
 6524:         my ($cid, $rest) = split(/\0/, $key);
 6525:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6526:     }
 6527: 
 6528:     foreach my $key (keys(%timerinterval)) {
 6529:         my ($cid,$rest) = split(/\0/,$key);
 6530:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6531:     }
 6532: 
 6533:     my %allroles=();
 6534:     my %allgroups=();
 6535: 
 6536:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6537:         my $role = $rolesdump{$area};
 6538:         $area =~ s/\_\w\w$//;
 6539: 
 6540:         my ($trole, $tend, $tstart, $group_privs);
 6541: 
 6542:         if ($role =~ /^cr/) {
 6543:         # Custom role, defined by a user 
 6544:         # e.g., user.role.cr/msu/smith/mynewrole
 6545:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6546:                 $trole = $1;
 6547:                 ($tend, $tstart) = split('_', $2);
 6548:             } else {
 6549:                 $trole = $role;
 6550:             }
 6551:         } elsif ($role =~ m|^gr/|) {
 6552:         # Role of member in a group, defined within a course/community
 6553:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6554:             ($trole, $tend, $tstart) = split(/_/, $role);
 6555:             next if $tstart eq '-1';
 6556:             ($trole, $group_privs) = split(/\//, $trole);
 6557:             $group_privs = &unescape($group_privs);
 6558:         } else {
 6559:         # Just a normal role, defined in roles.tab
 6560:             ($trole, $tend, $tstart) = split(/_/,$role);
 6561:         }
 6562: 
 6563:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6564:                  $username);
 6565:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6566: 
 6567:         # role expired or not available yet?
 6568:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6569:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6570: 
 6571:         next if $area eq '' or $trole eq '';
 6572: 
 6573:         my $spec = "$trole.$area";
 6574:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6575: 
 6576:         if ($trole =~ /^cr\//) {
 6577:         # Custom role, defined by a user
 6578:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6579:         } elsif ($trole eq 'gr') {
 6580:         # Role of a member in a group, defined within a course/community
 6581:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6582:             next;
 6583:         } else {
 6584:         # Normal role, defined in roles.tab
 6585:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6586:         }
 6587: 
 6588:         my $cid = $tdomain.'_'.$trest;
 6589:         unless ($firstaccchk{$cid}) {
 6590:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6591:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6592:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6593:                         $coursetimerstarts{$cid}{$item}; 
 6594:                 }
 6595:             }
 6596:             $firstaccchk{$cid} = 1;
 6597:         }
 6598:         unless ($timerintchk{$cid}) {
 6599:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6600:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6601:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6602:                        $coursetimerintervals{$cid}{$item};
 6603:                 }
 6604:             }
 6605:             $timerintchk{$cid} = 1;
 6606:         }
 6607:     }
 6608: 
 6609:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6610:                                                           \%allroles, \%allgroups);
 6611:     $env{'user.adv'} = $userroles{'user.adv'};
 6612:     $env{'user.rar'} = $userroles{'user.rar'};
 6613: 
 6614:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6615: }
 6616: 
 6617: sub set_arearole {
 6618:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6619:     unless ($nolog) {
 6620: # log the associated role with the area
 6621:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6622:     }
 6623:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6624: }
 6625: 
 6626: sub custom_roleprivs {
 6627:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6628:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6629:     my $homsvr = &homeserver($rauthor,$rdomain);
 6630:     if (&hostname($homsvr) ne '') {
 6631:         my ($rdummy,$roledef)=
 6632:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6633:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6634:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6635:             if (defined($syspriv)) {
 6636:                 if ($trest =~ /^$match_community$/) {
 6637:                     $syspriv =~ s/bre\&S//; 
 6638:                 }
 6639:                 $$allroles{'cm./'}.=':'.$syspriv;
 6640:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6641:             }
 6642:             if ($tdomain ne '') {
 6643:                 if (defined($dompriv)) {
 6644:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6645:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6646:                 }
 6647:                 if (($trest ne '') && (defined($coursepriv))) {
 6648:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6649:                         my $rolename = $1;
 6650:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6651:                     }
 6652:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6653:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6654:                 }
 6655:             }
 6656:         }
 6657:     }
 6658: }
 6659: 
 6660: sub course_adhocrole_privs {
 6661:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6662:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6663:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6664:         my (%currprivs,%storeprivs);
 6665:         foreach my $item (split(/:/,$coursepriv)) {
 6666:             my ($priv,$restrict) = split(/\&/,$item);
 6667:             $currprivs{$priv} = $restrict;
 6668:         }
 6669:         my (%possadd,%possremove,%full);
 6670:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6671:             my ($priv,$restrict)=split(/\&/,$item);
 6672:             $full{$priv} = $restrict;
 6673:         }
 6674:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6675:              next if ($item eq '');
 6676:              my ($rule,$rest) = split(/=/,$item);
 6677:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6678:              foreach my $priv (split(/:/,$rest)) {
 6679:                  if ($priv ne '') {
 6680:                      if ($rule eq 'off') {
 6681:                          $possremove{$priv} = 1;
 6682:                      } else {
 6683:                          $possadd{$priv} = 1;
 6684:                      }
 6685:                  }
 6686:              }
 6687:          }
 6688:          foreach my $priv (sort(keys(%full))) {
 6689:              if (exists($currprivs{$priv})) {
 6690:                  unless (exists($possremove{$priv})) {
 6691:                      $storeprivs{$priv} = $currprivs{$priv};
 6692:                  }
 6693:              } elsif (exists($possadd{$priv})) {
 6694:                  $storeprivs{$priv} = $full{$priv};
 6695:              }
 6696:          }
 6697:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6698:      }
 6699:      return $coursepriv;
 6700: }
 6701: 
 6702: sub group_roleprivs {
 6703:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6704:     my $access = 1;
 6705:     my $now = time;
 6706:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6707:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6708:     if ($access) {
 6709:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6710:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6711:     }
 6712: }
 6713: 
 6714: sub standard_roleprivs {
 6715:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6716:     if (defined($pr{$trole.':s'})) {
 6717:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6718:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6719:     }
 6720:     if ($tdomain ne '') {
 6721:         if (defined($pr{$trole.':d'})) {
 6722:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6723:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6724:         }
 6725:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6726:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6727:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6728:         }
 6729:     }
 6730: }
 6731: 
 6732: sub set_userprivs {
 6733:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6734:     my $author=0;
 6735:     my $adv=0;
 6736:     my $rar=0;
 6737:     my %grouproles = ();
 6738:     if (keys(%{$allgroups}) > 0) {
 6739:         my @groupkeys; 
 6740:         foreach my $role (keys(%{$allroles})) {
 6741:             push(@groupkeys,$role);
 6742:         }
 6743:         if (ref($groups_roles) eq 'HASH') {
 6744:             foreach my $key (keys(%{$groups_roles})) {
 6745:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6746:                     push(@groupkeys,$key);
 6747:                 }
 6748:             }
 6749:         }
 6750:         if (@groupkeys > 0) {
 6751:             foreach my $role (@groupkeys) {
 6752:                 my ($trole,$area,$sec,$extendedarea);
 6753:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6754:                     $trole = $1;
 6755:                     $area = $2;
 6756:                     $sec = $3;
 6757:                     $extendedarea = $area.$sec;
 6758:                     if (exists($$allgroups{$area})) {
 6759:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6760:                             my $spec = $trole.'.'.$extendedarea;
 6761:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6762:                                                 $$allgroups{$area}{$group};
 6763:                         }
 6764:                     }
 6765:                 }
 6766:             }
 6767:         }
 6768:     }
 6769:     foreach my $group (keys(%grouproles)) {
 6770:         $$allroles{$group} = $grouproles{$group};
 6771:     }
 6772:     foreach my $role (keys(%{$allroles})) {
 6773:         my %thesepriv;
 6774:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6775:         foreach my $item (split(/:/,$$allroles{$role})) {
 6776:             if ($item ne '') {
 6777:                 my ($privilege,$restrictions)=split(/&/,$item);
 6778:                 if ($restrictions eq '') {
 6779:                     $thesepriv{$privilege}='F';
 6780:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6781:                     $thesepriv{$privilege}.=$restrictions;
 6782:                 }
 6783:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6784:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6785:             }
 6786:         }
 6787:         my $thesestr='';
 6788:         foreach my $priv (sort(keys(%thesepriv))) {
 6789: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6790: 	}
 6791:         $userroles->{'user.priv.'.$role} = $thesestr;
 6792:     }
 6793:     return ($author,$adv,$rar);
 6794: }
 6795: 
 6796: sub role_status {
 6797:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6798:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6799:         my ($one,$two) = split(m{\./},$rolekey,2);
 6800:         (undef,undef,$$role) = split(/\./,$one,3);
 6801:         unless (!defined($$role) || $$role eq '') {
 6802:             $$where = '/'.$two;
 6803:             $$trolecode=$$role.'.'.$$where;
 6804:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6805:             $$tstatus='is';
 6806:             if ($$tstart && $$tstart>$update) {
 6807:                 $$tstatus='future';
 6808:                 if ($$tstart<$now) {
 6809:                     if ($$tstart && $$tstart>$refresh) {
 6810:                         if (($$where ne '') && ($$role ne '')) {
 6811:                             my (%allroles,%allgroups,$group_privs,
 6812:                                 %groups_roles,@rolecodes);
 6813:                             my %userroles = (
 6814:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6815:                             );
 6816:                             @rolecodes = ('cm'); 
 6817:                             my $spec=$$role.'.'.$$where;
 6818:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6819:                             if ($$role =~ /^cr\//) {
 6820:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6821:                                 push(@rolecodes,'cr');
 6822:                             } elsif ($$role eq 'gr') {
 6823:                                 push(@rolecodes,$$role);
 6824:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6825:                                                     $env{'user.name'});
 6826:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6827:                                 (undef,my $group_privs) = split(/\//,$trole);
 6828:                                 $group_privs = &unescape($group_privs);
 6829:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6830:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6831:                                 &get_groups_roles($tdomain,$trest,
 6832:                                                   \%course_roles,\@rolecodes,
 6833:                                                   \%groups_roles);
 6834:                             } else {
 6835:                                 push(@rolecodes,$$role);
 6836:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6837:                             }
 6838:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6839:                                                                    \%groups_roles);
 6840:                             &appenv(\%userroles,\@rolecodes);
 6841:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6842:                         }
 6843:                     }
 6844:                     $$tstatus = 'is';
 6845:                 }
 6846:             }
 6847:             if ($$tend) {
 6848:                 if ($$tend<$update) {
 6849:                     $$tstatus='expired';
 6850:                 } elsif ($$tend<$now) {
 6851:                     $$tstatus='will_not';
 6852:                 }
 6853:             }
 6854:         }
 6855:     }
 6856: }
 6857: 
 6858: sub get_groups_roles {
 6859:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6860:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6861:                   (ref($rolecodes) eq 'ARRAY') && 
 6862:                   (ref($groups_roles) eq 'HASH')); 
 6863:     if (keys(%{$cdom_courseroles}) > 0) {
 6864:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6865:         if ($cdom ne '' && $cnum ne '') {
 6866:             foreach my $key (keys(%{$cdom_courseroles})) {
 6867:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6868:                     my $crsrole = $1;
 6869:                     my $crssec = $2;
 6870:                     if ($crsrole =~ /^cr/) {
 6871:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6872:                             push(@{$rolecodes},'cr');
 6873:                         }
 6874:                     } else {
 6875:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6876:                             push(@{$rolecodes},$crsrole);
 6877:                         }
 6878:                     }
 6879:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6880:                     if ($crssec ne '') {
 6881:                         $rolekey .= "/$crssec";
 6882:                     }
 6883:                     $rolekey .= './';
 6884:                     $groups_roles->{$rolekey} = $rolecodes;
 6885:                 }
 6886:             }
 6887:         }
 6888:     }
 6889:     return;
 6890: }
 6891: 
 6892: sub delete_env_groupprivs {
 6893:     my ($where,$courseroles,$possroles) = @_;
 6894:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6895:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6896:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6897:         %{$courseroles->{$udom}} =
 6898:             &get_my_roles('','','userroles',['active'],
 6899:                           $possroles,[$udom],1);
 6900:     }
 6901:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6902:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6903:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6904:             my $area = '/'.$cdom.'/'.$cnum;
 6905:             my $privkey = "user.priv.$crsrole.$area";
 6906:             if ($crssec ne '') {
 6907:                 $privkey .= '/'.$crssec;
 6908:             }
 6909:             $privkey .= ".$area/$group";
 6910:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6911:         }
 6912:     }
 6913:     return;
 6914: }
 6915: 
 6916: sub check_adhoc_privs {
 6917:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6918:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6919:     if ($sec) {
 6920:         $cckey .= '/'.$sec;
 6921:     } 
 6922:     my $setprivs;
 6923:     if ($env{$cckey}) {
 6924:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 6925:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 6926:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 6927:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6928:             $setprivs = 1;
 6929:         }
 6930:     } else {
 6931:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6932:         $setprivs = 1;
 6933:     }
 6934:     return $setprivs;
 6935: }
 6936: 
 6937: sub set_adhoc_privileges {
 6938: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 6939:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 6940:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 6941:     if ($sec ne '') {
 6942:         $area .= '/'.$sec;
 6943:     }
 6944:     my $spec = $role.'.'.$area;
 6945:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 6946:                                   $env{'user.name'},1);
 6947:     my %rolehash = ();
 6948:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 6949:         my $rolename = $1;
 6950:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 6951:         my %domdef = &get_domain_defaults($dcdom);
 6952:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 6953:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 6954:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 6955:             }
 6956:         }
 6957:     } else {
 6958:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 6959:     }
 6960:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 6961:     &appenv(\%userroles,[$role,'cm']);
 6962:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6963:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 6964:             ($caller eq 'tiny')) {
 6965:         &appenv( {'request.role'        => $spec,
 6966:                   'request.role.domain' => $dcdom,
 6967:                   'request.course.sec'  => $sec,
 6968:                  }
 6969:                );
 6970:         my $tadv=0;
 6971:         if (&allowed('adv') eq 'F') { $tadv=1; }
 6972:         &appenv({'request.role.adv'    => $tadv});
 6973:     }
 6974: }
 6975: 
 6976: # --------------------------------------------------------------- get interface
 6977: 
 6978: sub get {
 6979:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6980:    my $items='';
 6981:    foreach my $item (@$storearr) {
 6982:        $items.=&escape($item).'&';
 6983:    }
 6984:    $items=~s/\&$//;
 6985:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6986:    if (!$uname) { $uname=$env{'user.name'}; }
 6987:    my $uhome=&homeserver($uname,$udomain);
 6988: 
 6989:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 6990:    my @pairs=split(/\&/,$rep);
 6991:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 6992:      return @pairs;
 6993:    }
 6994:    my %returnhash=();
 6995:    my $i=0;
 6996:    foreach my $item (@$storearr) {
 6997:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 6998:       $i++;
 6999:    }
 7000:    return %returnhash;
 7001: }
 7002: 
 7003: # --------------------------------------------------------------- del interface
 7004: 
 7005: sub del {
 7006:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7007:    my $items='';
 7008:    foreach my $item (@$storearr) {
 7009:        $items.=&escape($item).'&';
 7010:    }
 7011: 
 7012:    $items=~s/\&$//;
 7013:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7014:    if (!$uname) { $uname=$env{'user.name'}; }
 7015:    my $uhome=&homeserver($uname,$udomain);
 7016:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7017: }
 7018: 
 7019: # -------------------------------------------------------------- dump interface
 7020: 
 7021: sub unserialize {
 7022:     my ($rep, $escapedkeys) = @_;
 7023: 
 7024:     return {} if $rep =~ /^error/;
 7025: 
 7026:     my %returnhash=();
 7027: 	foreach my $item (split(/\&/,$rep)) {
 7028: 	    my ($key, $value) = split(/=/, $item, 2);
 7029: 	    $key = unescape($key) unless $escapedkeys;
 7030: 	    next if $key =~ /^error: 2 /;
 7031: 	    $returnhash{$key} = &thaw_unescape($value);
 7032: 	}
 7033:     #return %returnhash;
 7034:     return \%returnhash;
 7035: }        
 7036: 
 7037: # see Lond::dump_with_regexp
 7038: # if $escapedkeys hash keys won't get unescaped.
 7039: sub dump {
 7040:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 7041:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7042:     if (!$uname) { $uname=$env{'user.name'}; }
 7043:     my $uhome=&homeserver($uname,$udomain);
 7044: 
 7045:     if ($regexp) {
 7046:         $regexp=&escape($regexp);
 7047:     } else {
 7048:         $regexp='.';
 7049:     }
 7050:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7051:         # user is hosted on this machine
 7052:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7053:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7054:         return %{unserialize($reply, $escapedkeys)};
 7055:     }
 7056:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7057:     my @pairs=split(/\&/,$rep);
 7058:     my %returnhash=();
 7059:     if (!($rep =~ /^error/ )) {
 7060: 	foreach my $item (@pairs) {
 7061: 	    my ($key,$value)=split(/=/,$item,2);
 7062:         $key = unescape($key) unless $escapedkeys;
 7063:         #$key = &unescape($key);
 7064: 	    next if ($key =~ /^error: 2 /);
 7065: 	    $returnhash{$key}=&thaw_unescape($value);
 7066: 	}
 7067:     }
 7068:     return %returnhash;
 7069: }
 7070: 
 7071: 
 7072: # --------------------------------------------------------- dumpstore interface
 7073: 
 7074: sub dumpstore {
 7075:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7076:    # same as dump but keys must be escaped. They may contain colon separated
 7077:    # lists of values that may themself contain colons (e.g. symbs).
 7078:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7079: }
 7080: 
 7081: # -------------------------------------------------------------- keys interface
 7082: 
 7083: sub getkeys {
 7084:    my ($namespace,$udomain,$uname)=@_;
 7085:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7086:    if (!$uname) { $uname=$env{'user.name'}; }
 7087:    my $uhome=&homeserver($uname,$udomain);
 7088:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7089:    my @keyarray=();
 7090:    foreach my $key (split(/\&/,$rep)) {
 7091:       next if ($key =~ /^error: 2 /);
 7092:       push(@keyarray,&unescape($key));
 7093:    }
 7094:    return @keyarray;
 7095: }
 7096: 
 7097: # --------------------------------------------------------------- currentdump
 7098: sub currentdump {
 7099:    my ($courseid,$sdom,$sname)=@_;
 7100:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7101:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7102:    $sname    = $env{'user.name'}         if (! defined($sname));
 7103:    my $uhome = &homeserver($sname,$sdom);
 7104:    my $rep;
 7105: 
 7106:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7107:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7108:                    $courseid)));
 7109:    } else {
 7110:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7111:    }
 7112: 
 7113:    return if ($rep =~ /^(error:|no_such_host)/);
 7114:    #
 7115:    my %returnhash=();
 7116:    #
 7117:    if ($rep eq 'unknown_cmd') {
 7118:        # an old lond will not know currentdump
 7119:        # Do a dump and make it look like a currentdump
 7120:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7121:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7122:        my %hash = @tmp;
 7123:        @tmp=();
 7124:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7125:    } else {
 7126:        my @pairs=split(/\&/,$rep);
 7127:        foreach my $pair (@pairs) {
 7128:            my ($key,$value)=split(/=/,$pair,2);
 7129:            my ($symb,$param) = split(/:/,$key);
 7130:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7131:                                                         &thaw_unescape($value);
 7132:        }
 7133:    }
 7134:    return %returnhash;
 7135: }
 7136: 
 7137: sub convert_dump_to_currentdump{
 7138:     my %hash = %{shift()};
 7139:     my %returnhash;
 7140:     # Code ripped from lond, essentially.  The only difference
 7141:     # here is the unescaping done by lonnet::dump().  Conceivably
 7142:     # we might run in to problems with parameter names =~ /^v\./
 7143:     while (my ($key,$value) = each(%hash)) {
 7144:         my ($v,$symb,$param) = split(/:/,$key);
 7145: 	$symb  = &unescape($symb);
 7146: 	$param = &unescape($param);
 7147:         next if ($v eq 'version' || $symb eq 'keys');
 7148:         next if (exists($returnhash{$symb}) &&
 7149:                  exists($returnhash{$symb}->{$param}) &&
 7150:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7151:         $returnhash{$symb}->{$param}=$value;
 7152:         $returnhash{$symb}->{'v.'.$param}=$v;
 7153:     }
 7154:     #
 7155:     # Remove all of the keys in the hashes which keep track of
 7156:     # the version of the parameter.
 7157:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7158:         # use a foreach because we are going to delete from the hash.
 7159:         foreach my $key (keys(%$param_hash)) {
 7160:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7161:         }
 7162:     }
 7163:     return \%returnhash;
 7164: }
 7165: 
 7166: # ------------------------------------------------------ critical inc interface
 7167: 
 7168: sub cinc {
 7169:     return &inc(@_,'critical');
 7170: }
 7171: 
 7172: # --------------------------------------------------------------- inc interface
 7173: 
 7174: sub inc {
 7175:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7176:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7177:     if (!$uname) { $uname=$env{'user.name'}; }
 7178:     my $uhome=&homeserver($uname,$udomain);
 7179:     my $items='';
 7180:     if (! ref($store)) {
 7181:         # got a single value, so use that instead
 7182:         $items = &escape($store).'=&';
 7183:     } elsif (ref($store) eq 'SCALAR') {
 7184:         $items = &escape($$store).'=&';        
 7185:     } elsif (ref($store) eq 'ARRAY') {
 7186:         $items = join('=&',map {&escape($_);} @{$store});
 7187:     } elsif (ref($store) eq 'HASH') {
 7188:         while (my($key,$value) = each(%{$store})) {
 7189:             $items.= &escape($key).'='.&escape($value).'&';
 7190:         }
 7191:     }
 7192:     $items=~s/\&$//;
 7193:     if ($critical) {
 7194: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7195:     } else {
 7196: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7197:     }
 7198: }
 7199: 
 7200: # --------------------------------------------------------------- put interface
 7201: 
 7202: sub put {
 7203:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7204:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7205:    if (!$uname) { $uname=$env{'user.name'}; }
 7206:    my $uhome=&homeserver($uname,$udomain);
 7207:    my $items='';
 7208:    foreach my $item (keys(%$storehash)) {
 7209:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7210:    }
 7211:    $items=~s/\&$//;
 7212:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7213: }
 7214: 
 7215: # ------------------------------------------------------------ newput interface
 7216: 
 7217: sub newput {
 7218:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7219:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7220:    if (!$uname) { $uname=$env{'user.name'}; }
 7221:    my $uhome=&homeserver($uname,$udomain);
 7222:    my $items='';
 7223:    foreach my $key (keys(%$storehash)) {
 7224:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7225:    }
 7226:    $items=~s/\&$//;
 7227:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7228: }
 7229: 
 7230: # ---------------------------------------------------------  putstore interface
 7231: 
 7232: sub putstore {
 7233:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7234:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7235:    if (!$uname) { $uname=$env{'user.name'}; }
 7236:    my $uhome=&homeserver($uname,$udomain);
 7237:    my $items='';
 7238:    foreach my $key (keys(%$storehash)) {
 7239:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7240:    }
 7241:    $items=~s/\&$//;
 7242:    my $esc_symb=&escape($symb);
 7243:    my $esc_v=&escape($version);
 7244:    my $reply =
 7245:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7246: 	      $uhome);
 7247:    if (($tolog) && ($reply eq 'ok')) {
 7248:        my $namevalue='';
 7249:        foreach my $key (keys(%{$storehash})) {
 7250:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7251:        }
 7252:        $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
 7253:                      '&host='.&escape($perlvar{'lonHostID'}).
 7254:                      '&version='.$esc_v.
 7255:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7256:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7257:    }
 7258:    if ($reply eq 'unknown_cmd') {
 7259:        # gfall back to way things use to be done
 7260:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7261: 			    $uname);
 7262:    }
 7263:    return $reply;
 7264: }
 7265: 
 7266: sub old_putstore {
 7267:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7268:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7269:     if (!$uname) { $uname=$env{'user.name'}; }
 7270:     my $uhome=&homeserver($uname,$udomain);
 7271:     my %newstorehash;
 7272:     foreach my $item (keys(%$storehash)) {
 7273: 	my $key = $version.':'.&escape($symb).':'.$item;
 7274: 	$newstorehash{$key} = $storehash->{$item};
 7275:     }
 7276:     my $items='';
 7277:     my %allitems = ();
 7278:     foreach my $item (keys(%newstorehash)) {
 7279: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7280: 	    my $key = $1.':keys:'.$2;
 7281: 	    $allitems{$key} .= $3.':';
 7282: 	}
 7283: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7284:     }
 7285:     foreach my $item (keys(%allitems)) {
 7286: 	$allitems{$item} =~ s/\:$//;
 7287: 	$items.= $item.'='.$allitems{$item}.'&';
 7288:     }
 7289:     $items=~s/\&$//;
 7290:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7291: }
 7292: 
 7293: # ------------------------------------------------------ critical put interface
 7294: 
 7295: sub cput {
 7296:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7297:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7298:    if (!$uname) { $uname=$env{'user.name'}; }
 7299:    my $uhome=&homeserver($uname,$udomain);
 7300:    my $items='';
 7301:    foreach my $item (keys(%$storehash)) {
 7302:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7303:    }
 7304:    $items=~s/\&$//;
 7305:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7306: }
 7307: 
 7308: # -------------------------------------------------------------- eget interface
 7309: 
 7310: sub eget {
 7311:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7312:    my $items='';
 7313:    foreach my $item (@$storearr) {
 7314:        $items.=&escape($item).'&';
 7315:    }
 7316:    $items=~s/\&$//;
 7317:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7318:    if (!$uname) { $uname=$env{'user.name'}; }
 7319:    my $uhome=&homeserver($uname,$udomain);
 7320:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7321:    my @pairs=split(/\&/,$rep);
 7322:    my %returnhash=();
 7323:    my $i=0;
 7324:    foreach my $item (@$storearr) {
 7325:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7326:       $i++;
 7327:    }
 7328:    return %returnhash;
 7329: }
 7330: 
 7331: # ------------------------------------------------------------ tmpput interface
 7332: sub tmpput {
 7333:     my ($storehash,$server,$context)=@_;
 7334:     my $items='';
 7335:     foreach my $item (keys(%$storehash)) {
 7336: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7337:     }
 7338:     $items=~s/\&$//;
 7339:     if (defined($context)) {
 7340:         $items .= ':'.&escape($context);
 7341:     }
 7342:     return &reply("tmpput:$items",$server);
 7343: }
 7344: 
 7345: # ------------------------------------------------------------ tmpget interface
 7346: sub tmpget {
 7347:     my ($token,$server)=@_;
 7348:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7349:     my $rep=&reply("tmpget:$token",$server);
 7350:     my %returnhash;
 7351:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7352:         return %returnhash;
 7353:     }
 7354:     foreach my $item (split(/\&/,$rep)) {
 7355: 	my ($key,$value)=split(/=/,$item);
 7356: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7357:     }
 7358:     return %returnhash;
 7359: }
 7360: 
 7361: # ------------------------------------------------------------ tmpdel interface
 7362: sub tmpdel {
 7363:     my ($token,$server)=@_;
 7364:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7365:     return &reply("tmpdel:$token",$server);
 7366: }
 7367: 
 7368: # ------------------------------------------------------------ get_timebased_id 
 7369: 
 7370: sub get_timebased_id {
 7371:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7372:         $maxtries) = @_;
 7373:     my ($newid,$error,$dellock);
 7374:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7375:         return ('','ok','invalid call to get suffix');
 7376:     }
 7377: 
 7378: # set defaults for any optional args for which values were not supplied
 7379:     if ($who eq '') {
 7380:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7381:     }
 7382:     if (!$locktries) {
 7383:         $locktries = 3;
 7384:     }
 7385:     if (!$maxtries) {
 7386:         $maxtries = 10;
 7387:     }
 7388:     
 7389:     if (($cdom eq '') || ($cnum eq '')) {
 7390:         if ($env{'request.course.id'}) {
 7391:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7392:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7393:         }
 7394:         if (($cdom eq '') || ($cnum eq '')) {
 7395:             return ('','ok','call to get suffix not in course context');
 7396:         }
 7397:     }
 7398: 
 7399: # construct locking item
 7400:     my $lockhash = {
 7401:                       $prefix."\0".'locked_'.$keyid => $who,
 7402:                    };
 7403:     my $tries = 0;
 7404: 
 7405: # attempt to get lock on nohist_$namespace file
 7406:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7407:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7408:         $tries ++;
 7409:         sleep 1;
 7410:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7411:     }
 7412: 
 7413: # attempt to get unique identifier, based on current timestamp
 7414:     if ($gotlock eq 'ok') {
 7415:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7416:         my $id = time;
 7417:         $newid = $id;
 7418:         if ($idtype eq 'addcode') {
 7419:             $newid .= &sixnum_code();
 7420:         }
 7421:         my $idtries = 0;
 7422:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7423:             if ($idtype eq 'concat') {
 7424:                 $newid = $id.$idtries;
 7425:             } elsif ($idtype eq 'addcode') {
 7426:                 $newid = $newid.&sixnum_code();
 7427:             } else {
 7428:                 $newid ++;
 7429:             }
 7430:             $idtries ++;
 7431:         }
 7432:         if (!exists($inuse{$prefix."\0".$newid})) {
 7433:             my %new_item =  (
 7434:                               $prefix."\0".$newid => $who,
 7435:                             );
 7436:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7437:                                                  $cdom,$cnum);
 7438:             if ($putresult ne 'ok') {
 7439:                 undef($newid);
 7440:                 $error = 'error saving new item: '.$putresult;
 7441:             }
 7442:         } else {
 7443:              undef($newid);
 7444:              $error = ('error: no unique suffix available for the new item ');
 7445:         }
 7446: #  remove lock
 7447:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7448:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7449:     } else {
 7450:         $error = "error: could not obtain lockfile\n";
 7451:         $dellock = 'ok';
 7452:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7453:             $dellock = 'nolock';
 7454:         }
 7455:     }
 7456:     return ($newid,$dellock,$error);
 7457: }
 7458: 
 7459: sub sixnum_code {
 7460:     my $code;
 7461:     for (0..6) {
 7462:         $code .= int( rand(9) );
 7463:     }
 7464:     return $code;
 7465: }
 7466: 
 7467: # -------------------------------------------------- portfolio access checking
 7468: 
 7469: sub portfolio_access {
 7470:     my ($requrl,$clientip) = @_;
 7471:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7472:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7473:     if ($result) {
 7474:         my %setters;
 7475:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7476:             my ($startblock,$endblock) =
 7477:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 7478:             if ($startblock && $endblock) {
 7479:                 return 'B';
 7480:             }
 7481:         } else {
 7482:             my ($startblock,$endblock) =
 7483:                 &Apache::loncommon::blockcheck(\%setters,'port');
 7484:             if ($startblock && $endblock) {
 7485:                 return 'B';
 7486:             }
 7487:         }
 7488:     }
 7489:     if ($result eq 'ok') {
 7490:        return 'F';
 7491:     } elsif ($result =~ /^[^:]+:guest_/) {
 7492:        return 'A';
 7493:     }
 7494:     return '';
 7495: }
 7496: 
 7497: sub get_portfolio_access {
 7498:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7499: 
 7500:     if (!ref($access_hash)) {
 7501: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7502: 	my %access_controls = &get_access_controls($current_perms,$group,
 7503: 						   $file_name);
 7504: 	$access_hash = $access_controls{$file_name};
 7505:     }
 7506: 
 7507:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7508:     my $now = time;
 7509:     if (ref($access_hash) eq 'HASH') {
 7510:         foreach my $key (keys(%{$access_hash})) {
 7511:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7512:             if ($start > $now) {
 7513:                 next;
 7514:             }
 7515:             if ($end && $end<$now) {
 7516:                 next;
 7517:             }
 7518:             if ($scope eq 'public') {
 7519:                 $public = $key;
 7520:                 last;
 7521:             } elsif ($scope eq 'guest') {
 7522:                 $guest = $key;
 7523:             } elsif ($scope eq 'domains') {
 7524:                 push(@domains,$key);
 7525:             } elsif ($scope eq 'users') {
 7526:                 push(@users,$key);
 7527:             } elsif ($scope eq 'course') {
 7528:                 push(@courses,$key);
 7529:             } elsif ($scope eq 'group') {
 7530:                 push(@groups,$key);
 7531:             } elsif ($scope eq 'ip') {
 7532:                 push(@ips,$key);
 7533:             }
 7534:         }
 7535:         if ($public) {
 7536:             return 'ok';
 7537:         } elsif (@ips > 0) {
 7538:             my $allowed;
 7539:             foreach my $ipkey (@ips) {
 7540:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7541:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7542:                         $allowed = 1;
 7543:                         last; 
 7544:                     }
 7545:                 }
 7546:             }
 7547:             if ($allowed) {
 7548:                 return 'ok';
 7549:             }
 7550:         }
 7551:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7552:             if ($guest) {
 7553:                 return $guest;
 7554:             }
 7555:         } else {
 7556:             if (@domains > 0) {
 7557:                 foreach my $domkey (@domains) {
 7558:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7559:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7560:                             return 'ok';
 7561:                         }
 7562:                     }
 7563:                 }
 7564:             }
 7565:             if (@users > 0) {
 7566:                 foreach my $userkey (@users) {
 7567:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7568:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7569:                             if (ref($item) eq 'HASH') {
 7570:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7571:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7572:                                     return 'ok';
 7573:                                 }
 7574:                             }
 7575:                         }
 7576:                     } 
 7577:                 }
 7578:             }
 7579:             my %roleshash;
 7580:             my @courses_and_groups = @courses;
 7581:             push(@courses_and_groups,@groups); 
 7582:             if (@courses_and_groups > 0) {
 7583:                 my (%allgroups,%allroles); 
 7584:                 my ($start,$end,$role,$sec,$group);
 7585:                 foreach my $envkey (%env) {
 7586:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7587:                         my $cid = $2.'_'.$3; 
 7588:                         if ($1 eq 'gr') {
 7589:                             $group = $4;
 7590:                             $allgroups{$cid}{$group} = $env{$envkey};
 7591:                         } else {
 7592:                             if ($4 eq '') {
 7593:                                 $sec = 'none';
 7594:                             } else {
 7595:                                 $sec = $4;
 7596:                             }
 7597:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7598:                         }
 7599:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7600:                         my $cid = $2.'_'.$3;
 7601:                         if ($4 eq '') {
 7602:                             $sec = 'none';
 7603:                         } else {
 7604:                             $sec = $4;
 7605:                         }
 7606:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7607:                     }
 7608:                 }
 7609:                 if (keys(%allroles) == 0) {
 7610:                     return;
 7611:                 }
 7612:                 foreach my $key (@courses_and_groups) {
 7613:                     my %content = %{$$access_hash{$key}};
 7614:                     my $cnum = $content{'number'};
 7615:                     my $cdom = $content{'domain'};
 7616:                     my $cid = $cdom.'_'.$cnum;
 7617:                     if (!exists($allroles{$cid})) {
 7618:                         next;
 7619:                     }    
 7620:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7621:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7622:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7623:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7624:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7625:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7626:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7627:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7628:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7629:                                         if (grep/^all$/,@sections) {
 7630:                                             return 'ok';
 7631:                                         } else {
 7632:                                             if (grep/^$sec$/,@sections) {
 7633:                                                 return 'ok';
 7634:                                             }
 7635:                                         }
 7636:                                     }
 7637:                                 }
 7638:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7639:                                     if (grep/^none$/,@groups) {
 7640:                                         return 'ok';
 7641:                                     }
 7642:                                 } else {
 7643:                                     if (grep/^all$/,@groups) {
 7644:                                         return 'ok';
 7645:                                     } 
 7646:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7647:                                         if (grep/^$group$/,@groups) {
 7648:                                             return 'ok';
 7649:                                         }
 7650:                                     }
 7651:                                 } 
 7652:                             }
 7653:                         }
 7654:                     }
 7655:                 }
 7656:             }
 7657:             if ($guest) {
 7658:                 return $guest;
 7659:             }
 7660:         }
 7661:     }
 7662:     return;
 7663: }
 7664: 
 7665: sub course_group_datechecker {
 7666:     my ($dates,$now,$status) = @_;
 7667:     my ($start,$end) = split(/\./,$dates);
 7668:     if (!$start && !$end) {
 7669:         return 'ok';
 7670:     }
 7671:     if (grep/^active$/,@{$status}) {
 7672:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7673:             return 'ok';
 7674:         }
 7675:     }
 7676:     if (grep/^previous$/,@{$status}) {
 7677:         if ($end > $now ) {
 7678:             return 'ok';
 7679:         }
 7680:     }
 7681:     if (grep/^future$/,@{$status}) {
 7682:         if ($start > $now) {
 7683:             return 'ok';
 7684:         }
 7685:     }
 7686:     return; 
 7687: }
 7688: 
 7689: sub parse_portfolio_url {
 7690:     my ($url) = @_;
 7691: 
 7692:     my ($type,$udom,$unum,$group,$file_name);
 7693:     
 7694:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7695: 	$type = 1;
 7696:         $udom = $1;
 7697:         $unum = $2;
 7698:         $file_name = $3;
 7699:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7700: 	$type = 2;
 7701:         $udom = $1;
 7702:         $unum = $2;
 7703:         $group = $3;
 7704:         $file_name = $3.'/'.$4;
 7705:     }
 7706:     if (wantarray) {
 7707: 	return ($type,$udom,$unum,$file_name,$group);
 7708:     }
 7709:     return $type;
 7710: }
 7711: 
 7712: sub is_portfolio_url {
 7713:     my ($url) = @_;
 7714:     return scalar(&parse_portfolio_url($url));
 7715: }
 7716: 
 7717: sub is_portfolio_file {
 7718:     my ($file) = @_;
 7719:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7720:         return 1;
 7721:     }
 7722:     return;
 7723: }
 7724: 
 7725: sub usertools_access {
 7726:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7727:     my ($access,%tools);
 7728:     if ($context eq '') {
 7729:         $context = 'tools';
 7730:     }
 7731:     if ($context eq 'requestcourses') {
 7732:         %tools = (
 7733:                       official   => 1,
 7734:                       unofficial => 1,
 7735:                       community  => 1,
 7736:                       textbook   => 1,
 7737:                       placement  => 1,
 7738:                       lti        => 1,
 7739:                  );
 7740:     } elsif ($context eq 'requestauthor') {
 7741:         %tools = (
 7742:                       requestauthor => 1,
 7743:                  );
 7744:     } else {
 7745:         %tools = (
 7746:                       aboutme   => 1,
 7747:                       blog      => 1,
 7748:                       webdav    => 1,
 7749:                       portfolio => 1,
 7750:                  );
 7751:     }
 7752:     return if (!defined($tools{$tool}));
 7753: 
 7754:     if (($udom eq '') || ($uname eq '')) {
 7755:         $udom = $env{'user.domain'};
 7756:         $uname = $env{'user.name'};
 7757:     }
 7758: 
 7759:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7760:         if ($action ne 'reload') {
 7761:             if ($context eq 'requestcourses') {
 7762:                 return $env{'environment.canrequest.'.$tool};
 7763:             } elsif ($context eq 'requestauthor') {
 7764:                 return $env{'environment.canrequest.author'};
 7765:             } else {
 7766:                 return $env{'environment.availabletools.'.$tool};
 7767:             }
 7768:         }
 7769:     }
 7770: 
 7771:     my ($toolstatus,$inststatus,$envkey);
 7772:     if ($context eq 'requestauthor') {
 7773:         $envkey = $context; 
 7774:     } else {
 7775:         $envkey = $context.'.'.$tool;
 7776:     }
 7777: 
 7778:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7779:          ($action ne 'reload')) {
 7780:         $toolstatus = $env{'environment.'.$envkey};
 7781:         $inststatus = $env{'environment.inststatus'};
 7782:     } else {
 7783:         if (ref($userenvref) eq 'HASH') {
 7784:             $toolstatus = $userenvref->{$envkey};
 7785:             $inststatus = $userenvref->{'inststatus'};
 7786:         } else {
 7787:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7788:             $toolstatus = $userenv{$envkey};
 7789:             $inststatus = $userenv{'inststatus'};
 7790:         }
 7791:     }
 7792: 
 7793:     if ($toolstatus ne '') {
 7794:         if ($toolstatus) {
 7795:             $access = 1;
 7796:         } else {
 7797:             $access = 0;
 7798:         }
 7799:         return $access;
 7800:     }
 7801: 
 7802:     my ($is_adv,%domdef);
 7803:     if (ref($is_advref) eq 'HASH') {
 7804:         $is_adv = $is_advref->{'is_adv'};
 7805:     } else {
 7806:         $is_adv = &is_advanced_user($udom,$uname);
 7807:     }
 7808:     if (ref($domdefref) eq 'HASH') {
 7809:         %domdef = %{$domdefref};
 7810:     } else {
 7811:         %domdef = &get_domain_defaults($udom);
 7812:     }
 7813:     if (ref($domdef{$tool}) eq 'HASH') {
 7814:         if ($is_adv) {
 7815:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7816:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7817:                     $access = 1;
 7818:                 } else {
 7819:                     $access = 0;
 7820:                 }
 7821:                 return $access;
 7822:             }
 7823:         }
 7824:         if ($inststatus ne '') {
 7825:             my ($hasaccess,$hasnoaccess);
 7826:             foreach my $affiliation (split(/:/,$inststatus)) {
 7827:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7828:                     if ($domdef{$tool}{$affiliation}) {
 7829:                         $hasaccess = 1;
 7830:                     } else {
 7831:                         $hasnoaccess = 1;
 7832:                     }
 7833:                 }
 7834:             }
 7835:             if ($hasaccess || $hasnoaccess) {
 7836:                 if ($hasaccess) {
 7837:                     $access = 1;
 7838:                 } elsif ($hasnoaccess) {
 7839:                     $access = 0; 
 7840:                 }
 7841:                 return $access;
 7842:             }
 7843:         } else {
 7844:             if ($domdef{$tool}{'default'} ne '') {
 7845:                 if ($domdef{$tool}{'default'}) {
 7846:                     $access = 1;
 7847:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7848:                     $access = 0;
 7849:                 }
 7850:                 return $access;
 7851:             }
 7852:         }
 7853:     } else {
 7854:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7855:             $access = 1;
 7856:         } else {
 7857:             $access = 0;
 7858:         }
 7859:         return $access;
 7860:     }
 7861: }
 7862: 
 7863: sub is_course_owner {
 7864:     my ($cdom,$cnum,$udom,$uname) = @_;
 7865:     if (($udom eq '') || ($uname eq '')) {
 7866:         $udom = $env{'user.domain'};
 7867:         $uname = $env{'user.name'};
 7868:     }
 7869:     unless (($udom eq '') || ($uname eq '')) {
 7870:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7871:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7872:                 return 1;
 7873:             } else {
 7874:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7875:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7876:                     return 1;
 7877:                 }
 7878:             }
 7879:         }
 7880:     }
 7881:     return;
 7882: }
 7883: 
 7884: sub is_advanced_user {
 7885:     my ($udom,$uname) = @_;
 7886:     if ($udom ne '' && $uname ne '') {
 7887:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7888:             if (wantarray) {
 7889:                 return ($env{'user.adv'},$env{'user.author'});
 7890:             } else {
 7891:                 return $env{'user.adv'};
 7892:             }
 7893:         }
 7894:     }
 7895:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7896:     my %allroles;
 7897:     my ($is_adv,$is_author);
 7898:     foreach my $role (keys(%roleshash)) {
 7899:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7900:         my $area = '/'.$tdomain.'/'.$trest;
 7901:         if ($sec ne '') {
 7902:             $area .= '/'.$sec;
 7903:         }
 7904:         if (($area ne '') && ($trole ne '')) {
 7905:             my $spec=$trole.'.'.$area;
 7906:             if ($trole =~ /^cr\//) {
 7907:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7908:             } elsif ($trole ne 'gr') {
 7909:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7910:             }
 7911:             if ($trole eq 'au') {
 7912:                 $is_author = 1;
 7913:             }
 7914:         }
 7915:     }
 7916:     foreach my $role (keys(%allroles)) {
 7917:         last if ($is_adv);
 7918:         foreach my $item (split(/:/,$allroles{$role})) {
 7919:             if ($item ne '') {
 7920:                 my ($privilege,$restrictions)=split(/&/,$item);
 7921:                 if ($privilege eq 'adv') {
 7922:                     $is_adv = 1;
 7923:                     last;
 7924:                 }
 7925:             }
 7926:         }
 7927:     }
 7928:     if (wantarray) {
 7929:         return ($is_adv,$is_author);
 7930:     }
 7931:     return $is_adv;
 7932: }
 7933: 
 7934: sub check_can_request {
 7935:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 7936:     my $canreq = 0;
 7937:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 7938:         $uname = $env{'user.name'};
 7939:         $udom = $env{'user.domain'};
 7940:     }
 7941:     my ($types,$typename) = &Apache::loncommon::course_types();
 7942:     my @options = ('approval','validate','autolimit');
 7943:     my $optregex = join('|',@options);
 7944:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 7945:         foreach my $type (@{$types}) {
 7946:             if (&usertools_access($uname,$udom,$type,undef,
 7947:                                   'requestcourses')) {
 7948:                 $canreq ++;
 7949:                 if (ref($request_domains) eq 'HASH') {
 7950:                     push(@{$request_domains->{$type}},$udom);
 7951:                 }
 7952:                 if ($dom eq $udom) {
 7953:                     $can_request->{$type} = 1;
 7954:                 }
 7955:             }
 7956:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 7957:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 7958:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 7959:                 if (@curr > 0) {
 7960:                     foreach my $item (@curr) {
 7961:                         if (ref($request_domains) eq 'HASH') {
 7962:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 7963:                             if ($otherdom ne '') {
 7964:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 7965:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 7966:                                         push(@{$request_domains->{$type}},$otherdom);
 7967:                                     }
 7968:                                 } else {
 7969:                                     push(@{$request_domains->{$type}},$otherdom);
 7970:                                 }
 7971:                             }
 7972:                         }
 7973:                     }
 7974:                     unless ($dom eq $env{'user.domain'}) {
 7975:                         $canreq ++;
 7976:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 7977:                             $can_request->{$type} = 1;
 7978:                         }
 7979:                     }
 7980:                 }
 7981:             }
 7982:         }
 7983:     }
 7984:     return $canreq;
 7985: }
 7986: 
 7987: # ---------------------------------------------- Custom access rule evaluation
 7988: 
 7989: sub customaccess {
 7990:     my ($priv,$uri)=@_;
 7991:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 7992:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 7993:     $udom = &LONCAPA::clean_domain($udom);
 7994:     $ucrs = &LONCAPA::clean_username($ucrs);
 7995:     my $access=0;
 7996:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 7997: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 7998: 	if ($type eq 'user') {
 7999: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8000: 		my ($tdom,$tuname)=split(m{/},$scope);
 8001: 		if ($tdom) {
 8002: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8003: 		}
 8004: 		if ($tuname) {
 8005: 		    if ($tuname ne $env{'user.name'}) { next; }
 8006: 		}
 8007: 		$access=($effect eq 'allow');
 8008: 		last;
 8009: 	    }
 8010: 	} else {
 8011: 	    if ($role) {
 8012: 		if ($role ne $urole) { next; }
 8013: 	    }
 8014: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8015: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8016: 		if ($tdom) {
 8017: 		    if ($tdom ne $udom) { next; }
 8018: 		}
 8019: 		if ($tcrs) {
 8020: 		    if ($tcrs ne $ucrs) { next; }
 8021: 		}
 8022: 		if ($tsec) {
 8023: 		    if ($tsec ne $usec) { next; }
 8024: 		}
 8025: 		$access=($effect eq 'allow');
 8026: 		last;
 8027: 	    }
 8028: 	    if ($realm eq '' && $role eq '') {
 8029: 		$access=($effect eq 'allow');
 8030: 	    }
 8031: 	}
 8032:     }
 8033:     return $access;
 8034: }
 8035: 
 8036: # ------------------------------------------------- Check for a user privilege
 8037: 
 8038: sub allowed {
 8039:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache)=@_;
 8040:     my $ver_orguri=$uri;
 8041:     $uri=&deversion($uri);
 8042:     my $orguri=$uri;
 8043:     $uri=&declutter($uri);
 8044: 
 8045:     if ($priv eq 'evb') {
 8046: # Evade communication block restrictions for specified role in a course
 8047:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8048:             return $1;
 8049:         } else {
 8050:             return;
 8051:         }
 8052:     }
 8053: 
 8054:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8055: # Free bre access to adm and meta resources
 8056:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$})) 
 8057: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8058: 	&& ($priv eq 'bre')) {
 8059: 	return 'F';
 8060:     }
 8061: 
 8062: # Free bre access to user's own portfolio contents
 8063:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8064:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8065: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8066:         my %setters;
 8067:         my ($startblock,$endblock) = 
 8068:             &Apache::loncommon::blockcheck(\%setters,'port');
 8069:         if ($startblock && $endblock) {
 8070:             return 'B';
 8071:         } else {
 8072:             return 'F';
 8073:         }
 8074:     }
 8075: 
 8076: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8077:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8078:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8079:         if (exists($env{'request.course.id'})) {
 8080:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8081:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8082:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8083:                 my $courseprivid=$env{'request.course.id'};
 8084:                 $courseprivid=~s/\_/\//;
 8085:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8086:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8087:                     return $1; 
 8088:                 } else {
 8089:                     if ($env{'request.course.sec'}) {
 8090:                         $courseprivid.='/'.$env{'request.course.sec'};
 8091:                     }
 8092:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8093:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8094:                         return $2;
 8095:                     }
 8096:                 }
 8097:             }
 8098:         }
 8099:     }
 8100: 
 8101: # Free bre to public access
 8102: 
 8103:     if ($priv eq 'bre') {
 8104:         my $copyright;
 8105:         unless ($uri =~ /ext\.tool/) {
 8106:             $copyright=&metadata($uri,'copyright');
 8107:         }
 8108: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8109:            return 'F'; 
 8110:         }
 8111:         if ($copyright eq 'priv') {
 8112:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8113: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8114: 		return '';
 8115:             }
 8116:         }
 8117:         if ($copyright eq 'domain') {
 8118:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8119: 	    unless (($env{'user.domain'} eq $1) ||
 8120:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8121: 		return '';
 8122:             }
 8123:         }
 8124:         if ($env{'request.role'}=~ /li\.\//) {
 8125:             # Library role, so allow browsing of resources in this domain.
 8126:             return 'F';
 8127:         }
 8128:         if ($copyright eq 'custom') {
 8129: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8130:         }
 8131:     }
 8132:     # Domain coordinator is trying to create a course
 8133:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8134:         # uri is the requested domain in this case.
 8135:         # comparison to 'request.role.domain' shows if the user has selected
 8136:         # a role of dc for the domain in question.
 8137:         return 'F' if ($uri eq $env{'request.role.domain'});
 8138:     }
 8139: 
 8140:     my $thisallowed='';
 8141:     my $statecond=0;
 8142:     my $courseprivid='';
 8143: 
 8144:     my $ownaccess;
 8145:     # Community Coordinator or Assistant Co-author browsing resource space.
 8146:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8147:         if ($uri eq '') {
 8148:             $ownaccess = 1;
 8149:         } else {
 8150:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8151:                 my $udom = $env{'user.domain'};
 8152:                 my $uname = $env{'user.name'};
 8153:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8154:                     $ownaccess = 1;
 8155:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8156:                     unless ($uri =~ m{\.\./}) {
 8157:                         $ownaccess = 1;
 8158:                     }
 8159:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8160:                     my $now = time;
 8161:                     if ($uri =~ m{^([^/]+)/?$}) {
 8162:                         my $adom = $1;
 8163:                         foreach my $key (keys(%env)) {
 8164:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8165:                                 my ($start,$end) = split('.',$env{$key});
 8166:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8167:                                     $ownaccess = 1;
 8168:                                     last;
 8169:                                 }
 8170:                             }
 8171:                         }
 8172:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8173:                         my $adom = $1;
 8174:                         my $aname = $2;
 8175:                         foreach my $role ('ca','aa') { 
 8176:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8177:                                 my ($start,$end) =
 8178:                                     split('.',$env{"user.role.$role./$adom/$aname"});
 8179:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8180:                                     $ownaccess = 1;
 8181:                                     last;
 8182:                                 }
 8183:                             }
 8184:                         }
 8185:                     }
 8186:                 }
 8187:             }
 8188:         }
 8189:     }
 8190: 
 8191: # Course
 8192: 
 8193:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8194:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8195:             $thisallowed.=$1;
 8196:         }
 8197:     }
 8198: 
 8199: # Domain
 8200: 
 8201:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8202:        =~/\Q$priv\E\&([^\:]*)/) {
 8203:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8204:             $thisallowed.=$1;
 8205:         }
 8206:     }
 8207: 
 8208: # User who is not author or co-author might still be able to edit
 8209: # resource of an author in the domain (e.g., if Domain Coordinator).
 8210:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8211:         (&allowed('mdc',$env{'request.course.id'}))) {
 8212:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8213:             $thisallowed.=$1;
 8214:         }
 8215:     }
 8216: 
 8217: # Course: uri itself is a course
 8218:     my $courseuri=$uri;
 8219:     $courseuri=~s/\_(\d)/\/$1/;
 8220:     $courseuri=~s/^([^\/])/\/$1/;
 8221: 
 8222:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8223:        =~/\Q$priv\E\&([^\:]*)/) {
 8224:         if ($priv eq 'mip') {
 8225:             my $rem = $1;
 8226:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8227:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8228:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8229:                 if ($cdom ne '') {
 8230:                     my %passwdconf = &get_passwdconf($cdom);
 8231:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8232:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8233:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8234:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8235:                                 unless (@inststatuses) {
 8236:                                     @inststatuses = ('default');
 8237:                                 }
 8238:                                 foreach my $status (@inststatuses) {
 8239:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8240:                                         $thisallowed.=$rem;
 8241:                                     }
 8242:                                 }
 8243:                             }
 8244:                         }
 8245:                     }
 8246:                 }
 8247:             }
 8248:         } else {
 8249:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8250:                 $thisallowed.=$1;
 8251:             }
 8252:         }
 8253:     }
 8254: 
 8255: # URI is an uploaded document for this course, default permissions don't matter
 8256: # not allowing 'edit' access (editupload) to uploaded course docs
 8257:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8258: 	$thisallowed='';
 8259:         my ($match)=&is_on_map($uri);
 8260:         if ($match) {
 8261:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8262:                   =~/\Q$priv\E\&([^\:]*)/) {
 8263:                 my $value = $1;
 8264:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8265:                 if ($deeplinkblock) {
 8266:                     $thisallowed='D';
 8267:                 } elsif ($noblockcheck) {
 8268:                     $thisallowed.=$value;
 8269:                 } else {
 8270:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8271:                     if (@blockers > 0) {
 8272:                         $thisallowed = 'B';
 8273:                     } else {
 8274:                         $thisallowed.=$value;
 8275:                     }
 8276:                 }
 8277:             }
 8278:         } else {
 8279:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8280:             if ($refuri) {
 8281:                 if ($refuri =~ m|^/adm/|) {
 8282:                     $thisallowed='F';
 8283:                 } else {
 8284:                     $refuri=&declutter($refuri);
 8285:                     my ($match) = &is_on_map($refuri);
 8286:                     if ($match) {
 8287:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8288:                         if ($deeplinkblock) {
 8289:                             $thisallowed='D';
 8290:                         } elsif ($noblockcheck) {
 8291:                             $thisallowed='F';
 8292:                         } else {
 8293:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8294:                             if (@blockers > 0) {
 8295:                                 $thisallowed = 'B';
 8296:                             } else {
 8297:                                 $thisallowed='F';
 8298:                             }
 8299:                         }
 8300:                     }
 8301:                 }
 8302:             }
 8303:         }
 8304:     }
 8305: 
 8306:     if ($priv eq 'bre'
 8307: 	&& $thisallowed ne 'F' 
 8308: 	&& $thisallowed ne '2'
 8309: 	&& &is_portfolio_url($uri)) {
 8310: 	$thisallowed = &portfolio_access($uri,$clientip);
 8311:     }
 8312: 
 8313: # Full access at system, domain or course-wide level? Exit.
 8314:     if ($thisallowed=~/F/) {
 8315: 	return 'F';
 8316:     }
 8317: 
 8318: # If this is generating or modifying users, exit with special codes
 8319: 
 8320:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8321: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8322: 	    my ($audom,$auname)=split('/',$uri);
 8323: # no author name given, so this just checks on the general right to make a co-author in this domain
 8324: 	    unless ($auname) { return $thisallowed; }
 8325: # an author name is given, so we are about to actually make a co-author for a certain account
 8326: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8327: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8328: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8329: 	}
 8330: 	return $thisallowed;
 8331:     }
 8332: #
 8333: # Gathered so far: system, domain and course wide privileges
 8334: #
 8335: # Course: See if uri or referer is an individual resource that is part of 
 8336: # the course
 8337: 
 8338:     if ($env{'request.course.id'}) {
 8339: 
 8340: # If this is modifying password (internal auth) domains must match for user and user's role.
 8341: 
 8342:         if ($priv eq 'mip') {
 8343:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8344:                 return $thisallowed;
 8345:             } else {
 8346:                 return '';
 8347:             }
 8348:         }
 8349: 
 8350:        $courseprivid=$env{'request.course.id'};
 8351:        if ($env{'request.course.sec'}) {
 8352:           $courseprivid.='/'.$env{'request.course.sec'};
 8353:        }
 8354:        $courseprivid=~s/\_/\//;
 8355:        my $checkreferer=1;
 8356:        my ($match,$cond)=&is_on_map($uri);
 8357:        if ($match) {
 8358:            $statecond=$cond;
 8359:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8360:                =~/\Q$priv\E\&([^\:]*)/) {
 8361:                my $value = $1;
 8362:                if ($priv eq 'bre') {
 8363:                    if ($noblockcheck) {
 8364:                        $thisallowed.=$value;
 8365:                    } else {
 8366:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8367:                        if (@blockers > 0) {
 8368:                            $thisallowed = 'B';
 8369:                        } else {
 8370:                            $thisallowed.=$value;
 8371:                        }
 8372:                    }
 8373:                } else {
 8374:                    $thisallowed.=$value;
 8375:                }
 8376:                $checkreferer=0;
 8377:            }
 8378:        }
 8379: 
 8380:        if ($checkreferer) {
 8381: 	  my $refuri=$env{'httpref.'.$orguri};
 8382:             unless ($refuri) {
 8383:                 foreach my $key (keys(%env)) {
 8384: 		    if ($key=~/^httpref\..*\*/) {
 8385: 			my $pattern=$key;
 8386:                         $pattern=~s/^httpref\.\/res\///;
 8387:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8388:                         $pattern=~s/\//\\\//g;
 8389:                         if ($orguri=~/$pattern/) {
 8390: 			    $refuri=$env{$key};
 8391:                         }
 8392:                     }
 8393:                 }
 8394:             }
 8395: 
 8396:          if ($refuri) { 
 8397: 	  $refuri=&declutter($refuri);
 8398:           my ($match,$cond)=&is_on_map($refuri);
 8399:             if ($match) {
 8400:               my $refstatecond=$cond;
 8401:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8402:                   =~/\Q$priv\E\&([^\:]*)/) {
 8403:                   my $value = $1;
 8404:                   if ($priv eq 'bre') {
 8405:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8406:                       if ($deeplinkblock) {
 8407:                           $thisallowed = 'D';
 8408:                       } elsif ($noblockcheck) {
 8409:                           $thisallowed.=$value;
 8410:                       } else {
 8411:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8412:                           if (@blockers > 0) {
 8413:                               $thisallowed = 'B';
 8414:                           } else {
 8415:                               $thisallowed.=$value;
 8416:                           }
 8417:                       }
 8418:                   } else {
 8419:                       $thisallowed.=$value;
 8420:                   }
 8421:                   $uri=$refuri;
 8422:                   $statecond=$refstatecond;
 8423:               }
 8424:           }
 8425:         }
 8426:        }
 8427:    }
 8428: 
 8429: #
 8430: # Gathered now: all privileges that could apply, and condition number
 8431: # 
 8432: #
 8433: # Full or no access?
 8434: #
 8435: 
 8436:     if ($thisallowed=~/F/) {
 8437: 	return 'F';
 8438:     }
 8439: 
 8440:     unless ($thisallowed) {
 8441:         return '';
 8442:     }
 8443: 
 8444: # Restrictions exist, deal with them
 8445: #
 8446: #   C:according to course preferences
 8447: #   R:according to resource settings
 8448: #   L:unless locked
 8449: #   X:according to user session state
 8450: #
 8451: 
 8452: # Possibly locked functionality, check all courses
 8453: # Locks might take effect only after 10 minutes cache expiration for other
 8454: # courses, and 2 minutes for current course
 8455: 
 8456:     my $envkey;
 8457:     if ($thisallowed=~/L/) {
 8458:         foreach $envkey (keys(%env)) {
 8459:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8460:                my $courseid=$2;
 8461:                my $roleid=$1.'.'.$2;
 8462:                $courseid=~s/^\///;
 8463:                my $expiretime=600;
 8464:                if ($env{'request.role'} eq $roleid) {
 8465: 		  $expiretime=120;
 8466:                }
 8467: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8468:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8469:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8470: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8471:                }
 8472:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8473:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8474: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8475:                        &log($env{'user.domain'},$env{'user.name'},
 8476:                             $env{'user.home'},
 8477:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8478:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8479:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8480: 		       return '';
 8481:                    }
 8482:                }
 8483:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8484:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8485: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 8486:                        &log($env{'user.domain'},$env{'user.name'},
 8487:                             $env{'user.home'},
 8488:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8489:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8490:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8491: 		       return '';
 8492:                    }
 8493:                }
 8494: 	   }
 8495:        }
 8496:     }
 8497: 
 8498: #
 8499: # Rest of the restrictions depend on selected course
 8500: #
 8501: 
 8502:     unless ($env{'request.course.id'}) {
 8503: 	if ($thisallowed eq 'A') {
 8504: 	    return 'A';
 8505:         } elsif ($thisallowed eq 'B') {
 8506:             return 'B';
 8507: 	} else {
 8508: 	    return '1';
 8509: 	}
 8510:     }
 8511: 
 8512: #
 8513: # Now user is definitely in a course
 8514: #
 8515: 
 8516: 
 8517: # Course preferences
 8518: 
 8519:    if ($thisallowed=~/C/) {
 8520:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8521:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8522:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8523: 	   =~/\Q$rolecode\E/) {
 8524: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8525: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8526: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8527: 			$env{'request.course.id'});
 8528: 	   }
 8529:            return '';
 8530:        }
 8531: 
 8532:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8533: 	   =~/\Q$unamedom\E/) {
 8534: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8535: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8536: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8537: 			$env{'request.course.id'});
 8538: 	   }
 8539:            return '';
 8540:        }
 8541:    }
 8542: 
 8543: # Resource preferences
 8544: 
 8545:    if ($thisallowed=~/R/) {
 8546:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8547:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8548: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8549: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8550: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8551: 	   }
 8552: 	   return '';
 8553:        }
 8554:    }
 8555: 
 8556: # Restricted by state or randomout?
 8557: 
 8558:    if ($thisallowed=~/X/) {
 8559:       if ($env{'acc.randomout'}) {
 8560: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8561:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8562:             return ''; 
 8563:          }
 8564:       }
 8565:       if (&condval($statecond)) {
 8566: 	 return '2';
 8567:       } else {
 8568:          return '';
 8569:       }
 8570:    }
 8571: 
 8572:     if ($thisallowed eq 'A') {
 8573: 	return 'A';
 8574:     } elsif ($thisallowed eq 'B') {
 8575:         return 'B';
 8576:     } elsif ($thisallowed eq 'D') {
 8577:         return 'D';
 8578:     }
 8579:    return 'F';
 8580: }
 8581: 
 8582: # ------------------------------------------- Check construction space access
 8583: 
 8584: sub constructaccess {
 8585:     my ($url,$setpriv)=@_;
 8586: 
 8587: # We do not allow editing of previous versions of files
 8588:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8589: 
 8590: # Get username and domain from URL
 8591:     my ($ownername,$ownerdomain,$ownerhome);
 8592: 
 8593:     ($ownerdomain,$ownername) =
 8594:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8595: 
 8596: # The URL does not really point to any authorspace, forget it
 8597:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8598: 
 8599: # Now we need to see if the user has access to the authorspace of
 8600: # $ownername at $ownerdomain
 8601: 
 8602:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8603: # Real author for this?
 8604:        $ownerhome = $env{'user.home'};
 8605:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8606:           return ($ownername,$ownerdomain,$ownerhome);
 8607:        }
 8608:     } else {
 8609: # Co-author for this?
 8610:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8611:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8612:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8613:             return ($ownername,$ownerdomain,$ownerhome);
 8614:         }
 8615:         if ($env{'request.course.id'}) {
 8616:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8617:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8618:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8619:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8620:                     return ($ownername,$ownerdomain,$ownerhome);
 8621:                 }
 8622:             }
 8623:         }
 8624:     }
 8625: 
 8626: # We don't have any access right now. If we are not possibly going to do anything about this,
 8627: # we might as well leave
 8628:    unless ($setpriv) { return ''; }
 8629: 
 8630: # Backdoor access?
 8631:     my $allowed=&allowed('eco',$ownerdomain);
 8632: # Nope
 8633:     unless ($allowed) { return ''; }
 8634: # Looks like we may have access, but could be locked by the owner of the construction space
 8635:     if ($allowed eq 'U') {
 8636:         my %blocked=&get('environment',['domcoord.author'],
 8637:                          $ownerdomain,$ownername);
 8638: # Is blocked by owner
 8639:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8640:     }
 8641:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8642: # Grant temporary access
 8643:         my $then=$env{'user.login.time'};
 8644:         my $update=$env{'user.update.time'};
 8645:         if (!$update) { $update = $then; }
 8646:         my $refresh=$env{'user.refresh.time'};
 8647:         if (!$refresh) { $refresh = $update; }
 8648:         my $now = time;
 8649:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8650:                            $now,'ca','constructaccess');
 8651:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8652:         return($ownername,$ownerdomain,$ownerhome);
 8653:     }
 8654: # No business here
 8655:     return '';
 8656: }
 8657: 
 8658: # ----------------------------------------------------------- Content Blocking
 8659: 
 8660: {
 8661: # Caches for faster Course Contents display where content blocking
 8662: # is in operation (i.e., interval param set) for timed quiz.
 8663: #
 8664: # User for whom data are being temporarily cached.
 8665: my $cacheduser='';
 8666: # Course for which data are being temporarily cached.
 8667: my $cachedcid='';
 8668: # Cached blockers for this user (a hash of blocking items). 
 8669: my %cachedblockers=();
 8670: # When the data were last cached.
 8671: my $cachedlast='';
 8672: 
 8673: sub load_all_blockers {
 8674:     my ($uname,$udom)=@_;
 8675:     if (($uname ne '') && ($udom ne '')) { 
 8676:         if (($cacheduser eq $uname.':'.$udom) &&
 8677:             ($cachedcid eq $env{'request.course.id'}) &&
 8678:             (abs($cachedlast-time)<5)) {
 8679:             return;
 8680:         }
 8681:     }
 8682:     $cachedlast=time;
 8683:     $cacheduser=$uname.':'.$udom;
 8684:     $cachedcid=$env{'request.course.id'};
 8685:     %cachedblockers = &get_commblock_resources();
 8686:     return;
 8687: }
 8688: 
 8689: sub get_comm_blocks {
 8690:     my ($cdom,$cnum) = @_;
 8691:     if ($cdom eq '' || $cnum eq '') {
 8692:         return unless ($env{'request.course.id'});
 8693:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8694:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8695:     }
 8696:     my %commblocks;
 8697:     my $hashid=$cdom.'_'.$cnum;
 8698:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8699:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8700:         %commblocks = %{$blocksref};
 8701:     } else {
 8702:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8703:         my $cachetime = 600;
 8704:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8705:     }
 8706:     return %commblocks;
 8707: }
 8708: 
 8709: sub get_commblock_resources {
 8710:     my ($blocks) = @_;
 8711:     my %blockers = ();
 8712:     return %blockers unless ($env{'request.course.id'});
 8713:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8714:     my %commblocks;
 8715:     if (ref($blocks) eq 'HASH') {
 8716:         %commblocks = %{$blocks};
 8717:     } else {
 8718:         %commblocks = &get_comm_blocks();
 8719:     }
 8720:     return %blockers unless (keys(%commblocks) > 0); 
 8721:     my $navmap = Apache::lonnavmaps::navmap->new();
 8722:     return %blockers unless (ref($navmap));
 8723:     my $now = time;
 8724:     foreach my $block (keys(%commblocks)) {
 8725:         if ($block =~ /^(\d+)____(\d+)$/) {
 8726:             my ($start,$end) = ($1,$2);
 8727:             if ($start <= $now && $end >= $now) {
 8728:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8729:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8730:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8731:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8732:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8733:                             }
 8734:                         }
 8735:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8736:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8737:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8738:                             }
 8739:                         }
 8740:                     }
 8741:                 }
 8742:             }
 8743:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8744:             my $item = $1;
 8745:             my @to_test;
 8746:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8747:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8748:                     my @interval;
 8749:                     my $type = 'map';
 8750:                     if ($item eq 'course') {
 8751:                         $type = 'course';
 8752:                         @interval=&EXT("resource.0.interval");
 8753:                     } else {
 8754:                         if ($item =~ /___\d+___/) {
 8755:                             $type = 'resource';
 8756:                             @interval=&EXT("resource.0.interval",$item);
 8757:                             if (ref($navmap)) {                        
 8758:                                 my $res = $navmap->getBySymb($item); 
 8759:                                 push(@to_test,$res);
 8760:                             }
 8761:                         } else {
 8762:                             my $mapsymb = &symbread($item,1);
 8763:                             if ($mapsymb) {
 8764:                                 if (ref($navmap)) {
 8765:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8766:                                     @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
 8767:                                     foreach my $res (@to_test) {
 8768:                                         my $symb = $res->symb();
 8769:                                         next if ($symb eq $mapsymb);
 8770:                                         if ($symb ne '') {
 8771:                                             @interval=&EXT("resource.0.interval",$symb);
 8772:                                             if ($interval[1] eq 'map') {
 8773:                                                 last;
 8774:                                             }
 8775:                                         }
 8776:                                     }
 8777:                                 }
 8778:                             }
 8779:                         }
 8780:                     }
 8781:                     if ($interval[0] =~ /^(\d+)/) {
 8782:                         my $timelimit = $1; 
 8783:                         my $first_access;
 8784:                         if ($type eq 'resource') {
 8785:                             $first_access=&get_first_access($interval[1],$item);
 8786:                         } elsif ($type eq 'map') {
 8787:                             $first_access=&get_first_access($interval[1],undef,$item);
 8788:                         } else {
 8789:                             $first_access=&get_first_access($interval[1]);
 8790:                         }
 8791:                         if ($first_access) {
 8792:                             my $timesup = $first_access+$timelimit;
 8793:                             if ($timesup > $now) {
 8794:                                 my $activeblock;
 8795:                                 foreach my $res (@to_test) {
 8796:                                     if ($res->answerable()) {
 8797:                                         $activeblock = 1;
 8798:                                         last;
 8799:                                     }
 8800:                                 }
 8801:                                 if ($activeblock) {
 8802:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8803:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8804:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8805:                                          }
 8806:                                     }
 8807:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8808:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8809:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8810:                                         }
 8811:                                     }
 8812:                                 }
 8813:                             }
 8814:                         }
 8815:                     }
 8816:                 }
 8817:             }
 8818:         }
 8819:     }
 8820:     return %blockers;
 8821: }
 8822: 
 8823: sub has_comm_blocking {
 8824:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 8825:     my @blockers;
 8826:     return unless ($env{'request.course.id'});
 8827:     return unless ($priv eq 'bre');
 8828:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8829:     return if ($env{'request.state'} eq 'construct');
 8830:     my %blockinfo;
 8831:     if (ref($blocks) eq 'HASH') {
 8832:         %blockinfo = &get_commblock_resources($blocks);
 8833:     } else {
 8834:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 8835:         %blockinfo = %cachedblockers;
 8836:     }
 8837:     return unless (keys(%blockinfo) > 0);
 8838:     my (%possibles,@symbs);
 8839:     if (!$symb) {
 8840:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 8841:     }
 8842:     if ($symb) {
 8843:         @symbs = ($symb);
 8844:     } elsif (keys(%possibles)) { 
 8845:         @symbs = keys(%possibles);
 8846:     }
 8847:     my $noblock;
 8848:     foreach my $symb (@symbs) {
 8849:         last if ($noblock);
 8850:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8851:         foreach my $block (keys(%blockinfo)) {
 8852:             if ($block =~ /^firstaccess____(.+)$/) {
 8853:                 my $item = $1;
 8854:                 unless ($blocked) {
 8855:                     if (($item eq $map) || ($item eq $symb)) {
 8856:                         $noblock = 1;
 8857:                         last;
 8858:                     }
 8859:                 }
 8860:             }
 8861:             if (ref($blockinfo{$block}) eq 'HASH') {
 8862:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 8863:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 8864:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8865:                             push(@blockers,$block);
 8866:                         }
 8867:                     }
 8868:                 }
 8869:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 8870:                     if ($blockinfo{$block}{'maps'}{$map}) {
 8871:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8872:                             push(@blockers,$block);
 8873:                         }
 8874:                     }
 8875:                 }
 8876:             }
 8877:         }
 8878:     }
 8879:     unless ($noblock) { 
 8880:         return @blockers;
 8881:     }
 8882:     return;
 8883: }
 8884: }
 8885: 
 8886: sub deeplink_check {
 8887:     my ($priv,$symb,$uri) = @_;
 8888:     return unless ($env{'request.course.id'});
 8889:     return unless ($priv eq 'bre');
 8890:     return if ($env{'request.state'} eq 'construct');
 8891:     return if ($env{'request.role.adv'});
 8892:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8893:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8894:     my (%possibles,@symbs);
 8895:     if (!$symb) {
 8896:         $symb = &symbread($uri,1,1,1,\%possibles);
 8897:     }
 8898:     if ($symb) {
 8899:         @symbs = ($symb);
 8900:     } elsif (keys(%possibles)) {
 8901:         @symbs = keys(%possibles);
 8902:     }
 8903: 
 8904:     my ($login,$switchrole,$allow);
 8905:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
 8906:         my $key = $1;
 8907:         my $tinyurl;
 8908:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
 8909:         if (defined($cached)) {
 8910:              $tinyurl = $result;
 8911:         } else {
 8912:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
 8913:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
 8914:              if ($currtiny{$key} ne '') {
 8915:                  $tinyurl = $currtiny{$key};
 8916:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
 8917:              }
 8918:         }
 8919:         if ($tinyurl ne '') {
 8920:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
 8921:             if ($cnumreq eq $cnum) {
 8922:                 $login = $posslogin;
 8923:             } else {
 8924:                 $switchrole = 1;
 8925:             }
 8926:         }
 8927:     }
 8928:     foreach my $symb (@symbs) {
 8929:         last if ($allow);
 8930:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 8931:         if ($deeplink eq '') {
 8932:             $allow = 1;
 8933:         } else {
 8934:             my ($listed,$scope,$access) = split(/,/,$deeplink);
 8935:             if ($access eq 'any') {
 8936:                 $allow = 1;
 8937:             } elsif ($login) {
 8938:                 if ($access eq 'only') {
 8939:                     if ($scope eq 'res') {
 8940:                         if ($symb eq $login) {
 8941:                             $allow = 1;
 8942:                         }
 8943:                     } elsif ($scope eq 'map') {
 8944: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
 8945:                     } elsif ($scope eq 'rec') {
 8946: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
 8947:                     }
 8948:                 } else {
 8949:                     my ($acctype,$item) = split(/:/,$access);
 8950:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
 8951:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
 8952:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 8953:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
 8954:                                 $allow = 1;
 8955:                             }
 8956:                         }
 8957:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 8958:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 8959:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 8960:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
 8961:                                 $allow = 1;
 8962:                             }
 8963:                         }
 8964:                     }
 8965:                 }
 8966:             }
 8967:         }
 8968:     }
 8969:     return if ($allow);
 8970:     return 1;
 8971: }
 8972: 
 8973: # -------------------------------- Deversion and split uri into path an filename   
 8974: 
 8975: #
 8976: #   Removes the version from a URI and
 8977: #   splits it in to its filename and path to the filename.
 8978: #   Seems like File::Basename could have done this more clearly.
 8979: #   Parameters:
 8980: #      $uri   - input URI
 8981: #   Returns:
 8982: #     Two element list consisting of 
 8983: #     $pathname  - the URI up to and excluding the trailing /
 8984: #     $filename  - The part of the URI following the last /
 8985: #  NOTE:
 8986: #    Another realization of this is simply:
 8987: #    use File::Basename;
 8988: #    ...
 8989: #    $uri = shift;
 8990: #    $filename = basename($uri);
 8991: #    $path     = dirname($uri);
 8992: #    return ($filename, $path);
 8993: #
 8994: #     The implementation below is probably faster however.
 8995: #
 8996: sub split_uri_for_cond {
 8997:     my $uri=&deversion(&declutter(shift));
 8998:     my @uriparts=split(/\//,$uri);
 8999:     my $filename=pop(@uriparts);
 9000:     my $pathname=join('/',@uriparts);
 9001:     return ($pathname,$filename);
 9002: }
 9003: # --------------------------------------------------- Is a resource on the map?
 9004: 
 9005: sub is_on_map {
 9006:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9007:     #Trying to find the conditional for the file
 9008:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9009: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9010:     if ($match) {
 9011: 	return (1,$1);
 9012:     } else {
 9013: 	return (0,0);
 9014:     }
 9015: }
 9016: 
 9017: # --------------------------------------------------------- Get symb from alias
 9018: 
 9019: sub get_symb_from_alias {
 9020:     my $symb=shift;
 9021:     my ($map,$resid,$url)=&decode_symb($symb);
 9022: # Already is a symb
 9023:     if ($url) { return $symb; }
 9024: # Must be an alias
 9025:     my $aliassymb='';
 9026:     my %bighash;
 9027:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9028:                             &GDBM_READER(),0640)) {
 9029:         my $rid=$bighash{'mapalias_'.$symb};
 9030: 	if ($rid) {
 9031: 	    my ($mapid,$resid)=split(/\./,$rid);
 9032: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9033: 				    $resid,$bighash{'src_'.$rid});
 9034: 	}
 9035:         untie %bighash;
 9036:     }
 9037:     return $aliassymb;
 9038: }
 9039: 
 9040: # ----------------------------------------------------------------- Define Role
 9041: 
 9042: sub definerole {
 9043:   if (allowed('mcr','/')) {
 9044:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9045:     foreach my $role (split(':',$sysrole)) {
 9046: 	my ($crole,$cqual)=split(/\&/,$role);
 9047:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9048:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9049: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9050:                return "refused:s:$crole&$cqual"; 
 9051:             }
 9052:         }
 9053:     }
 9054:     foreach my $role (split(':',$domrole)) {
 9055: 	my ($crole,$cqual)=split(/\&/,$role);
 9056:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9057:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9058: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9059:                return "refused:d:$crole&$cqual"; 
 9060:             }
 9061:         }
 9062:     }
 9063:     foreach my $role (split(':',$courole)) {
 9064: 	my ($crole,$cqual)=split(/\&/,$role);
 9065:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9066:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9067: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9068:                return "refused:c:$crole&$cqual"; 
 9069:             }
 9070:         }
 9071:     }
 9072:     my $uhome;
 9073:     if (($uname ne '') && ($udom ne '')) {
 9074:         $uhome = &homeserver($uname,$udom);
 9075:         return $uhome if ($uhome eq 'no_host');
 9076:     } else {
 9077:         $uname = $env{'user.name'};
 9078:         $udom = $env{'user.domain'};
 9079:         $uhome = $env{'user.home'};
 9080:     }
 9081:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9082:                 "$udom:$uname:rolesdef_$rolename=".
 9083:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9084:     return reply($command,$uhome);
 9085:   } else {
 9086:     return 'refused';
 9087:   }
 9088: }
 9089: 
 9090: # ---------------- Make a metadata query against the network of library servers
 9091: 
 9092: sub metadata_query {
 9093:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9094:     my %rhash;
 9095:     my %libserv = &all_library();
 9096:     my @server_list = (defined($server_array) ? @$server_array
 9097:                                               : keys(%libserv) );
 9098:     for my $server (@server_list) {
 9099:         my $domains = ''; 
 9100:         if (ref($domains_hash) eq 'HASH') {
 9101:             $domains = $domains_hash->{$server}; 
 9102:         }
 9103: 	unless ($custom or $customshow) {
 9104: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9105: 	    $rhash{$server}=$reply;
 9106: 	}
 9107: 	else {
 9108: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9109: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9110: 			     $server);
 9111: 	    $rhash{$server}=$reply;
 9112: 	}
 9113:     }
 9114:     return \%rhash;
 9115: }
 9116: 
 9117: # ----------------------------------------- Send log queries and wait for reply
 9118: 
 9119: sub log_query {
 9120:     my ($uname,$udom,$query,%filters)=@_;
 9121:     my $uhome=&homeserver($uname,$udom);
 9122:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9123:     my $uhost=&hostname($uhome);
 9124:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9125:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9126:                        $uhome);
 9127:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9128:     return get_query_reply($queryid);
 9129: }
 9130: 
 9131: # -------------------------- Update MySQL table for portfolio file
 9132: 
 9133: sub update_portfolio_table {
 9134:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9135:     if ($group ne '') {
 9136:         $file_name =~s /^\Q$group\E//;
 9137:     }
 9138:     my $homeserver = &homeserver($uname,$udom);
 9139:     my $queryid=
 9140:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9141:                ':'.&escape($file_name).':'.$action,$homeserver);
 9142:     my $reply = &get_query_reply($queryid);
 9143:     return $reply;
 9144: }
 9145: 
 9146: # -------------------------- Update MySQL allusers table
 9147: 
 9148: sub update_allusers_table {
 9149:     my ($uname,$udom,$names) = @_;
 9150:     my $homeserver = &homeserver($uname,$udom);
 9151:     my $queryid=
 9152:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9153:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9154:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9155:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9156:                'generation='.&escape($names->{'generation'}).'%%'.
 9157:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9158:                'id='.&escape($names->{'id'}),$homeserver);
 9159:     return;
 9160: }
 9161: 
 9162: # ------- Request retrieval of institutional classlists for course(s)
 9163: 
 9164: sub fetch_enrollment_query {
 9165:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9166:     my ($homeserver,$sleep,$loopmax);
 9167:     my $maxtries = 1;
 9168:     if ($context eq 'automated') {
 9169:         $homeserver = $perlvar{'lonHostID'};
 9170:         $sleep = 2;
 9171:         $loopmax = 100;
 9172:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9173:     } else {
 9174:         $homeserver = &homeserver($cnum,$dom);
 9175:     }
 9176:     my $host=&hostname($homeserver);
 9177:     my $cmd = '';
 9178:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9179:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9180:     }
 9181:     $cmd =~ s/%%$//;
 9182:     $cmd = &escape($cmd);
 9183:     my $query = 'fetchenrollment';
 9184:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9185:     unless ($queryid=~/^\Q$host\E\_/) { 
 9186:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9187:         return 'error: '.$queryid;
 9188:     }
 9189:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9190:     my $tries = 1;
 9191:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9192:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9193:         $tries ++;
 9194:     }
 9195:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9196:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9197:     } else {
 9198:         my @responses = split(/:/,$reply);
 9199:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9200:             foreach my $line (@responses) {
 9201:                 my ($key,$value) = split(/=/,$line,2);
 9202:                 $$replyref{$key} = $value;
 9203:             }
 9204:         } else {
 9205:             my $pathname = LONCAPA::tempdir();
 9206:             foreach my $line (@responses) {
 9207:                 my ($key,$value) = split(/=/,$line);
 9208:                 $$replyref{$key} = $value;
 9209:                 if ($value > 0) {
 9210:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9211:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9212:                         my $destname = $pathname.'/'.$filename;
 9213:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9214:                         if ($xml_classlist =~ /^error/) {
 9215:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9216:                         } else {
 9217:                             if ( open(FILE,">",$destname) ) {
 9218:                                 print FILE &unescape($xml_classlist);
 9219:                                 close(FILE);
 9220:                             } else {
 9221:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9222:                             }
 9223:                         }
 9224:                     }
 9225:                 }
 9226:             }
 9227:         }
 9228:         return 'ok';
 9229:     }
 9230:     return 'error';
 9231: }
 9232: 
 9233: sub get_query_reply {
 9234:     my ($queryid,$sleep,$loopmax) = @_;;
 9235:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9236:         $sleep = 0.2;
 9237:     }
 9238:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9239:         $loopmax = 100;
 9240:     }
 9241:     my $replyfile=LONCAPA::tempdir().$queryid;
 9242:     my $reply='';
 9243:     for (1..$loopmax) {
 9244: 	sleep($sleep);
 9245:         if (-e $replyfile.'.end') {
 9246: 	    if (open(my $fh,"<",$replyfile)) {
 9247: 		$reply = join('',<$fh>);
 9248: 		close($fh);
 9249: 	   } else { return 'error: reply_file_error'; }
 9250:            return &unescape($reply);
 9251: 	}
 9252:     }
 9253:     return 'timeout:'.$queryid;
 9254: }
 9255: 
 9256: sub courselog_query {
 9257: #
 9258: # possible filters:
 9259: # url: url or symb
 9260: # username
 9261: # domain
 9262: # action: view, submit, grade
 9263: # start: timestamp
 9264: # end: timestamp
 9265: #
 9266:     my (%filters)=@_;
 9267:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9268:     if ($filters{'url'}) {
 9269: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9270:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9271:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9272:     }
 9273:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9274:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9275:     return &log_query($cname,$cdom,'courselog',%filters);
 9276: }
 9277: 
 9278: sub userlog_query {
 9279: #
 9280: # possible filters:
 9281: # action: log check role
 9282: # start: timestamp
 9283: # end: timestamp
 9284: #
 9285:     my ($uname,$udom,%filters)=@_;
 9286:     return &log_query($uname,$udom,'userlog',%filters);
 9287: }
 9288: 
 9289: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9290: 
 9291: sub auto_run {
 9292:     my ($cnum,$cdom) = @_;
 9293:     my $response = 0;
 9294:     my $settings;
 9295:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9296:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9297:         $settings = $domconfig{'autoenroll'};
 9298:         if ($settings->{'run'} eq '1') {
 9299:             $response = 1;
 9300:         }
 9301:     } else {
 9302:         my $homeserver;
 9303:         if (&is_course($cdom,$cnum)) {
 9304:             $homeserver = &homeserver($cnum,$cdom);
 9305:         } else {
 9306:             $homeserver = &domain($cdom,'primary');
 9307:         }
 9308:         if ($homeserver ne 'no_host') {
 9309:             $response = &reply('autorun:'.$cdom,$homeserver);
 9310:         }
 9311:     }
 9312:     return $response;
 9313: }
 9314: 
 9315: sub auto_get_sections {
 9316:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9317:     my $homeserver;
 9318:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9319:         $homeserver = &homeserver($cnum,$cdom);
 9320:     }
 9321:     if (!defined($homeserver)) { 
 9322:         if ($cdom =~ /^$match_domain$/) {
 9323:             $homeserver = &domain($cdom,'primary');
 9324:         }
 9325:     }
 9326:     my @secs;
 9327:     if (defined($homeserver)) {
 9328:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9329:         unless ($response eq 'refused') {
 9330:             @secs = split(/:/,$response);
 9331:         }
 9332:     }
 9333:     return @secs;
 9334: }
 9335: 
 9336: sub auto_new_course {
 9337:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9338:     my $homeserver = &homeserver($cnum,$cdom);
 9339:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9340:     return $response;
 9341: }
 9342: 
 9343: sub auto_validate_courseID {
 9344:     my ($cnum,$cdom,$inst_course_id) = @_;
 9345:     my $homeserver = &homeserver($cnum,$cdom);
 9346:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9347:     return $response;
 9348: }
 9349: 
 9350: sub auto_validate_instcode {
 9351:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9352:     my ($homeserver,$response);
 9353:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9354:         $homeserver = &homeserver($cnum,$cdom);
 9355:     }
 9356:     if (!defined($homeserver)) {
 9357:         if ($cdom =~ /^$match_domain$/) {
 9358:             $homeserver = &domain($cdom,'primary');
 9359:         }
 9360:     }
 9361:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9362:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9363:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9364:     return ($outcome,$description,$defaultcredits);
 9365: }
 9366: 
 9367: sub auto_create_password {
 9368:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9369:     my ($homeserver,$response);
 9370:     my $create_passwd = 0;
 9371:     my $authchk = '';
 9372:     if ($udom =~ /^$match_domain$/) {
 9373:         $homeserver = &domain($udom,'primary');
 9374:     }
 9375:     if ($homeserver eq '') {
 9376:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9377:             $homeserver = &homeserver($cnum,$cdom);
 9378:         }
 9379:     }
 9380:     if ($homeserver eq '') {
 9381:         $authchk = 'nodomain';
 9382:     } else {
 9383:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9384:         if ($response eq 'refused') {
 9385:             $authchk = 'refused';
 9386:         } else {
 9387:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9388:         }
 9389:     }
 9390:     return ($authparam,$create_passwd,$authchk);
 9391: }
 9392: 
 9393: sub auto_photo_permission {
 9394:     my ($cnum,$cdom,$students) = @_;
 9395:     my $homeserver = &homeserver($cnum,$cdom);
 9396:     my ($outcome,$perm_reqd,$conditions) = 
 9397: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9398:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9399: 	return (undef,undef);
 9400:     }
 9401:     return ($outcome,$perm_reqd,$conditions);
 9402: }
 9403: 
 9404: sub auto_checkphotos {
 9405:     my ($uname,$udom,$pid) = @_;
 9406:     my $homeserver = &homeserver($uname,$udom);
 9407:     my ($result,$resulttype);
 9408:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9409: 				   &escape($uname).':'.&escape($pid),
 9410: 				   $homeserver));
 9411:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9412: 	return (undef,undef);
 9413:     }
 9414:     if ($outcome) {
 9415:         ($result,$resulttype) = split(/:/,$outcome);
 9416:     } 
 9417:     return ($result,$resulttype);
 9418: }
 9419: 
 9420: sub auto_photochoice {
 9421:     my ($cnum,$cdom) = @_;
 9422:     my $homeserver = &homeserver($cnum,$cdom);
 9423:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9424: 						       &escape($cdom),
 9425: 						       $homeserver)));
 9426:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9427: 	return (undef,undef);
 9428:     }
 9429:     return ($update,$comment);
 9430: }
 9431: 
 9432: sub auto_photoupdate {
 9433:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9434:     my $homeserver = &homeserver($cnum,$dom);
 9435:     my $host=&hostname($homeserver);
 9436:     my $cmd = '';
 9437:     my $maxtries = 1;
 9438:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9439:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9440:     }
 9441:     $cmd =~ s/%%$//;
 9442:     $cmd = &escape($cmd);
 9443:     my $query = 'institutionalphotos';
 9444:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9445:     unless ($queryid=~/^\Q$host\E\_/) {
 9446:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9447:         return 'error: '.$queryid;
 9448:     }
 9449:     my $reply = &get_query_reply($queryid);
 9450:     my $tries = 1;
 9451:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9452:         $reply = &get_query_reply($queryid);
 9453:         $tries ++;
 9454:     }
 9455:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9456:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9457:     } else {
 9458:         my @responses = split(/:/,$reply);
 9459:         my $outcome = shift(@responses); 
 9460:         foreach my $item (@responses) {
 9461:             my ($key,$value) = split(/=/,$item);
 9462:             $$photo{$key} = $value;
 9463:         }
 9464:         return $outcome;
 9465:     }
 9466:     return 'error';
 9467: }
 9468: 
 9469: sub auto_instcode_format {
 9470:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9471: 	$cat_order) = @_;
 9472:     my $courses = '';
 9473:     my @homeservers;
 9474:     if ($caller eq 'global') {
 9475: 	my %servers = &get_servers($codedom,'library');
 9476: 	foreach my $tryserver (keys(%servers)) {
 9477: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9478: 		push(@homeservers,$tryserver);
 9479: 	    }
 9480:         }
 9481:     } elsif ($caller eq 'requests') {
 9482:         if ($codedom =~ /^$match_domain$/) {
 9483:             my $chome = &domain($codedom,'primary');
 9484:             unless ($chome eq 'no_host') {
 9485:                 push(@homeservers,$chome);
 9486:             }
 9487:         }
 9488:     } else {
 9489:         push(@homeservers,&homeserver($caller,$codedom));
 9490:     }
 9491:     foreach my $code (keys(%{$instcodes})) {
 9492:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9493:     }
 9494:     chop($courses);
 9495:     my $ok_response = 0;
 9496:     my $response;
 9497:     while (@homeservers > 0 && $ok_response == 0) {
 9498:         my $server = shift(@homeservers); 
 9499:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9500:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9501:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9502: 		split(/:/,$response);
 9503:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9504:             push(@{$codetitles},&str2array($codetitles_str));
 9505:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9506:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9507:             $ok_response = 1;
 9508:         }
 9509:     }
 9510:     if ($ok_response) {
 9511:         return 'ok';
 9512:     } else {
 9513:         return $response;
 9514:     }
 9515: }
 9516: 
 9517: sub auto_instcode_defaults {
 9518:     my ($domain,$returnhash,$code_order) = @_;
 9519:     my @homeservers;
 9520: 
 9521:     my %servers = &get_servers($domain,'library');
 9522:     foreach my $tryserver (keys(%servers)) {
 9523: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9524: 	    push(@homeservers,$tryserver);
 9525: 	}
 9526:     }
 9527: 
 9528:     my $response;
 9529:     foreach my $server (@homeservers) {
 9530:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9531:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9532: 	
 9533: 	foreach my $pair (split(/\&/,$response)) {
 9534: 	    my ($name,$value)=split(/\=/,$pair);
 9535: 	    if ($name eq 'code_order') {
 9536: 		@{$code_order} = split(/\&/,&unescape($value));
 9537: 	    } else {
 9538: 		$returnhash->{&unescape($name)}=&unescape($value);
 9539: 	    }
 9540: 	}
 9541: 	return 'ok';
 9542:     }
 9543: 
 9544:     return $response;
 9545: }
 9546: 
 9547: sub auto_possible_instcodes {
 9548:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9549:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9550:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9551:         return;
 9552:     }
 9553:     my (@homeservers,$uhome);
 9554:     if (defined(&domain($domain,'primary'))) {
 9555:         $uhome=&domain($domain,'primary');
 9556:         push(@homeservers,&domain($domain,'primary'));
 9557:     } else {
 9558:         my %servers = &get_servers($domain,'library');
 9559:         foreach my $tryserver (keys(%servers)) {
 9560:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9561:                 push(@homeservers,$tryserver);
 9562:             }
 9563:         }
 9564:     }
 9565:     my $response;
 9566:     foreach my $server (@homeservers) {
 9567:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9568:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9569:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9570:             split(':',$response);
 9571:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9572:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9573:         foreach my $item (split('&',$cat_title)) {   
 9574:             my ($name,$value)=split('=',$item);
 9575:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9576:         }
 9577:         foreach my $item (split('&',$cat_order)) {
 9578:             my ($name,$value)=split('=',$item);
 9579:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9580:         }
 9581:         return 'ok';
 9582:     }
 9583:     return $response;
 9584: }
 9585: 
 9586: sub auto_courserequest_checks {
 9587:     my ($dom) = @_;
 9588:     my ($homeserver,%validations);
 9589:     if ($dom =~ /^$match_domain$/) {
 9590:         $homeserver = &domain($dom,'primary');
 9591:     }
 9592:     unless ($homeserver eq 'no_host') {
 9593:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9594:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9595:             my @items = split(/&/,$response);
 9596:             foreach my $item (@items) {
 9597:                 my ($key,$value) = split('=',$item);
 9598:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9599:             }
 9600:         }
 9601:     }
 9602:     return %validations; 
 9603: }
 9604: 
 9605: sub auto_courserequest_validation {
 9606:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9607:     my ($homeserver,$response);
 9608:     if ($dom =~ /^$match_domain$/) {
 9609:         $homeserver = &domain($dom,'primary');
 9610:     }
 9611:     unless ($homeserver eq 'no_host') {
 9612:         my $customdata;
 9613:         if (ref($custominfo) eq 'HASH') {
 9614:             $customdata = &freeze_escape($custominfo);
 9615:         }
 9616:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9617:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9618:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9619:                                     $customdata,$homeserver));
 9620:     }
 9621:     return $response;
 9622: }
 9623: 
 9624: sub auto_validate_class_sec {
 9625:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9626:     my $homeserver = &homeserver($cnum,$cdom);
 9627:     my $ownerlist;
 9628:     if (ref($owners) eq 'ARRAY') {
 9629:         $ownerlist = join(',',@{$owners});
 9630:     } else {
 9631:         $ownerlist = $owners;
 9632:     }
 9633:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9634:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9635:     return $response;
 9636: }
 9637: 
 9638: sub auto_validate_instclasses {
 9639:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9640:     my ($homeserver,%validations);
 9641:     $homeserver = &homeserver($cnum,$cdom);
 9642:     unless ($homeserver eq 'no_host') {
 9643:         my $ownerlist;
 9644:         if (ref($owners) eq 'ARRAY') {
 9645:             $ownerlist = join(',',@{$owners});
 9646:         } else {
 9647:             $ownerlist = $owners;
 9648:         }
 9649:         if (ref($classesref) eq 'HASH') {
 9650:             my $classes = &freeze_escape($classesref);
 9651:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9652:                                 ':'.$cdom.':'.$classes,$homeserver);
 9653:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9654:                 my @items = split(/&/,$response);
 9655:                 foreach my $item (@items) {
 9656:                     my ($key,$value) = split('=',$item);
 9657:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9658:                 }
 9659:             }
 9660:         }
 9661:     }
 9662:     return %validations;
 9663: }
 9664: 
 9665: sub auto_crsreq_update {
 9666:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9667:         $code,$accessstart,$accessend,$inbound) = @_;
 9668:     my ($homeserver,%crsreqresponse);
 9669:     if ($cdom =~ /^$match_domain$/) {
 9670:         $homeserver = &domain($cdom,'primary');
 9671:     }
 9672:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9673:         my $info;
 9674:         if (ref($inbound) eq 'HASH') {
 9675:             $info = &freeze_escape($inbound);
 9676:         }
 9677:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9678:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9679:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9680:                             &escape($title).':'.&escape($code).':'.
 9681:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9682:                             $homeserver);
 9683:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9684:             my @items = split(/&/,$response);
 9685:             foreach my $item (@items) {
 9686:                 my ($key,$value) = split('=',$item);
 9687:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9688:             }
 9689:         }
 9690:     }
 9691:     return \%crsreqresponse;
 9692: }
 9693: 
 9694: sub auto_export_grades {
 9695:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9696:     my ($homeserver,%exportresponse);
 9697:     if ($cdom =~ /^$match_domain$/) {
 9698:         $homeserver = &domain($cdom,'primary');
 9699:     }
 9700:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9701:         my $info;
 9702:         if (ref($inforef) eq 'HASH') {
 9703:             $info = &freeze_escape($inforef);
 9704:         }
 9705:         if (ref($gradesref) eq 'HASH') {
 9706:             my $grades = &freeze_escape($gradesref);
 9707:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9708:                                 $info.':'.$grades,$homeserver);
 9709:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9710:                 my @items = split(/&/,$response);
 9711:                 foreach my $item (@items) {
 9712:                     my ($key,$value) = split('=',$item);
 9713:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9714:                 }
 9715:             }
 9716:         }
 9717:     }
 9718:     return \%exportresponse;
 9719: }
 9720: 
 9721: sub check_instcode_cloning {
 9722:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9723:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9724:         return;
 9725:     }
 9726:     my $canclone;
 9727:     if (@{$code_order} > 0) {
 9728:         my $instcoderegexp ='^';
 9729:         my @clonecodes = split(/\&/,$cloner);
 9730:         foreach my $item (@{$code_order}) {
 9731:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9732:                 foreach my $pair (@clonecodes) {
 9733:                     my ($key,$val) = split(/\=/,$pair,2);
 9734:                     $val = &unescape($val);
 9735:                     if ($key eq $item) {
 9736:                         $instcoderegexp .= '('.$val.')';
 9737:                         last;
 9738:                     }
 9739:                 }
 9740:             } else {
 9741:                 $instcoderegexp .= $codedefaults->{$item};
 9742:             }
 9743:         }
 9744:         $instcoderegexp .= '$';
 9745:         my (@from,@to);
 9746:         eval {
 9747:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9748:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9749:         };
 9750:         if ((@from > 0) && (@to > 0)) {
 9751:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9752:             if (!@diffs) {
 9753:                 $canclone = 1;
 9754:             }
 9755:         }
 9756:     }
 9757:     return $canclone;
 9758: }
 9759: 
 9760: sub default_instcode_cloning {
 9761:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9762:     my (%codedefaults,@code_order,$canclone);
 9763:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9764:         %codedefaults = %{$codedefaultsref};
 9765:         @code_order = @{$codeorderref};
 9766:     } elsif ($clonedom) {
 9767:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 9768:     }
 9769:     if (($domdefclone) && (@code_order)) {
 9770:         my @clonecodes = split(/\+/,$domdefclone);
 9771:         my $instcoderegexp ='^';
 9772:         foreach my $item (@code_order) {
 9773:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 9774:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 9775:             } else {
 9776:                 $instcoderegexp .= $codedefaults{$item};
 9777:             }
 9778:         }
 9779:         $instcoderegexp .= '$';
 9780:         my (@from,@to);
 9781:         eval {
 9782:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9783:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 9784:         };
 9785:         if ((@from > 0) && (@to > 0)) {
 9786:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9787:             if (!@diffs) {
 9788:                 $canclone = 1;
 9789:             }
 9790:         }
 9791:     }
 9792:     return $canclone;
 9793: }
 9794: 
 9795: # ------------------------------------------------------- Course Group routines
 9796: 
 9797: sub get_coursegroups {
 9798:     my ($cdom,$cnum,$group,$namespace) = @_;
 9799:     return(&dump($namespace,$cdom,$cnum,$group));
 9800: }
 9801: 
 9802: sub modify_coursegroup {
 9803:     my ($cdom,$cnum,$groupsettings) = @_;
 9804:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9805: }
 9806: 
 9807: sub toggle_coursegroup_status {
 9808:     my ($cdom,$cnum,$group,$action) = @_;
 9809:     my ($from_namespace,$to_namespace);
 9810:     if ($action eq 'delete') {
 9811:         $from_namespace = 'coursegroups';
 9812:         $to_namespace = 'deleted_groups';
 9813:     } else {
 9814:         $from_namespace = 'deleted_groups';
 9815:         $to_namespace = 'coursegroups';
 9816:     }
 9817:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 9818:     if (my $tmp = &error(%curr_group)) {
 9819:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 9820:         return ('read error',$tmp);
 9821:     } else {
 9822:         my %savedsettings = %curr_group; 
 9823:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 9824:         my $deloutcome;
 9825:         if ($result eq 'ok') {
 9826:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 9827:         } else {
 9828:             return ('write error',$result);
 9829:         }
 9830:         if ($deloutcome eq 'ok') {
 9831:             return 'ok';
 9832:         } else {
 9833:             return ('delete error',$deloutcome);
 9834:         }
 9835:     }
 9836: }
 9837: 
 9838: sub modify_group_roles {
 9839:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
 9840:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 9841:     my $role = 'gr/'.&escape($userprivs);
 9842:     my ($uname,$udom) = split(/:/,$user);
 9843:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
 9844:     if ($result eq 'ok') {
 9845:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 9846:     }
 9847:     return $result;
 9848: }
 9849: 
 9850: sub modify_coursegroup_membership {
 9851:     my ($cdom,$cnum,$membership) = @_;
 9852:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 9853:     return $result;
 9854: }
 9855: 
 9856: sub get_active_groups {
 9857:     my ($udom,$uname,$cdom,$cnum) = @_;
 9858:     my $now = time;
 9859:     my %groups = ();
 9860:     foreach my $key (keys(%env)) {
 9861:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 9862:             my ($start,$end) = split(/\./,$env{$key});
 9863:             if (($end!=0) && ($end<$now)) { next; }
 9864:             if (($start!=0) && ($start>$now)) { next; }
 9865:             if ($1 eq $cdom && $2 eq $cnum) {
 9866:                 $groups{$3} = $env{$key} ;
 9867:             }
 9868:         }
 9869:     }
 9870:     return %groups;
 9871: }
 9872: 
 9873: sub get_group_membership {
 9874:     my ($cdom,$cnum,$group) = @_;
 9875:     return(&dump('groupmembership',$cdom,$cnum,$group));
 9876: }
 9877: 
 9878: sub get_users_groups {
 9879:     my ($udom,$uname,$courseid) = @_;
 9880:     my @usersgroups;
 9881:     my $cachetime=1800;
 9882: 
 9883:     my $hashid="$udom:$uname:$courseid";
 9884:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 9885:     if (defined($cached)) {
 9886:         @usersgroups = split(/:/,$grouplist);
 9887:     } else {  
 9888:         $grouplist = '';
 9889:         my $courseurl = &courseid_to_courseurl($courseid);
 9890:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 9891:         my $access_end = $env{'course.'.$courseid.
 9892:                               '.default_enrollment_end_date'};
 9893:         my $now = time;
 9894:         foreach my $key (keys(%roleshash)) {
 9895:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 9896:                 my $group = $1;
 9897:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 9898:                     my $start = $2;
 9899:                     my $end = $1;
 9900:                     if ($start == -1) { next; } # deleted from group
 9901:                     if (($start!=0) && ($start>$now)) { next; }
 9902:                     if (($end!=0) && ($end<$now)) {
 9903:                         if ($access_end && $access_end < $now) {
 9904:                             if ($access_end - $end < 86400) {
 9905:                                 push(@usersgroups,$group);
 9906:                             }
 9907:                         }
 9908:                         next;
 9909:                     }
 9910:                     push(@usersgroups,$group);
 9911:                 }
 9912:             }
 9913:         }
 9914:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
 9915:         $grouplist = join(':',@usersgroups);
 9916:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
 9917:     }
 9918:     return @usersgroups;
 9919: }
 9920: 
 9921: sub devalidate_getgroups_cache {
 9922:     my ($udom,$uname,$cdom,$cnum)=@_;
 9923:     my $courseid = $cdom.'_'.$cnum;
 9924: 
 9925:     my $hashid="$udom:$uname:$courseid";
 9926:     &devalidate_cache_new('getgroups',$hashid);
 9927: }
 9928: 
 9929: # ------------------------------------------------------------------ Plain Text
 9930: 
 9931: sub plaintext {
 9932:     my ($short,$type,$cid,$forcedefault) = @_;
 9933:     if ($short =~ m{^cr/}) {
 9934: 	return (split('/',$short))[-1];
 9935:     }
 9936:     if (!defined($cid)) {
 9937:         $cid = $env{'request.course.id'};
 9938:     }
 9939:     my %rolenames = (
 9940:                       Course    => 'std',
 9941:                       Community => 'alt1',
 9942:                       Placement => 'std',
 9943:                     );
 9944:     if ($cid ne '') {
 9945:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
 9946:             unless ($forcedefault) {
 9947:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
 9948:                 &Apache::lonlocal::mt_escape(\$roletext);
 9949:                 return &Apache::lonlocal::mt($roletext);
 9950:             }
 9951:         }
 9952:     }
 9953:     if ((defined($type)) && (defined($rolenames{$type})) &&
 9954:         (defined($rolenames{$type})) && 
 9955:         (defined($prp{$short}{$rolenames{$type}}))) {
 9956:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
 9957:     } elsif ($cid ne '') {
 9958:         my $crstype = $env{'course.'.$cid.'.type'};
 9959:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
 9960:             (defined($prp{$short}{$rolenames{$crstype}}))) {
 9961:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
 9962:         }
 9963:     }
 9964:     return &Apache::lonlocal::mt($prp{$short}{'std'});
 9965: }
 9966: 
 9967: # ----------------------------------------------------------------- Assign Role
 9968: 
 9969: sub assignrole {
 9970:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
 9971:         $context)=@_;
 9972:     my $mrole;
 9973:     if ($role =~ /^cr\//) {
 9974:         my $cwosec=$url;
 9975:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 9976: 	unless (&allowed('ccr',$cwosec)) {
 9977:            my $refused = 1;
 9978:            if ($context eq 'requestcourses') {
 9979:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 9980:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
 9981:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
 9982:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
 9983:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9984:                            if ($crsenv{'internal.courseowner'} eq
 9985:                                $env{'user.name'}.':'.$env{'user.domain'}) {
 9986:                                $refused = '';
 9987:                            }
 9988:                        }
 9989:                    }
 9990:                }
 9991:            }
 9992:            if ($refused) {
 9993:                &logthis('Refused custom assignrole: '.
 9994:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
 9995:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
 9996:                return 'refused';
 9997:            }
 9998:         }
 9999:         $mrole='cr';
10000:     } elsif ($role =~ /^gr\//) {
10001:         my $cwogrp=$url;
10002:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10003:         unless (&allowed('mdg',$cwogrp)) {
10004:             &logthis('Refused group assignrole: '.
10005:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10006:                     $env{'user.name'}.' at '.$env{'user.domain'});
10007:             return 'refused';
10008:         }
10009:         $mrole='gr';
10010:     } else {
10011:         my $cwosec=$url;
10012:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10013:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10014:             my $refused;
10015:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10016:                 if (!(&allowed('c'.$role,$url))) {
10017:                     $refused = 1;
10018:                 }
10019:             } else {
10020:                 $refused = 1;
10021:             }
10022:             if ($refused) {
10023:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10024:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10025:                     my %crsenv;
10026:                     if ($role eq 'cc' || $role eq 'co') {
10027:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10028:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10029:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10030:                                 if ($crsenv{'internal.courseowner'} eq 
10031:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10032:                                     $refused = '';
10033:                                 }
10034:                             }
10035:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10036:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10037:                                 if ($crsenv{'internal.courseowner'} eq 
10038:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10039:                                     $refused = '';
10040:                                 }
10041:                             }
10042:                         }
10043:                     }
10044:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10045:                     if ($role eq 'st') {
10046:                         $refused = '';
10047:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10048:                         $refused = '';
10049:                     }
10050:                 } elsif ($context eq 'requestcourses') {
10051:                     my @possroles = ('st','ta','ep','in','cc','co');
10052:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10053:                         my $wrongcc;
10054:                         if ($cnum =~ /^$match_community$/) {
10055:                             $wrongcc = 1 if ($role eq 'cc');
10056:                         } else {
10057:                             $wrongcc = 1 if ($role eq 'co');
10058:                         }
10059:                         unless ($wrongcc) {
10060:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10061:                             if ($crsenv{'internal.courseowner'} eq 
10062:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10063:                                 $refused = '';
10064:                             }
10065:                         }
10066:                     }
10067:                 } elsif ($context eq 'requestauthor') {
10068:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10069:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10070:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10071:                             $refused = '';
10072:                         } else {
10073:                             my %domdefaults = &get_domain_defaults($udom);
10074:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10075:                                 my $checkbystatus;
10076:                                 if ($env{'user.adv'}) { 
10077:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10078:                                     if ($disposition eq 'automatic') {
10079:                                         $refused = '';
10080:                                     } elsif ($disposition eq '') {
10081:                                         $checkbystatus = 1;
10082:                                     } 
10083:                                 } else {
10084:                                     $checkbystatus = 1;
10085:                                 }
10086:                                 if ($checkbystatus) {
10087:                                     if ($env{'environment.inststatus'}) {
10088:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10089:                                         foreach my $type (@inststatuses) {
10090:                                             if (($type ne '') &&
10091:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10092:                                                 $refused = '';
10093:                                             }
10094:                                         }
10095:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10096:                                         $refused = '';
10097:                                     }
10098:                                 }
10099:                             }
10100:                         }
10101:                     }
10102:                 }
10103:                 if ($refused) {
10104:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10105:                              ' '.$role.' '.$end.' '.$start.' by '.
10106: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10107:                     return 'refused';
10108:                 }
10109:             }
10110:         } elsif ($role eq 'au') {
10111:             if ($url ne '/'.$udom.'/') {
10112:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10113:                          ' to assign author role for '.$uname.':'.$udom.
10114:                          ' in domain: '.$url.' refused (wrong domain).');
10115:                 return 'refused';
10116:             }
10117:         }
10118:         $mrole=$role;
10119:     }
10120:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10121:                 "$udom:$uname:$url".'_'."$mrole=$role";
10122:     if ($end) { $command.='_'.$end; }
10123:     if ($start) {
10124: 	if ($end) { 
10125:            $command.='_'.$start; 
10126:         } else {
10127:            $command.='_0_'.$start;
10128:         }
10129:     }
10130:     my $origstart = $start;
10131:     my $origend = $end;
10132:     my $delflag;
10133: # actually delete
10134:     if ($deleteflag) {
10135: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10136: # modify command to delete the role
10137:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10138:                 "$udom:$uname:$url".'_'."$mrole";
10139: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10140: # set start and finish to negative values for userrolelog
10141:            $start=-1;
10142:            $end=-1;
10143:            $delflag = 1;
10144:         }
10145:     }
10146: # send command
10147:     my $answer=&reply($command,&homeserver($uname,$udom));
10148: # log new user role if status is ok
10149:     if ($answer eq 'ok') {
10150: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10151:         if (($role eq 'cc') || ($role eq 'in') ||
10152:             ($role eq 'ep') || ($role eq 'ad') ||
10153:             ($role eq 'ta') || ($role eq 'st') ||
10154:             ($role=~/^cr/) || ($role eq 'gr') ||
10155:             ($role eq 'co')) {
10156: # for course roles, perform group memberships changes triggered by role change.
10157:             unless ($role =~ /^gr/) {
10158:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10159:                                                  $origstart,$selfenroll,$context);
10160:             }
10161:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10162:                            $selfenroll,$context);
10163:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10164:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10165:                  ($role eq 'da')) {
10166:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10167:                            $context);
10168:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10169:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10170:                              $context); 
10171:         }
10172:         if ($role eq 'cc') {
10173:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10174:         }
10175:     }
10176:     return $answer;
10177: }
10178: 
10179: sub autoupdate_coowners {
10180:     my ($url,$end,$start,$uname,$udom) = @_;
10181:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10182:     if (($cdom ne '') && ($cnum ne '')) {
10183:         my $now = time;
10184:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10185:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10186:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10187:             my $instcode = $coursehash{'internal.coursecode'};
10188:             if ($instcode ne '') {
10189:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10190:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10191:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10192:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10193:                         if ($result eq 'valid') {
10194:                             if ($coursehash{'internal.co-owners'}) {
10195:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10196:                                     push(@newcoowners,$coowner);
10197:                                 }
10198:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10199:                                     push(@newcoowners,$uname.':'.$udom);
10200:                                 }
10201:                                 @newcoowners = sort(@newcoowners);
10202:                             } else {
10203:                                 push(@newcoowners,$uname.':'.$udom);
10204:                             }
10205:                         } else {
10206:                             if ($coursehash{'internal.co-owners'}) {
10207:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10208:                                     unless ($coowner eq $uname.':'.$udom) {
10209:                                         push(@newcoowners,$coowner);
10210:                                     }
10211:                                 }
10212:                                 unless (@newcoowners > 0) {
10213:                                     $delcoowners = 1;
10214:                                     $coowners = '';
10215:                                 }
10216:                             }
10217:                         }
10218:                         if (@newcoowners || $delcoowners) {
10219:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10220:                                             $delcoowners,@newcoowners);
10221:                         }
10222:                     }
10223:                 }
10224:             }
10225:         }
10226:     }
10227: }
10228: 
10229: sub store_coowners {
10230:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10231:     my $cid = $cdom.'_'.$cnum;
10232:     my ($coowners,$delresult,$putresult);
10233:     if (@newcoowners) {
10234:         $coowners = join(',',@newcoowners);
10235:         my %coownershash = (
10236:                             'internal.co-owners' => $coowners,
10237:                            );
10238:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10239:         if ($putresult eq 'ok') {
10240:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10241:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10242:             }
10243:         }
10244:     }
10245:     if ($delcoowners) {
10246:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10247:         if ($delresult eq 'ok') {
10248:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10249:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10250:             }
10251:         }
10252:     }
10253:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10254:         my %crsinfo =
10255:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10256:         if (ref($crsinfo{$cid}) eq 'HASH') {
10257:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10258:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10259:         }
10260:     }
10261: }
10262: 
10263: # -------------------------------------------------- Modify user authentication
10264: # Overrides without validation
10265: 
10266: sub modifyuserauth {
10267:     my ($udom,$uname,$umode,$upass)=@_;
10268:     my $uhome=&homeserver($uname,$udom);
10269:     my $allowed;
10270:     if (&allowed('mau',$udom)) {
10271:         $allowed = 1;
10272:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10273:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10274:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10275:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10276:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10277:         if (($cdom ne '') && ($cnum ne '')) {
10278:             my $is_owner = &is_course_owner($cdom,$cnum);
10279:             if ($is_owner) {
10280:                 $allowed = 1;
10281:             }
10282:         }
10283:     }
10284:     unless ($allowed) { return 'refused'; }
10285:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10286:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10287:              ' in domain '.$env{'request.role.domain'});  
10288:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10289: 		     &escape($upass),$uhome);
10290:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10291:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10292:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
10293:     &log($udom,,$uname,$uhome,
10294:         'Authentication changed by '.$env{'user.domain'}.', '.
10295:                                      $env{'user.name'}.', '.$umode.
10296:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
10297:     unless ($reply eq 'ok') {
10298:         &logthis('Authentication mode error: '.$reply);
10299: 	return 'error: '.$reply;
10300:     }   
10301:     return 'ok';
10302: }
10303: 
10304: # --------------------------------------------------------------- Modify a user
10305: 
10306: sub modifyuser {
10307:     my ($udom,    $uname, $uid,
10308:         $umode,   $upass, $first,
10309:         $middle,  $last,  $gene,
10310:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10311:     $udom= &LONCAPA::clean_domain($udom);
10312:     $uname=&LONCAPA::clean_username($uname);
10313:     my $showcandelete = 'none';
10314:     if (ref($candelete) eq 'ARRAY') {
10315:         if (@{$candelete} > 0) {
10316:             $showcandelete = join(', ',@{$candelete});
10317:         }
10318:     }
10319:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10320:              $umode.', '.$first.', '.$middle.', '.
10321: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10322:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10323:                                      ' desiredhome not specified'). 
10324:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10325:              ' in domain '.$env{'request.role.domain'});
10326:     my $uhome=&homeserver($uname,$udom,'true');
10327:     my $newuser;
10328:     if ($uhome eq 'no_host') {
10329:         $newuser = 1;
10330:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10331:                 ($umode eq 'lti')) {
10332:             return 'error: more information needed to create new user';
10333:         }
10334:     }
10335: # ----------------------------------------------------------------- Create User
10336:     if (($uhome eq 'no_host') && 
10337: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10338:         my $unhome='';
10339:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10340:             $unhome = $desiredhome;
10341: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10342: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10343:         } else { # load balancing routine for determining $unhome
10344:             my $loadm=10000000;
10345: 	    my %servers = &get_servers($udom,'library');
10346: 	    foreach my $tryserver (keys(%servers)) {
10347: 		my $answer=reply('load',$tryserver);
10348: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10349: 		    $loadm=$answer;
10350: 		    $unhome=$tryserver;
10351: 		}
10352: 	    }
10353:         }
10354:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10355: 	    return 'error: unable to find a home server for '.$uname.
10356:                    ' in domain '.$udom;
10357:         }
10358:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10359:                          &escape($upass),$unhome);
10360: 	unless ($reply eq 'ok') {
10361:             return 'error: '.$reply;
10362:         }   
10363:         $uhome=&homeserver($uname,$udom,'true');
10364:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10365: 	    return 'error: unable verify users home machine.';
10366:         }
10367:     }   # End of creation of new user
10368: # ---------------------------------------------------------------------- Add ID
10369:     if ($uid) {
10370:        $uid=~tr/A-Z/a-z/;
10371:        my %uidhash=&idrget($udom,$uname);
10372:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10373:          && (!$forceid)) {
10374: 	  unless ($uid eq $uidhash{$uname}) {
10375: 	      return 'error: user id "'.$uid.'" does not match '.
10376:                   'current user id "'.$uidhash{$uname}.'".';
10377:           }
10378:        } else {
10379: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10380:        }
10381:     }
10382: # -------------------------------------------------------------- Add names, etc
10383:     my @tmp=&get('environment',
10384: 		   ['firstname','middlename','lastname','generation','id',
10385:                     'permanentemail','inststatus'],
10386: 		   $udom,$uname);
10387:     my (%names,%oldnames);
10388:     if ($tmp[0] =~ m/^error:.*/) { 
10389:         %names=(); 
10390:     } else {
10391:         %names = @tmp;
10392:         %oldnames = %names;
10393:     }
10394: #
10395: # If name, email and/or uid are blank (e.g., because an uploaded file
10396: # of users did not contain them), do not overwrite existing values
10397: # unless field is in $candelete array ref.  
10398: #
10399: 
10400:     my @fields = ('firstname','middlename','lastname','generation',
10401:                   'permanentemail','id');
10402:     my %newvalues;
10403:     if (ref($candelete) eq 'ARRAY') {
10404:         foreach my $field (@fields) {
10405:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10406:                 if ($field eq 'firstname') {
10407:                     $names{$field} = $first;
10408:                 } elsif ($field eq 'middlename') {
10409:                     $names{$field} = $middle;
10410:                 } elsif ($field eq 'lastname') {
10411:                     $names{$field} = $last;
10412:                 } elsif ($field eq 'generation') { 
10413:                     $names{$field} = $gene;
10414:                 } elsif ($field eq 'permanentemail') {
10415:                     $names{$field} = $email;
10416:                 } elsif ($field eq 'id') {
10417:                     $names{$field}  = $uid;
10418:                 }
10419:             }
10420:         }
10421:     }
10422:     if ($first)  { $names{'firstname'}  = $first; }
10423:     if (defined($middle)) { $names{'middlename'} = $middle; }
10424:     if ($last)   { $names{'lastname'}   = $last; }
10425:     if (defined($gene))   { $names{'generation'} = $gene; }
10426:     if ($email) {
10427:        $email=~s/[^\w\@\.\-\,]//gs;
10428:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10429:     }
10430:     if ($uid) { $names{'id'}  = $uid; }
10431:     if (defined($inststatus)) {
10432:         $names{'inststatus'} = '';
10433:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10434:         if (ref($usertypes) eq 'HASH') {
10435:             my @okstatuses; 
10436:             foreach my $item (split(/:/,$inststatus)) {
10437:                 if (defined($usertypes->{$item})) {
10438:                     push(@okstatuses,$item);  
10439:                 }
10440:             }
10441:             if (@okstatuses) {
10442:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10443:             }
10444:         }
10445:     }
10446:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10447:                  $umode.', '.$first.', '.$middle.', '.
10448:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10449:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10450:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10451:     } else {
10452:         $logmsg .= ' during self creation';
10453:     }
10454:     my $changed;
10455:     if ($newuser) {
10456:         $changed = 1;
10457:     } else {
10458:         foreach my $field (@fields) {
10459:             if ($names{$field} ne $oldnames{$field}) {
10460:                 $changed = 1;
10461:                 last;
10462:             }
10463:         }
10464:     }
10465:     unless ($changed) {
10466:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10467:         &logthis($logmsg);
10468:         return 'ok';
10469:     }
10470:     my $reply = &put('environment', \%names, $udom,$uname);
10471:     if ($reply ne 'ok') { 
10472:         return 'error: '.$reply;
10473:     }
10474:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10475:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10476:     }
10477:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10478:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10479:     $logmsg = 'Success modifying user '.$logmsg;
10480:     &logthis($logmsg);
10481:     return 'ok';
10482: }
10483: 
10484: # -------------------------------------------------------------- Modify student
10485: 
10486: sub modifystudent {
10487:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10488:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10489:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10490:     if (!$cid) {
10491: 	unless ($cid=$env{'request.course.id'}) {
10492: 	    return 'not_in_class';
10493: 	}
10494:     }
10495: # --------------------------------------------------------------- Make the user
10496:     my $reply=&modifyuser
10497: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10498:          $desiredhome,$email,$inststatus);
10499:     unless ($reply eq 'ok') { return $reply; }
10500:     # This will cause &modify_student_enrollment to get the uid from the
10501:     # student's environment
10502:     $uid = undef if (!$forceid);
10503:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10504:                                         $gene,$usec,$end,$start,$type,$locktype,
10505:                                         $cid,$selfenroll,$context,$credits,$instsec);
10506:     return $reply;
10507: }
10508: 
10509: sub modify_student_enrollment {
10510:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10511:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10512:     my ($cdom,$cnum,$chome);
10513:     if (!$cid) {
10514: 	unless ($cid=$env{'request.course.id'}) {
10515: 	    return 'not_in_class';
10516: 	}
10517: 	$cdom=$env{'course.'.$cid.'.domain'};
10518: 	$cnum=$env{'course.'.$cid.'.num'};
10519:     } else {
10520: 	($cdom,$cnum)=split(/_/,$cid);
10521:     }
10522:     $chome=$env{'course.'.$cid.'.home'};
10523:     if (!$chome) {
10524: 	$chome=&homeserver($cnum,$cdom);
10525:     }
10526:     if (!$chome) { return 'unknown_course'; }
10527:     # Make sure the user exists
10528:     my $uhome=&homeserver($uname,$udom);
10529:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10530: 	return 'error: no such user';
10531:     }
10532:     # Get student data if we were not given enough information
10533:     if (!defined($first)  || $first  eq '' || 
10534:         !defined($last)   || $last   eq '' || 
10535:         !defined($uid)    || $uid    eq '' || 
10536:         !defined($middle) || $middle eq '' || 
10537:         !defined($gene)   || $gene   eq '') {
10538:         # They did not supply us with enough data to enroll the student, so
10539:         # we need to pick up more information.
10540:         my %tmp = &get('environment',
10541:                        ['firstname','middlename','lastname', 'generation','id']
10542:                        ,$udom,$uname);
10543: 
10544:         #foreach my $key (keys(%tmp)) {
10545:         #    &logthis("key $key = ".$tmp{$key});
10546:         #}
10547:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10548:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10549:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10550:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10551:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10552:     }
10553:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10554:     my $user = "$uname:$udom";
10555:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10556:     my $reply=cput('classlist',
10557: 		   {$user => 
10558: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10559: 		   $cdom,$cnum);
10560:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10561:         &devalidate_getsection_cache($udom,$uname,$cid);
10562:     } else { 
10563: 	return 'error: '.$reply;
10564:     }
10565:     # Add student role to user
10566:     my $uurl='/'.$cid;
10567:     $uurl=~s/\_/\//g;
10568:     if ($usec) {
10569: 	$uurl.='/'.$usec;
10570:     }
10571:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10572:                              $selfenroll,$context);
10573:     if ($result ne 'ok') {
10574:         if ($old_entry{$user} ne '') {
10575:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10576:         } else {
10577:             $reply = &del('classlist',[$user],$cdom,$cnum);
10578:         }
10579:     }
10580:     return $result; 
10581: }
10582: 
10583: sub format_name {
10584:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10585:     my $name;
10586:     if ($first ne 'lastname') {
10587: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10588:     } else {
10589: 	if ($lastname=~/\S/) {
10590: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10591: 	    $name=~s/\s+,/,/;
10592: 	} else {
10593: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10594: 	}
10595:     }
10596:     $name=~s/^\s+//;
10597:     $name=~s/\s+$//;
10598:     $name=~s/\s+/ /g;
10599:     return $name;
10600: }
10601: 
10602: # ------------------------------------------------- Write to course preferences
10603: 
10604: sub writecoursepref {
10605:     my ($courseid,%prefs)=@_;
10606:     $courseid=~s/^\///;
10607:     $courseid=~s/\_/\//g;
10608:     my ($cdomain,$cnum)=split(/\//,$courseid);
10609:     my $chome=homeserver($cnum,$cdomain);
10610:     if (($chome eq '') || ($chome eq 'no_host')) { 
10611: 	return 'error: no such course';
10612:     }
10613:     my $cstring='';
10614:     foreach my $pref (keys(%prefs)) {
10615: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10616:     }
10617:     $cstring=~s/\&$//;
10618:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10619: }
10620: 
10621: # ---------------------------------------------------------- Make/modify course
10622: 
10623: sub createcourse {
10624:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10625:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
10626:     $url=&declutter($url);
10627:     my $cid='';
10628:     if ($context eq 'requestcourses') {
10629:         my $can_create = 0;
10630:         my ($ownername,$ownerdom) = split(':',$course_owner);
10631:         if ($udom eq $ownerdom) {
10632:             my $reload;
10633:             if (($callercontext eq 'auto') &&
10634:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
10635:                 $reload = 'reload';
10636:             }
10637:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
10638:                                   $context)) {
10639:                 $can_create = 1;
10640:             }
10641:         } else {
10642:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10643:                                            $category);
10644:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10645:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10646:                 if (@curr > 0) {
10647:                     my @options = qw(approval validate autolimit);
10648:                     my $optregex = join('|',@options);
10649:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10650:                         $can_create = 1;
10651:                     }
10652:                 }
10653:             }
10654:         }
10655:         if ($can_create) {
10656:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10657:                 unless (&allowed('ccc',$udom)) {
10658:                     return 'refused'; 
10659:                 }
10660:             }
10661:         } else {
10662:             return 'refused';
10663:         }
10664:     } elsif (!&allowed('ccc',$udom)) {
10665:         return 'refused';
10666:     }
10667: # --------------------------------------------------------------- Get Unique ID
10668:     my $uname;
10669:     if ($cnum =~ /^$match_courseid$/) {
10670:         my $chome=&homeserver($cnum,$udom,'true');
10671:         if (($chome eq '') || ($chome eq 'no_host')) {
10672:             $uname = $cnum;
10673:         } else {
10674:             $uname = &generate_coursenum($udom,$crstype);
10675:         }
10676:     } else {
10677:         $uname = &generate_coursenum($udom,$crstype);
10678:     }
10679:     return $uname if ($uname =~ /^error/);
10680: # -------------------------------------------------- Check supplied server name
10681:     if (!defined($course_server)) {
10682:         if (defined(&domain($udom,'primary'))) {
10683:             $course_server = &domain($udom,'primary');
10684:         } else {
10685:             $course_server = $env{'user.home'}; 
10686:         }
10687:     }
10688:     my %host_servers =
10689:         &Apache::lonnet::get_servers($udom,'library');
10690:     unless ($host_servers{$course_server}) {
10691:         return 'error: invalid home server for course: '.$course_server;
10692:     }
10693: # ------------------------------------------------------------- Make the course
10694:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10695:                       $course_server);
10696:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10697:     my $uhome=&homeserver($uname,$udom,'true');
10698:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10699: 	return 'error: no such course';
10700:     }
10701: # ----------------------------------------------------------------- Course made
10702: # log existence
10703:     my $now = time;
10704:     my $newcourse = {
10705:                     $udom.'_'.$uname => {
10706:                                      description => $description,
10707:                                      inst_code   => $inst_code,
10708:                                      owner       => $course_owner,
10709:                                      type        => $crstype,
10710:                                      creator     => $env{'user.name'}.':'.
10711:                                                     $env{'user.domain'},
10712:                                      created     => $now,
10713:                                      context     => $context,
10714:                                                 },
10715:                     };
10716:     &courseidput($udom,$newcourse,$uhome,'notime');
10717: # set toplevel url
10718:     my $topurl=$url;
10719:     unless ($nonstandard) {
10720: # ------------------------------------------ For standard courses, make top url
10721:         my $mapurl=&clutter($url);
10722:         if ($mapurl eq '/res/') { $mapurl=''; }
10723:         $env{'form.initmap'}=(<<ENDINITMAP);
10724: <map>
10725: <resource id="1" type="start"></resource>
10726: <resource id="2" src="$mapurl"></resource>
10727: <resource id="3" type="finish"></resource>
10728: <link index="1" from="1" to="2"></link>
10729: <link index="2" from="2" to="3"></link>
10730: </map>
10731: ENDINITMAP
10732:         $topurl=&declutter(
10733:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10734:                           );
10735:     }
10736: # ----------------------------------------------------------- Write preferences
10737:     &writecoursepref($udom.'_'.$uname,
10738:                      ('description'              => $description,
10739:                       'url'                      => $topurl,
10740:                       'internal.creator'         => $env{'user.name'}.':'.
10741:                                                     $env{'user.domain'},
10742:                       'internal.created'         => $now,
10743:                       'internal.creationcontext' => $context)
10744:                     );
10745:     return '/'.$udom.'/'.$uname;
10746: }
10747: 
10748: # ------------------------------------------------------------------- Create ID
10749: sub generate_coursenum {
10750:     my ($udom,$crstype) = @_;
10751:     my $domdesc = &domain($udom);
10752:     return 'error: invalid domain' if ($domdesc eq '');
10753:     my $first;
10754:     if ($crstype eq 'Community') {
10755:         $first = '0';
10756:     } else {
10757:         $first = int(1+rand(9)); 
10758:     } 
10759:     my $uname=$first.
10760:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10761:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
10762:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10763: # ----------------------------------------------- Make sure that does not exist
10764:     my $uhome=&homeserver($uname,$udom,'true');
10765:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
10766:         if ($crstype eq 'Community') {
10767:             $first = '0';
10768:         } else {
10769:             $first = int(1+rand(9));
10770:         }
10771:         $uname=$first.
10772:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10773:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
10774:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10775:         $uhome=&homeserver($uname,$udom,'true');
10776:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
10777:             return 'error: unable to generate unique course-ID';
10778:         }
10779:     }
10780:     return $uname;
10781: }
10782: 
10783: sub is_course {
10784:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
10785:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
10786: 
10787:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
10788:     my $uhome=&homeserver($cnum,$cdom);
10789:     my $iscourse;
10790:     if (grep { $_ eq $uhome } current_machine_ids()) {
10791:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
10792:     } else {
10793:         my $hashid = $cdom.':'.$cnum;
10794:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
10795:         unless (defined($cached)) {
10796:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
10797:                                         $cnum,undef,undef,'.');
10798:             $iscourse = 0;
10799:             if (exists($courses{$cdom.'_'.$cnum})) {
10800:                 $iscourse = 1;
10801:             }
10802:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
10803:         }
10804:     }
10805:     return unless ($iscourse);
10806:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
10807: }
10808: 
10809: sub store_userdata {
10810:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
10811:     my $result;
10812:     if ($datakey ne '') {
10813:         if (ref($storehash) eq 'HASH') {
10814:             if ($udom eq '' || $uname eq '') {
10815:                 $udom = $env{'user.domain'};
10816:                 $uname = $env{'user.name'};
10817:             }
10818:             my $uhome=&homeserver($uname,$udom);
10819:             if (($uhome eq '') || ($uhome eq 'no_host')) {
10820:                 $result = 'error: no_host';
10821:             } else {
10822:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
10823:                 $storehash->{'host'} = $perlvar{'lonHostID'};
10824: 
10825:                 my $namevalue='';
10826:                 foreach my $key (keys(%{$storehash})) {
10827:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
10828:                 }
10829:                 $namevalue=~s/\&$//;
10830:                 unless ($namespace eq 'courserequests') {
10831:                     $datakey = &escape($datakey);
10832:                 }
10833:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
10834:                                   $namevalue,$uhome);
10835:             }
10836:         } else {
10837:             $result = 'error: data to store was not a hash reference'; 
10838:         }
10839:     } else {
10840:         $result= 'error: invalid requestkey'; 
10841:     }
10842:     return $result;
10843: }
10844: 
10845: # ---------------------------------------------------------- Assign Custom Role
10846: 
10847: sub assigncustomrole {
10848:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10849:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10850:                        $end,$start,$deleteflag,$selfenroll,$context);
10851: }
10852: 
10853: # ----------------------------------------------------------------- Revoke Role
10854: 
10855: sub revokerole {
10856:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10857:     my $now=time;
10858:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10859: }
10860: 
10861: # ---------------------------------------------------------- Revoke Custom Role
10862: 
10863: sub revokecustomrole {
10864:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10865:     my $now=time;
10866:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10867:            $deleteflag,$selfenroll,$context);
10868: }
10869: 
10870: # ------------------------------------------------------------ Disk usage
10871: sub diskusage {
10872:     my ($udom,$uname,$directorypath,$getpropath)=@_;
10873:     $directorypath =~ s/\/$//;
10874:     my $listing=&reply('du2:'.&escape($directorypath).':'
10875:                        .&escape($getpropath).':'.&escape($uname).':'
10876:                        .&escape($udom),homeserver($uname,$udom));
10877:     if ($listing eq 'unknown_cmd') {
10878:         if ($getpropath) {
10879:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
10880:         }
10881:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10882:     }
10883:     return $listing;
10884: }
10885: 
10886: sub is_locked {
10887:     my ($file_name, $domain, $user, $which) = @_;
10888:     my @check;
10889:     my $is_locked;
10890:     push (@check,$file_name);
10891:     my %locked = &get('file_permissions',\@check,
10892: 		      $env{'user.domain'},$env{'user.name'});
10893:     my ($tmp)=keys(%locked);
10894:     if ($tmp=~/^error:/) { undef(%locked); }
10895:     
10896:     if (ref($locked{$file_name}) eq 'ARRAY') {
10897:         $is_locked = 'false';
10898:         foreach my $entry (@{$locked{$file_name}}) {
10899:            if (ref($entry) eq 'ARRAY') {
10900:                $is_locked = 'true';
10901:                if (ref($which) eq 'ARRAY') {
10902:                    push(@{$which},$entry);
10903:                } else {
10904:                    last;
10905:                }
10906:            }
10907:        }
10908:     } else {
10909:         $is_locked = 'false';
10910:     }
10911:     return $is_locked;
10912: }
10913: 
10914: sub declutter_portfile {
10915:     my ($file) = @_;
10916:     $file =~ s{^(/portfolio/|portfolio/)}{/};
10917:     return $file;
10918: }
10919: 
10920: # ------------------------------------------------------------- Mark as Read Only
10921: 
10922: sub mark_as_readonly {
10923:     my ($domain,$user,$files,$what) = @_;
10924:     my %current_permissions = &dump('file_permissions',$domain,$user);
10925:     my ($tmp)=keys(%current_permissions);
10926:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10927:     foreach my $file (@{$files}) {
10928: 	$file = &declutter_portfile($file);
10929:         push(@{$current_permissions{$file}},$what);
10930:     }
10931:     &put('file_permissions',\%current_permissions,$domain,$user);
10932:     return;
10933: }
10934: 
10935: # ------------------------------------------------------------Save Selected Files
10936: 
10937: sub save_selected_files {
10938:     my ($user, $path, @files) = @_;
10939:     my $filename = $user."savedfiles";
10940:     my @other_files = &files_not_in_path($user, $path);
10941:     open (OUT,'>',LONCAPA::tempdir().$filename);
10942:     foreach my $file (@files) {
10943:         print (OUT $env{'form.currentpath'}.$file."\n");
10944:     }
10945:     foreach my $file (@other_files) {
10946:         print (OUT $file."\n");
10947:     }
10948:     close (OUT);
10949:     return 'ok';
10950: }
10951: 
10952: sub clear_selected_files {
10953:     my ($user) = @_;
10954:     my $filename = $user."savedfiles";
10955:     open (OUT,'>',LONCAPA::tempdir().$filename);
10956:     print (OUT undef);
10957:     close (OUT);
10958:     return ("ok");    
10959: }
10960: 
10961: sub files_in_path {
10962:     my ($user, $path) = @_;
10963:     my $filename = $user."savedfiles";
10964:     my %return_files;
10965:     open (IN,'<',LONCAPA::tempdir().$filename);
10966:     while (my $line_in = <IN>) {
10967:         chomp ($line_in);
10968:         my @paths_and_file = split (m!/!, $line_in);
10969:         my $file_part = pop (@paths_and_file);
10970:         my $path_part = join ('/', @paths_and_file);
10971:         $path_part.='/';
10972:         my $path_and_file = $path_part.$file_part;
10973:         if ($path_part eq $path) {
10974:             $return_files{$file_part}= 'selected';
10975:         }
10976:     }
10977:     close (IN);
10978:     return (\%return_files);
10979: }
10980: 
10981: # called in portfolio select mode, to show files selected NOT in current directory
10982: sub files_not_in_path {
10983:     my ($user, $path) = @_;
10984:     my $filename = $user."savedfiles";
10985:     my @return_files;
10986:     my $path_part;
10987:     open(IN, '<',LONCAPA::tempdir().$filename);
10988:     while (my $line = <IN>) {
10989:         #ok, I know it's clunky, but I want it to work
10990:         my @paths_and_file = split(m|/|, $line);
10991:         my $file_part = pop(@paths_and_file);
10992:         chomp($file_part);
10993:         my $path_part = join('/', @paths_and_file);
10994:         $path_part .= '/';
10995:         my $path_and_file = $path_part.$file_part;
10996:         if ($path_part ne $path) {
10997:             push(@return_files, ($path_and_file));
10998:         }
10999:     }
11000:     close(OUT);
11001:     return (@return_files);
11002: }
11003: 
11004: #------------------------------Submitted/Handedback Portfolio Files Versioning
11005:  
11006: sub portfiles_versioning {
11007:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11008:     my $portfolio_root = '/userfiles/portfolio';
11009:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11010:     foreach my $file (@{$portfiles}) {
11011:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11012:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11013:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11014:         my $getpropath = 1;
11015:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11016:                                              $stu_name,$getpropath);
11017:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11018:         my $new_answer = 
11019:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11020:         if ($new_answer ne 'problem getting file') {
11021:             push(@{$versioned_portfiles}, $directory.$new_answer);
11022:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11023:                               [$symb,$env{'request.course.id'},'graded']);
11024:         }
11025:     }
11026: }
11027: 
11028: sub get_next_version {
11029:     my ($answer_name, $answer_ext, $dir_list) = @_;
11030:     my $version;
11031:     if (ref($dir_list) eq 'ARRAY') {
11032:         foreach my $row (@{$dir_list}) {
11033:             my ($file) = split(/\&/,$row,2);
11034:             my ($file_name,$file_version,$file_ext) =
11035:                 &file_name_version_ext($file);
11036:             if (($file_name eq $answer_name) &&
11037:                 ($file_ext eq $answer_ext)) {
11038:                      # gets here if filename and extension match,
11039:                      # regardless of version
11040:                 if ($file_version ne '') {
11041:                     # a versioned file is found  so save it for later
11042:                     if ($file_version > $version) {
11043:                         $version = $file_version;
11044:                     }
11045:                 }
11046:             }
11047:         }
11048:     }
11049:     $version ++;
11050:     return($version);
11051: }
11052: 
11053: sub version_selected_portfile {
11054:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11055:     my ($answer_name,$answer_ver,$answer_ext) =
11056:         &file_name_version_ext($file_name);
11057:     my $new_answer;
11058:     $env{'form.copy'} =
11059:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11060:     if($env{'form.copy'} eq '-1') {
11061:         $new_answer = 'problem getting file';
11062:     } else {
11063:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11064:         my $copy_result = 
11065:             &finishuserfileupload($stu_name,$domain,'copy',
11066:                                   '/portfolio'.$directory.$new_answer);
11067:     }
11068:     undef($env{'form.copy'});
11069:     return ($new_answer);
11070: }
11071: 
11072: sub file_name_version_ext {
11073:     my ($file)=@_;
11074:     my @file_parts = split(/\./, $file);
11075:     my ($name,$version,$ext);
11076:     if (@file_parts > 1) {
11077:         $ext=pop(@file_parts);
11078:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11079:             $version=pop(@file_parts);
11080:         }
11081:         $name=join('.',@file_parts);
11082:     } else {
11083:         $name=join('.',@file_parts);
11084:     }
11085:     return($name,$version,$ext);
11086: }
11087: 
11088: #----------------------------------------------Get portfolio file permissions
11089: 
11090: sub get_portfile_permissions {
11091:     my ($domain,$user) = @_;
11092:     my %current_permissions = &dump('file_permissions',$domain,$user);
11093:     my ($tmp)=keys(%current_permissions);
11094:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11095:     return \%current_permissions;
11096: }
11097: 
11098: #---------------------------------------------Get portfolio file access controls
11099: 
11100: sub get_access_controls {
11101:     my ($current_permissions,$group,$file) = @_;
11102:     my %access;
11103:     my $real_file = $file;
11104:     $file =~ s/\.meta$//;
11105:     if (defined($file)) {
11106:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11107:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11108:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11109:             }
11110:         }
11111:     } else {
11112:         foreach my $key (keys(%{$current_permissions})) {
11113:             if ($key =~ /\0accesscontrol$/) {
11114:                 if (defined($group)) {
11115:                     if ($key !~ m-^\Q$group\E/-) {
11116:                         next;
11117:                     }
11118:                 }
11119:                 my ($fullpath) = split(/\0/,$key);
11120:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11121:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11122:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11123:                     }
11124:                 }
11125:             }
11126:         }
11127:     }
11128:     return %access;
11129: }
11130: 
11131: sub modify_access_controls {
11132:     my ($file_name,$changes,$domain,$user)=@_;
11133:     my ($outcome,$deloutcome);
11134:     my %store_permissions;
11135:     my %new_values;
11136:     my %new_control;
11137:     my %translation;
11138:     my @deletions = ();
11139:     my $now = time;
11140:     if (exists($$changes{'activate'})) {
11141:         if (ref($$changes{'activate'}) eq 'HASH') {
11142:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11143:             my $numnew = scalar(@newitems);
11144:             for (my $i=0; $i<$numnew; $i++) {
11145:                 my $newkey = $newitems[$i];
11146:                 my $newid = &Apache::loncommon::get_cgi_id();
11147:                 if ($newkey =~ /^\d+:/) { 
11148:                     $newkey =~ s/^(\d+)/$newid/;
11149:                     $translation{$1} = $newid;
11150:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11151:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11152:                     $translation{$1} = $newid;
11153:                 }
11154:                 $new_values{$file_name."\0".$newkey} = 
11155:                                           $$changes{'activate'}{$newitems[$i]};
11156:                 $new_control{$newkey} = $now;
11157:             }
11158:         }
11159:     }
11160:     my %todelete;
11161:     my %changed_items;
11162:     foreach my $action ('delete','update') {
11163:         if (exists($$changes{$action})) {
11164:             if (ref($$changes{$action}) eq 'HASH') {
11165:                 foreach my $key (keys(%{$$changes{$action}})) {
11166:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11167:                     if ($action eq 'delete') { 
11168:                         $todelete{$itemnum} = 1;
11169:                     } else {
11170:                         $changed_items{$itemnum} = $key;
11171:                     }
11172:                 }
11173:             }
11174:         }
11175:     }
11176:     # get lock on access controls for file.
11177:     my $lockhash = {
11178:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11179:                                                        ':'.$env{'user.domain'},
11180:                    }; 
11181:     my $tries = 0;
11182:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11183:    
11184:     while (($gotlock ne 'ok') && $tries < 10) {
11185:         $tries ++;
11186:         sleep(0.1);
11187:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11188:     }
11189:     if ($gotlock eq 'ok') {
11190:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11191:         my ($tmp)=keys(%curr_permissions);
11192:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11193:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11194:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11195:             if (ref($curr_controls) eq 'HASH') {
11196:                 foreach my $control_item (keys(%{$curr_controls})) {
11197:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11198:                     if (defined($todelete{$itemnum})) {
11199:                         push(@deletions,$file_name."\0".$control_item);
11200:                     } else {
11201:                         if (defined($changed_items{$itemnum})) {
11202:                             $new_control{$changed_items{$itemnum}} = $now;
11203:                             push(@deletions,$file_name."\0".$control_item);
11204:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11205:                         } else {
11206:                             $new_control{$control_item} = $$curr_controls{$control_item};
11207:                         }
11208:                     }
11209:                 }
11210:             }
11211:         }
11212:         my ($group);
11213:         if (&is_course($domain,$user)) {
11214:             ($group,my $file) = split(/\//,$file_name,2);
11215:         }
11216:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11217:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11218:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11219:         #  remove lock
11220:         my @del_lock = ($file_name."\0".'locked_access_records');
11221:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11222:         my $sqlresult =
11223:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11224:                                     $group);
11225:     } else {
11226:         $outcome = "error: could not obtain lockfile\n";  
11227:     }
11228:     return ($outcome,$deloutcome,\%new_values,\%translation);
11229: }
11230: 
11231: sub make_public_indefinitely {
11232:     my (@requrl) = @_;
11233:     return &automated_portfile_access('public',\@requrl);
11234: }
11235: 
11236: sub automated_portfile_access {
11237:     my ($accesstype,$addsref,$delsref,$info) = @_;
11238:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11239:         return 'invalid';
11240:     }
11241:     my %urls;
11242:     if (ref($addsref) eq 'ARRAY') {
11243:         foreach my $requrl (@{$addsref}) {
11244:             if (&is_portfolio_url($requrl)) {
11245:                 unless (exists($urls{$requrl})) {
11246:                     $urls{$requrl} = 'add';
11247:                 }
11248:             }
11249:         }
11250:     }
11251:     if (ref($delsref) eq 'ARRAY') {
11252:         foreach my $requrl (@{$delsref}) { 
11253:             if (&is_portfolio_url($requrl)) {
11254:                 unless (exists($urls{$requrl})) {
11255:                     $urls{$requrl} = 'delete'; 
11256:                 }
11257:             }
11258:         }
11259:     }
11260:     unless (keys(%urls)) {
11261:         return 'invalid';
11262:     }
11263:     my $ip;
11264:     if ($accesstype eq 'ip') {
11265:         if (ref($info) eq 'HASH') {
11266:             if ($info->{'ip'} ne '') {
11267:                 $ip = $info->{'ip'};
11268:             }
11269:         }
11270:         if ($ip eq '') {
11271:             return 'invalid';
11272:         }
11273:     }
11274:     my $errors;
11275:     my $now = time;
11276:     my %current_perms;
11277:     foreach my $requrl (sort(keys(%urls))) {
11278:         my $action;
11279:         if ($urls{$requrl} eq 'add') {
11280:             $action = 'activate';
11281:         } else {
11282:             $action = 'none';
11283:         }
11284:         my $aclnum = 0;
11285:         my (undef,$udom,$unum,$file_name,$group) =
11286:             &parse_portfolio_url($requrl);
11287:         unless (exists($current_perms{$unum.':'.$udom})) {
11288:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11289:         }
11290:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11291:                                                    $group,$file_name);
11292:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11293:             my ($num,$scope,$end,$start) = 
11294:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11295:             if ($scope eq $accesstype) {
11296:                 if (($start <= $now) && ($end == 0)) {
11297:                     if ($accesstype eq 'ip') {
11298:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11299:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11300:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11301:                                     if ($urls{$requrl} eq 'add') {
11302:                                         $action = 'none';
11303:                                         last;
11304:                                     } else {
11305:                                         $action = 'delete';
11306:                                         $aclnum = $num;
11307:                                         last;
11308:                                     }
11309:                                 }
11310:                             }
11311:                         }
11312:                     } elsif ($accesstype eq 'public') {
11313:                         if ($urls{$requrl} eq 'add') {
11314:                             $action = 'none';
11315:                             last;
11316:                         } else {
11317:                             $action = 'delete';
11318:                             $aclnum = $num;
11319:                             last;
11320:                         }
11321:                     }
11322:                 } elsif ($accesstype eq 'public') {
11323:                     $action = 'update';
11324:                     $aclnum = $num;
11325:                     last;
11326:                 }
11327:             }
11328:         }
11329:         if ($action eq 'none') {
11330:             next;
11331:         } else {
11332:             my %changes;
11333:             my $newend = 0;
11334:             my $newstart = $now;
11335:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11336:             $changes{$action}{$newkey} = {
11337:                 type => $accesstype,
11338:                 time => {
11339:                     start => $newstart,
11340:                     end   => $newend,
11341:                 },
11342:             };
11343:             if ($accesstype eq 'ip') {
11344:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11345:             }
11346:             my ($outcome,$deloutcome,$new_values,$translation) =
11347:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11348:             unless ($outcome eq 'ok') {
11349:                 $errors .= $outcome.' ';
11350:             }
11351:         }
11352:     }
11353:     if ($errors) {
11354:         $errors =~ s/\s$//;
11355:         return $errors;
11356:     } else {
11357:         return 'ok';
11358:     }
11359: }
11360: 
11361: #------------------------------------------------------Get Marked as Read Only
11362: 
11363: sub get_marked_as_readonly {
11364:     my ($domain,$user,$what,$group) = @_;
11365:     my $current_permissions = &get_portfile_permissions($domain,$user);
11366:     my @readonly_files;
11367:     my $cmp1=$what;
11368:     if (ref($what)) { $cmp1=join('',@{$what}) };
11369:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11370:         if (defined($group)) {
11371:             if ($file_name !~ m-^\Q$group\E/-) {
11372:                 next;
11373:             }
11374:         }
11375:         if (ref($value) eq "ARRAY"){
11376:             foreach my $stored_what (@{$value}) {
11377:                 my $cmp2=$stored_what;
11378:                 if (ref($stored_what) eq 'ARRAY') {
11379:                     $cmp2=join('',@{$stored_what});
11380:                 }
11381:                 if ($cmp1 eq $cmp2) {
11382:                     push(@readonly_files, $file_name);
11383:                     last;
11384:                 } elsif (!defined($what)) {
11385:                     push(@readonly_files, $file_name);
11386:                     last;
11387:                 }
11388:             }
11389:         }
11390:     }
11391:     return @readonly_files;
11392: }
11393: #-----------------------------------------------------------Get Marked as Read Only Hash
11394: 
11395: sub get_marked_as_readonly_hash {
11396:     my ($current_permissions,$group,$what) = @_;
11397:     my %readonly_files;
11398:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11399:         if (defined($group)) {
11400:             if ($file_name !~ m-^\Q$group\E/-) {
11401:                 next;
11402:             }
11403:         }
11404:         if (ref($value) eq "ARRAY"){
11405:             foreach my $stored_what (@{$value}) {
11406:                 if (ref($stored_what) eq 'ARRAY') {
11407:                     foreach my $lock_descriptor(@{$stored_what}) {
11408:                         if ($lock_descriptor eq 'graded') {
11409:                             $readonly_files{$file_name} = 'graded';
11410:                         } elsif ($lock_descriptor eq 'handback') {
11411:                             $readonly_files{$file_name} = 'handback';
11412:                         } else {
11413:                             if (!exists($readonly_files{$file_name})) {
11414:                                 $readonly_files{$file_name} = 'locked';
11415:                             }
11416:                         }
11417:                     }
11418:                 } 
11419:             }
11420:         } 
11421:     }
11422:     return %readonly_files;
11423: }
11424: # ------------------------------------------------------------ Unmark as Read Only
11425: 
11426: sub unmark_as_readonly {
11427:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11428:     # for portfolio submissions, $what contains [$symb,$crsid] 
11429:     my ($domain,$user,$what,$file_name,$group) = @_;
11430:     $file_name = &declutter_portfile($file_name);
11431:     my $symb_crs = $what;
11432:     if (ref($what)) { $symb_crs=join('',@$what); }
11433:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11434:     my ($tmp)=keys(%current_permissions);
11435:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11436:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11437:     foreach my $file (@readonly_files) {
11438: 	my $clean_file = &declutter_portfile($file);
11439: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11440: 	my $current_locks = $current_permissions{$file};
11441:         my @new_locks;
11442:         my @del_keys;
11443:         if (ref($current_locks) eq "ARRAY"){
11444:             foreach my $locker (@{$current_locks}) {
11445:                 my $compare=$locker;
11446:                 if (ref($locker) eq 'ARRAY') {
11447:                     $compare=join('',@{$locker});
11448:                     if ($compare ne $symb_crs) {
11449:                         push(@new_locks, $locker);
11450:                     }
11451:                 }
11452:             }
11453:             if (scalar(@new_locks) > 0) {
11454:                 $current_permissions{$file} = \@new_locks;
11455:             } else {
11456:                 push(@del_keys, $file);
11457:                 &del('file_permissions',\@del_keys, $domain, $user);
11458:                 delete($current_permissions{$file});
11459:             }
11460:         }
11461:     }
11462:     &put('file_permissions',\%current_permissions,$domain,$user);
11463:     return;
11464: }
11465: 
11466: # ------------------------------------------------------------ Directory lister
11467: 
11468: sub dirlist {
11469:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11470:     $uri=~s/^\///;
11471:     $uri=~s/\/$//;
11472:     my ($udom, $uname);
11473:     if ($getuserdir) {
11474:         $udom = $userdomain;
11475:         $uname = $username;
11476:     } else {
11477:         (undef,$udom,$uname)=split(/\//,$uri);
11478:         if(defined($userdomain)) {
11479:             $udom = $userdomain;
11480:         }
11481:         if(defined($username)) {
11482:             $uname = $username;
11483:         }
11484:     }
11485:     my ($dirRoot,$listing,@listing_results);
11486: 
11487:     $dirRoot = $perlvar{'lonDocRoot'};
11488:     if (defined($getpropath)) {
11489:         $dirRoot = &propath($udom,$uname);
11490:         $dirRoot =~ s/\/$//;
11491:     } elsif (defined($getuserdir)) {
11492:         my $subdir=$uname.'__';
11493:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11494:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11495:                    ."/$udom/$subdir/$uname";
11496:     } elsif (defined($alternateRoot)) {
11497:         $dirRoot = $alternateRoot;
11498:     }
11499: 
11500:     if($udom) {
11501:         if($uname) {
11502:             my $uhome = &homeserver($uname,$udom);
11503:             if ($uhome eq 'no_host') {
11504:                 return ([],'no_host');
11505:             }
11506:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11507:                               .$getuserdir.':'.&escape($dirRoot)
11508:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11509:             if ($listing eq 'unknown_cmd') {
11510:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11511:             } else {
11512:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11513:             }
11514:             if ($listing eq 'unknown_cmd') {
11515:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11516:                 @listing_results = split(/:/,$listing);
11517:             } else {
11518:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11519:             }
11520:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11521:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11522:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11523:                 return ([],$listing);
11524:             } else {
11525:                 return (\@listing_results);
11526:             }
11527:         } elsif(!$alternateRoot) {
11528:             my (%allusers,%listerror);
11529: 	    my %servers = &get_servers($udom,'library');
11530:  	    foreach my $tryserver (keys(%servers)) {
11531:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11532:                                   &escape($udom),$tryserver);
11533:                 if ($listing eq 'unknown_cmd') {
11534: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11535: 				      $udom, $tryserver);
11536:                 } else {
11537:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11538:                 }
11539: 		if ($listing eq 'unknown_cmd') {
11540: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11541: 				      $udom, $tryserver);
11542: 		    @listing_results = split(/:/,$listing);
11543: 		} else {
11544: 		    @listing_results =
11545: 			map { &unescape($_); } split(/:/,$listing);
11546: 		}
11547:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11548:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11549:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11550:                     $listerror{$tryserver} = $listing;
11551:                 } else {
11552: 		    foreach my $line (@listing_results) {
11553: 			my ($entry) = split(/&/,$line,2);
11554: 			$allusers{$entry} = 1;
11555: 		    }
11556: 		}
11557:             }
11558:             my @alluserslist=();
11559:             foreach my $user (sort(keys(%allusers))) {
11560:                 push(@alluserslist,$user.'&user');
11561:             }
11562: 
11563:             if (!%listerror) {
11564:                 # no errors
11565:                 return (\@alluserslist);
11566:             } elsif (scalar(keys(%servers)) == 1) {
11567:                 # one library server, one error 
11568:                 my ($key) = keys(%listerror);
11569:                 return (\@alluserslist, $listerror{$key});
11570:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11571:                 # con_lost indicates that we might miss data from at least one
11572:                 # library server
11573:                 return (\@alluserslist, 'con_lost');
11574:             } else {
11575:                 # multiple library servers and no con_lost -> data should be
11576:                 # complete. 
11577:                 return (\@alluserslist);
11578:             }
11579: 
11580:         } else {
11581:             return ([],'missing username');
11582:         }
11583:     } elsif(!defined($getpropath)) {
11584:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11585:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11586:         return (\@all_domains);
11587:     } else {
11588:         return ([],'missing domain');
11589:     }
11590: }
11591: 
11592: # --------------------------------------------- GetFileTimestamp
11593: # This function utilizes dirlist and returns the date stamp for
11594: # when it was last modified.  It will also return an error of -1
11595: # if an error occurs
11596: 
11597: sub GetFileTimestamp {
11598:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11599:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11600:     $studentName   = &LONCAPA::clean_username($studentName);
11601:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11602:                                     undef,$getuserdir);
11603:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11604:         return -1;
11605:     }
11606:     if (ref($fileref) eq 'ARRAY') {
11607:         my @stats = split('&',$fileref->[0]);
11608:         # @stats contains first the filename, then the stat output
11609:         return $stats[10]; # so this is 10 instead of 9.
11610:     } else {
11611:         return -1;
11612:     }
11613: }
11614: 
11615: sub stat_file {
11616:     my ($uri) = @_;
11617:     $uri = &clutter_with_no_wrapper($uri);
11618: 
11619:     my ($udom,$uname,$file);
11620:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11621: 	($udom,$uname,$file) =
11622: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11623: 	$file = 'userfiles/'.$file;
11624:     }
11625:     if ($uri =~ m-^/res/-) {
11626: 	($udom,$uname) = 
11627: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11628: 	$file = $uri;
11629:     }
11630: 
11631:     if (!$udom || !$uname || !$file) {
11632: 	# unable to handle the uri
11633: 	return ();
11634:     }
11635:     my $getpropath;
11636:     if ($file =~ /^userfiles\//) {
11637:         $getpropath = 1;
11638:     }
11639:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11640:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11641:         return ();
11642:     } else {
11643:         if (ref($listref) eq 'ARRAY') {
11644:             my @stats = split('&',$listref->[0]);
11645: 	    shift(@stats); #filename is first
11646: 	    return @stats;
11647:         }
11648:     }
11649:     return ();
11650: }
11651: 
11652: # --------------------------------------------------------- recursedirs
11653: # Recursive function to traverse either a specific user's Authoring Space
11654: # or corresponding Published Resource Space, and populate the hash ref:
11655: # $dirhashref with URLs of all directories, and if $filehashref hash
11656: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11657: # or .rights files in resource space, and .meta, .save, .log, and .bak
11658: # files in Authoring Space.
11659: #
11660: # Inputs:
11661: #
11662: # $is_home - true if current server is home server for user's space
11663: # $context - either: priv, or res respectively for Authoring or Resource Space.
11664: # $docroot - Document root (i.e., /home/httpd/html
11665: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11666: # $relpath - Current path (relative to top level).
11667: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11668: # $filehashref - reference to hash to populate with URLs of files (Optional)
11669: #
11670: # Returns: nothing
11671: #
11672: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11673: #
11674: # Currently used by interface/londocs.pm to create linked select boxes for
11675: # directory and filename to import a Course "Author" resource into a course, and
11676: # also to create linked select boxes for Authoring Space and Directory to choose
11677: # save location for creation of a new "standard" problem from the Course Editor.
11678: #
11679: 
11680: sub recursedirs {
11681:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11682:     return unless (ref($dirhashref) eq 'HASH');
11683:     my $currpath = $docroot.$toppath;
11684:     if ($relpath) {
11685:         $currpath .= "/$relpath";
11686:     }
11687:     my $savefile;
11688:     if (ref($filehashref)) {
11689:         $savefile = 1;
11690:     }
11691:     if ($is_home) {
11692:         if (opendir(my $dirh,$currpath)) {
11693:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11694:                 next if ($item eq '');
11695:                 if (-d "$currpath/$item") {
11696:                     my $newpath;
11697:                     if ($relpath) {
11698:                         $newpath = "$relpath/$item";
11699:                     } else {
11700:                         $newpath = $item;
11701:                     }
11702:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11703:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11704:                 } elsif ($savefile) {
11705:                     if ($context eq 'priv') {
11706:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11707:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11708:                         }
11709:                     } else {
11710:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11711:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11712:                         }
11713:                     }
11714:                 }
11715:             }
11716:             closedir($dirh);
11717:         }
11718:     } else {
11719:         my ($dirlistref,$listerror) =
11720:             &dirlist($toppath.$relpath);
11721:         my @dir_lines;
11722:         my $dirptr=16384;
11723:         if (ref($dirlistref) eq 'ARRAY') {
11724:             foreach my $dir_line (sort
11725:                               {
11726:                                   my ($afile)=split('&',$a,2);
11727:                                   my ($bfile)=split('&',$b,2);
11728:                                   return (lc($afile) cmp lc($bfile));
11729:                               } (@{$dirlistref})) {
11730:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11731:                     split(/\&/,$dir_line,16);
11732:                 $item =~ s/\s+$//;
11733:                 next if (($item =~ /^\.\.?$/) || ($obs));
11734:                 if ($dirptr&$testdir) {
11735:                     my $newpath;
11736:                     if ($relpath) {
11737:                         $newpath = "$relpath/$item";
11738:                     } else {
11739:                         $relpath = '/';
11740:                         $newpath = $item;
11741:                     }
11742:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11743:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11744:                 } elsif ($savefile) {
11745:                     if ($context eq 'priv') {
11746:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11747:                             $filehashref->{$relpath}{$item} = 1;
11748:                         }
11749:                     } else {
11750:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11751:                             $filehashref->{$relpath}{$item} = 1;
11752:                         }
11753:                     }
11754:                 }
11755:             }
11756:         }
11757:     }
11758:     return;
11759: }
11760: 
11761: # -------------------------------------------------------- Value of a Condition
11762: 
11763: # gets the value of a specific preevaluated condition
11764: #    stored in the string  $env{user.state.<cid>}
11765: # or looks up a condition reference in the bighash and if if hasn't
11766: # already been evaluated recurses into docondval to get the value of
11767: # the condition, then memoizing it to 
11768: #   $env{user.state.<cid>.<condition>}
11769: sub directcondval {
11770:     my $number=shift;
11771:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
11772: 	&Apache::lonuserstate::evalstate();
11773:     }
11774:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
11775: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
11776:     } elsif ($number =~ /^_/) {
11777: 	my $sub_condition;
11778: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11779: 		&GDBM_READER(),0640)) {
11780: 	    $sub_condition=$bighash{'conditions'.$number};
11781: 	    untie(%bighash);
11782: 	}
11783: 	my $value = &docondval($sub_condition);
11784: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11785: 	return $value;
11786:     }
11787:     if ($env{'user.state.'.$env{'request.course.id'}}) {
11788:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11789:     } else {
11790:        return 2;
11791:     }
11792: }
11793: 
11794: # get the collection of conditions for this resource
11795: sub condval {
11796:     my $condidx=shift;
11797:     my $allpathcond='';
11798:     foreach my $cond (split(/\|/,$condidx)) {
11799: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
11800: 	    $allpathcond.=
11801: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
11802: 	}
11803:     }
11804:     $allpathcond=~s/\|$//;
11805:     return &docondval($allpathcond);
11806: }
11807: 
11808: #evaluates an expression of conditions
11809: sub docondval {
11810:     my ($allpathcond) = @_;
11811:     my $result=0;
11812:     if ($env{'request.course.id'}
11813: 	&& defined($allpathcond)) {
11814: 	my $operand='|';
11815: 	my @stack;
11816: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
11817: 	    if ($chunk eq '(') {
11818: 		push @stack,($operand,$result);
11819: 	    } elsif ($chunk eq ')') {
11820: 		my $before=pop @stack;
11821: 		if (pop @stack eq '&') {
11822: 		    $result=$result>$before?$before:$result;
11823: 		} else {
11824: 		    $result=$result>$before?$result:$before;
11825: 		}
11826: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
11827: 		$operand=$chunk;
11828: 	    } else {
11829: 		my $new=directcondval($chunk);
11830: 		if ($operand eq '&') {
11831: 		    $result=$result>$new?$new:$result;
11832: 		} else {
11833: 		    $result=$result>$new?$result:$new;
11834: 		}
11835: 	    }
11836: 	}
11837:     }
11838:     return $result;
11839: }
11840: 
11841: # ---------------------------------------------------- Devalidate courseresdata
11842: 
11843: sub devalidatecourseresdata {
11844:     my ($coursenum,$coursedomain)=@_;
11845:     my $hashid=$coursenum.':'.$coursedomain;
11846:     &devalidate_cache_new('courseres',$hashid);
11847: }
11848: 
11849: 
11850: # --------------------------------------------------- Course Resourcedata Query
11851: #
11852: #  Parameters:
11853: #      $coursenum    - Number of the course.
11854: #      $coursedomain - Domain at which the course was created.
11855: #  Returns:
11856: #     A hash of the course parameters along (I think) with timestamps
11857: #     and version info.
11858: 
11859: sub get_courseresdata {
11860:     my ($coursenum,$coursedomain)=@_;
11861:     my $coursehom=&homeserver($coursenum,$coursedomain);
11862:     my $hashid=$coursenum.':'.$coursedomain;
11863:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
11864:     my %dumpreply;
11865:     unless (defined($cached)) {
11866: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11867: 	$result=\%dumpreply;
11868: 	my ($tmp) = keys(%dumpreply);
11869: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11870: 	    &do_cache_new('courseres',$hashid,$result,600);
11871: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11872: 	    return $tmp;
11873: 	} elsif ($tmp =~ /^(error)/) {
11874: 	    $result=undef;
11875: 	    &do_cache_new('courseres',$hashid,$result,600);
11876: 	}
11877:     }
11878:     return $result;
11879: }
11880: 
11881: sub devalidateuserresdata {
11882:     my ($uname,$udom)=@_;
11883:     my $hashid="$udom:$uname";
11884:     &devalidate_cache_new('userres',$hashid);
11885: }
11886: 
11887: sub get_userresdata {
11888:     my ($uname,$udom)=@_;
11889:     #most student don\'t have any data set, check if there is some data
11890:     if (&EXT_cache_status($udom,$uname)) { return undef; }
11891: 
11892:     my $hashid="$udom:$uname";
11893:     my ($result,$cached)=&is_cached_new('userres',$hashid);
11894:     if (!defined($cached)) {
11895: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
11896: 	$result=\%resourcedata;
11897: 	&do_cache_new('userres',$hashid,$result,600);
11898:     }
11899:     my ($tmp)=keys(%$result);
11900:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
11901: 	return $result;
11902:     }
11903:     #error 2 occurs when the .db doesn't exist
11904:     if ($tmp!~/error: 2 /) {
11905:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
11906: 	    &logthis("<font color=\"blue\">WARNING:".
11907: 		     " Trying to get resource data for ".
11908: 		     $uname." at ".$udom.": ".
11909: 		     $tmp."</font>");
11910:         }
11911:     } elsif ($tmp=~/error: 2 /) {
11912: 	#&EXT_cache_set($udom,$uname);
11913: 	&do_cache_new('userres',$hashid,undef,600);
11914: 	undef($tmp); # not really an error so don't send it back
11915:     }
11916:     return $tmp;
11917: }
11918: #----------------------------------------------- resdata - return resource data
11919: #  Purpose:
11920: #    Return resource data for either users or for a course.
11921: #  Parameters:
11922: #     $name      - Course/user name.
11923: #     $domain    - Name of the domain the user/course is registered on.
11924: #     $type      - Type of thing $name is (must be 'course' or 'user')
11925: #     $mapp      - decluttered URL of enclosing map  
11926: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
11927: #     $recurseup - Ref to array of map URLs, starting with map containing
11928: #                  $mapp up through hierarchy of nested maps to top level map.  
11929: #     $courseid  - CourseID (first part of param identifier).
11930: #     $modifier  - Middle part of param identifier.
11931: #     $what      - Last part of param identifier.
11932: #     @which     - Array of names of resources desired.
11933: #  Returns:
11934: #     The value of the first reasource in @which that is found in the
11935: #     resource hash.
11936: #  Exceptional Conditions:
11937: #     If the $type passed in is not valid (not the string 'course' or 
11938: #     'user', an undefined  reference is returned.
11939: #     If none of the resources are found, an undef is returned
11940: sub resdata {
11941:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
11942:         $modifier,$what,@which)=@_;
11943:     my $result;
11944:     if ($type eq 'course') {
11945: 	$result=&get_courseresdata($name,$domain);
11946:     } elsif ($type eq 'user') {
11947: 	$result=&get_userresdata($name,$domain);
11948:     }
11949:     if (!ref($result)) { return $result; }    
11950:     foreach my $item (@which) {
11951:         if ($item->[1] eq 'course') {
11952:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
11953:                 unless ($$recursed) {
11954:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
11955:                     $$recursed = 1;
11956:                 }
11957:                 foreach my $item (@${recurseup}) {
11958:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
11959:                     last if (defined($result->{$norecursechk}));
11960:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
11961:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
11962:                 }
11963:             }
11964:         }
11965:         if (defined($result->{$item->[0]})) {
11966: 	    return [$result->{$item->[0]},$item->[1]];
11967: 	}
11968:     }
11969:     return undef;
11970: }
11971: 
11972: sub get_domain_lti {
11973:     my ($cdom,$context) = @_;
11974:     my ($name,%lti);
11975:     if ($context eq 'consumer') {
11976:         $name = 'ltitools';
11977:     } elsif ($context eq 'provider') {
11978:         $name = 'lti';
11979:     } else {
11980:         return %lti;
11981:     }
11982:     my ($result,$cached)=&is_cached_new($name,$cdom);
11983:     if (defined($cached)) {
11984:         if (ref($result) eq 'HASH') {
11985:             %lti = %{$result};
11986:         }
11987:     } else {
11988:         my %domconfig = &get_dom('configuration',[$name],$cdom);
11989:         if (ref($domconfig{$name}) eq 'HASH') {
11990:             %lti = %{$domconfig{$name}};
11991:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
11992:             if (ref($encdomconfig{$name}) eq 'HASH') {
11993:                 foreach my $id (keys(%lti)) {
11994:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
11995:                         foreach my $item ('key','secret') {
11996:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
11997:                         }
11998:                     }
11999:                 }
12000:             }
12001:         }
12002:         my $cachetime = 24*60*60;
12003:         &do_cache_new($name,$cdom,\%lti,$cachetime);
12004:     }
12005:     return %lti;
12006: }
12007: 
12008: sub get_numsuppfiles {
12009:     my ($cnum,$cdom,$ignorecache)=@_;
12010:     my $hashid=$cnum.':'.$cdom;
12011:     my ($suppcount,$cached);
12012:     unless ($ignorecache) {
12013:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
12014:     }
12015:     unless (defined($cached)) {
12016:         my $chome=&homeserver($cnum,$cdom);
12017:         unless ($chome eq 'no_host') {
12018:             ($suppcount,my $supptools,my $errors) = (0,0,0);
12019:             my $suppmap = 'supplemental.sequence';
12020:             ($suppcount,$supptools,$errors) =
12021:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
12022:                                                          $supptools,$errors);
12023:         }
12024:         &do_cache_new('suppcount',$hashid,$suppcount,600);
12025:     }
12026:     return $suppcount;
12027: }
12028: 
12029: #
12030: # EXT resource caching routines
12031: #
12032: 
12033: {
12034: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12035: #
12036: # The course for which we cache
12037: my $cachedmapkey='';
12038: # The cached recursive maps for this course
12039: my %cachedmaps=();
12040: # When this was last done
12041: my $cachedmaptime='';
12042: 
12043: sub clear_EXT_cache_status {
12044:     &delenv('cache.EXT.');
12045: }
12046: 
12047: sub EXT_cache_status {
12048:     my ($target_domain,$target_user) = @_;
12049:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12050:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12051:         # We know already the user has no data
12052:         return 1;
12053:     } else {
12054:         return 0;
12055:     }
12056: }
12057: 
12058: sub EXT_cache_set {
12059:     my ($target_domain,$target_user) = @_;
12060:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12061:     #&appenv({$cachename => time});
12062: }
12063: 
12064: # --------------------------------------------------------- Value of a Variable
12065: sub EXT {
12066: 
12067:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
12068:     unless ($varname) { return ''; }
12069:     #get real user name/domain, courseid and symb
12070:     my $courseid;
12071:     my $publicuser;
12072:     if ($symbparm) {
12073: 	$symbparm=&get_symb_from_alias($symbparm);
12074:     }
12075:     if (!($uname && $udom)) {
12076:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12077:       if (!$symbparm) {	$symbparm=$cursymb; }
12078:     } else {
12079: 	$courseid=$env{'request.course.id'};
12080:     }
12081:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12082:     my $rest;
12083:     if (defined($therest[0])) {
12084:        $rest=join('.',@therest);
12085:     } else {
12086:        $rest='';
12087:     }
12088: 
12089:     my $qualifierrest=$qualifier;
12090:     if ($rest) { $qualifierrest.='.'.$rest; }
12091:     my $spacequalifierrest=$space;
12092:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12093:     if ($realm eq 'user') {
12094: # --------------------------------------------------------------- user.resource
12095: 	if ($space eq 'resource') {
12096: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12097: 		  || defined($Apache::lonhomework::parsing_a_task))
12098: 		 &&
12099: 		 ($symbparm eq &symbread()) ) {	
12100: 		# if we are in the middle of processing the resource the
12101: 		# get the value we are planning on committing
12102:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12103:                     return $Apache::lonhomework::results{$qualifierrest};
12104:                 } else {
12105:                     return $Apache::lonhomework::history{$qualifierrest};
12106:                 }
12107: 	    } else {
12108: 		my %restored;
12109: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12110: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12111: 		} else {
12112: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12113: 		}
12114: 		return $restored{$qualifierrest};
12115: 	    }
12116: # ----------------------------------------------------------------- user.access
12117:         } elsif ($space eq 'access') {
12118: 	    # FIXME - not supporting calls for a specific user
12119:             return &allowed($qualifier,$rest);
12120: # ------------------------------------------ user.preferences, user.environment
12121:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12122: 	    if (($uname eq $env{'user.name'}) &&
12123: 		($udom eq $env{'user.domain'})) {
12124: 		return $env{join('.',('environment',$qualifierrest))};
12125: 	    } else {
12126: 		my %returnhash;
12127: 		if (!$publicuser) {
12128: 		    %returnhash=&userenvironment($udom,$uname,
12129: 						 $qualifierrest);
12130: 		}
12131: 		return $returnhash{$qualifierrest};
12132: 	    }
12133: # ----------------------------------------------------------------- user.course
12134:         } elsif ($space eq 'course') {
12135: 	    # FIXME - not supporting calls for a specific user
12136:             return $env{join('.',('request.course',$qualifier))};
12137: # ------------------------------------------------------------------- user.role
12138:         } elsif ($space eq 'role') {
12139: 	    # FIXME - not supporting calls for a specific user
12140:             my ($role,$where)=split(/\./,$env{'request.role'});
12141:             if ($qualifier eq 'value') {
12142: 		return $role;
12143:             } elsif ($qualifier eq 'extent') {
12144:                 return $where;
12145:             }
12146: # ----------------------------------------------------------------- user.domain
12147:         } elsif ($space eq 'domain') {
12148:             return $udom;
12149: # ------------------------------------------------------------------- user.name
12150:         } elsif ($space eq 'name') {
12151:             return $uname;
12152: # ---------------------------------------------------- Any other user namespace
12153:         } else {
12154: 	    my %reply;
12155: 	    if (!$publicuser) {
12156: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12157: 	    }
12158: 	    return $reply{$qualifierrest};
12159:         }
12160:     } elsif ($realm eq 'query') {
12161: # ---------------------------------------------- pull stuff out of query string
12162:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12163: 						[$spacequalifierrest]);
12164: 	return $env{'form.'.$spacequalifierrest}; 
12165:    } elsif ($realm eq 'request') {
12166: # ------------------------------------------------------------- request.browser
12167:         if ($space eq 'browser') {
12168:             return $env{'browser.'.$qualifier};
12169: # ------------------------------------------------------------ request.filename
12170:         } else {
12171:             return $env{'request.'.$spacequalifierrest};
12172:         }
12173:     } elsif ($realm eq 'course') {
12174: # ---------------------------------------------------------- course.description
12175:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12176:     } elsif ($realm eq 'resource') {
12177: 
12178: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12179: 	    if (!$symbparm) { $symbparm=&symbread(); }
12180: 	}
12181: 
12182:         if ($qualifier eq '') {
12183: 	    if ($space eq 'title') {
12184: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12185: 	        return &gettitle($symbparm);
12186: 	    }
12187: 	
12188: 	    if ($space eq 'map') {
12189: 	        my ($map) = &decode_symb($symbparm);
12190: 	        return &symbread($map);
12191: 	    }
12192:             if ($space eq 'maptitle') {
12193:                 my ($map) = &decode_symb($symbparm);
12194:                 return &gettitle($map);
12195:             }
12196: 	    if ($space eq 'filename') {
12197: 	        if ($symbparm) {
12198: 		    return &clutter((&decode_symb($symbparm))[2]);
12199: 	        }
12200: 	        return &hreflocation('',$env{'request.filename'});
12201: 	    }
12202: 
12203:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12204:                 if ($space eq 'visibleparts') {
12205:                     my $navmap = Apache::lonnavmaps::navmap->new();
12206:                     my $item;
12207:                     if (ref($navmap)) {
12208:                         my $res = $navmap->getBySymb($symbparm);
12209:                         my $parts = $res->parts();
12210:                         if (ref($parts) eq 'ARRAY') {
12211:                             $item = join(',',@{$parts});
12212:                         }
12213:                         undef($navmap);
12214:                     }
12215:                     return $item;
12216:                 }
12217:             }
12218:         }
12219: 
12220: 	my ($section, $group, @groups, @recurseup, $recursed);
12221: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12222:         if (($courseid eq '') && ($cid)) {
12223:             $courseid = $cid;
12224:         }
12225: 	if (($symbparm && $courseid) && 
12226: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12227: 
12228: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12229: 
12230: # ----------------------------------------------------- Cascading lookup scheme
12231: 	    my $symbp=$symbparm;
12232: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12233: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12234:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12235: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12236: 	    if (($env{'user.name'} eq $uname) &&
12237: 		($env{'user.domain'} eq $udom)) {
12238: 		$section=$env{'request.course.sec'};
12239:                 @groups = split(/:/,$env{'request.course.groups'});  
12240:                 @groups=&sort_course_groups($courseid,@groups); 
12241: 	    } else {
12242: 		if (! defined($usection)) {
12243: 		    $section=&getsection($udom,$uname,$courseid);
12244: 		} else {
12245: 		    $section = $usection;
12246: 		}
12247:                 @groups = &get_users_groups($udom,$uname,$courseid);
12248: 	    }
12249: 
12250: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12251: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12252:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12253: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12254: 
12255: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12256: 	    my $courselevelr=$courseid.'.'.$symbparm;
12257:             $courseleveli=$courseid.'.'.$recurseparm;
12258: 	    $courselevelm=$courseid.'.'.$mapparm;
12259: 
12260: # ----------------------------------------------------------- first, check user
12261: 
12262: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12263:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12264: 				       ([$courselevelr,'resource'],
12265: 					[$courselevelm,'map'     ],
12266:                                         [$courseleveli,'map'     ],
12267: 					[$courselevel, 'course'  ]));
12268: 	    if (defined($userreply)) { return &get_reply($userreply); }
12269: 
12270: # ------------------------------------------------ second, check some of course
12271:             my $coursereply;
12272:             if (@groups > 0) {
12273:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12274:                                        $recurseparm,$mapparm,$spacequalifierrest,
12275:                                        $mapp,\$recursed,\@recurseup);
12276:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12277:             }
12278: 
12279: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12280: 				  $env{'course.'.$courseid.'.domain'},
12281: 				  'course',$mapp,\$recursed,\@recurseup,
12282:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12283: 				  ([$seclevelr,   'resource'],
12284: 				   [$seclevelm,   'map'     ],
12285:                                    [$secleveli,   'map'     ],
12286: 				   [$seclevel,    'course'  ],
12287: 				   [$courselevelr,'resource']));
12288: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12289: 
12290: # ------------------------------------------------------ third, check map parms
12291: 	    my %parmhash=();
12292: 	    my $thisparm='';
12293: 	    if (tie(%parmhash,'GDBM_File',
12294: 		    $env{'request.course.fn'}.'_parms.db',
12295: 		    &GDBM_READER(),0640)) {
12296: 		$thisparm=$parmhash{$symbparm};
12297: 		untie(%parmhash);
12298: 	    }
12299: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12300: 	}
12301: # ------------------------------------------ fourth, look in resource metadata
12302:  
12303:         my $what = $spacequalifierrest;
12304: 	$what=~s/\./\_/;
12305: 	my $filename;
12306: 	if (!$symbparm) { $symbparm=&symbread(); }
12307: 	if ($symbparm) {
12308: 	    $filename=(&decode_symb($symbparm))[2];
12309: 	} else {
12310: 	    $filename=$env{'request.filename'};
12311: 	}
12312:         my $toolsymb;
12313:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12314:             $toolsymb = $symbparm;
12315:         }
12316: 	my $metadata=&metadata($filename,$what,$toolsymb);
12317: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12318: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12319: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12320: 
12321: # ----------------------------------------------- fifth, look in rest of course
12322: 	if ($symbparm && defined($courseid) && 
12323: 	    $courseid eq $env{'request.course.id'}) {
12324: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12325: 				     $env{'course.'.$courseid.'.domain'},
12326: 				     'course',$mapp,\$recursed,\@recurseup,
12327:                                      $courseid,'.',$spacequalifierrest,
12328: 				     ([$courselevelm,'map'   ],
12329:                                       [$courseleveli,'map'   ],
12330: 				      [$courselevel, 'course']));
12331: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12332: 	}
12333: # ------------------------------------------------------------------ Cascade up
12334: 	unless ($space eq '0') {
12335: 	    my @parts=split(/_/,$space);
12336: 	    my $id=pop(@parts);
12337: 	    my $part=join('_',@parts);
12338: 	    if ($part eq '') { $part='0'; }
12339: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12340: 				 $symbparm,$udom,$uname,$section,1);
12341: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12342: 	}
12343: 	if ($recurse) { return undef; }
12344: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12345: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12346: # ---------------------------------------------------- Any other user namespace
12347:     } elsif ($realm eq 'environment') {
12348: # ----------------------------------------------------------------- environment
12349: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12350: 	    return $env{'environment.'.$spacequalifierrest};
12351: 	} else {
12352: 	    if ($uname eq 'anonymous' && $udom eq '') {
12353: 		return '';
12354: 	    }
12355: 	    my %returnhash=&userenvironment($udom,$uname,
12356: 					    $spacequalifierrest);
12357: 	    return $returnhash{$spacequalifierrest};
12358: 	}
12359:     } elsif ($realm eq 'system') {
12360: # ----------------------------------------------------------------- system.time
12361: 	if ($space eq 'time') {
12362: 	    return time;
12363:         }
12364:     } elsif ($realm eq 'server') {
12365: # ----------------------------------------------------------------- system.time
12366: 	if ($space eq 'name') {
12367: 	    return $ENV{'SERVER_NAME'};
12368:         }
12369:     } elsif ($realm eq 'client') {
12370:         if ($space eq 'remote_addr') {
12371:             return $ENV{'REMOTE_ADDR'};
12372:         }
12373:     }
12374:     return '';
12375: }
12376: 
12377: sub get_reply {
12378:     my ($reply_value) = @_;
12379:     if (ref($reply_value) eq 'ARRAY') {
12380:         if (wantarray) {
12381: 	    return @$reply_value;
12382:         }
12383:         return $reply_value->[0];
12384:     } else {
12385:         return $reply_value;
12386:     }
12387: }
12388: 
12389: sub check_group_parms {
12390:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12391:         $recursed,$recurseupref) = @_;
12392:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12393:                   [$what,'course']);
12394:     my $coursereply;
12395:     foreach my $group (@{$groups}) {
12396:         my @groupitems = ();
12397:         foreach my $level (@levels) {
12398:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12399:              push(@groupitems,[$item,$level->[1]]);
12400:         }
12401:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12402:                                    $env{'course.'.$courseid.'.domain'},
12403:                                    'course',$mapp,$recursed,$recurseupref,
12404:                                    $courseid,'.['.$group.'].',$what,
12405:                                    @groupitems);
12406:         last if (defined($coursereply));
12407:     }
12408:     return $coursereply;
12409: }
12410: 
12411: sub get_map_hierarchy {
12412:     my ($mapname,$courseid) = @_;
12413:     my @recurseup = ();
12414:     if ($mapname) {
12415:         if (($cachedmapkey eq $courseid) &&
12416:             (abs($cachedmaptime-time)<5)) {
12417:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12418:                 return @{$cachedmaps{$mapname}};
12419:             }
12420:         }
12421:         my $navmap = Apache::lonnavmaps::navmap->new();
12422:         if (ref($navmap)) {
12423:             @recurseup = $navmap->recurseup_maps($mapname);
12424:             undef($navmap);
12425:             $cachedmaps{$mapname} = \@recurseup;
12426:             $cachedmaptime=time;
12427:             $cachedmapkey=$courseid;
12428:         }
12429:     }
12430:     return @recurseup;
12431: }
12432: 
12433: }
12434: 
12435: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12436:     my ($courseid,@groups) = @_;
12437:     @groups = sort(@groups);
12438:     return @groups;
12439: }
12440: 
12441: sub packages_tab_default {
12442:     my ($uri,$varname,$toolsymb)=@_;
12443:     my (undef,$part,$name)=split(/\./,$varname);
12444: 
12445:     my (@extension,@specifics,$do_default);
12446:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12447: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12448: 	if ($pack_type eq 'default') {
12449: 	    $do_default=1;
12450: 	} elsif ($pack_type eq 'extension') {
12451: 	    push(@extension,[$package,$pack_type,$pack_part]);
12452: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12453: 	    # only look at packages defaults for packages that this id is
12454: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12455: 	}
12456:     }
12457:     # first look for a package that matches the requested part id
12458:     foreach my $package (@specifics) {
12459: 	my (undef,$pack_type,$pack_part)=@{$package};
12460: 	next if ($pack_part ne $part);
12461: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12462: 	    return $packagetab{"$pack_type&$name&default"};
12463: 	}
12464:     }
12465:     # look for any possible matching non extension_ package
12466:     foreach my $package (@specifics) {
12467: 	my (undef,$pack_type,$pack_part)=@{$package};
12468: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12469: 	    return $packagetab{"$pack_type&$name&default"};
12470: 	}
12471: 	if ($pack_type eq 'part') { $pack_part='0'; }
12472: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12473: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12474: 	}
12475:     }
12476:     # look for any posible extension_ match
12477:     foreach my $package (@extension) {
12478: 	my ($package,$pack_type)=@{$package};
12479: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12480: 	    return $packagetab{"$pack_type&$name&default"};
12481: 	}
12482: 	if (defined($packagetab{$package."&$name&default"})) {
12483: 	    return $packagetab{$package."&$name&default"};
12484: 	}
12485:     }
12486:     # look for a global default setting
12487:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12488: 	return $packagetab{"default&$name&default"};
12489:     }
12490:     return undef;
12491: }
12492: 
12493: sub add_prefix_and_part {
12494:     my ($prefix,$part)=@_;
12495:     my $keyroot;
12496:     if (defined($prefix) && $prefix !~ /^__/) {
12497: 	# prefix that has a part already
12498: 	$keyroot=$prefix;
12499:     } elsif (defined($prefix)) {
12500: 	# prefix that is missing a part
12501: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12502:     } else {
12503: 	# no prefix at all
12504: 	if (defined($part)) { $keyroot='_'.$part; }
12505:     }
12506:     return $keyroot;
12507: }
12508: 
12509: # ---------------------------------------------------------------- Get metadata
12510: 
12511: my %metaentry;
12512: my %importedpartids;
12513: my %importedrespids;
12514: sub metadata {
12515:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12516:     $uri=&declutter($uri);
12517:     # if it is a non metadata possible uri return quickly
12518:     if (($uri eq '') || 
12519: 	(($uri =~ m|^/*adm/|) && 
12520: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12521:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12522: 	return undef;
12523:     }
12524:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12525: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12526: 	return undef;
12527:     }
12528:     my $filename=$uri;
12529:     $uri=~s/\.meta$//;
12530: #
12531: # Is the metadata already cached?
12532: # Look at timestamp of caching
12533: # Everything is cached by the main uri, libraries are never directly cached
12534: #
12535:     if (!defined($liburi)) {
12536: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12537: 	if (defined($cached)) { return $result->{':'.$what}; }
12538:     }
12539: 
12540: #
12541: # If the uri is for an external tool the file from
12542: # which metadata should be retrieved depends on whether
12543: # the tool had been configured to be gradable (set in the Course
12544: # Editor or Resource Editor).
12545: #
12546: # If a valid symb has been included as the third arg in the call
12547: # to &metadata() that can be used to retrieve the value of
12548: # parameter_0_gradable set for the resource, and included in the
12549: # uploaded map containing the tool. The value is retrieved via
12550: # &EXT(), if a valid symb is available.  Otherwise the value of
12551: # gradable in the exttool_$marker.db file for the tool instance
12552: # is retrieved via &get().
12553: #
12554: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12555: # hiddenresource and encrypturl (during course initialization)
12556: # the map-level parameter for resource.0.gradable included in the 
12557: # uploaded map containing the tool will not yet have been stored
12558: # in the user_course_parms.db file for the user's session, so in 
12559: # this case fall back to retrieving gradable status from the
12560: # exttool_$marker.db file.
12561: #
12562: # In order to avoid an infinite loop, &metadata() will return
12563: # before a call to &EXT(), if the uri is for an external tool
12564: # and the $what for which metadata is being requested is
12565: # parameter_0_gradable or 0_gradable.
12566: #
12567: 
12568:     if ($uri =~ /ext\.tool$/) {
12569:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12570:             return;
12571:         } else {
12572:             my ($checked,$use_passback);
12573:             if ($toolsymb ne '') {
12574:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12575:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12576:                     $checked = 1;
12577:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12578:                         $use_passback = 1;
12579:                     }
12580:                 }
12581:             }
12582:             unless ($checked) {
12583:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12584:                 $marker=~s/\D//g;
12585:                 if ($marker) {
12586:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12587:                     $use_passback = $toolsettings{'gradable'};
12588:                 }
12589:             }
12590:             if ($use_passback) {
12591:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12592:             } else {
12593:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12594:             }
12595:         }
12596:     }
12597: 
12598:     {
12599: # Imported parts would go here
12600:         my @origfiletagids=();
12601:         my $importedparts=0;
12602: 
12603: # Imported responseids would go here
12604:         my $importedresponses=0;
12605: #
12606: # Is this a recursive call for a library?
12607: #
12608: #	if (! exists($metacache{$uri})) {
12609: #	    $metacache{$uri}={};
12610: #	}
12611: 	my $cachetime = 60*60;
12612:         if ($liburi) {
12613: 	    $liburi=&declutter($liburi);
12614:             $filename=$liburi;
12615:         } else {
12616: 	    &devalidate_cache_new('meta',$uri);
12617: 	    undef(%metaentry);
12618: 	}
12619:         my %metathesekeys=();
12620:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12621: 	my $metastring;
12622: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12623: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12624: 	    $metastring = 
12625: 		&Apache::lonnet::ssi_body($which,
12626: 					  ('grade_target' => 'meta'));
12627: 	    $cachetime = 1; # only want this cached in the child not long term
12628: 	} elsif (($uri !~ m -^(editupload)/-) && 
12629:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12630: 	    my $file=&filelocation('',&clutter($filename));
12631: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12632: 	    $metastring=&getfile($file);
12633: 	}
12634:         my $parser=HTML::LCParser->new(\$metastring);
12635:         my $token;
12636:         undef %metathesekeys;
12637:         while ($token=$parser->get_token) {
12638: 	    if ($token->[0] eq 'S') {
12639: 		if (defined($token->[2]->{'package'})) {
12640: #
12641: # This is a package - get package info
12642: #
12643: 		    my $package=$token->[2]->{'package'};
12644: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12645: 		    if (defined($token->[2]->{'id'})) { 
12646: 			$keyroot.='_'.$token->[2]->{'id'}; 
12647: 		    }
12648: 		    if ($metaentry{':packages'}) {
12649: 			$metaentry{':packages'}.=','.$package.$keyroot;
12650: 		    } else {
12651: 			$metaentry{':packages'}=$package.$keyroot;
12652: 		    }
12653: 		    foreach my $pack_entry (keys(%packagetab)) {
12654: 			my $part=$keyroot;
12655: 			$part=~s/^\_//;
12656: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12657: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12658: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12659: 			    # ignore package.tab specified default values
12660:                             # here &package_tab_default() will fetch those
12661: 			    if ($subp eq 'default') { next; }
12662: 			    my $value=$packagetab{$pack_entry};
12663: 			    my $unikey;
12664: 			    if ($pack =~ /_0$/) {
12665: 				$unikey='parameter_0_'.$name;
12666: 				$part=0;
12667: 			    } else {
12668: 				$unikey='parameter'.$keyroot.'_'.$name;
12669: 			    }
12670: 			    if ($subp eq 'display') {
12671: 				$value.=' [Part: '.$part.']';
12672: 			    }
12673: 			    $metaentry{':'.$unikey.'.part'}=$part;
12674: 			    $metathesekeys{$unikey}=1;
12675: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12676: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12677: 			    }
12678: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12679: 				$metaentry{':'.$unikey}=
12680: 				    $metaentry{':'.$unikey.'.default'};
12681: 			    }
12682: 			}
12683: 		    }
12684: 		} else {
12685: #
12686: # This is not a package - some other kind of start tag
12687: #
12688: 		    my $entry=$token->[1];
12689: 		    my $unikey='';
12690: 
12691: 		    if ($entry eq 'import') {
12692: #
12693: # Importing a library here
12694: #
12695:                         my $location=$parser->get_text('/import');
12696:                         my $dir=$filename;
12697:                         $dir=~s|[^/]*$||;
12698:                         $location=&filelocation($dir,$location);
12699: 
12700:                         my $importid=$token->[2]->{'id'};
12701:                         my $importmode=$token->[2]->{'importmode'};
12702: #
12703: # Check metadata for imported file to
12704: # see if it contained response items
12705: #
12706:                         my ($origfile,@libfilekeys);
12707:                         my %currmetaentry = %metaentry;
12708:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12709:                                                            $depthcount+1));
12710:                         if (grep(/^responseorder$/,@libfilekeys)) {
12711:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12712:                                                              undef,$depthcount+1);
12713:                             if ($libresponseorder ne '') {
12714:                                 if ($#origfiletagids<0) {
12715:                                     undef(%importedrespids);
12716:                                     undef(%importedpartids);
12717:                                 }
12718:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12719:                                 if (@respids) {
12720:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12721:                                 }
12722:                                 if ($importedrespids{$importid} ne '') {
12723:                                     $importedresponses = 1;
12724: # We need to get the original file and the imported file to get the response order correct
12725: # Load and inspect original file
12726:                                     if ($#origfiletagids<0) {
12727:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12728:                                         $origfile=&getfile($origfilelocation);
12729:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12730:                                     }
12731:                                 }
12732:                             }
12733:                         }
12734: # Do not overwrite contents of %metaentry hash for resource itself with 
12735: # hash populated for imported library file
12736:                         %metaentry = %currmetaentry;
12737:                         undef(%currmetaentry);
12738:                         if ($importmode eq 'part') {
12739: # Import as part(s)
12740:                            $importedparts=1;
12741: # We need to get the original file and the imported file to get the part order correct
12742: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
12743: # Load and inspect original file if we didn't do that already
12744:                            if ($#origfiletagids<0) {
12745:                                undef(%importedrespids);
12746:                                undef(%importedpartids);
12747:                                if ($origfile eq '') {
12748:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12749:                                    $origfile=&getfile($origfilelocation);
12750:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12751:                                }
12752:                            }
12753:                            my @impfilepartids;
12754: # If <partorder> tag is included in metadata for the imported file
12755: # get the parts in the imported file from that.
12756:                            if (grep(/^partorder$/,@libfilekeys)) {
12757:                                %currmetaentry = %metaentry;
12758:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12759:                                                             $depthcount+1);
12760:                                %metaentry = %currmetaentry;
12761:                                undef(%currmetaentry);
12762:                                if ($libpartorder ne '') {
12763:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
12764:                                }
12765:                            } else {
12766: # If no <partorder> tag available, load and inspect imported file
12767:                                my $impfile=&getfile($location);
12768:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12769:                            }
12770:                            if ($#impfilepartids>=0) {
12771: # This problem had parts
12772:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
12773:                            } else {
12774: # Importing by turning a single problem into a problem part
12775: # It gets the import-tags ID as part-ID
12776:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
12777:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
12778:                            }
12779:                         } else {
12780: # Import as problem or as normal import
12781:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12782:                             unless ($importmode eq 'problem') {
12783: # Normal import
12784:                                 if (defined($token->[2]->{'id'})) {
12785:                                     $unikey.='_'.$token->[2]->{'id'};
12786:                                 }
12787:                             }
12788: # Check metadata for imported file to
12789: # see if it contained parts
12790:                             if (grep(/^partorder$/,@libfilekeys)) {
12791:                                 %currmetaentry = %metaentry;
12792:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12793:                                                              $depthcount+1);
12794:                                 %metaentry = %currmetaentry;
12795:                                 undef(%currmetaentry);
12796:                                 if ($libpartorder ne '') {
12797:                                     $importedparts = 1;
12798:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
12799:                                 }
12800:                             }
12801:                         }
12802: 			if ($depthcount<20) {
12803: 			    my $metadata = 
12804: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
12805: 					  $depthcount+1);
12806: 			    foreach my $meta (split(',',$metadata)) {
12807: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
12808: 				$metathesekeys{$meta}=1;
12809: 			    }
12810:                         }
12811: 		    } else {
12812: #
12813: # Not importing, some other kind of non-package, non-library start tag
12814: # 
12815:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
12816:                         if (defined($token->[2]->{'id'})) {
12817:                             $unikey.='_'.$token->[2]->{'id'};
12818:                         }
12819: 			if (defined($token->[2]->{'name'})) { 
12820: 			    $unikey.='_'.$token->[2]->{'name'}; 
12821: 			}
12822: 			$metathesekeys{$unikey}=1;
12823: 			foreach my $param (@{$token->[3]}) {
12824: 			    $metaentry{':'.$unikey.'.'.$param} =
12825: 				$token->[2]->{$param};
12826: 			}
12827: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
12828: 			my $default=$metaentry{':'.$unikey.'.default'};
12829: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
12830: 		 # only ws inside the tag, and not in default, so use default
12831: 		 # as value
12832: 			    $metaentry{':'.$unikey}=$default;
12833: 			} elsif ( $internaltext =~ /\S/ ) {
12834: 		  # something interesting inside the tag
12835: 			    $metaentry{':'.$unikey}=$internaltext;
12836: 			} else {
12837: 		  # no interesting values, don't set a default
12838: 			}
12839: # end of not-a-package not-a-library import
12840: 		    }
12841: # end of not-a-package start tag
12842: 		}
12843: # the next is the end of "start tag"
12844: 	    }
12845: 	}
12846: 	my ($extension) = ($uri =~ /\.(\w+)$/);
12847: 	$extension = lc($extension);
12848: 	if ($extension eq 'htm') { $extension='html'; }
12849: 
12850: 	foreach my $key (keys(%packagetab)) {
12851: 	    #no specific packages #how's our extension
12852: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
12853: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
12854: 					 \%metathesekeys);
12855: 	}
12856: 
12857: 	if (!exists($metaentry{':packages'})
12858: 	    || $packagetab{"import_defaults&extension_$extension"}) {
12859: 	    foreach my $key (keys(%packagetab)) {
12860: 		#no specific packages well let's get default then
12861: 		if ($key!~/^default&/) { next; }
12862: 		&metadata_create_package_def($uri,$key,'default',
12863: 					     \%metathesekeys);
12864: 	    }
12865: 	}
12866: # are there custom rights to evaluate
12867: 	if ($metaentry{':copyright'} eq 'custom') {
12868: 
12869:     #
12870:     # Importing a rights file here
12871:     #
12872: 	    unless ($depthcount) {
12873: 		my $location=$metaentry{':customdistributionfile'};
12874: 		my $dir=$filename;
12875: 		$dir=~s|[^/]*$||;
12876: 		$location=&filelocation($dir,$location);
12877: 		my $rights_metadata =
12878: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
12879: 			      $depthcount+1);
12880: 		foreach my $rights (split(',',$rights_metadata)) {
12881: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
12882: 		    $metathesekeys{$rights}=1;
12883: 		}
12884: 	    }
12885: 	}
12886: 	# uniqifiy package listing
12887: 	my %seen;
12888: 	my @uniq_packages =
12889: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
12890: 	$metaentry{':packages'} = join(',',@uniq_packages);
12891: 
12892:         if (($importedresponses) || ($importedparts)) {
12893:             if ($importedparts) {
12894: # We had imported parts and need to rebuild partorder
12895:                 $metaentry{':partorder'}='';
12896:                 $metathesekeys{'partorder'}=1;
12897:             }
12898:             if ($importedresponses) {
12899: # We had imported responses and need to rebuil responseorder
12900:                 $metaentry{':responseorder'}='';
12901:                 $metathesekeys{'responseorder'}=1;
12902:             }
12903:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
12904:                 my $origid = $origfiletagids[$index+1];
12905:                 if ($origfiletagids[$index] eq 'part') {
12906: # Original part, part of the problem
12907:                     if ($importedparts) {
12908:                         $metaentry{':partorder'}.=','.$origid;
12909:                     }
12910:                 } elsif ($origfiletagids[$index] eq 'import') {
12911:                     if ($importedparts) {
12912: # We have imported parts at this position
12913:                         if ($importedpartids{$origid} ne '') {
12914:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
12915:                         }
12916:                     }
12917:                     if ($importedresponses) {
12918: # We have imported responses at this position
12919:                         if ($importedrespids{$origid} ne '') {
12920:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
12921:                         }
12922:                     }
12923:                 } else {
12924: # Original response item, part of the problem
12925:                     if ($importedresponses) {
12926:                         $metaentry{':responseorder'}.=','.$origid;
12927:                     }
12928:                 }
12929:             }
12930:             if ($importedparts) {
12931:                 $metaentry{':partorder'}=~s/^\,//;
12932:             }
12933:             if ($importedresponses) {
12934:                 $metaentry{':responseorder'}=~s/^\,//;
12935:             }
12936:         }
12937: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
12938: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
12939: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
12940:         unless ($liburi) {
12941: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
12942:         }
12943: # this is the end of "was not already recently cached
12944:     }
12945:     return $metaentry{':'.$what};
12946: }
12947: 
12948: sub metadata_create_package_def {
12949:     my ($uri,$key,$package,$metathesekeys)=@_;
12950:     my ($pack,$name,$subp)=split(/\&/,$key);
12951:     if ($subp eq 'default') { next; }
12952:     
12953:     if (defined($metaentry{':packages'})) {
12954: 	$metaentry{':packages'}.=','.$package;
12955:     } else {
12956: 	$metaentry{':packages'}=$package;
12957:     }
12958:     my $value=$packagetab{$key};
12959:     my $unikey;
12960:     $unikey='parameter_0_'.$name;
12961:     $metaentry{':'.$unikey.'.part'}=0;
12962:     $$metathesekeys{$unikey}=1;
12963:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12964: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
12965:     }
12966:     if (defined($metaentry{':'.$unikey.'.default'})) {
12967: 	$metaentry{':'.$unikey}=
12968: 	    $metaentry{':'.$unikey.'.default'};
12969:     }
12970: }
12971: 
12972: sub metadata_generate_part0 {
12973:     my ($metadata,$metacache,$uri) = @_;
12974:     my %allnames;
12975:     foreach my $metakey (keys(%$metadata)) {
12976: 	if ($metakey=~/^parameter\_(.*)/) {
12977: 	  my $part=$$metacache{':'.$metakey.'.part'};
12978: 	  my $name=$$metacache{':'.$metakey.'.name'};
12979: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
12980: 	    $allnames{$name}=$part;
12981: 	  }
12982: 	}
12983:     }
12984:     foreach my $name (keys(%allnames)) {
12985:       $$metadata{"parameter_0_$name"}=1;
12986:       my $key=":parameter_0_$name";
12987:       $$metacache{"$key.part"}='0';
12988:       $$metacache{"$key.name"}=$name;
12989:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
12990: 					   $allnames{$name}.'_'.$name.
12991: 					   '.type'};
12992:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
12993: 			     '.display'};
12994:       my $expr='[Part: '.$allnames{$name}.']';
12995:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
12996:       $$metacache{"$key.display"}=$olddis;
12997:     }
12998: }
12999: 
13000: # ------------------------------------------------------ Devalidate title cache
13001: 
13002: sub devalidate_title_cache {
13003:     my ($url)=@_;
13004:     if (!$env{'request.course.id'}) { return; }
13005:     my $symb=&symbread($url);
13006:     if (!$symb) { return; }
13007:     my $key=$env{'request.course.id'}."\0".$symb;
13008:     &devalidate_cache_new('title',$key);
13009: }
13010: 
13011: # ------------------------------------------------- Get the title of a course
13012: 
13013: sub current_course_title {
13014:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13015: }
13016: # ------------------------------------------------- Get the title of a resource
13017: 
13018: sub gettitle {
13019:     my $urlsymb=shift;
13020:     my $symb=&symbread($urlsymb);
13021:     if ($symb) {
13022: 	my $key=$env{'request.course.id'}."\0".$symb;
13023: 	my ($result,$cached)=&is_cached_new('title',$key);
13024: 	if (defined($cached)) { 
13025: 	    return $result;
13026: 	}
13027: 	my ($map,$resid,$url)=&decode_symb($symb);
13028: 	my $title='';
13029: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13030: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13031: 	} else {
13032: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13033: 		    &GDBM_READER(),0640)) {
13034: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13035: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13036: 		untie(%bighash);
13037: 	    }
13038: 	}
13039: 	$title=~s/\&colon\;/\:/gs;
13040: 	if ($title) {
13041: # Remember both $symb and $title for dynamic metadata
13042:             $accesshash{$symb.'___crstitle'}=$title;
13043:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13044: # Cache this title and then return it
13045: 	    return &do_cache_new('title',$key,$title,600);
13046: 	}
13047: 	$urlsymb=$url;
13048:     }
13049:     my $title=&metadata($urlsymb,'title');
13050:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13051:     return $title;
13052: }
13053: 
13054: sub get_slot {
13055:     my ($which,$cnum,$cdom)=@_;
13056:     if (!$cnum || !$cdom) {
13057: 	(undef,my $courseid)=&whichuser();
13058: 	$cdom=$env{'course.'.$courseid.'.domain'};
13059: 	$cnum=$env{'course.'.$courseid.'.num'};
13060:     }
13061:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13062:     my %slotinfo;
13063:     if (exists($remembered{$key})) {
13064: 	$slotinfo{$which} = $remembered{$key};
13065:     } else {
13066: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13067: 	&Apache::lonhomework::showhash(%slotinfo);
13068: 	my ($tmp)=keys(%slotinfo);
13069: 	if ($tmp=~/^error:/) { return (); }
13070: 	$remembered{$key} = $slotinfo{$which};
13071:     }
13072:     if (ref($slotinfo{$which}) eq 'HASH') {
13073: 	return %{$slotinfo{$which}};
13074:     }
13075:     return $slotinfo{$which};
13076: }
13077: 
13078: sub get_reservable_slots {
13079:     my ($cnum,$cdom,$uname,$udom) = @_;
13080:     my $now = time;
13081:     my $reservable_info;
13082:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13083:     if (exists($remembered{$key})) {
13084:         $reservable_info = $remembered{$key};
13085:     } else {
13086:         my %resv;
13087:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13088:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13089:         $reservable_info = \%resv;
13090:         $remembered{$key} = $reservable_info;
13091:     }
13092:     return $reservable_info;
13093: }
13094: 
13095: sub get_course_slots {
13096:     my ($cnum,$cdom) = @_;
13097:     my $hashid=$cnum.':'.$cdom;
13098:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13099:     if (defined($cached)) {
13100:         if (ref($result) eq 'HASH') {
13101:             return %{$result};
13102:         }
13103:     } else {
13104:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13105:         my ($tmp) = keys(%slots);
13106:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13107:             &do_cache_new('allslots',$hashid,\%slots,600);
13108:             return %slots;
13109:         }
13110:     }
13111:     return;
13112: }
13113: 
13114: sub devalidate_slots_cache {
13115:     my ($cnum,$cdom)=@_;
13116:     my $hashid=$cnum.':'.$cdom;
13117:     &devalidate_cache_new('allslots',$hashid);
13118: }
13119: 
13120: sub get_coursechange {
13121:     my ($cdom,$cnum) = @_;
13122:     if ($cdom eq '' || $cnum eq '') {
13123:         return unless ($env{'request.course.id'});
13124:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13125:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13126:     }
13127:     my $hashid=$cdom.'_'.$cnum;
13128:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13129:     if ((defined($cached)) && ($change ne '')) {
13130:         return $change;
13131:     } else {
13132:         my %crshash;
13133:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13134:         if ($crshash{'internal.contentchange'} eq '') {
13135:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13136:             if ($change eq '') {
13137:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13138:                 $change = $crshash{'internal.created'};
13139:             }
13140:         } else {
13141:             $change = $crshash{'internal.contentchange'};
13142:         }
13143:         my $cachetime = 600;
13144:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13145:     }
13146:     return $change;
13147: }
13148: 
13149: sub devalidate_coursechange_cache {
13150:     my ($cnum,$cdom)=@_;
13151:     my $hashid=$cnum.':'.$cdom;
13152:     &devalidate_cache_new('crschange',$hashid);
13153: }
13154: 
13155: # ------------------------------------------------- Update symbolic store links
13156: 
13157: sub symblist {
13158:     my ($mapname,%newhash)=@_;
13159:     $mapname=&deversion(&declutter($mapname));
13160:     my %hash;
13161:     if (($env{'request.course.fn'}) && (%newhash)) {
13162:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13163:                       &GDBM_WRCREAT(),0640)) {
13164: 	    foreach my $url (keys(%newhash)) {
13165: 		next if ($url eq 'last_known'
13166: 			 && $env{'form.no_update_last_known'});
13167: 		$hash{declutter($url)}=&encode_symb($mapname,
13168: 						    $newhash{$url}->[1],
13169: 						    $newhash{$url}->[0]);
13170:             }
13171:             if (untie(%hash)) {
13172: 		return 'ok';
13173:             }
13174:         }
13175:     }
13176:     return 'error';
13177: }
13178: 
13179: # --------------------------------------------------------------- Verify a symb
13180: 
13181: sub symbverify {
13182:     my ($symb,$thisurl,$encstate)=@_;
13183:     my $thisfn=$thisurl;
13184:     $thisfn=&declutter($thisfn);
13185: # direct jump to resource in page or to a sequence - will construct own symbs
13186:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13187: # check URL part
13188:     my ($map,$resid,$url)=&decode_symb($symb);
13189: 
13190:     unless ($url eq $thisfn) { return 0; }
13191: 
13192:     $symb=&symbclean($symb);
13193:     $thisurl=&deversion($thisurl);
13194:     $thisfn=&deversion($thisfn);
13195: 
13196:     my %bighash;
13197:     my $okay=0;
13198: 
13199:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13200:                             &GDBM_READER(),0640)) {
13201:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13202:             $thisurl =~ s/\?.+$//;
13203:             if ($map =~ m{^uploaded/.+\.page$}) {
13204:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13205:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13206:             }
13207:         }
13208:         my $ids;
13209:         if ($map =~ m{^uploaded/.+\.page$}) {
13210:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13211:         } else {
13212:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13213:         }
13214:         unless ($ids) {
13215:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13216:             $ids=$bighash{$idkey};
13217:         }
13218:         if ($ids) {
13219: # ------------------------------------------------------------------- Has ID(s)
13220:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13221:                 $symb =~ s/\?.+$//;
13222:             }
13223: 	    foreach my $id (split(/\,/,$ids)) {
13224: 	       my ($mapid,$resid)=split(/\./,$id);
13225:                if (
13226:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13227:    eq $symb) {
13228:                    if (ref($encstate)) {
13229:                        $$encstate = $bighash{'encrypted_'.$id};
13230:                    }
13231: 		   if (($env{'request.role.adv'}) ||
13232: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13233:                        ($thisurl eq '/adm/navmaps')) {
13234: 		       $okay=1;
13235:                        last;
13236: 		   }
13237: 	       }
13238: 	   }
13239:         }
13240: 	untie(%bighash);
13241:     }
13242:     return $okay;
13243: }
13244: 
13245: # --------------------------------------------------------------- Clean-up symb
13246: 
13247: sub symbclean {
13248:     my $symb=shift;
13249:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13250: # remove version from map
13251:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13252: 
13253: # remove version from URL
13254:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13255: 
13256: # remove wrapper
13257: 
13258:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13259:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13260:     return $symb;
13261: }
13262: 
13263: # ---------------------------------------------- Split symb to find map and url
13264: 
13265: sub encode_symb {
13266:     my ($map,$resid,$url)=@_;
13267:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13268: }
13269: 
13270: sub decode_symb {
13271:     my $symb=shift;
13272:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13273:     my ($map,$resid,$url)=split(/___/,$symb);
13274:     return (&fixversion($map),$resid,&fixversion($url));
13275: }
13276: 
13277: sub fixversion {
13278:     my $fn=shift;
13279:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13280:     my %bighash;
13281:     my $uri=&clutter($fn);
13282:     my $key=$env{'request.course.id'}.'_'.$uri;
13283: # is this cached?
13284:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13285:     if (defined($cached)) { return $result; }
13286: # unfortunately not cached, or expired
13287:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13288: 	    &GDBM_READER(),0640)) {
13289:  	if ($bighash{'version_'.$uri}) {
13290:  	    my $version=$bighash{'version_'.$uri};
13291:  	    unless (($version eq 'mostrecent') || 
13292: 		    ($version==&getversion($uri))) {
13293:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13294:  	    }
13295:  	}
13296:  	untie %bighash;
13297:     }
13298:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13299: }
13300: 
13301: sub deversion {
13302:     my $url=shift;
13303:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13304:     return $url;
13305: }
13306: 
13307: # ------------------------------------------------------ Return symb list entry
13308: 
13309: sub symbread {
13310:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
13311:         $ignoresymbdb,$noenccheck)=@_;
13312:     my $cache_str='request.symbread.cached.'.$thisfn;
13313:     if (defined($env{$cache_str})) {
13314:         unless (ref($possibles) eq 'HASH') {
13315:             if ($ignorecachednull) {
13316:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
13317:             } else {
13318:                 return $env{$cache_str};
13319:             }
13320:         }
13321:     }
13322: # no filename provided? try from environment
13323:     unless ($thisfn) {
13324:         if ($env{'request.symb'}) {
13325:             return $env{$cache_str}=&symbclean($env{'request.symb'});
13326: 	}
13327: 	$thisfn=$env{'request.filename'};
13328:     }
13329:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13330: # is that filename actually a symb? Verify, clean, and return
13331:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13332: 	if (&symbverify($thisfn,$1)) {
13333: 	    return $env{$cache_str}=&symbclean($thisfn);
13334: 	}
13335:     }
13336:     $thisfn=declutter($thisfn);
13337:     my %hash;
13338:     my %bighash;
13339:     my $syval='';
13340:     if (($env{'request.course.fn'}) && ($thisfn)) {
13341:         my $targetfn = $thisfn;
13342:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13343:             $targetfn = 'adm/wrapper/'.$thisfn;
13344:         }
13345: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13346: 	    $targetfn=$1;
13347: 	}
13348:         unless ($ignoresymbdb) {
13349:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13350:                           &GDBM_READER(),0640)) {
13351: 	        $syval=$hash{$targetfn};
13352:                 untie(%hash);
13353:             }
13354:             if ($syval && $checkforblock) {
13355:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
13356:                 if (@blockers) {
13357:                     $syval='';
13358:                 }
13359:             }
13360:         }
13361: # ---------------------------------------------------------- There was an entry
13362:         if ($syval) {
13363: 	    #unless ($syval=~/\_\d+$/) {
13364: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13365: 		    #&appenv({'request.ambiguous' => $thisfn});
13366: 		    #return $env{$cache_str}='';
13367: 		#}    
13368: 		#$syval.=$1;
13369: 	    #}
13370:         } else {
13371: # ------------------------------------------------------- Was not in symb table
13372:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13373:                             &GDBM_READER(),0640)) {
13374: # ---------------------------------------------- Get ID(s) for current resource
13375:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13376:               unless ($ids) { 
13377:                  $ids=$bighash{'ids_/'.$thisfn};
13378:               }
13379:               unless ($ids) {
13380: # alias?
13381: 		  $ids=$bighash{'mapalias_'.$thisfn};
13382:               }
13383:               if ($ids) {
13384: # ------------------------------------------------------------------- Has ID(s)
13385:                  my @possibilities=split(/\,/,$ids);
13386:                  if ($#possibilities==0) {
13387: # ----------------------------------------------- There is only one possibility
13388: 		     my ($mapid,$resid)=split(/\./,$ids);
13389: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13390: 						    $resid,$thisfn);
13391:                      if (ref($possibles) eq 'HASH') {
13392:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13393:                              $possibles->{$syval} = 1;
13394:                          }
13395:                      }
13396:                      if ($checkforblock) {
13397:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13398:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
13399:                              if (@blockers) {
13400:                                  $syval = '';
13401:                                  untie(%bighash);
13402:                                  return $env{$cache_str}='';
13403:                              }
13404:                          }
13405:                      }
13406:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13407: # ------------------------------------------ There is more than one possibility
13408:                      my $realpossible=0;
13409:                      foreach my $id (@possibilities) {
13410: 			 my $file=$bighash{'src_'.$id};
13411:                          my $canaccess;
13412:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13413:                              $canaccess = 1;
13414:                          } else { 
13415:                              $canaccess = &allowed('bre',$file);
13416:                          }
13417:                          if ($canaccess) {
13418:          		     my ($mapid,$resid)=split(/\./,$id);
13419:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13420:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13421: 						             $resid,$thisfn);
13422:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
13423:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
13424:                                  if ($checkforblock) {
13425:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
13426:                                      if (@blockers > 0) {
13427:                                          $syval = '';
13428:                                      } else {
13429:                                          $syval = $poss_syval;
13430:                                          $realpossible++;
13431:                                      }
13432:                                  } else {
13433:                                      $syval = $poss_syval;
13434:                                      $realpossible++;
13435:                                  }
13436:                                  if ($syval) {
13437:                                      if (ref($possibles) eq 'HASH') {
13438:                                          $possibles->{$syval} = 1;
13439:                                      }
13440:                                  }
13441:                              }
13442: 			 }
13443:                      }
13444: 		     if ($realpossible!=1) { $syval=''; }
13445:                  } else {
13446:                      $syval='';
13447:                  }
13448: 	      }
13449:               untie(%bighash);
13450:            }
13451:         }
13452:         if ($syval) {
13453: 	    return $env{$cache_str}=$syval;
13454:         }
13455:     }
13456:     &appenv({'request.ambiguous' => $thisfn});
13457:     return $env{$cache_str}='';
13458: }
13459: 
13460: # ---------------------------------------------------------- Return random seed
13461: 
13462: sub numval {
13463:     my $txt=shift;
13464:     $txt=~tr/A-J/0-9/;
13465:     $txt=~tr/a-j/0-9/;
13466:     $txt=~tr/K-T/0-9/;
13467:     $txt=~tr/k-t/0-9/;
13468:     $txt=~tr/U-Z/0-5/;
13469:     $txt=~tr/u-z/0-5/;
13470:     $txt=~s/\D//g;
13471:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13472:     return int($txt);
13473: }
13474: 
13475: sub numval2 {
13476:     my $txt=shift;
13477:     $txt=~tr/A-J/0-9/;
13478:     $txt=~tr/a-j/0-9/;
13479:     $txt=~tr/K-T/0-9/;
13480:     $txt=~tr/k-t/0-9/;
13481:     $txt=~tr/U-Z/0-5/;
13482:     $txt=~tr/u-z/0-5/;
13483:     $txt=~s/\D//g;
13484:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13485:     my $total;
13486:     foreach my $val (@txts) { $total+=$val; }
13487:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13488:     return int($total);
13489: }
13490: 
13491: sub numval3 {
13492:     use integer;
13493:     my $txt=shift;
13494:     $txt=~tr/A-J/0-9/;
13495:     $txt=~tr/a-j/0-9/;
13496:     $txt=~tr/K-T/0-9/;
13497:     $txt=~tr/k-t/0-9/;
13498:     $txt=~tr/U-Z/0-5/;
13499:     $txt=~tr/u-z/0-5/;
13500:     $txt=~s/\D//g;
13501:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13502:     my $total;
13503:     foreach my $val (@txts) { $total+=$val; }
13504:     if ($_64bit) { $total=(($total<<32)>>32); }
13505:     return $total;
13506: }
13507: 
13508: sub digest {
13509:     my ($data)=@_;
13510:     my $digest=&Digest::MD5::md5($data);
13511:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13512:     my ($e,$f);
13513:     {
13514:         use integer;
13515:         $e=($a+$b);
13516:         $f=($c+$d);
13517:         if ($_64bit) {
13518:             $e=(($e<<32)>>32);
13519:             $f=(($f<<32)>>32);
13520:         }
13521:     }
13522:     if (wantarray) {
13523: 	return ($e,$f);
13524:     } else {
13525: 	my $g;
13526: 	{
13527: 	    use integer;
13528: 	    $g=($e+$f);
13529: 	    if ($_64bit) {
13530: 		$g=(($g<<32)>>32);
13531: 	    }
13532: 	}
13533: 	return $g;
13534:     }
13535: }
13536: 
13537: sub latest_rnd_algorithm_id {
13538:     return '64bit5';
13539: }
13540: 
13541: sub get_rand_alg {
13542:     my ($courseid)=@_;
13543:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13544:     if ($courseid) {
13545: 	return $env{"course.$courseid.rndseed"};
13546:     }
13547:     return &latest_rnd_algorithm_id();
13548: }
13549: 
13550: sub validCODE {
13551:     my ($CODE)=@_;
13552:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13553:     return 0;
13554: }
13555: 
13556: sub getCODE {
13557:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13558:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13559: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13560: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13561: 	return $Apache::lonhomework::history{'resource.CODE'};
13562:     }
13563:     return undef;
13564: }
13565: #
13566: #  Determines the random seed for a specific context:
13567: #
13568: # parameters:
13569: #   symb      - in course context the symb for the seed.
13570: #   course_id - The course id of the form domain_coursenum.
13571: #   domain    - Domain for the user.
13572: #   course    - Course for the user.
13573: #   cenv      - environment of the course.
13574: #
13575: # NOTE:
13576: #   All parameters are picked out of the environment if missing
13577: #   or not defined.
13578: #   If a symb cannot be determined the current time is used instead.
13579: #
13580: #  For a given well defined symb, courside, domain, username,
13581: #  and course environment, the seed is reproducible.
13582: #
13583: sub rndseed {
13584:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13585:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13586:     if (!defined($symb)) {
13587: 	unless ($symb=$wsymb) { return time; }
13588:     }
13589:     if (!defined $courseid) { 
13590: 	$courseid=$wcourseid; 
13591:     }
13592:     if (!defined $domain) { $domain=$wdomain; }
13593:     if (!defined $username) { $username=$wusername }
13594: 
13595:     my $which;
13596:     if (defined($cenv->{'rndseed'})) {
13597: 	$which = $cenv->{'rndseed'};
13598:     } else {
13599: 	$which =&get_rand_alg($courseid);
13600:     }
13601:     if (defined(&getCODE())) {
13602: 
13603: 	if ($which eq '64bit5') {
13604: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13605: 	} elsif ($which eq '64bit4') {
13606: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13607: 	} else {
13608: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13609: 	}
13610:     } elsif ($which eq '64bit5') {
13611: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13612:     } elsif ($which eq '64bit4') {
13613: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13614:     } elsif ($which eq '64bit3') {
13615: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13616:     } elsif ($which eq '64bit2') {
13617: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13618:     } elsif ($which eq '64bit') {
13619: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13620:     }
13621:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13622: }
13623: 
13624: sub rndseed_32bit {
13625:     my ($symb,$courseid,$domain,$username)=@_;
13626:     {
13627: 	use integer;
13628: 	my $symbchck=unpack("%32C*",$symb) << 27;
13629: 	my $symbseed=numval($symb) << 22;
13630: 	my $namechck=unpack("%32C*",$username) << 17;
13631: 	my $nameseed=numval($username) << 12;
13632: 	my $domainseed=unpack("%32C*",$domain) << 7;
13633: 	my $courseseed=unpack("%32C*",$courseid);
13634: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13635: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13636: 	#&logthis("rndseed :$num:$symb");
13637: 	if ($_64bit) { $num=(($num<<32)>>32); }
13638: 	return $num;
13639:     }
13640: }
13641: 
13642: sub rndseed_64bit {
13643:     my ($symb,$courseid,$domain,$username)=@_;
13644:     {
13645: 	use integer;
13646: 	my $symbchck=unpack("%32S*",$symb) << 21;
13647: 	my $symbseed=numval($symb) << 10;
13648: 	my $namechck=unpack("%32S*",$username);
13649: 	
13650: 	my $nameseed=numval($username) << 21;
13651: 	my $domainseed=unpack("%32S*",$domain) << 10;
13652: 	my $courseseed=unpack("%32S*",$courseid);
13653: 	
13654: 	my $num1=$symbchck+$symbseed+$namechck;
13655: 	my $num2=$nameseed+$domainseed+$courseseed;
13656: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13657: 	#&logthis("rndseed :$num:$symb");
13658: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13659: 	return "$num1,$num2";
13660:     }
13661: }
13662: 
13663: sub rndseed_64bit2 {
13664:     my ($symb,$courseid,$domain,$username)=@_;
13665:     {
13666: 	use integer;
13667: 	# strings need to be an even # of cahracters long, it it is odd the
13668:         # last characters gets thrown away
13669: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13670: 	my $symbseed=numval($symb) << 10;
13671: 	my $namechck=unpack("%32S*",$username.' ');
13672: 	
13673: 	my $nameseed=numval($username) << 21;
13674: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13675: 	my $courseseed=unpack("%32S*",$courseid.' ');
13676: 	
13677: 	my $num1=$symbchck+$symbseed+$namechck;
13678: 	my $num2=$nameseed+$domainseed+$courseseed;
13679: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13680: 	#&logthis("rndseed :$num:$symb");
13681: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13682: 	return "$num1,$num2";
13683:     }
13684: }
13685: 
13686: sub rndseed_64bit3 {
13687:     my ($symb,$courseid,$domain,$username)=@_;
13688:     {
13689: 	use integer;
13690: 	# strings need to be an even # of cahracters long, it it is odd the
13691:         # last characters gets thrown away
13692: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13693: 	my $symbseed=numval2($symb) << 10;
13694: 	my $namechck=unpack("%32S*",$username.' ');
13695: 	
13696: 	my $nameseed=numval2($username) << 21;
13697: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13698: 	my $courseseed=unpack("%32S*",$courseid.' ');
13699: 	
13700: 	my $num1=$symbchck+$symbseed+$namechck;
13701: 	my $num2=$nameseed+$domainseed+$courseseed;
13702: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13703: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13704: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13705: 	
13706: 	return "$num1:$num2";
13707:     }
13708: }
13709: 
13710: sub rndseed_64bit4 {
13711:     my ($symb,$courseid,$domain,$username)=@_;
13712:     {
13713: 	use integer;
13714: 	# strings need to be an even # of cahracters long, it it is odd the
13715:         # last characters gets thrown away
13716: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13717: 	my $symbseed=numval3($symb) << 10;
13718: 	my $namechck=unpack("%32S*",$username.' ');
13719: 	
13720: 	my $nameseed=numval3($username) << 21;
13721: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13722: 	my $courseseed=unpack("%32S*",$courseid.' ');
13723: 	
13724: 	my $num1=$symbchck+$symbseed+$namechck;
13725: 	my $num2=$nameseed+$domainseed+$courseseed;
13726: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13727: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13728: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13729: 	
13730: 	return "$num1:$num2";
13731:     }
13732: }
13733: 
13734: sub rndseed_64bit5 {
13735:     my ($symb,$courseid,$domain,$username)=@_;
13736:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
13737:     return "$num1:$num2";
13738: }
13739: 
13740: sub rndseed_CODE_64bit {
13741:     my ($symb,$courseid,$domain,$username)=@_;
13742:     {
13743: 	use integer;
13744: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13745: 	my $symbseed=numval2($symb);
13746: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13747: 	my $CODEseed=numval(&getCODE());
13748: 	my $courseseed=unpack("%32S*",$courseid.' ');
13749: 	my $num1=$symbseed+$CODEchck;
13750: 	my $num2=$CODEseed+$courseseed+$symbchck;
13751: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13752: 	#&logthis("rndseed :$num1:$num2:$symb");
13753: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13754: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13755: 	return "$num1:$num2";
13756:     }
13757: }
13758: 
13759: sub rndseed_CODE_64bit4 {
13760:     my ($symb,$courseid,$domain,$username)=@_;
13761:     {
13762: 	use integer;
13763: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13764: 	my $symbseed=numval3($symb);
13765: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13766: 	my $CODEseed=numval3(&getCODE());
13767: 	my $courseseed=unpack("%32S*",$courseid.' ');
13768: 	my $num1=$symbseed+$CODEchck;
13769: 	my $num2=$CODEseed+$courseseed+$symbchck;
13770: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13771: 	#&logthis("rndseed :$num1:$num2:$symb");
13772: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13773: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13774: 	return "$num1:$num2";
13775:     }
13776: }
13777: 
13778: sub rndseed_CODE_64bit5 {
13779:     my ($symb,$courseid,$domain,$username)=@_;
13780:     my $code = &getCODE();
13781:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
13782:     return "$num1:$num2";
13783: }
13784: 
13785: sub setup_random_from_rndseed {
13786:     my ($rndseed)=@_;
13787:     if ($rndseed =~/([,:])/) {
13788:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
13789:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
13790:             &Math::Random::random_set_seed_from_phrase($rndseed);
13791:         } else {
13792:             &Math::Random::random_set_seed($num1,$num2);
13793:         }
13794:     } else {
13795: 	&Math::Random::random_set_seed_from_phrase($rndseed);
13796:     }
13797: }
13798: 
13799: sub latest_receipt_algorithm_id {
13800:     return 'receipt3';
13801: }
13802: 
13803: sub recunique {
13804:     my $fucourseid=shift;
13805:     my $unique;
13806:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
13807: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13808: 	$unique=$env{"course.$fucourseid.internal.encseed"};
13809:     } else {
13810: 	$unique=$perlvar{'lonReceipt'};
13811:     }
13812:     return unpack("%32C*",$unique);
13813: }
13814: 
13815: sub recprefix {
13816:     my $fucourseid=shift;
13817:     my $prefix;
13818:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
13819: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13820: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
13821:     } else {
13822: 	$prefix=$perlvar{'lonHostID'};
13823:     }
13824:     return unpack("%32C*",$prefix);
13825: }
13826: 
13827: sub ireceipt {
13828:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
13829: 
13830:     my $return =&recprefix($fucourseid).'-';
13831: 
13832:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
13833: 	$env{'request.state'} eq 'construct') {
13834: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
13835: 	return $return;
13836:     }
13837: 
13838:     my $cuname=unpack("%32C*",$funame);
13839:     my $cudom=unpack("%32C*",$fudom);
13840:     my $cucourseid=unpack("%32C*",$fucourseid);
13841:     my $cusymb=unpack("%32C*",$fusymb);
13842:     my $cunique=&recunique($fucourseid);
13843:     my $cpart=unpack("%32S*",$part);
13844:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
13845: 
13846: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
13847: 			       
13848: 	$return.= ($cunique%$cuname+
13849: 		   $cunique%$cudom+
13850: 		   $cusymb%$cuname+
13851: 		   $cusymb%$cudom+
13852: 		   $cucourseid%$cuname+
13853: 		   $cucourseid%$cudom+
13854: 		   $cpart%$cuname+
13855: 		   $cpart%$cudom);
13856:     } else {
13857: 	$return.= ($cunique%$cuname+
13858: 		   $cunique%$cudom+
13859: 		   $cusymb%$cuname+
13860: 		   $cusymb%$cudom+
13861: 		   $cucourseid%$cuname+
13862: 		   $cucourseid%$cudom);
13863:     }
13864:     return $return;
13865: }
13866: 
13867: sub receipt {
13868:     my ($part)=@_;
13869:     my ($symb,$courseid,$domain,$name) = &whichuser();
13870:     return &ireceipt($name,$domain,$courseid,$symb,$part);
13871: }
13872: 
13873: sub whichuser {
13874:     my ($passedsymb)=@_;
13875:     my ($symb,$courseid,$domain,$name,$publicuser);
13876:     if (defined($env{'form.grade_symb'})) {
13877: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
13878: 	my $allowed=&allowed('vgr',$tmp_courseid);
13879: 	if (!$allowed &&
13880: 	    exists($env{'request.course.sec'}) &&
13881: 	    $env{'request.course.sec'} !~ /^\s*$/) {
13882: 	    $allowed=&allowed('vgr',$tmp_courseid.
13883: 			      '/'.$env{'request.course.sec'});
13884: 	}
13885: 	if ($allowed) {
13886: 	    ($symb)=&get_env_multiple('form.grade_symb');
13887: 	    $courseid=$tmp_courseid;
13888: 	    ($domain)=&get_env_multiple('form.grade_domain');
13889: 	    ($name)=&get_env_multiple('form.grade_username');
13890: 	    return ($symb,$courseid,$domain,$name,$publicuser);
13891: 	}
13892:     }
13893:     if (!$passedsymb) {
13894: 	$symb=&symbread();
13895:     } else {
13896: 	$symb=$passedsymb;
13897:     }
13898:     $courseid=$env{'request.course.id'};
13899:     $domain=$env{'user.domain'};
13900:     $name=$env{'user.name'};
13901:     if ($name eq 'public' && $domain eq 'public') {
13902: 	if (!defined($env{'form.username'})) {
13903: 	    $env{'form.username'}.=time.rand(10000000);
13904: 	}
13905: 	$name.=$env{'form.username'};
13906:     }
13907:     return ($symb,$courseid,$domain,$name,$publicuser);
13908: 
13909: }
13910: 
13911: # ------------------------------------------------------------ Serves up a file
13912: # returns either the contents of the file or 
13913: # -1 if the file doesn't exist
13914: #
13915: # if the target is a file that was uploaded via DOCS, 
13916: # a check will be made to see if a current copy exists on the local server,
13917: # if it does this will be served, otherwise a copy will be retrieved from
13918: # the home server for the course and stored in /home/httpd/html/userfiles on
13919: # the local server.   
13920: 
13921: sub getfile {
13922:     my ($file) = @_;
13923:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
13924:     &repcopy($file);
13925:     return &readfile($file);
13926: }
13927: 
13928: sub repcopy_userfile {
13929:     my ($file)=@_;
13930:     my $londocroot = $perlvar{'lonDocRoot'};
13931:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
13932:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
13933:     my ($cdom,$cnum,$filename) = 
13934: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
13935:     my $uri="/uploaded/$cdom/$cnum/$filename";
13936:     if (-e "$file") {
13937: # we already have a local copy, check it out
13938: 	my @fileinfo = stat($file);
13939: 	my $rtncode;
13940: 	my $info;
13941: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
13942: 	if ($lwpresp ne 'ok') {
13943: # there is no such file anymore, even though we had a local copy
13944: 	    if ($rtncode eq '404') {
13945: 		unlink($file);
13946: 	    }
13947: 	    return -1;
13948: 	}
13949: 	if ($info < $fileinfo[9]) {
13950: # nice, the file we have is up-to-date, just say okay
13951: 	    return 'ok';
13952: 	} else {
13953: # the file is outdated, get rid of it
13954: 	    unlink($file);
13955: 	}
13956:     }
13957: # one way or the other, at this point, we don't have the file
13958: # construct the correct path for the file
13959:     my @parts = ($cdom,$cnum); 
13960:     if ($filename =~ m|^(.+)/[^/]+$|) {
13961: 	push @parts, split(/\//,$1);
13962:     }
13963:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
13964:     foreach my $part (@parts) {
13965: 	$path .= '/'.$part;
13966: 	if (!-e $path) {
13967: 	    mkdir($path,0770);
13968: 	}
13969:     }
13970: # now the path exists for sure
13971: # get a user agent
13972:     my $transferfile=$file.'.in.transfer';
13973: # FIXME: this should flock
13974:     if (-e $transferfile) { return 'ok'; }
13975:     my $request;
13976:     $uri=~s/^\///;
13977:     my $homeserver = &homeserver($cnum,$cdom);
13978:     my $hostname = &hostname($homeserver);
13979:     my $protocol = $protocol{$homeserver};
13980:     $protocol = 'http' if ($protocol ne 'https');
13981:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
13982:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
13983: # did it work?
13984:     if ($response->is_error()) {
13985: 	unlink($transferfile);
13986: 	&logthis("Userfile repcopy failed for $uri");
13987: 	return -1;
13988:     }
13989: # worked, rename the transfer file
13990:     rename($transferfile,$file);
13991:     return 'ok';
13992: }
13993: 
13994: sub tokenwrapper {
13995:     my $uri=shift;
13996:     $uri=~s|^https?\://([^/]+)||;
13997:     $uri=~s|^/||;
13998:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
13999:     my $token=$1;
14000:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14001:     if ($udom && $uname && $file) {
14002: 	$file=~s|(\?\.*)*$||;
14003:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14004:         my $homeserver = &homeserver($uname,$udom);
14005:         my $hostname = &hostname($homeserver);
14006:         my $protocol = $protocol{$homeserver};
14007:         $protocol = 'http' if ($protocol ne 'https');
14008:         return $protocol.'://'.$hostname.'/'.$uri.
14009:                (($uri=~/\?/)?'&':'?').'token='.$token.
14010:                                '&tokenissued='.$perlvar{'lonHostID'};
14011:     } else {
14012:         return '/adm/notfound.html';
14013:     }
14014: }
14015: 
14016: # call with reqtype HEAD: get last modification time
14017: # call with reqtype GET: get the file contents
14018: # Do not call this with reqtype GET for large files! It loads everything into memory
14019: #
14020: sub getuploaded {
14021:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14022:     $uri=~s/^\///;
14023:     my $homeserver = &homeserver($cnum,$cdom);
14024:     my $hostname = &hostname($homeserver);
14025:     my $protocol = $protocol{$homeserver};
14026:     $protocol = 'http' if ($protocol ne 'https');
14027:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14028:     my $request=new HTTP::Request($reqtype,$uri);
14029:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14030:     $$rtncode = $response->code;
14031:     if (! $response->is_success()) {
14032: 	return 'failed';
14033:     }      
14034:     if ($reqtype eq 'HEAD') {
14035: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14036:     } elsif ($reqtype eq 'GET') {
14037: 	$$info = $response->content;
14038:     }
14039:     return 'ok';
14040: }
14041: 
14042: sub readfile {
14043:     my $file = shift;
14044:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14045:     my $fh;
14046:     open($fh,"<",$file);
14047:     my $a='';
14048:     while (my $line = <$fh>) { $a .= $line; }
14049:     return $a;
14050: }
14051: 
14052: sub filelocation {
14053:     my ($dir,$file) = @_;
14054:     my $location;
14055:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14056: 
14057:     if ($file =~ m-^/adm/-) {
14058: 	$file=~s-^/adm/wrapper/-/-;
14059: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14060:     }
14061: 
14062:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14063:         $location = $file;
14064:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14065:         my ($udom,$uname,$filename)=
14066:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14067:         my $home=&homeserver($uname,$udom);
14068:         my $is_me=0;
14069:         my @ids=&current_machine_ids();
14070:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14071:         if ($is_me) {
14072:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14073:         } else {
14074:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14075:   	      $udom.'/'.$uname.'/'.$filename;
14076:         }
14077:     } elsif ($file =~ m-^/adm/-) {
14078: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14079:     } else {
14080:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14081:         $file=~s:^/(res|priv)/:/:;
14082:         my $space=$1;
14083:         if ( !( $file =~ m:^/:) ) {
14084:             $location = $dir. '/'.$file;
14085:         } else {
14086:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14087:         }
14088:     }
14089:     $location=~s://+:/:g; # remove duplicate /
14090:     while ($location=~m{/\.\./}) {
14091: 	if ($location =~ m{/[^/]+/\.\./}) {
14092: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14093: 	} else {
14094: 	    $location=~ s{/\.\./}{/}g;
14095: 	}
14096:     } #remove dir/..
14097:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14098:     return $location;
14099: }
14100: 
14101: sub hreflocation {
14102:     my ($dir,$file)=@_;
14103:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14104: 	$file=filelocation($dir,$file);
14105:     } elsif ($file=~m-^/adm/-) {
14106: 	$file=~s-^/adm/wrapper/-/-;
14107: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14108:     }
14109:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14110: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14111:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14112: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14113: 	        {/uploaded/$1/$2/}x;
14114:     }
14115:     if ($file=~ m{^/userfiles/}) {
14116: 	$file =~ s{^/userfiles/}{/uploaded/};
14117:     }
14118:     return $file;
14119: }
14120: 
14121: 
14122: 
14123: 
14124: 
14125: sub current_machine_domains {
14126:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14127: }
14128: 
14129: sub machine_domains {
14130:     my ($hostname) = @_;
14131:     my @domains;
14132:     my %hostname = &all_hostnames();
14133:     while( my($id, $name) = each(%hostname)) {
14134: #	&logthis("-$id-$name-$hostname-");
14135: 	if ($hostname eq $name) {
14136: 	    push(@domains,&host_domain($id));
14137: 	}
14138:     }
14139:     return @domains;
14140: }
14141: 
14142: sub current_machine_ids {
14143:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14144: }
14145: 
14146: sub machine_ids {
14147:     my ($hostname) = @_;
14148:     $hostname ||= &hostname($perlvar{'lonHostID'});
14149:     my @ids;
14150:     my %name_to_host = &all_names();
14151:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14152: 	return @{ $name_to_host{$hostname} };
14153:     }
14154:     return;
14155: }
14156: 
14157: sub additional_machine_domains {
14158:     my @domains;
14159:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
14160:     while( my $line = <$fh>) {
14161:         $line =~ s/\s//g;
14162:         push(@domains,$line);
14163:     }
14164:     return @domains;
14165: }
14166: 
14167: sub default_login_domain {
14168:     my $domain = $perlvar{'lonDefDomain'};
14169:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14170:     foreach my $posdom (&current_machine_domains(),
14171:                         &additional_machine_domains()) {
14172:         if (lc($posdom) eq lc($testdomain)) {
14173:             $domain=$posdom;
14174:             last;
14175:         }
14176:     }
14177:     return $domain;
14178: }
14179: 
14180: sub shared_institution {
14181:     my ($dom) = @_;
14182:     my $same_intdom;
14183:     my $hostintdom = &internet_dom($perlvar{'lonHostID'});
14184:     if ($hostintdom ne '') {
14185:         my %iphost = &get_iphost();
14186:         my $primary_id = &domain($dom,'primary');
14187:         my $primary_ip = &get_host_ip($primary_id);
14188:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14189:             foreach my $id (@{$iphost{$primary_ip}}) {
14190:                 my $intdom = &internet_dom($id);
14191:                 if ($intdom eq $hostintdom) {
14192:                     $same_intdom = 1;
14193:                     last;
14194:                 }
14195:             }
14196:         }
14197:     }
14198:     return $same_intdom;
14199: }
14200: 
14201: sub uses_sts {
14202:     my ($ignore_cache) = @_;
14203:     my $lonhost = $perlvar{'lonHostID'};
14204:     my $hostname = &hostname($lonhost);
14205:     my $sts_on;
14206:     if ($protocol{$lonhost} eq 'https') {
14207:         my $cachetime = 12*3600;
14208:         if (!$ignore_cache) {
14209:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14210:             if (defined($cached)) {
14211:                 return $sts_on;
14212:             }
14213:         }
14214:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14215:         my $request=new HTTP::Request('HEAD',$url);
14216:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14217:         if ($response->is_success) {
14218:             my $has_sts = $response->header('Strict-Transport-Security');
14219:             if ($has_sts eq '') {
14220:                 $sts_on = 0;
14221:             } else {
14222:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14223:                     my $maxage = $1;
14224:                     if ($maxage) {
14225:                         $sts_on = 1;
14226:                     } else {
14227:                         $sts_on = 0;
14228:                     }
14229:                 } else {
14230:                     $sts_on = 0;
14231:                 }
14232:             }
14233:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14234:         }
14235:     }
14236:     return;
14237: }
14238: 
14239: # ------------------------------------------------------------- Declutters URLs
14240: 
14241: sub declutter {
14242:     my $thisfn=shift;
14243:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14244:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14245:         $thisfn=~s{^/home/httpd/html}{};
14246:     }
14247:     $thisfn=~s/^\///;
14248:     $thisfn=~s|^adm/wrapper/||;
14249:     $thisfn=~s|^adm/coursedocs/showdoc/||;
14250:     $thisfn=~s/^res\///;
14251:     $thisfn=~s/^priv\///;
14252:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14253:         $thisfn=~s/\?.+$//;
14254:     }
14255:     return $thisfn;
14256: }
14257: 
14258: # ------------------------------------------------------------- Clutter up URLs
14259: 
14260: sub clutter {
14261:     my $thisfn='/'.&declutter(shift);
14262:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14263: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14264:        $thisfn='/res'.$thisfn; 
14265:     }
14266:     if ($thisfn !~m|^/adm|) {
14267: 	if ($thisfn =~ m|^/ext/|) {
14268: 	    $thisfn='/adm/wrapper'.$thisfn;
14269: 	} else {
14270: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14271: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14272: 	    if ($embstyle eq 'ssi'
14273: 		|| ($embstyle eq 'hdn')
14274: 		|| ($embstyle eq 'rat')
14275: 		|| ($embstyle eq 'prv')
14276: 		|| ($embstyle eq 'ign')) {
14277: 		#do nothing with these
14278: 	    } elsif (($embstyle eq 'img') 
14279: 		|| ($embstyle eq 'emb')
14280: 		|| ($embstyle eq 'wrp')) {
14281: 		$thisfn='/adm/wrapper'.$thisfn;
14282: 	    } elsif ($embstyle eq 'unk'
14283: 		     && $thisfn!~/\.(sequence|page)$/) {
14284: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14285: 	    } else {
14286: #		&logthis("Got a blank emb style");
14287: 	    }
14288: 	}
14289:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14290:         $thisfn='/adm/wrapper'.$thisfn;
14291:     }
14292:     return $thisfn;
14293: }
14294: 
14295: sub clutter_with_no_wrapper {
14296:     my $uri = &clutter(shift);
14297:     if ($uri =~ m-^/adm/-) {
14298: 	$uri =~ s-^/adm/wrapper/-/-;
14299: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14300:     }
14301:     return $uri;
14302: }
14303: 
14304: sub freeze_escape {
14305:     my ($value)=@_;
14306:     if (ref($value)) {
14307: 	$value=&nfreeze($value);
14308: 	return '__FROZEN__'.&escape($value);
14309:     }
14310:     return &escape($value);
14311: }
14312: 
14313: 
14314: sub thaw_unescape {
14315:     my ($value)=@_;
14316:     if ($value =~ /^__FROZEN__/) {
14317: 	substr($value,0,10,undef);
14318: 	$value=&unescape($value);
14319: 	return &thaw($value);
14320:     }
14321:     return &unescape($value);
14322: }
14323: 
14324: sub correct_line_ends {
14325:     my ($result)=@_;
14326:     $$result =~s/\r\n/\n/mg;
14327:     $$result =~s/\r/\n/mg;
14328: }
14329: # ================================================================ Main Program
14330: 
14331: sub goodbye {
14332:    &logthis("Starting Shut down");
14333: #not converted to using infrastruture and probably shouldn't be
14334:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14335: #converted
14336: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14337:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14338: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14339: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14340: #1.1 only
14341: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14342: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14343: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14344: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14345:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14346:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14347:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14348:    &flushcourselogs();
14349:    &logthis("Shutting down");
14350: }
14351: 
14352: sub get_dns {
14353:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14354:     if (!$ignore_cache) {
14355: 	my ($content,$cached)=
14356: 	    &Apache::lonnet::is_cached_new('dns',$url);
14357: 	if ($cached) {
14358: 	    &$func($content,$hashref);
14359: 	    return;
14360: 	}
14361:     }
14362: 
14363:     my %alldns;
14364:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14365:         foreach my $dns (<$config>) {
14366: 	    next if ($dns !~ /^\^(\S*)/x);
14367:             my $line = $1;
14368:             my ($host,$protocol) = split(/:/,$line);
14369:             if ($protocol ne 'https') {
14370:                 $protocol = 'http';
14371:             }
14372: 	    $alldns{$host} = $protocol;
14373:         }
14374:         close($config);
14375:     }
14376:     while (%alldns) {
14377: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14378: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14379:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14380:         delete($alldns{$dns});
14381: 	next if ($response->is_error());
14382:         if ($url eq '/adm/dns/loncapaCRL') {
14383:             return &$func($response);
14384:         } else {
14385: 	    my @content = split("\n",$response->content);
14386: 	    unless ($nocache) {
14387: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14388: 	    }
14389: 	    &$func(\@content,$hashref);
14390:             return;
14391:         }
14392:     }
14393:     my $which = (split('/',$url,4))[3];
14394:     if ($which eq 'loncapaCRL') {
14395:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14396:         if (-e $diskfile) {
14397:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14398:         } else {
14399:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14400:         }
14401:     } else {
14402:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14403:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14404:             my @content = <$config>;
14405:             close($config);
14406:             &$func(\@content,$hashref);
14407:         }
14408:     }
14409:     return;
14410: }
14411: 
14412: # ------------------------------------------------------Get DNS checksums file
14413: sub parse_dns_checksums_tab {
14414:     my ($lines,$hashref) = @_;
14415:     my $lonhost = $perlvar{'lonHostID'};
14416:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14417:     my $loncaparev = &get_server_loncaparev($machine_dom);
14418:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14419:     my $webconfdir = '/etc/httpd/conf';
14420:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14421:         $webconfdir = '/etc/apache2';
14422:     } elsif ($distro =~ /^sles(\d+)$/) {
14423:         if ($1 >= 10) {
14424:             $webconfdir = '/etc/apache2';
14425:         }
14426:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14427:         if ($1 >= 10.0) {
14428:             $webconfdir = '/etc/apache2';
14429:         }
14430:     }
14431:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14432:     my (%chksum,%revnum);
14433:     if (ref($lines) eq 'ARRAY') {
14434:         chomp(@{$lines});
14435:         my $version = shift(@{$lines});
14436:         if ($version eq $release) {  
14437:             foreach my $line (@{$lines}) {
14438:                 my ($file,$version,$shasum) = split(/,/,$line);
14439:                 if ($file =~ m{^/etc/httpd/conf}) {
14440:                     if ($webconfdir eq '/etc/apache2') {
14441:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14442:                     }
14443:                 }
14444:                 $chksum{$file} = $shasum;
14445:                 $revnum{$file} = $version;
14446:             }
14447:             if (ref($hashref) eq 'HASH') {
14448:                 %{$hashref} = (
14449:                                 sums     => \%chksum,
14450:                                 versions => \%revnum,
14451:                               );
14452:             }
14453:         }
14454:     }
14455:     return;
14456: }
14457: 
14458: sub fetch_dns_checksums {
14459:     my %checksums;
14460:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14461:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14462:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14463:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14464:              \%checksums);
14465:     return \%checksums;
14466: }
14467: 
14468: sub fetch_crl_pemfile {
14469:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14470: }
14471: 
14472: sub save_crl_pem {
14473:     my ($response) = @_;
14474:     my ($msg,$hadchanges);
14475:     if (ref($response)) {
14476:         my $now = time;
14477:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14478:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14479:         if (open(my $fh,'>',"$tmpcrl")) {
14480:             print $fh $response->content;
14481:             close($fh);
14482:             if (-e $lonca) {
14483:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14484:                     my $check = <PIPE>;
14485:                     close(PIPE);
14486:                     chomp($check);
14487:                     if ($check eq 'verify OK') {
14488:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14489:                         my $backup;
14490:                         if (-e $dest) {
14491:                             if (&File::Copy::move($dest,"$dest.bak")) {
14492:                                 $backup = 'ok';
14493:                             }
14494:                         }
14495:                         if (&File::Copy::move($tmpcrl,$dest)) {
14496:                             $msg = 'ok';
14497:                             if ($backup) {
14498:                                 my (%oldnums,%newnums);
14499:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
14500:                                     while (<PIPE>) {
14501:                                         $oldnums{(split(/:/))[1]} = 1;
14502:                                     }
14503:                                     close(PIPE);
14504:                                 }
14505:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
14506:                                     while(<PIPE>) {
14507:                                         $newnums{(split(/:/))[1]} = 1;
14508:                                     }
14509:                                     close(PIPE);
14510:                                 }
14511:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
14512:                                     unless (exists($oldnums{$key})) {
14513:                                         $hadchanges = 1;
14514:                                         last;
14515:                                     }
14516:                                 }
14517:                                 unless ($hadchanges) {
14518:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
14519:                                         unless (exists($newnums{$key})) {
14520:                                             $hadchanges = 1;
14521:                                             last;
14522:                                         }
14523:                                     }
14524:                                 }
14525:                             }
14526:                         }
14527:                     } else {
14528:                         unlink($tmpcrl);
14529:                     }
14530:                 } else {
14531:                     unlink($tmpcrl);
14532:                 }
14533:             } else {
14534:                 unlink($tmpcrl);
14535:             }
14536:         }
14537:     }
14538:     return ($msg,$hadchanges);
14539: }
14540: 
14541: # ------------------------------------------------------------ Read domain file
14542: {
14543:     my $loaded;
14544:     my %domain;
14545: 
14546:     sub parse_domain_tab {
14547: 	my ($lines) = @_;
14548: 	foreach my $line (@$lines) {
14549: 	    next if ($line =~ /^(\#|\s*$ )/x);
14550: 
14551: 	    chomp($line);
14552: 	    my ($name,@elements) = split(/:/,$line,9);
14553: 	    my %this_domain;
14554: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
14555: 			       'lang_def', 'city', 'longi', 'lati',
14556: 			       'primary') {
14557: 		$this_domain{$field} = shift(@elements);
14558: 	    }
14559: 	    $domain{$name} = \%this_domain;
14560: 	}
14561:     }
14562: 
14563:     sub reset_domain_info {
14564: 	undef($loaded);
14565: 	undef(%domain);
14566:     }
14567: 
14568:     sub load_domain_tab {
14569: 	my ($ignore_cache,$nocache) = @_;
14570: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
14571: 	my $fh;
14572: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
14573: 	    my @lines = <$fh>;
14574: 	    &parse_domain_tab(\@lines);
14575: 	}
14576: 	close($fh);
14577: 	$loaded = 1;
14578:     }
14579: 
14580:     sub domain {
14581: 	&load_domain_tab() if (!$loaded);
14582: 
14583: 	my ($name,$what) = @_;
14584: 	return if ( !exists($domain{$name}) );
14585: 
14586: 	if (!$what) {
14587: 	    return $domain{$name}{'description'};
14588: 	}
14589: 	return $domain{$name}{$what};
14590:     }
14591: 
14592:     sub domain_info {
14593:         &load_domain_tab() if (!$loaded);
14594:         return %domain;
14595:     }
14596: 
14597: }
14598: 
14599: 
14600: # ------------------------------------------------------------- Read hosts file
14601: {
14602:     my %hostname;
14603:     my %hostdom;
14604:     my %libserv;
14605:     my $loaded;
14606:     my %name_to_host;
14607:     my %internetdom;
14608:     my %LC_dns_serv;
14609: 
14610:     sub parse_hosts_tab {
14611: 	my ($file) = @_;
14612: 	foreach my $configline (@$file) {
14613: 	    next if ($configline =~ /^(\#|\s*$ )/x);
14614:             chomp($configline);
14615: 	    if ($configline =~ /^\^/) {
14616:                 if ($configline =~ /^\^([\w.\-]+)/) {
14617:                     $LC_dns_serv{$1} = 1;
14618:                 }
14619:                 next;
14620:             }
14621: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
14622: 	    $name=~s/\s//g;
14623: 	    if ($id && $domain && $role && $name) {
14624:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
14625:                     my $curr = $hostname{$id};
14626:                     my $skip;
14627:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
14628:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
14629:                             $skip = 1;
14630:                         } else {
14631:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
14632:                         }
14633:                     }
14634:                     unless ($skip) {
14635:                         push(@{$name_to_host{$name}},$id);
14636:                     }
14637:                 } else {
14638:                     push(@{$name_to_host{$name}},$id);
14639:                 }
14640: 		$hostname{$id}=$name;
14641: 		$hostdom{$id}=$domain;
14642: 		if ($role eq 'library') { $libserv{$id}=$name; }
14643:                 if (defined($protocol)) {
14644:                     if ($protocol eq 'https') {
14645:                         $protocol{$id} = $protocol;
14646:                     } else {
14647:                         $protocol{$id} = 'http'; 
14648:                     }
14649:                 } else {
14650:                     $protocol{$id} = 'http';
14651:                 }
14652:                 if (defined($intdom)) {
14653:                     $internetdom{$id} = $intdom;
14654:                 }
14655: 	    }
14656: 	}
14657:     }
14658:     
14659:     sub reset_hosts_info {
14660: 	&purge_remembered();
14661: 	&reset_domain_info();
14662: 	&reset_hosts_ip_info();
14663:         undef(%internetdom);
14664: 	undef(%name_to_host);
14665: 	undef(%hostname);
14666: 	undef(%hostdom);
14667: 	undef(%libserv);
14668: 	undef($loaded);
14669:     }
14670: 
14671:     sub load_hosts_tab {
14672: 	my ($ignore_cache,$nocache) = @_;
14673: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
14674: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
14675: 	my @config = <$config>;
14676: 	&parse_hosts_tab(\@config);
14677: 	close($config);
14678: 	$loaded=1;
14679:     }
14680: 
14681:     sub hostname {
14682: 	&load_hosts_tab() if (!$loaded);
14683: 
14684: 	my ($lonid) = @_;
14685: 	return $hostname{$lonid};
14686:     }
14687: 
14688:     sub all_hostnames {
14689: 	&load_hosts_tab() if (!$loaded);
14690: 
14691: 	return %hostname;
14692:     }
14693: 
14694:     sub all_names {
14695:         my ($ignore_cache,$nocache) = @_;
14696: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
14697: 
14698: 	return %name_to_host;
14699:     }
14700: 
14701:     sub all_host_domain {
14702:         &load_hosts_tab() if (!$loaded);
14703:         return %hostdom;
14704:     }
14705: 
14706:     sub all_host_intdom {
14707:         &load_hosts_tab() if (!$loaded);
14708:         return %internetdom;
14709:     }
14710: 
14711:     sub is_library {
14712: 	&load_hosts_tab() if (!$loaded);
14713: 
14714: 	return exists($libserv{$_[0]});
14715:     }
14716: 
14717:     sub all_library {
14718: 	&load_hosts_tab() if (!$loaded);
14719: 
14720: 	return %libserv;
14721:     }
14722: 
14723:     sub unique_library {
14724: 	#2x reverse removes all hostnames that appear more than once
14725:         my %unique = reverse &all_library();
14726:         return reverse %unique;
14727:     }
14728: 
14729:     sub get_servers {
14730: 	&load_hosts_tab() if (!$loaded);
14731: 
14732: 	my ($domain,$type) = @_;
14733: 	my %possible_hosts = ($type eq 'library') ? %libserv
14734: 	                                          : %hostname;
14735: 	my %result;
14736: 	if (ref($domain) eq 'ARRAY') {
14737: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14738: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
14739: 		    $result{$host} = $hostname;
14740: 		}
14741: 	    }
14742: 	} else {
14743: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14744: 		if ($hostdom{$host} eq $domain) {
14745: 		    $result{$host} = $hostname;
14746: 		}
14747: 	    }
14748: 	}
14749: 	return %result;
14750:     }
14751: 
14752:     sub get_unique_servers {
14753:         my %unique = reverse &get_servers(@_);
14754: 	return reverse %unique;
14755:     }
14756: 
14757:     sub host_domain {
14758: 	&load_hosts_tab() if (!$loaded);
14759: 
14760: 	my ($lonid) = @_;
14761: 	return $hostdom{$lonid};
14762:     }
14763: 
14764:     sub all_domains {
14765: 	&load_hosts_tab() if (!$loaded);
14766: 
14767: 	my %seen;
14768: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
14769: 	return @uniq;
14770:     }
14771: 
14772:     sub internet_dom {
14773:         &load_hosts_tab() if (!$loaded);
14774: 
14775:         my ($lonid) = @_;
14776:         return $internetdom{$lonid};
14777:     }
14778: 
14779:     sub is_LC_dns {
14780:         &load_hosts_tab() if (!$loaded);
14781: 
14782:         my ($hostname) = @_;
14783:         return exists($LC_dns_serv{$hostname});
14784:     }
14785: 
14786: }
14787: 
14788: { 
14789:     my %iphost;
14790:     my %name_to_ip;
14791:     my %lonid_to_ip;
14792: 
14793:     sub get_hosts_from_ip {
14794: 	my ($ip) = @_;
14795: 	my %iphosts = &get_iphost();
14796: 	if (ref($iphosts{$ip})) {
14797: 	    return @{$iphosts{$ip}};
14798: 	}
14799: 	return;
14800:     }
14801:     
14802:     sub reset_hosts_ip_info {
14803: 	undef(%iphost);
14804: 	undef(%name_to_ip);
14805: 	undef(%lonid_to_ip);
14806:     }
14807: 
14808:     sub get_host_ip {
14809: 	my ($lonid) = @_;
14810: 	if (exists($lonid_to_ip{$lonid})) {
14811: 	    return $lonid_to_ip{$lonid};
14812: 	}
14813: 	my $name=&hostname($lonid);
14814:    	my $ip = gethostbyname($name);
14815: 	return if (!$ip || length($ip) ne 4);
14816: 	$ip=inet_ntoa($ip);
14817: 	$name_to_ip{$name}   = $ip;
14818: 	$lonid_to_ip{$lonid} = $ip;
14819: 	return $ip;
14820:     }
14821:     
14822:     sub get_iphost {
14823: 	my ($ignore_cache,$nocache) = @_;
14824: 
14825: 	if (!$ignore_cache) {
14826: 	    if (%iphost) {
14827: 		return %iphost;
14828: 	    }
14829: 	    my ($ip_info,$cached)=
14830: 		&Apache::lonnet::is_cached_new('iphost','iphost');
14831: 	    if ($cached) {
14832: 		%iphost      = %{$ip_info->[0]};
14833: 		%name_to_ip  = %{$ip_info->[1]};
14834: 		%lonid_to_ip = %{$ip_info->[2]};
14835: 		return %iphost;
14836: 	    }
14837: 	}
14838: 
14839: 	# get yesterday's info for fallback
14840: 	my %old_name_to_ip;
14841: 	my ($ip_info,$cached)=
14842: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
14843: 	if ($cached) {
14844: 	    %old_name_to_ip = %{$ip_info->[1]};
14845: 	}
14846: 
14847: 	my %name_to_host = &all_names($ignore_cache,$nocache);
14848: 	foreach my $name (keys(%name_to_host)) {
14849: 	    my $ip;
14850: 	    if (!exists($name_to_ip{$name})) {
14851: 		$ip = gethostbyname($name);
14852: 		if (!$ip || length($ip) ne 4) {
14853: 		    if (defined($old_name_to_ip{$name})) {
14854: 			$ip = $old_name_to_ip{$name};
14855: 			&logthis("Can't find $name defaulting to old $ip");
14856: 		    } else {
14857: 			&logthis("Name $name no IP found");
14858: 			next;
14859: 		    }
14860: 		} else {
14861: 		    $ip=inet_ntoa($ip);
14862: 		}
14863: 		$name_to_ip{$name} = $ip;
14864: 	    } else {
14865: 		$ip = $name_to_ip{$name};
14866: 	    }
14867: 	    foreach my $id (@{ $name_to_host{$name} }) {
14868: 		$lonid_to_ip{$id} = $ip;
14869: 	    }
14870: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
14871: 	}
14872:         unless ($nocache) {
14873: 	    &do_cache_new('iphost','iphost',
14874: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
14875: 		          48*60*60);
14876:         }
14877: 
14878: 	return %iphost;
14879:     }
14880: 
14881:     #
14882:     #  Given a DNS returns the loncapa host name for that DNS 
14883:     # 
14884:     sub host_from_dns {
14885:         my ($dns) = @_;
14886:         my @hosts;
14887:         my $ip;
14888: 
14889:         if (exists($name_to_ip{$dns})) {
14890:             $ip = $name_to_ip{$dns};
14891:         }
14892:         if (!$ip) {
14893:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
14894:             if (length($ip) == 4) { 
14895: 	        $ip   = &IO::Socket::inet_ntoa($ip);
14896:             }
14897:         }
14898:         if ($ip) {
14899: 	    @hosts = get_hosts_from_ip($ip);
14900: 	    return $hosts[0];
14901:         }
14902:         return undef;
14903:     }
14904: 
14905:     sub get_internet_names {
14906:         my ($lonid) = @_;
14907:         return if ($lonid eq '');
14908:         my ($idnref,$cached)=
14909:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
14910:         if ($cached) {
14911:             return $idnref;
14912:         }
14913:         my $ip = &get_host_ip($lonid);
14914:         my @hosts = &get_hosts_from_ip($ip);
14915:         my %iphost = &get_iphost();
14916:         my (@idns,%seen);
14917:         foreach my $id (@hosts) {
14918:             my $dom = &host_domain($id);
14919:             my $prim_id = &domain($dom,'primary');
14920:             my $prim_ip = &get_host_ip($prim_id);
14921:             next if ($seen{$prim_ip});
14922:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
14923:                 foreach my $id (@{$iphost{$prim_ip}}) {
14924:                     my $intdom = &internet_dom($id);
14925:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
14926:                         push(@idns,$intdom);
14927:                     }
14928:                 }
14929:             }
14930:             $seen{$prim_ip} = 1;
14931:         }
14932:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
14933:     }
14934: 
14935: }
14936: 
14937: sub all_loncaparevs {
14938:     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);
14939: }
14940: 
14941: # ---------------------------------------------------------- Read loncaparev table
14942: {
14943:     sub load_loncaparevs { 
14944:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
14945:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
14946:                 while (my $configline=<$config>) {
14947:                     chomp($configline);
14948:                     my ($hostid,$loncaparev)=split(/:/,$configline);
14949:                     $loncaparevs{$hostid}=$loncaparev;
14950:                 }
14951:                 close($config);
14952:             }
14953:         }
14954:     }
14955: }
14956: 
14957: # ---------------------------------------------------------- Read serverhostID table
14958: {
14959:     sub load_serverhomeIDs {
14960:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
14961:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
14962:                 while (my $configline=<$config>) {
14963:                     chomp($configline);
14964:                     my ($name,$id)=split(/:/,$configline);
14965:                     $serverhomeIDs{$name}=$id;
14966:                 }
14967:                 close($config);
14968:             }
14969:         }
14970:     }
14971: }
14972: 
14973: 
14974: BEGIN {
14975: 
14976: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
14977:     unless ($readit) {
14978: {
14979:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
14980:     %perlvar = (%perlvar,%{$configvars});
14981: }
14982: 
14983: 
14984: # ------------------------------------------------------ Read spare server file
14985: {
14986:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
14987: 
14988:     while (my $configline=<$config>) {
14989:        chomp($configline);
14990:        if ($configline) {
14991: 	   my ($host,$type) = split(':',$configline,2);
14992: 	   if (!defined($type) || $type eq '') { $type = 'default' };
14993: 	   push(@{ $spareid{$type} }, $host);
14994:        }
14995:     }
14996:     close($config);
14997: }
14998: # ------------------------------------------------------------ Read permissions
14999: {
15000:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
15001: 
15002:     while (my $configline=<$config>) {
15003: 	chomp($configline);
15004: 	if ($configline) {
15005: 	    my ($role,$perm)=split(/ /,$configline);
15006: 	    if ($perm ne '') { $pr{$role}=$perm; }
15007: 	}
15008:     }
15009:     close($config);
15010: }
15011: 
15012: # -------------------------------------------- Read plain texts for permissions
15013: {
15014:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
15015: 
15016:     while (my $configline=<$config>) {
15017: 	chomp($configline);
15018: 	if ($configline) {
15019: 	    my ($short,@plain)=split(/:/,$configline);
15020:             %{$prp{$short}} = ();
15021: 	    if (@plain > 0) {
15022:                 $prp{$short}{'std'} = $plain[0];
15023:                 for (my $i=1; $i<@plain; $i++) {
15024:                     $prp{$short}{'alt'.$i} = $plain[$i];  
15025:                 }
15026:             }
15027: 	}
15028:     }
15029:     close($config);
15030: }
15031: 
15032: # ---------------------------------------------------------- Read package table
15033: {
15034:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
15035: 
15036:     while (my $configline=<$config>) {
15037: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
15038: 	chomp($configline);
15039: 	my ($short,$plain)=split(/:/,$configline);
15040: 	my ($pack,$name)=split(/\&/,$short);
15041: 	if ($plain ne '') {
15042: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
15043: 	    $packagetab{$short}=$plain; 
15044: 	}
15045:     }
15046:     close($config);
15047: }
15048: 
15049: # ---------------------------------------------------------- Read loncaparev table
15050: 
15051: &load_loncaparevs();
15052: 
15053: # ---------------------------------------------------------- Read serverhostID table
15054: 
15055: &load_serverhomeIDs();
15056: 
15057: # ---------------------------------------------------------- Read releaseslist XML
15058: {
15059:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15060:     if (-e $file) {
15061:         my $parser = HTML::LCParser->new($file);
15062:         while (my $token = $parser->get_token()) {
15063:             if ($token->[0] eq 'S') {
15064:                 my $item = $token->[1];
15065:                 my $name = $token->[2]{'name'};
15066:                 my $value = $token->[2]{'value'};
15067:                 my $valuematch = $token->[2]{'valuematch'};
15068:                 my $namematch = $token->[2]{'namematch'};
15069:                 if ($item eq 'parameter') {
15070:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
15071:                         my $release = $parser->get_text();
15072:                         $release =~ s/(^\s*|\s*$ )//gx;
15073:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
15074:                     }
15075:                 } elsif ($item ne '' && $name ne '') {
15076:                     my $release = $parser->get_text();
15077:                     $release =~ s/(^\s*|\s*$ )//gx;
15078:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
15079:                 }
15080:             }
15081:         }
15082:     }
15083: }
15084: 
15085: # ---------------------------------------------------------- Read managers table
15086: {
15087:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
15088:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
15089:             while (my $configline=<$config>) {
15090:                 chomp($configline);
15091:                 next if ($configline =~ /^\#/);
15092:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15093:                     $managerstab{$configline} = 1;
15094:                 }
15095:             }
15096:             close($config);
15097:         }
15098:     }
15099: }
15100: 
15101: # ------------- set up temporary directory
15102: {
15103:     $tmpdir = LONCAPA::tempdir();
15104: 
15105: }
15106: 
15107: # ------------- set default texengine (domain default overrides this)
15108: {
15109:     $deftex = LONCAPA::texengine();
15110: }
15111: 
15112: # ------------- set default minimum length for passwords for internal auth users
15113: {
15114:     $passwdmin = LONCAPA::passwd_min();
15115: }
15116: 
15117: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
15118: 				'compress_threshold'=> 20_000,
15119:  			        });
15120: 
15121: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
15122: $dumpcount=0;
15123: $locknum=0;
15124: 
15125: &logtouch();
15126: &logthis('<font color="yellow">INFO: Read configuration</font>');
15127: $readit=1;
15128:     {
15129: 	use integer;
15130: 	my $test=(2**32)+1;
15131: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
15132: 	&logthis(" Detected 64bit platform ($_64bit)");
15133:     }
15134: }
15135: }
15136: 
15137: 1;
15138: __END__
15139: 
15140: =pod
15141: 
15142: =head1 NAME
15143: 
15144: Apache::lonnet - Subroutines to ask questions about things in the network.
15145: 
15146: =head1 SYNOPSIS
15147: 
15148: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
15149: 
15150:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15151: 
15152: Common parameters:
15153: 
15154: =over 4
15155: 
15156: =item *
15157: 
15158: $uname : an internal username (if $cname expecting a course Id specifically)
15159: 
15160: =item *
15161: 
15162: $udom : a domain (if $cdom expecting a course's domain specifically)
15163: 
15164: =item *
15165: 
15166: $symb : a resource instance identifier
15167: 
15168: =item *
15169: 
15170: $namespace : the name of a .db file that contains the data needed or
15171: being set.
15172: 
15173: =back
15174: 
15175: =head1 OVERVIEW
15176: 
15177: lonnet provides subroutines which interact with the
15178: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15179: about classes, users, and resources.
15180: 
15181: For many of these objects you can also use this to store data about
15182: them or modify them in various ways.
15183: 
15184: =head2 Symbs
15185: 
15186: To identify a specific instance of a resource, LON-CAPA uses symbols
15187: or "symbs"X<symb>. These identifiers are built from the URL of the
15188: map, the resource number of the resource in the map, and the URL of
15189: the resource itself. The latter is somewhat redundant, but might help
15190: if maps change.
15191: 
15192: An example is
15193: 
15194:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15195: 
15196: The respective map entry is
15197: 
15198:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
15199:   title="Problem 2">
15200:  </resource>
15201: 
15202: Symbs are used by the random number generator, as well as to store and
15203: restore data specific to a certain instance of for example a problem.
15204: 
15205: =head2 Storing And Retrieving Data
15206: 
15207: X<store()>X<cstore()>X<restore()>Three of the most important functions
15208: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15209: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15210: is is the non-critical message twin of cstore. These functions are for
15211: handlers to store a perl hash to a user's permanent data space in an
15212: easy manner, and to retrieve it again on another call. It is expected
15213: that a handler would use this once at the beginning to retrieve data,
15214: and then again once at the end to send only the new data back.
15215: 
15216: The data is stored in the user's data directory on the user's
15217: homeserver under the ID of the course.
15218: 
15219: The hash that is returned by restore will have all of the previous
15220: value for all of the elements of the hash.
15221: 
15222: Example:
15223: 
15224:  #creating a hash
15225:  my %hash;
15226:  $hash{'foo'}='bar';
15227: 
15228:  #storing it
15229:  &Apache::lonnet::cstore(\%hash);
15230: 
15231:  #changing a value
15232:  $hash{'foo'}='notbar';
15233: 
15234:  #adding a new value
15235:  $hash{'bar'}='foo';
15236:  &Apache::lonnet::cstore(\%hash);
15237: 
15238:  #retrieving the hash
15239:  my %history=&Apache::lonnet::restore();
15240: 
15241:  #print the hash
15242:  foreach my $key (sort(keys(%history))) {
15243:    print("\%history{$key} = $history{$key}");
15244:  }
15245: 
15246: Will print out:
15247: 
15248:  %history{1:foo} = bar
15249:  %history{1:keys} = foo:timestamp
15250:  %history{1:timestamp} = 990455579
15251:  %history{2:bar} = foo
15252:  %history{2:foo} = notbar
15253:  %history{2:keys} = foo:bar:timestamp
15254:  %history{2:timestamp} = 990455580
15255:  %history{bar} = foo
15256:  %history{foo} = notbar
15257:  %history{timestamp} = 990455580
15258:  %history{version} = 2
15259: 
15260: Note that the special hash entries C<keys>, C<version> and
15261: C<timestamp> were added to the hash. C<version> will be equal to the
15262: total number of versions of the data that have been stored. The
15263: C<timestamp> attribute will be the UNIX time the hash was
15264: stored. C<keys> is available in every historical section to list which
15265: keys were added or changed at a specific historical revision of a
15266: hash.
15267: 
15268: B<Warning>: do not store the hash that restore returns directly. This
15269: will cause a mess since it will restore the historical keys as if the
15270: were new keys. I.E. 1:foo will become 1:1:foo etc.
15271: 
15272: Calling convention:
15273: 
15274:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15275:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15276: 
15277: For more detailed information, see lonnet specific documentation.
15278: 
15279: =head1 RETURN MESSAGES
15280: 
15281: =over 4
15282: 
15283: =item * B<con_lost>: unable to contact remote host
15284: 
15285: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15286: when the connection is brought back up
15287: 
15288: =item * B<con_failed>: unable to contact remote host and unable to save message
15289: for later delivery
15290: 
15291: =item * B<error:>: an error a occurred, a description of the error follows the :
15292: 
15293: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15294: that was requested
15295: 
15296: =back
15297: 
15298: =head1 PUBLIC SUBROUTINES
15299: 
15300: =head2 Session Environment Functions
15301: 
15302: =over 4
15303: 
15304: =item * 
15305: X<appenv()>
15306: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15307: the user envirnoment file, and will be restored for each access this
15308: user makes during this session, also modifies the %env for the current
15309: process. Optional rolesarrayref - if defined contains a reference to an array
15310: of roles which are exempt from the restriction on modifying user.role entries 
15311: in the user's environment.db and in %env.    
15312: 
15313: =item *
15314: X<delenv()>
15315: B<delenv($delthis,$regexp)>: removes all items from the session
15316: environment file that begin with $delthis. If the 
15317: optional second arg - $regexp - is true, $delthis is treated as a 
15318: regular expression, otherwise \Q$delthis\E is used. 
15319: The values are also deleted from the current processes %env.
15320: 
15321: =item * get_env_multiple($name) 
15322: 
15323: gets $name from the %env hash, it seemlessly handles the cases where multiple
15324: values may be defined and end up as an array ref.
15325: 
15326: returns an array of values
15327: 
15328: =back
15329: 
15330: =head2 User Information
15331: 
15332: =over 4
15333: 
15334: =item *
15335: X<queryauthenticate()>
15336: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15337: authentication scheme
15338: 
15339: =item *
15340: X<authenticate()>
15341: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15342: authenticate user from domain's lib servers (first use the current
15343: one). C<$upass> should be the users password.
15344: $checkdefauth is optional (value is 1 if a check should be made to
15345:    authenticate user using default authentication method, and allow
15346:    account creation if username does not have account in the domain).
15347: $clientcancheckhost is optional (value is 1 if checking whether the
15348:    server can host will occur on the client side in lonauth.pm).   
15349: 
15350: =item *
15351: X<homeserver()>
15352: B<homeserver($uname,$udom)>: find the server which has
15353: the user's directory and files (there must be only one), this caches
15354: the answer, and also caches if there is a borken connection.
15355: 
15356: =item *
15357: X<idget()>
15358: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15359: a list of student/employee IDs or clicker IDs
15360: (student/employee IDs are a unique resource in a domain, there must be 
15361: only 1 ID per username, and only 1 username per ID in a specific domain).
15362: clickerIDs are not necessarily unique, as students might share clickers.
15363: (returns hash: id=>name,id=>name)
15364: 
15365: =item *
15366: X<idrget()>
15367: B<idrget($udom,@unames)>: find the IDs behind a list of
15368: usernames (returns hash: name=>id,name=>id)
15369: 
15370: =item *
15371: X<idput()>
15372: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15373: names and associated student/employee IDs or clicker IDs.
15374: 
15375: =item *
15376: X<iddel()>
15377: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15378: student/employee ID or clicker ID username look-ups from domain.
15379: The homeserver ($uhome) and namespace ($namespace) are optional.
15380: If no $uhome is provided, it will be determined usig &homeserver()
15381: for each user.  If no $namespace is provided, the default is ids.
15382: 
15383: =item *
15384: X<updateclickers()>
15385: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15386: clicker ID-to-username look-ups in clickers.db on library server.
15387: Permitted actions are add or del (i.e., add or delete). The 
15388: clickers.db contains clickerID as keys (escaped), and each corresponding
15389: value is an escaped comma-separated list of usernames (for whom the
15390: library server is the homeserver), who registered that particular ID.
15391: If $critical is true, the update will be sent via &critical, otherwise
15392: &reply() will be used.
15393: 
15394: =item *
15395: X<rolesinit()>
15396: B<rolesinit($udom,$username)>: get user privileges.
15397: returns user role, first access and timer interval hashes
15398: 
15399: =item *
15400: X<privileged()>
15401: B<privileged($username,$domain)>: returns a true if user has a
15402: privileged and active role (i.e. su or dc), false otherwise.
15403: 
15404: =item *
15405: X<getsection()>
15406: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15407: course $cname, return section name/number or '' for "not in course"
15408: and '-1' for "no section"
15409: 
15410: =item *
15411: X<userenvironment()>
15412: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15413: passed in @what from the requested user's environment, returns a hash
15414: 
15415: =item * 
15416: X<userlog_query()>
15417: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15418: activity.log file. %filters defines filters applied when parsing the
15419: log file. These can be start or end timestamps, or the type of action
15420: - log to look for Login or Logout events, check for Checkin or
15421: Checkout, role for role selection. The response is in the form
15422: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15423: escaped strings of the action recorded in the activity.log file.
15424: 
15425: =back
15426: 
15427: =head2 User Roles
15428: 
15429: =over 4
15430: 
15431: =item *
15432: 
15433: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15434: returns codes for allowed actions.
15435: 
15436: The first argument is required, all others are optional.
15437: 
15438: $priv is the privilege being checked.
15439: $uri contains additional information about what is being checked for access (e.g.,
15440: URL, course ID etc.). 
15441: $symb is the unique resource instance identifier in a course; if needed,
15442: but not provided, it will be retrieved via a call to &symbread(). 
15443: $role is the role for which a priv is being checked (only used if priv is evb). 
15444: $clientip is the user's IP address (only used when checking for access to portfolio 
15445: files).
15446: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15447: prevents recursive calls to &allowed.
15448: 
15449:  F: full access
15450:  U,I,K: authentication modes (cxx only)
15451:  '': forbidden
15452:  1: user needs to choose course
15453:  2: browse allowed
15454:  A: passphrase authentication needed
15455:  B: access temporarily blocked because of a blocking event in a course.
15456:  D: access blocked because access is required via session initiated via deep-link 
15457: 
15458: =item *
15459: 
15460: constructaccess($url,$setpriv) : check for access to construction space URL
15461: 
15462: See if the owner domain and name in the URL match those in the
15463: expected environment.  If so, return three element list
15464: ($ownername,$ownerdomain,$ownerhome).
15465: 
15466: Otherwise return the null string.
15467: 
15468: If second argument 'setpriv' is true, it assigns the privileges,
15469: and returns the same three element list, unless the owner has
15470: blocked "ad hoc" Domain Coordinator access to the Author Space,
15471: in which case the null string is returned.
15472: 
15473: =item *
15474: 
15475: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
15476: define a custom role rolename set privileges in format of lonTabs/roles.tab
15477: for system, domain, and course level. $uname and $udom are optional (current
15478: user's username and domain will be used when either of $uname or $udom are absent.
15479: 
15480: =item *
15481: 
15482: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
15483: (rolesplain.tab); plain text explanation of a user role term.
15484: $type is Course (default) or Community.
15485: If $forcedefault evaluates to true, text returned will be default 
15486: text for $type. Otherwise, if this is a course, the text returned 
15487: will be a custom name for the role (if defined in the course's 
15488: environment).  If no custom name is defined the default is returned.
15489:    
15490: =item *
15491: 
15492: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
15493: All arguments are optional. Returns a hash of a roles, either for
15494: co-author/assistant author roles for a user's Construction Space
15495: (default), or if $context is 'userroles', roles for the user himself,
15496: In the hash, keys are set to colon-separated $uname,$udom,$role, and
15497: (optionally) if $withsec is true, a fourth colon-separated item - $section.
15498: For each key, value is set to colon-separated start and end times for
15499: the role.  If no username and domain are specified, will default to
15500: current user/domain. Types, roles, and roledoms are references to arrays
15501: of role statuses (active, future or previous), roles 
15502: (e.g., cc,in, st etc.) and domains of the roles which can be used
15503: to restrict the list of roles reported. If no array ref is 
15504: provided for types, will default to return only active roles.
15505: 
15506: =item *
15507: 
15508: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
15509: user: $uname:$udom has a role in the course: $cdom_$cnum. 
15510: 
15511: Additional optional arguments are: $type (if role checking is to be restricted 
15512: to certain user status types -- previous (expired roles), active (currently
15513: available roles) or future (roles available in the future), and
15514: $hideprivileged -- if true will not report course roles for users who
15515: have active Domain Coordinator role in course's domain or in additional
15516: domains (specified in 'Domains to check for privileged users' in course
15517: environment -- set via:  Course Settings -> Classlists and staff listing).
15518: 
15519: =item *
15520: 
15521: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
15522: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
15523: $possdomains and $possroles are optional array refs -- to domains to check and
15524: roles to check.  If $possdomains is not specified, a dump will be done of the
15525: users' roles.db to check for a dc or su role in any domain. This can be
15526: time consuming if &privileged is called repeatedly (e.g., when displaying a
15527: classlist), so in such cases, supplying a $possdomains array is preferred, as
15528: this then allows &privileged_by_domain() to be used, which caches the identity
15529: of privileged users, eliminating the need for repeated calls to &dump().
15530: 
15531: =item *
15532: 
15533: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
15534: where the outer hash keys are domains specified in the $possdomains array ref,
15535: next inner hash keys are privileged roles specified in the $roles array ref,
15536: and the innermost hash contains key = value pairs for username:domain = end:start
15537: for active or future "privileged" users with that role in that domain. To avoid
15538: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
15539: innerhash are cached using priv_$role and $dom as the identifiers.
15540: 
15541: =back
15542: 
15543: =head2 User Modification
15544: 
15545: =over 4
15546: 
15547: =item *
15548: 
15549: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
15550: user for the level given by URL.  Optional start and end dates (leave empty
15551: string or zero for "no date")
15552: 
15553: =item *
15554: 
15555: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
15556: change a users, password, possible return values are: ok,
15557: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
15558: refused
15559: 
15560: =item *
15561: 
15562: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
15563: 
15564: =item *
15565: 
15566: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
15567:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
15568: 
15569: will update user information (firstname,middlename,lastname,generation,
15570: permanentemail), and if forceid is true, student/employee ID also.
15571: A user's institutional affiliation(s) can also be updated.
15572: User information fields will not be overwritten with empty entries 
15573: unless the field is included in the $candelete array reference.
15574: This array is included when a single user is modified via "Manage Users",
15575: or when Autoupdate.pl is run by cron in a domain.
15576: 
15577: =item *
15578: 
15579: modifystudent
15580: 
15581: modify a student's enrollment and identification information.
15582: The course id is resolved based on the current user's environment.  
15583: This means the invoking user must be a course coordinator or otherwise
15584: associated with a course.
15585: 
15586: This call is essentially a wrapper for lonnet::modifyuser and
15587: lonnet::modify_student_enrollment
15588: 
15589: Inputs: 
15590: 
15591: =over 4
15592: 
15593: =item B<$udom> Student's loncapa domain
15594: 
15595: =item B<$uname> Student's loncapa login name
15596: 
15597: =item B<$uid> Student/Employee ID
15598: 
15599: =item B<$umode> Student's authentication mode
15600: 
15601: =item B<$upass> Student's password
15602: 
15603: =item B<$first> Student's first name
15604: 
15605: =item B<$middle> Student's middle name
15606: 
15607: =item B<$last> Student's last name
15608: 
15609: =item B<$gene> Student's generation
15610: 
15611: =item B<$usec> Student's section in course
15612: 
15613: =item B<$end> Unix time of the roles expiration
15614: 
15615: =item B<$start> Unix time of the roles start date
15616: 
15617: =item B<$forceid> If defined, allow $uid to be changed
15618: 
15619: =item B<$desiredhome> server to use as home server for student
15620: 
15621: =item B<$email> Student's permanent e-mail address
15622: 
15623: =item B<$type> Type of enrollment (auto or manual)
15624: 
15625: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
15626: 
15627: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
15628: 
15629: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
15630: 
15631: =item B<$context> role change context (shown in User Management Logs display in a course)
15632: 
15633: =item B<$inststatus> institutional status of user - : separated string of escaped status types
15634: 
15635: =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.
15636: 
15637: =back
15638: 
15639: =item *
15640: 
15641: modify_student_enrollment
15642: 
15643: Change a student's enrollment status in a class.  The environment variable
15644: 'role.request.course' must be defined for this function to proceed.
15645: 
15646: Inputs:
15647: 
15648: =over 4
15649: 
15650: =item $udom, student's domain
15651: 
15652: =item $uname, student's name
15653: 
15654: =item $uid, student's user id
15655: 
15656: =item $first, student's first name
15657: 
15658: =item $middle
15659: 
15660: =item $last
15661: 
15662: =item $gene
15663: 
15664: =item $usec
15665: 
15666: =item $end
15667: 
15668: =item $start
15669: 
15670: =item $type
15671: 
15672: =item $locktype
15673: 
15674: =item $cid
15675: 
15676: =item $selfenroll
15677: 
15678: =item $context
15679: 
15680: =item $credits, number of credits student will earn from this class
15681: 
15682: =item $instsec, institutional course section code for student
15683: 
15684: =back
15685: 
15686: 
15687: =item *
15688: 
15689: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
15690: custom role; give a custom role to a user for the level given by URL.  Specify
15691: name and domain of role author, and role name
15692: 
15693: =item *
15694: 
15695: revokerole($udom,$uname,$url,$role) : revoke a role for url
15696: 
15697: =item *
15698: 
15699: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
15700: 
15701: =back
15702: 
15703: =head2 Course Infomation
15704: 
15705: =over 4
15706: 
15707: =item *
15708: 
15709: coursedescription($courseid,$options) : returns a hash of information about the
15710: specified course id, including all environment settings for the
15711: course, the description of the course will be in the hash under the
15712: key 'description'
15713: 
15714: $options is an optional parameter that if supplied is a hash reference that controls
15715: what how this function works.  It has the following key/values:
15716: 
15717: =over 4
15718: 
15719: =item freshen_cache
15720: 
15721: If defined, and the environment cache for the course is valid, it is 
15722: returned in the returned hash.
15723: 
15724: =item one_time
15725: 
15726: If defined, the last cache time is set to _now_
15727: 
15728: =item user
15729: 
15730: If defined, the supplied username is used instead of the current user.
15731: 
15732: 
15733: =back
15734: 
15735: =item *
15736: 
15737: resdata($name,$domain,$type,@which) : request for current parameter
15738: setting for a specific $type, where $type is either 'course' or 'user',
15739: @what should be a list of parameters to ask about. This routine caches
15740: answers for 10 minutes.
15741: 
15742: =item *
15743: 
15744: get_courseresdata($courseid, $domain) : dump the entire course resource
15745: data base, returning a hash that is keyed by the resource name and has
15746: values that are the resource value.  I believe that the timestamps and
15747: versions are also returned.
15748: 
15749: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
15750: supplemental content area. This routine caches the number of files for 
15751: 10 minutes.
15752: 
15753: =back
15754: 
15755: =head2 Course Modification
15756: 
15757: =over 4
15758: 
15759: =item *
15760: 
15761: writecoursepref($courseid,%prefs) : write preferences (environment
15762: database) for a course
15763: 
15764: =item *
15765: 
15766: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
15767: 
15768: =item *
15769: 
15770: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
15771: 
15772: =item *
15773: 
15774: is_course($courseid), is_course($cdom, $cnum)
15775: 
15776: Accepts either a combined $courseid (in the form of domain_courseid) or the
15777: two component version $cdom, $cnum. It checks if the specified course exists.
15778: 
15779: Returns:
15780:     undef if the course doesn't exist, otherwise
15781:     in scalar context the combined courseid.
15782:     in list context the two components of the course identifier, domain and 
15783:     courseid.    
15784: 
15785: =back
15786: 
15787: =head2 Bubblesheet Configuration
15788: 
15789: =over 4
15790: 
15791: =item *
15792: 
15793: get_scantron_config($which)
15794: 
15795: $which - the name of the configuration to parse from the file.
15796: 
15797: Parses and returns the bubblesheet configuration line selected as a
15798: hash of configuration file fields.
15799: 
15800: 
15801: Returns:
15802:     If the named configuration is not in the file, an empty
15803:     hash is returned.
15804: 
15805:     a hash with the fields
15806:       name         - internal name for the this configuration setup
15807:       description  - text to display to operator that describes this config
15808:       CODElocation - if 0 or the string 'none'
15809:                           - no CODE exists for this config
15810:                      if -1 || the string 'letter'
15811:                           - a CODE exists for this config and is
15812:                             a string of letters
15813:                      Unsupported value (but planned for future support)
15814:                           if a positive integer
15815:                                - The CODE exists as the first n items from
15816:                                  the question section of the form
15817:                           if the string 'number'
15818:                                - The CODE exists for this config and is
15819:                                  a string of numbers
15820:       CODEstart   - (only matter if a CODE exists) column in the line where
15821:                      the CODE starts
15822:       CODElength  - length of the CODE
15823:       IDstart     - column where the student/employee ID starts
15824:       IDlength    - length of the student/employee ID info
15825:       Qstart      - column where the information from the bubbled
15826:                     'questions' start
15827:       Qlength     - number of columns comprising a single bubble line from
15828:                     the sheet. (usually either 1 or 10)
15829:       Qon         - either a single character representing the character used
15830:                     to signal a bubble was chosen in the positional setup, or
15831:                     the string 'letter' if the letter of the chosen bubble is
15832:                     in the final, or 'number' if a number representing the
15833:                     chosen bubble is in the file (1->A 0->J)
15834:       Qoff        - the character used to represent that a bubble was
15835:                     left blank
15836:       PaperID     - if the scanning process generates a unique number for each
15837:                     sheet scanned the column that this ID number starts in
15838:       PaperIDlength - number of columns that comprise the unique ID number
15839:                       for the sheet of paper
15840:       FirstName   - column that the first name starts in
15841:       FirstNameLength - number of columns that the first name spans
15842:       LastName    - column that the last name starts in
15843:       LastNameLength - number of columns that the last name spans
15844:       BubblesPerRow - number of bubbles available in each row used to
15845:                       bubble an answer. (If not specified, 10 assumed).
15846: 
15847: 
15848: =item *
15849: 
15850: get_scantronformat_file($cdom)
15851: 
15852: $cdom - the course's domain (optional); if not supplied, uses
15853: domain for current $env{'request.course.id'}.
15854: 
15855: Returns an array containing lines from the scantron format file for
15856: the domain of the course.
15857: 
15858: If a url for a custom.tab file is listed in domain's configuration.db,
15859: lines are from this file.
15860: 
15861: Otherwise, if a default.tab has been published in RES space by the
15862: domainconfig user, lines are from this file.
15863: 
15864: Otherwise, fall back to getting lines from the legacy file on the
15865: local server:  /home/httpd/lonTabs/default_scantronformat.tab
15866: 
15867: =back
15868: 
15869: =head2 Resource Subroutines
15870: 
15871: =over 4
15872: 
15873: =item *
15874: 
15875: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
15876: 
15877: =item *
15878: 
15879: repcopy($filename) : subscribes to the requested file, and attempts to
15880: replicate from the owning library server, Might return
15881: 'unavailable', 'not_found', 'forbidden', 'ok', or
15882: 'bad_request', also attempts to grab the metadata for the
15883: resource. Expects the local filesystem pathname
15884: (/home/httpd/html/res/....)
15885: 
15886: =back
15887: 
15888: =head2 Resource Information
15889: 
15890: =over 4
15891: 
15892: =item *
15893: 
15894: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
15895: and returns the value of a variety of different possible values,
15896: $varname should be a request string, and the other parameters can be
15897: used to specify who and what one is asking about. Ordinarily, $cid 
15898: does not need to be specified, as it is retrived from 
15899: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
15900: within lonuserstate::loadmap() when initializing a course, before
15901: $env{'request.course.id'} has been set, so it needs to be provided
15902: in that one case.
15903: 
15904: Possible values for $varname are environment.lastname (or other item
15905: from the envirnment hash), user.name (or someother aspect about the
15906: user), resource.0.maxtries (or some other part and parameter of a
15907: resource)
15908: 
15909: =item *
15910: 
15911: directcondval($number) : get current value of a condition; reads from a state
15912: string
15913: 
15914: =item *
15915: 
15916: condval($condidx) : value of condition index based on state
15917: 
15918: =item *
15919: 
15920: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
15921: resource's metadata, $what should be either a specific key, or either
15922: 'keys' (to get a list of possible keys) or 'packages' to get a list of
15923: packages that this resource currently uses, the last 3 arguments are 
15924: only used internally for recursive metadata.
15925: 
15926: the toolsymb is only used where the uri is for an external tool (for which
15927: the uri as well as the symb are guaranteed to be unique).
15928: 
15929: this function automatically caches all requests except any made recursively
15930: to retrieve a list of metadata keys for an imported library file ($liburi is 
15931: defined).
15932: 
15933: =item *
15934: 
15935: metadata_query($query,$custom,$customshow) : make a metadata query against the
15936: network of library servers; returns file handle of where SQL and regex results
15937: will be stored for query
15938: 
15939: =item *
15940: 
15941: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
15942: return symbolic list entry (all arguments optional). 
15943: 
15944: Args: filename is the filename (including path) for the file for which a symb 
15945: is required; donotrecurse, if true will prevent calls to allowed() being made 
15946: to check access status if more than one resource was found in the bighash 
15947: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
15948: a randompick); ignorecachednull, if true will prevent a symb of '' being 
15949: returned if $env{$cache_str} is defined as ''; checkforblock if true will
15950: cause possible symbs to be checked to determine if they are subject to content
15951: blocking, if so they will not be included as possible symbs; possibles is a
15952: ref to a hash, which, as a side effect, will be populated with all possible 
15953: symbs (content blocking not tested).
15954:  
15955: returns the data handle
15956: 
15957: =item *
15958: 
15959: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
15960: and is a possible symb for the URL in $thisfn, and if is an encrypted
15961: resource that the user accessed using /enc/ returns a 1 on success, 0
15962: on failure, user must be in a course, as it assumes the existence of
15963: the course initial hash, and uses $env('request.course.id'}.  The third
15964: arg is an optional reference to a scalar.  If this arg is passed in the 
15965: call to symbverify, it will be set to 1 if the symb has been set to be 
15966: encrypted; otherwise it will be null.  
15967: 
15968: =item *
15969: 
15970: symbclean($symb) : removes versions numbers from a symb, returns the
15971: cleaned symb
15972: 
15973: =item *
15974: 
15975: is_on_map($uri) : checks if the $uri is somewhere on the current
15976: course map, user must be in a course for it to work.
15977: 
15978: =item *
15979: 
15980: numval($salt) : return random seed value (addend for rndseed)
15981: 
15982: =item *
15983: 
15984: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
15985: a random seed, all arguments are optional, if they aren't sent it uses the
15986: environment to derive them. Note: if symb isn't sent and it can't get one
15987: from &symbread it will use the current time as its return value
15988: 
15989: =item *
15990: 
15991: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
15992: unfakeable, receipt
15993: 
15994: =item *
15995: 
15996: receipt() : API to ireceipt working off of env values; given out to users
15997: 
15998: =item *
15999: 
16000: countacc($url) : count the number of accesses to a given URL
16001: 
16002: =item *
16003: 
16004: 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
16005: 
16006: =item *
16007: 
16008: 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)
16009: 
16010: =item *
16011: 
16012: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
16013: 
16014: =item *
16015: 
16016: devalidate($symb) : devalidate temporary spreadsheet calculations,
16017: forcing spreadsheet to reevaluate the resource scores next time.
16018: 
16019: =item * 
16020: 
16021: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16022: when viewing in course context.
16023: 
16024:  input: six args -- filename (decluttered), course number, course domain,
16025:                     url, symb (if registered) and group (if this is a 
16026:                     group item -- e.g., bulletin board, group page etc.).
16027: 
16028:  output: array of five scalars --
16029:          $cfile -- url for file editing if editable on current server
16030:          $home -- homeserver of resource (i.e., for author if published,
16031:                                           or course if uploaded.).
16032:          $switchserver --  1 if server switch will be needed.
16033:          $forceedit -- 1 if icon/link should be to go to edit mode 
16034:          $forceview -- 1 if icon/link should be to go to view mode
16035: 
16036: =item *
16037: 
16038: is_course_upload($file,$cnum,$cdom)
16039: 
16040: Used in course context to determine if current file was uploaded to 
16041: the course (i.e., would be found in /userfiles/docs on the course's 
16042: homeserver.
16043: 
16044:   input: 3 args -- filename (decluttered), course number and course domain.
16045:   output: boolean -- 1 if file was uploaded.
16046: 
16047: =back
16048: 
16049: =head2 Storing/Retreiving Data
16050: 
16051: =over 4
16052: 
16053: =item *
16054: 
16055: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16056: permanently for this url; hashref needs to be given and should be a \%hashname;
16057: the remaining args aren't required and if they aren't passed or are '' they will
16058: be derived from the env (with the exception of $laststore, which is an 
16059: optional arg used when a user's submission is stored in grading).
16060: $laststore is $version=$timestamp, where $version is the most recent version
16061: number retrieved for the corresponding $symb in the $namespace db file, and
16062: $timestamp is the timestamp for that transaction (UNIX time).
16063: $laststore is currently only passed when cstore() is called by 
16064: structuretags::finalize_storage().
16065: 
16066: =item *
16067: 
16068: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16069: but uses critical subroutine
16070: 
16071: =item *
16072: 
16073: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16074: all args are optional
16075: 
16076: =item *
16077: 
16078: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
16079: dumps the complete (or key matching regexp) namespace into a hash
16080: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16081: normally &store()ed into
16082: 
16083: $range should be either an integer '100' (give me the first 100
16084:                                            matching records)
16085:               or be  two integers sperated by a - with no spaces
16086:                  '30-50' (give me the 30th through the 50th matching
16087:                           records)
16088: 
16089: 
16090: =item *
16091: 
16092: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
16093: replaces a &store() version of data with a replacement set of data
16094: for a particular resource in a namespace passed in the $storehash hash 
16095: reference. If $tolog is true, the transaction is logged in the courselog
16096: with an action=PUTSTORE.
16097: 
16098: =item *
16099: 
16100: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16101: works very similar to store/cstore, but all data is stored in a
16102: temporary location and can be reset using tmpreset, $storehash should
16103: be a hash reference, returns nothing on success
16104: 
16105: =item *
16106: 
16107: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16108: similar to restore, but all data is stored in a temporary location and
16109: can be reset using tmpreset. Returns a hash of values on success,
16110: error string otherwise.
16111: 
16112: =item *
16113: 
16114: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16115: deltes all keys for $symb form the temporary storage hash.
16116: 
16117: =item *
16118: 
16119: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16120: reference filled in from namesp ($udom and $uname are optional)
16121: 
16122: =item *
16123: 
16124: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16125: namesp ($udom and $uname are optional)
16126: 
16127: =item *
16128: 
16129: dump($namespace,$udom,$uname,$regexp,$range) : 
16130: dumps the complete (or key matching regexp) namespace into a hash
16131: ($udom, $uname, $regexp, $range are optional)
16132: 
16133: $range should be either an integer '100' (give me the first 100
16134:                                            matching records)
16135:               or be  two integers sperated by a - with no spaces
16136:                  '30-50' (give me the 30th through the 50th matching
16137:                           records)
16138: =item *
16139: 
16140: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16141: $store can be a scalar, an array reference, or if the amount to be 
16142: incremented is > 1, a hash reference.
16143: 
16144: ($udom and $uname are optional)
16145: 
16146: =item *
16147: 
16148: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16149: ($udom and $uname are optional)
16150: 
16151: =item *
16152: 
16153: cput($namespace,$storehash,$udom,$uname) : critical put
16154: ($udom and $uname are optional)
16155: 
16156: =item *
16157: 
16158: newput($namespace,$storehash,$udom,$uname) :
16159: 
16160: Attempts to store the items in the $storehash, but only if they don't
16161: currently exist, if this succeeds you can be certain that you have 
16162: successfully created a new key value pair in the $namespace db.
16163: 
16164: 
16165: Args:
16166:  $namespace: name of database to store values to
16167:  $storehash: hashref to store to the db
16168:  $udom: (optional) domain of user containing the db
16169:  $uname: (optional) name of user caontaining the db
16170: 
16171: Returns:
16172:  'ok' -> succeeded in storing all keys of $storehash
16173:  'key_exists: <key>' -> failed to anything out of $storehash, as at
16174:                         least <key> already existed in the db (other
16175:                         requested keys may also already exist)
16176:  'error: <msg>' -> unable to tie the DB or other error occurred
16177:  'con_lost' -> unable to contact request server
16178:  'refused' -> action was not allowed by remote machine
16179: 
16180: 
16181: =item *
16182: 
16183: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16184: reference filled in from namesp (encrypts the return communication)
16185: ($udom and $uname are optional)
16186: 
16187: =item *
16188: 
16189: log($udom,$name,$home,$message) : write to permanent log for user; use
16190: critical subroutine
16191: 
16192: =item *
16193: 
16194: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16195: array reference filled in from namespace found in domain level on either
16196: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
16197: 
16198: =item *
16199: 
16200: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
16201: domain level either on specified domain server ($uhome) or primary domain 
16202: server ($udom and $uhome are optional)
16203: 
16204: =item * 
16205: 
16206: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
16207: for: authentication, language, quotas, timezone, date locale, and portal URL in
16208: the target domain.
16209: 
16210: May also include additional key => value pairs for the following groups:
16211: 
16212: =over
16213: 
16214: =item
16215: disk quotas (MB allocated by default to portfolios and authoring spaces).
16216: 
16217: =over
16218: 
16219: =item defaultquota, authorquota
16220: 
16221: =back
16222: 
16223: =item
16224: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16225: portfolio for users).
16226: 
16227: =over
16228: 
16229: =item
16230: aboutme, blog, webdav, portfolio
16231: 
16232: =back
16233: 
16234: =item
16235: requestcourses: ability to request courses, and how requests are processed.
16236: 
16237: =over
16238: 
16239: =item
16240: official, unofficial, community, textbook, placement
16241: 
16242: =back
16243: 
16244: =item
16245: inststatus: types of institutional affiliation, and order in which they are displayed.
16246: 
16247: =over
16248: 
16249: =item
16250: inststatustypes, inststatusorder, inststatusguest
16251: 
16252: =back
16253: 
16254: =item
16255: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16256: for course's uploaded content.
16257: 
16258: =over
16259: 
16260: =item
16261: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16262: communityquota, textbookquota, placementquota
16263: 
16264: =back
16265: 
16266: =item
16267: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16268: on your servers.
16269: 
16270: =over
16271: 
16272: =item 
16273: remotesessions, hostedsessions
16274: 
16275: =back
16276: 
16277: =back
16278: 
16279: In cases where a domain coordinator has never used the "Set Domain Configuration"
16280: utility to create a configuration.db file on a domain's primary library server 
16281: only the following domain defaults: auth_def, auth_arg_def, lang_def
16282: -- corresponding values are authentication type (internal, krb4, krb5,
16283: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16284: will be available. Values are retrieved from cache (if current), unless the
16285: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16286: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16287: 
16288: Typical usage:
16289: 
16290: %domdefaults = &get_domain_defaults($target_domain);
16291: 
16292: =back
16293: 
16294: =head2 Network Status Functions
16295: 
16296: =over 4
16297: 
16298: =item *
16299: 
16300: dirlist() : return directory list based on URI (first arg).
16301: 
16302: Inputs: 1 required, 5 optional.
16303: 
16304: =over
16305: 
16306: =item 
16307: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16308: 
16309: =item
16310: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16311: 
16312: =item
16313: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16314: 
16315: =item
16316: $getpropath - boolean: 1 if prepend path using &propath(). 
16317: 
16318: =item
16319: $getuserdir - boolean: 1 if prepend path for "userfiles".
16320: 
16321: =item 
16322: $alternateRoot - path to prepend in place of path from $uri.
16323: 
16324: =back
16325: 
16326: Returns: Array of up to two items.
16327: 
16328: =over
16329: 
16330: a reference to an array of files/subdirectories
16331: 
16332: =over
16333: 
16334: Each element in the array of files/subdirectories is a & separated list of
16335: item name and the result of running stat on the item.  If dirlist was requested
16336: for a file instead of a directory, the item name will be ''. For a directory 
16337: listing, if the item is a metadata file, the element will end &N&M 
16338: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16339: default copyright set (1).  
16340: 
16341: =back
16342: 
16343: a scalar containing error condition (if encountered).
16344: 
16345: =over
16346: 
16347: =item 
16348: no_host (no homeserver identified for $username:$domain).
16349: 
16350: =item 
16351: no_such_host (server contacted for listing not identified as valid host).
16352: 
16353: =item 
16354: con_lost (connection to remote server failed).
16355: 
16356: =item 
16357: refused (invalid $username:$domain received on lond side).
16358: 
16359: =item 
16360: no_such_dir (directory at specified path on lond side does not exist). 
16361: 
16362: =item 
16363: empty (directory at specified path on lond side is empty).
16364: 
16365: =over
16366: 
16367: This is currently not encountered because the &ls3, &ls2, 
16368: &ls (_handler) routines on the lond side do not filter out
16369: . and .. from a directory listing. 
16370: 
16371: =back
16372: 
16373: =back
16374: 
16375: =back
16376: 
16377: =item *
16378: 
16379: spareserver() : find server with least workload from spare.tab
16380: 
16381: 
16382: =item *
16383: 
16384: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16385: if there is no corresponding loncapa host.
16386: 
16387: =back
16388: 
16389: 
16390: =head2 Apache Request
16391: 
16392: =over 4
16393: 
16394: =item *
16395: 
16396: ssi($url,%hash) : server side include, does a complete request cycle on url to
16397: localhost, posts hash
16398: 
16399: =back
16400: 
16401: =head2 Data to String to Data
16402: 
16403: =over 4
16404: 
16405: =item *
16406: 
16407: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16408: and '&' separators, supports elements that are arrayrefs and hashrefs
16409: 
16410: =item *
16411: 
16412: hashref2str($hashref) : convert a hashref into a string complete with
16413: escaping and '=' and '&' separators, supports elements that are
16414: arrayrefs and hashrefs
16415: 
16416: =item *
16417: 
16418: arrayref2str($arrayref) : convert an arrayref into a string complete
16419: with escaping and '&' separators, supports elements that are arrayrefs
16420: and hashrefs
16421: 
16422: =item *
16423: 
16424: str2hash($string) : convert string to hash using unescaping and
16425: splitting on '=' and '&', supports elements that are arrayrefs and
16426: hashrefs
16427: 
16428: =item *
16429: 
16430: str2array($string) : convert string to hash using unescaping and
16431: splitting on '&', supports elements that are arrayrefs and hashrefs
16432: 
16433: =back
16434: 
16435: =head2 Logging Routines
16436: 
16437: 
16438: These routines allow one to make log messages in the lonnet.log and
16439: lonnet.perm logfiles.
16440: 
16441: =over 4
16442: 
16443: =item *
16444: 
16445: logtouch() : make sure the logfile, lonnet.log, exists
16446: 
16447: =item *
16448: 
16449: logthis() : append message to the normal lonnet.log file, it gets
16450: preiodically rolled over and deleted.
16451: 
16452: =item *
16453: 
16454: logperm() : append a permanent message to lonnet.perm.log, this log
16455: file never gets deleted by any automated portion of the system, only
16456: messages of critical importance should go in here.
16457: 
16458: 
16459: =back
16460: 
16461: =head2 General File Helper Routines
16462: 
16463: =over 4
16464: 
16465: =item *
16466: 
16467: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
16468: (a) files in /uploaded
16469:   (i) If a local copy of the file exists - 
16470:       compares modification date of local copy with last-modified date for 
16471:       definitive version stored on home server for course. If local copy is 
16472:       stale, requests a new version from the home server and stores it. 
16473:       If the original has been removed from the home server, then local copy 
16474:       is unlinked.
16475:   (ii) If local copy does not exist -
16476:       requests the file from the home server and stores it. 
16477:   
16478:   If $caller is 'uploadrep':  
16479:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
16480:     for request for files originally uploaded via DOCS. 
16481:      - returns 'ok' if fresh local copy now available, -1 otherwise.
16482:   
16483:   Otherwise:
16484:      This indicates a call from the content generation phase of the request.
16485:      -  returns the entire contents of the file or -1.
16486:      
16487: (b) files in /res
16488:    - returns the entire contents of a file or -1; 
16489:    it properly subscribes to and replicates the file if neccessary.
16490: 
16491: 
16492: =item *
16493: 
16494: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
16495:                   reference
16496: 
16497: returns either a stat() list of data about the file or an empty list
16498: if the file doesn't exist or couldn't find out about it (connection
16499: problems or user unknown)
16500: 
16501: =item *
16502: 
16503: filelocation($dir,$file) : returns file system location of a file
16504: based on URI; meant to be "fairly clean" absolute reference, $dir is a
16505: directory that relative $file lookups are to looked in ($dir of /a/dir
16506: and a file of ../bob will become /a/bob)
16507: 
16508: =item *
16509: 
16510: hreflocation($dir,$file) : returns file system location or a URL; same as
16511: filelocation except for hrefs
16512: 
16513: =item *
16514: 
16515: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
16516: also removes beginning /home/httpd/html unless /priv/ follows it.
16517: 
16518: =back
16519: 
16520: =head2 Usererfile file routines (/uploaded*)
16521: 
16522: =over 4
16523: 
16524: =item *
16525: 
16526: userfileupload(): main rotine for putting a file in a user or course's
16527:                   filespace, arguments are,
16528: 
16529:  formname - required - this is the name of the element in $env where the
16530:            filename, and the contents of the file to create/modifed exist
16531:            the filename is in $env{'form.'.$formname.'.filename'} and the
16532:            contents of the file is located in $env{'form.'.$formname}
16533:  context - if coursedoc, store the file in the course of the active role
16534:              of the current user; 
16535:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
16536:            if 'canceloverwrite': delete file in tmp/overwrites directory
16537:  subdir - required - subdirectory to put the file in under ../userfiles/
16538:          if undefined, it will be placed in "unknown"
16539: 
16540:  (This routine calls clean_filename() to remove any dangerous
16541:  characters from the filename, and then calls finuserfileupload() to
16542:  complete the transaction)
16543: 
16544:  returns either the url of the uploaded file (/uploaded/....) if successful
16545:  and /adm/notfound.html if unsuccessful
16546: 
16547: =item *
16548: 
16549: clean_filename(): routine for cleaing a filename up for storage in
16550:                  userfile space, argument is:
16551: 
16552:  filename - proposed filename
16553: 
16554: returns: the new clean filename
16555: 
16556: =item *
16557: 
16558: finishuserfileupload(): routine that creates and sends the file to
16559: userspace, probably shouldn't be called directly
16560: 
16561:   docuname: username or courseid of destination for the file
16562:   docudom: domain of user/course of destination for the file
16563:   formname: same as for userfileupload()
16564:   fname: filename (including subdirectories) for the file
16565:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
16566:           if hashref, and context is scantron, will convert csv format to standard format
16567:   allfiles: reference to hash used to store objects found by parser
16568:   codebase: reference to hash used for codebases of java objects found by parser
16569:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
16570:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
16571:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
16572:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
16573:   context: if 'overwrite', will move the uploaded file from its temporary location to
16574:             userfiles to facilitate overwriting a previously uploaded file with same name.
16575:   mimetype: reference to scalar to accommodate mime type determined
16576:             from File::MMagic if $parser = parse.
16577: 
16578:  returns either the url of the uploaded file (/uploaded/....) if successful
16579:  and /adm/notfound.html if unsuccessful (or an error message if context 
16580:  was 'overwrite').
16581:  
16582: 
16583: =item *
16584: 
16585: renameuserfile(): renames an existing userfile to a new name
16586: 
16587:   Args:
16588:    docuname: username or courseid of destination for the file
16589:    docudom: domain of user/course of destination for the file
16590:    old: current file name (including any subdirs under userfiles)
16591:    new: desired file name (including any subdirs under userfiles)
16592: 
16593: =item *
16594: 
16595: mkdiruserfile(): creates a directory is a userfiles dir
16596: 
16597:   Args:
16598:    docuname: username or courseid of destination for the file
16599:    docudom: domain of user/course of destination for the file
16600:    dir: dir to create (including any subdirs under userfiles)
16601: 
16602: =item *
16603: 
16604: removeuserfile(): removes a file that exists in userfiles
16605: 
16606:   Args:
16607:    docuname: username or courseid of destination for the file
16608:    docudom: domain of user/course of destination for the file
16609:    fname: filname to delete (including any subdirs under userfiles)
16610: 
16611: =item *
16612: 
16613: removeuploadedurl(): convience function for removeuserfile()
16614: 
16615:   Args:
16616:    url:  a full /uploaded/... url to delete
16617: 
16618: =item * 
16619: 
16620: get_portfile_permissions():
16621:   Args:
16622:     domain: domain of user or course contain the portfolio files
16623:     user: name of user or num of course contain the portfolio files
16624:   Returns:
16625:     hashref of a dump of the proper file_permissions.db
16626:    
16627: 
16628: =item * 
16629: 
16630: get_access_controls():
16631: 
16632: Args:
16633:   current_permissions: the hash ref returned from get_portfile_permissions()
16634:   group: (optional) the group you want the files associated with
16635:   file: (optional) the file you want access info on
16636: 
16637: Returns:
16638:     a hash (keys are file names) of hashes containing
16639:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
16640:         values are XML containing access control settings (see below) 
16641: 
16642: Internal notes:
16643: 
16644:  access controls are stored in file_permissions.db as key=value pairs.
16645:     key -> path to file/file_name\0uniqueID:scope_end_start
16646:         where scope -> public,guest,course,group,domains or users.
16647:               end -> UNIX time for end of access (0 -> no end date)
16648:               start -> UNIX time for start of access
16649: 
16650:     value -> XML description of access control
16651:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
16652:             <start></start>
16653:             <end></end>
16654: 
16655:             <password></password>  for scope type = guest
16656: 
16657:             <domain></domain>     for scope type = course or group
16658:             <number></number>
16659:             <roles id="">
16660:              <role></role>
16661:              <access></access>
16662:              <section></section>
16663:              <group></group>
16664:             </roles>
16665: 
16666:             <dom></dom>         for scope type = domains
16667: 
16668:             <users>             for scope type = users
16669:              <user>
16670:               <uname></uname>
16671:               <udom></udom>
16672:              </user>
16673:             </users>
16674:            </scope> 
16675:               
16676:  Access data is also aggregated for each file in an additional key=value pair:
16677:  key -> path to file/file_name\0accesscontrol 
16678:  value -> reference to hash
16679:           hash contains key = value pairs
16680:           where key = uniqueID:scope_end_start
16681:                 value = UNIX time record was last updated
16682: 
16683:           Used to improve speed of look-ups of access controls for each file.  
16684:  
16685:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
16686: 
16687: =item *
16688: 
16689: modify_access_controls():
16690: 
16691: Modifies access controls for a portfolio file
16692: Args
16693: 1. file name
16694: 2. reference to hash of required changes,
16695: 3. domain
16696: 4. username
16697:   where domain,username are the domain of the portfolio owner 
16698:   (either a user or a course) 
16699: 
16700: Returns:
16701: 1. result of additions or updates ('ok' or 'error', with error message). 
16702: 2. result of deletions ('ok' or 'error', with error message).
16703: 3. reference to hash of any new or updated access controls.
16704: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
16705:    key = integer (inbound ID)
16706:    value = uniqueID
16707: 
16708: =item *
16709: 
16710: get_timebased_id():
16711: 
16712: Attempts to get a unique timestamp-based suffix for use with items added to a 
16713: course via the Course Editor (e.g., folders, composite pages, 
16714: group bulletin boards).
16715: 
16716: Args: (first three required; six others optional)
16717: 
16718: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
16719:    docssequence, or name of group
16720: 
16721: 2. keyid (alphanumeric): name of temporary locking key in hash,
16722:    e.g., num, boardids
16723: 
16724: 3. namespace: name of gdbm file used to store suffixes already assigned;  
16725:    file will be named nohist_namespace.db
16726: 
16727: 4. cdom: domain of course; default is current course domain from %env
16728: 
16729: 5. cnum: course number; default is current course number from %env
16730: 
16731: 6. idtype: set to concat if an additional digit is to be appended to the 
16732:    unix timestamp to form the suffix, if the plain timestamp is already
16733:    in use.  Default is to not do this, but simply increment the unix 
16734:    timestamp by 1 until a unique key is obtained.
16735: 
16736: 7. who: holder of locking key; defaults to user:domain for user.
16737: 
16738: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
16739:    retrying); default is 3.
16740: 
16741: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
16742: 
16743: Returns:
16744: 
16745: 1. suffix obtained (numeric)
16746: 
16747: 2. result of deleting locking key (ok if deleted, or lock never obtained)
16748: 
16749: 3. error: contains (localized) error message if an error occurred.
16750: 
16751: 
16752: =back
16753: 
16754: =head2 HTTP Helper Routines
16755: 
16756: =over 4
16757: 
16758: =item *
16759: 
16760: escape() : unpack non-word characters into CGI-compatible hex codes
16761: 
16762: =item *
16763: 
16764: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
16765: 
16766: =back
16767: 
16768: =head1 PRIVATE SUBROUTINES
16769: 
16770: =head2 Underlying communication routines (Shouldn't call)
16771: 
16772: =over 4
16773: 
16774: =item *
16775: 
16776: subreply() : tries to pass a message to lonc, returns con_lost if incapable
16777: 
16778: =item *
16779: 
16780: reply() : uses subreply to send a message to remote machine, logs all failures
16781: 
16782: =item *
16783: 
16784: critical() : passes a critical message to another server; if cannot
16785: get through then place message in connection buffer directory and
16786: returns con_delayed, if incapable of saving message, returns
16787: con_failed
16788: 
16789: =item *
16790: 
16791: reconlonc() : tries to reconnect lonc client processes.
16792: 
16793: =back
16794: 
16795: =head2 Resource Access Logging
16796: 
16797: =over 4
16798: 
16799: =item *
16800: 
16801: flushcourselogs() : flush (save) buffer logs and access logs
16802: 
16803: =item *
16804: 
16805: courselog($what) : save message for course in hash
16806: 
16807: =item *
16808: 
16809: courseacclog($what) : save message for course using &courselog().  Perform
16810: special processing for specific resource types (problems, exams, quizzes, etc).
16811: 
16812: =item *
16813: 
16814: goodbye() : flush course logs and log shutting down; it is called in srm.conf
16815: as a PerlChildExitHandler
16816: 
16817: =back
16818: 
16819: =head2 Other
16820: 
16821: =over 4
16822: 
16823: =item *
16824: 
16825: symblist($mapname,%newhash) : update symbolic storage links
16826: 
16827: =back
16828: 
16829: =cut
16830: 

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