File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1447: download - view: text, annotated - select for diffs
Sun Apr 18 02:08:47 2021 UTC (3 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914
 - Update domain config UI to support settings documented in
   Domain_Configuration_WAF_Proxy.tex
 - Use /adm/migrateuser to handle redirects when user with a LON-CAPA session
   switches between access via VPN and not via VPN (and vice versa).

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1447 2021/04/18 02:08:47 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::lonnet.pm
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: This file is an interface to the lonc processes of
   39: the LON-CAPA network as well as set of elaborated functions for handling information
   40: necessary for navigating through a given cluster of LON-CAPA machines within a
   41: domain. There are over 40 specialized functions in this module which handle the
   42: reading and transmission of metadata, user information (ids, names, environments, roles,
   43: logs), file information (storage, reading, directories, extensions, replication, embedded
   44: styles and descriptors), educational resources (course descriptions, section names and
   45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
   46: and from more descriptive phrases or explanations.
   47: 
   48: This is part of the LearningOnline Network with CAPA project
   49: described at http://www.lon-capa.org.
   50: 
   51: =head1 Package Variables
   52: 
   53: These are largely undocumented, so if you decipher one please note it here.
   54: 
   55: =over 4
   56: 
   57: =item $processmarker
   58: 
   59: Contains the time this process was started and this servers host id.
   60: 
   61: =item $dumpcount
   62: 
   63: Counts the number of times a message log flush has been attempted (regardless
   64: of success) by this process.  Used as part of the filename when messages are
   65: delayed.
   66: 
   67: =back
   68: 
   69: =cut
   70: 
   71: package Apache::lonnet;
   72: 
   73: use strict;
   74: use HTTP::Date;
   75: use Image::Magick;
   76: use CGI::Cookie;
   77: 
   78: use Encode;
   79: 
   80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
   81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
   82:             %managerstab $passwdmin);
   83: 
   84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
   85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
   86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
   87:     %courseownerbuf, %coursetypebuf,$locknum);
   88: 
   89: use IO::Socket;
   90: use GDBM_File;
   91: use HTML::LCParser;
   92: use Fcntl qw(:flock);
   93: use Storable qw(thaw nfreeze);
   94: use Time::HiRes qw( sleep gettimeofday tv_interval );
   95: use Cache::Memcached;
   96: use Digest::MD5;
   97: use Math::Random;
   98: use File::MMagic;
   99: use Net::CIDR;
  100: use LONCAPA qw(:DEFAULT :match);
  101: use LONCAPA::Configuration;
  102: use LONCAPA::lonmetadata;
  103: use LONCAPA::Lond;
  104: use LONCAPA::LWPReq;
  105: use LONCAPA::transliterate;
  106: 
  107: use File::Copy;
  108: 
  109: my $readit;
  110: my $max_connection_retries = 20;     # Or some such value.
  111: 
  112: require Exporter;
  113: 
  114: our @ISA = qw (Exporter);
  115: our @EXPORT = qw(%env);
  116: 
  117: 
  118: # ------------------------------------ Logging (parameters, docs, slots, roles)
  119: {
  120:     my $logid;
  121:     sub write_log {
  122: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  123:         if ($context eq 'course') {
  124:             if (($cnum eq '') || ($cdom eq '')) {
  125:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  126:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  127:             }
  128:         }
  129: 	$logid ++;
  130:         my $now = time();
  131: 	my $id=$now.'00000'.$$.'00000'.$logid;
  132:         my $ip = &get_requestor_ip();
  133:         my $logentry = { 
  134:                           $id => {
  135:                                    'exe_uname' => $env{'user.name'},
  136:                                    'exe_udom'  => $env{'user.domain'},
  137:                                    'exe_time'  => $now,
  138:                                    'exe_ip'    => $ip,
  139:                                    'delflag'   => $delflag,
  140:                                    'logentry'  => $storehash,
  141:                                    'uname'     => $uname,
  142:                                    'udom'      => $udom,
  143:                                   }
  144:                        };
  145: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  146:     }
  147: }
  148: 
  149: sub logtouch {
  150:     my $execdir=$perlvar{'lonDaemons'};
  151:     unless (-e "$execdir/logs/lonnet.log") {	
  152: 	open(my $fh,">>","$execdir/logs/lonnet.log");
  153: 	close $fh;
  154:     }
  155:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  156:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  157: }
  158: 
  159: sub logthis {
  160:     my $message=shift;
  161:     my $execdir=$perlvar{'lonDaemons'};
  162:     my $now=time;
  163:     my $local=localtime($now);
  164:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
  165: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  166: 	print $fh $logstring;
  167: 	close($fh);
  168:     }
  169:     return 1;
  170: }
  171: 
  172: sub logperm {
  173:     my $message=shift;
  174:     my $execdir=$perlvar{'lonDaemons'};
  175:     my $now=time;
  176:     my $local=localtime($now);
  177:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
  178: 	print $fh "$now:$message:$local\n";
  179: 	close($fh);
  180:     }
  181:     return 1;
  182: }
  183: 
  184: sub create_connection {
  185:     my ($hostname,$lonid) = @_;
  186:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  187: 				     Type    => SOCK_STREAM,
  188: 				     Timeout => 10);
  189:     return 0 if (!$client);
  190:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
  191:     my $result = <$client>;
  192:     chomp($result);
  193:     return 1 if ($result eq 'done');
  194:     return 0;
  195: }
  196: 
  197: sub get_server_timezone {
  198:     my ($cnum,$cdom) = @_;
  199:     my $home=&homeserver($cnum,$cdom);
  200:     if ($home ne 'no_host') {
  201:         my $cachetime = 24*3600;
  202:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  203:         if (defined($cached)) {
  204:             return $timezone;
  205:         } else {
  206:             my $timezone = &reply('servertimezone',$home);
  207:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  208:         }
  209:     }
  210: }
  211: 
  212: sub get_server_distarch {
  213:     my ($lonhost,$ignore_cache) = @_;
  214:     if (defined($lonhost)) {
  215:         if (!defined(&hostname($lonhost))) {
  216:             return;
  217:         }
  218:         my $cachetime = 12*3600;
  219:         if (!$ignore_cache) {
  220:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  221:             if (defined($cached)) {
  222:                 return $distarch;
  223:             }
  224:         }
  225:         my $rep = &reply('serverdistarch',$lonhost);
  226:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  227:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  228:                 $rep eq '') {
  229:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  230:         }
  231:     }
  232:     return;
  233: }
  234: 
  235: sub get_servercerts_info {
  236:     my ($lonhost,$hostname,$context) = @_;
  237:     return if ($lonhost eq '');
  238:     if ($hostname eq '') {
  239:         $hostname = &hostname($lonhost);
  240:     }
  241:     return if ($hostname eq '');
  242:     my ($rep,$uselocal);
  243:     if ($context eq 'install') {
  244:         $uselocal = 1;
  245:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  246:         $uselocal = 1;
  247:     }
  248:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  249:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  250:         if ($distro eq '') {
  251:             $uselocal = 0;
  252:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  253:             if ($1 < 6) {
  254:                 $uselocal = 0;
  255:             }
  256:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  257:             if ($1 < 12) {
  258:                 $uselocal = 0;
  259:             }
  260:         }
  261:     }
  262:     if ($uselocal) {
  263:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  264:     } else {
  265:         $rep=&reply('servercerts',$lonhost);
  266:     }
  267:     my ($result,%returnhash);
  268:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  269:         ($rep eq 'unknown_cmd')) {
  270:         $result = $rep;
  271:     } else {
  272:         $result = 'ok';
  273:         my @pairs=split(/\&/,$rep);
  274:         foreach my $item (@pairs) {
  275:             my ($key,$value)=split(/=/,$item,2);
  276:             my $what = &unescape($key);
  277:             $returnhash{$what}=&thaw_unescape($value);
  278:         }
  279:     }
  280:     return ($result,\%returnhash);
  281: }
  282: 
  283: sub get_server_loncaparev {
  284:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  285:     if (defined($lonhost)) {
  286:         if (!defined(&hostname($lonhost))) {
  287:             undef($lonhost);
  288:         }
  289:     }
  290:     if (!defined($lonhost)) {
  291:         if (defined(&domain($dom,'primary'))) {
  292:             $lonhost=&domain($dom,'primary');
  293:             if ($lonhost eq 'no_host') {
  294:                 undef($lonhost);
  295:             }
  296:         }
  297:     }
  298:     if (defined($lonhost)) {
  299:         my $cachetime = 12*3600;
  300:         if (!$ignore_cache) {
  301:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  302:             if (defined($cached)) {
  303:                 return $loncaparev;
  304:             }
  305:         }
  306:         my ($answer,$loncaparev);
  307:         my @ids=&current_machine_ids();
  308:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  309:             $answer = $perlvar{'lonVersion'};
  310:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  311:                 $loncaparev = $1;
  312:             }
  313:         } else {
  314:             $answer = &reply('serverloncaparev',$lonhost);
  315:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  316:                 if ($caller eq 'loncron') {
  317:                     my $hostname = &hostname($lonhost);
  318:                     my $protocol = $protocol{$lonhost};
  319:                     $protocol = 'http' if ($protocol ne 'https');
  320:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  321:                     my $request=new HTTP::Request('GET',$url);
  322:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  323:                     unless ($response->is_error()) {
  324:                         my $content = $response->content;
  325:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  326:                             $loncaparev = $1;
  327:                         }
  328:                     }
  329:                 } else {
  330:                     $loncaparev = $loncaparevs{$lonhost};
  331:                 }
  332:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  333:                 $loncaparev = $1;
  334:             }
  335:         }
  336:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  337:     }
  338: }
  339: 
  340: sub get_server_homeID {
  341:     my ($hostname,$ignore_cache,$caller) = @_;
  342:     unless ($ignore_cache) {
  343:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  344:         if (defined($cached)) {
  345:             return $serverhomeID;
  346:         }
  347:     }
  348:     my $cachetime = 12*3600;
  349:     my $serverhomeID;
  350:     if ($caller eq 'loncron') { 
  351:         my @machine_ids = &machine_ids($hostname);
  352:         foreach my $id (@machine_ids) {
  353:             my $response = &reply('serverhomeID',$id);
  354:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  355:                 $serverhomeID = $response;
  356:                 last;
  357:             }
  358:         }
  359:         if ($serverhomeID eq '') {
  360:             $serverhomeID = $machine_ids[-1];
  361:         }
  362:     } else {
  363:         $serverhomeID = $serverhomeIDs{$hostname};
  364:     }
  365:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  366: }
  367: 
  368: sub get_remote_globals {
  369:     my ($lonhost,$whathash,$ignore_cache) = @_;
  370:     my ($result,%returnhash,%whatneeded);
  371:     if (ref($whathash) eq 'HASH') {
  372:         foreach my $what (sort(keys(%{$whathash}))) {
  373:             my $hashid = $lonhost.'-'.$what;
  374:             my ($response,$cached);
  375:             unless ($ignore_cache) {
  376:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  377:             }
  378:             if (defined($cached)) {
  379:                 $returnhash{$what} = $response;
  380:             } else {
  381:                 $whatneeded{$what} = 1;
  382:             }
  383:         }
  384:         if (keys(%whatneeded) == 0) {
  385:             $result = 'ok';
  386:         } else {
  387:             my $requested = &freeze_escape(\%whatneeded);
  388:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  389:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  390:                 ($rep eq 'unknown_cmd')) {
  391:                 $result = $rep;
  392:             } else {
  393:                 $result = 'ok';
  394:                 my @pairs=split(/\&/,$rep);
  395:                 foreach my $item (@pairs) {
  396:                     my ($key,$value)=split(/=/,$item,2);
  397:                     my $what = &unescape($key);
  398:                     my $hashid = $lonhost.'-'.$what;
  399:                     $returnhash{$what}=&thaw_unescape($value);
  400:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  401:                 }
  402:             }
  403:         }
  404:     }
  405:     return ($result,\%returnhash);
  406: }
  407: 
  408: sub remote_devalidate_cache {
  409:     my ($lonhost,$cachekeys) = @_;
  410:     my $items;
  411:     return unless (ref($cachekeys) eq 'ARRAY');
  412:     my $cachestr = join('&',@{$cachekeys});
  413:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  414:     return $response;
  415: }
  416: 
  417: # -------------------------------------------------- Non-critical communication
  418: sub subreply {
  419:     my ($cmd,$server)=@_;
  420:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  421:     #
  422:     #  With loncnew process trimming, there's a timing hole between lonc server
  423:     #  process exit and the master server picking up the listen on the AF_UNIX
  424:     #  socket.  In that time interval, a lock file will exist:
  425: 
  426:     my $lockfile=$peerfile.".lock";
  427:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  428: 	sleep(0.1);
  429:     }
  430:     # At this point, either a loncnew parent is listening or an old lonc
  431:     # or loncnew child is listening so we can connect or everything's dead.
  432:     #
  433:     #   We'll give the connection a few tries before abandoning it.  If
  434:     #   connection is not possible, we'll con_lost back to the client.
  435:     #   
  436:     my $client;
  437:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  438: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  439: 				      Type    => SOCK_STREAM,
  440: 				      Timeout => 10);
  441: 	if ($client) {
  442: 	    last;		# Connected!
  443: 	} else {
  444: 	    &create_connection(&hostname($server),$server);
  445: 	}
  446:         sleep(0.1);	# Try again later if failed connection.
  447:     }
  448:     my $answer;
  449:     if ($client) {
  450: 	print $client "sethost:$server:$cmd\n";
  451: 	$answer=<$client>;
  452: 	if (!$answer) { $answer="con_lost"; }
  453: 	chomp($answer);
  454:     } else {
  455: 	$answer = 'con_lost';	# Failed connection.
  456:     }
  457:     return $answer;
  458: }
  459: 
  460: sub reply {
  461:     my ($cmd,$server)=@_;
  462:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  463:     my $answer=subreply($cmd,$server);
  464:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  465:         my $logged = $cmd;
  466:         if ($cmd =~ /^encrypt:([^:]+):/) {
  467:             my $subcmd = $1;
  468:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  469:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  470:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
  471:                 (undef,undef,my @rest) = split(/:/,$cmd);
  472:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  473:                     splice(@rest,2,1,'Hidden');
  474:                 } elsif ($subcmd eq 'passwd') {
  475:                     splice(@rest,2,2,('Hidden','Hidden'));
  476:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  477:                          ($subcmd eq 'autoexportgrades')) {
  478:                     splice(@rest,3,1,'Hidden');
  479:                 }
  480:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  481:             }
  482:         }
  483:         &logthis("<font color=\"blue\">WARNING:".
  484:                  " $logged to $server returned $answer</font>");
  485:     }
  486:     return $answer;
  487: }
  488: 
  489: # ----------------------------------------------------------- Send USR1 to lonc
  490: 
  491: sub reconlonc {
  492:     my ($lonid) = @_;
  493:     if ($lonid) {
  494:         my $hostname = &hostname($lonid);
  495: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  496: 	if ($hostname && -e $peerfile) {
  497: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  498: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  499: 					     Type    => SOCK_STREAM,
  500: 					     Timeout => 10);
  501: 	    if ($client) {
  502: 		print $client ("reset_retries\n");
  503: 		my $answer=<$client>;
  504: 		#reset just this one.
  505: 	    }
  506: 	}
  507: 	return;
  508:     }
  509: 
  510:     &logthis("Trying to reconnect lonc");
  511:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  512:     if (open(my $fh,"<",$loncfile)) {
  513: 	my $loncpid=<$fh>;
  514:         chomp($loncpid);
  515:         if (kill 0 => $loncpid) {
  516: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  517:             kill USR1 => $loncpid;
  518:             sleep 1;
  519:         } else {
  520: 	    &logthis(
  521:                "<font color=\"blue\">WARNING:".
  522:                " lonc at pid $loncpid not responding, giving up</font>");
  523:         }
  524:     } else {
  525: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  526:     }
  527: }
  528: 
  529: # ------------------------------------------------------ Critical communication
  530: 
  531: sub critical {
  532:     my ($cmd,$server)=@_;
  533:     unless (&hostname($server)) {
  534:         &logthis("<font color=\"blue\">WARNING:".
  535:                " Critical message to unknown server ($server)</font>");
  536:         return 'no_such_host';
  537:     }
  538:     my $answer=reply($cmd,$server);
  539:     if ($answer eq 'con_lost') {
  540: 	&reconlonc($server);
  541: 	my $answer=reply($cmd,$server);
  542:         if ($answer eq 'con_lost') {
  543:             my $now=time;
  544:             my $middlename=$cmd;
  545:             $middlename=substr($middlename,0,16);
  546:             $middlename=~s/\W//g;
  547:             my $dfilename=
  548:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  549:             $dumpcount++;
  550:             {
  551: 		my $dfh;
  552: 		if (open($dfh,">",$dfilename)) {
  553: 		    print $dfh "$cmd\n"; 
  554: 		    close($dfh);
  555: 		}
  556:             }
  557:             sleep 1;
  558:             my $wcmd='';
  559:             {
  560: 		my $dfh;
  561: 		if (open($dfh,"<",$dfilename)) {
  562: 		    $wcmd=<$dfh>; 
  563: 		    close($dfh);
  564: 		}
  565:             }
  566:             chomp($wcmd);
  567:             if ($wcmd eq $cmd) {
  568: 		&logthis("<font color=\"blue\">WARNING: ".
  569:                          "Connection buffer $dfilename: $cmd</font>");
  570:                 &logperm("D:$server:$cmd");
  571: 	        return 'con_delayed';
  572:             } else {
  573:                 &logthis("<font color=\"red\">CRITICAL:"
  574:                         ." Critical connection failed: $server $cmd</font>");
  575:                 &logperm("F:$server:$cmd");
  576:                 return 'con_failed';
  577:             }
  578:         }
  579:     }
  580:     return $answer;
  581: }
  582: 
  583: # ------------------------------------------- check if return value is an error
  584: 
  585: sub error {
  586:     my ($result) = @_;
  587:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  588: 	if ($2 == 2) { return undef; }
  589: 	return $1;
  590:     }
  591:     return undef;
  592: }
  593: 
  594: sub convert_and_load_session_env {
  595:     my ($lonidsdir,$handle)=@_;
  596:     my @profile;
  597:     {
  598: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  599: 	if (!$opened) {
  600: 	    return 0;
  601: 	}
  602: 	flock($idf,LOCK_SH);
  603: 	@profile=<$idf>;
  604: 	close($idf);
  605:     }
  606:     my %temp_env;
  607:     foreach my $line (@profile) {
  608: 	if ($line !~ m/=/) {
  609: 	    return 0;
  610: 	}
  611: 	chomp($line);
  612: 	my ($envname,$envvalue)=split(/=/,$line,2);
  613: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  614:     }
  615:     unlink("$lonidsdir/$handle.id");
  616:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  617: 	    0640)) {
  618: 	%disk_env = %temp_env;
  619: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  620: 	untie(%disk_env);
  621:     }
  622:     return 1;
  623: }
  624: 
  625: # ------------------------------------------- Transfer profile into environment
  626: my $env_loaded;
  627: sub transfer_profile_to_env {
  628:     my ($lonidsdir,$handle,$force_transfer) = @_;
  629:     if (!$force_transfer && $env_loaded) { return; } 
  630: 
  631:     if (!defined($lonidsdir)) {
  632: 	$lonidsdir = $perlvar{'lonIDsDir'};
  633:     }
  634:     if (!defined($handle)) {
  635:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  636:     }
  637: 
  638:     my $convert;
  639:     {
  640:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  641: 	if (!$opened) {
  642: 	    return;
  643: 	}
  644: 	flock($idf,LOCK_SH);
  645: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  646: 		&GDBM_READER(),0640)) {
  647: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  648: 	    untie(%disk_env);
  649: 	} else {
  650: 	    $convert = 1;
  651: 	}
  652:     }
  653:     if ($convert) {
  654: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  655: 	    &logthis("Failed to load session, or convert session.");
  656: 	}
  657:     }
  658: 
  659:     my %remove;
  660:     while ( my $envname = each(%env) ) {
  661:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  662:             if ($time < time-300) {
  663:                 $remove{$key}++;
  664:             }
  665:         }
  666:     }
  667: 
  668:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  669:     $env_loaded=1;
  670:     foreach my $expired_key (keys(%remove)) {
  671:         &delenv($expired_key);
  672:     }
  673: }
  674: 
  675: # ---------------------------------------------------- Check for valid session 
  676: sub check_for_valid_session {
  677:     my ($r,$name,$userhashref,$domref) = @_;
  678:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  679:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  680:     if ($name eq 'lonDAV') {
  681:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  682:     } else {
  683:         $lonidsdir=$r->dir_config('lonIDsDir');
  684:         if ($name eq '') {
  685:             $name = 'lonID';
  686:         }
  687:     }
  688:     if ($name eq 'lonID') {
  689:         $secure = 'lonSID';
  690:         $linkname = 'lonLinkID';
  691:         $pubname = 'lonPubID';
  692:         if (exists($cookies{$secure})) {
  693:             $lonid=$cookies{$secure};
  694:         } elsif (exists($cookies{$name})) {
  695:             $lonid=$cookies{$name};
  696:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  697:             $lonid=$cookies{$linkname};
  698:         } elsif (exists($cookies{$pubname})) {
  699:             $lonid=$cookies{$pubname};
  700:         }
  701:     } else {
  702:         $lonid=$cookies{$name};
  703:     }
  704:     return undef if (!$lonid);
  705: 
  706:     my $handle=&LONCAPA::clean_handle($lonid->value);
  707:     if (-l "$lonidsdir/$handle.id") {
  708:         my $link = readlink("$lonidsdir/$handle.id");
  709:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  710:             $handle = $1;
  711:         }
  712:     }
  713:     if (!-e "$lonidsdir/$handle.id") {
  714:         if ((ref($domref)) && ($name eq 'lonID') && 
  715:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  716:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  717:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  718:                 $$domref = $possudom;
  719:             }
  720:         }
  721:         return undef;
  722:     }
  723: 
  724:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  725:     return undef if (!$opened);
  726: 
  727:     flock($idf,LOCK_SH);
  728:     my %disk_env;
  729:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  730: 	    &GDBM_READER(),0640)) {
  731: 	return undef;	
  732:     }
  733: 
  734:     if (!defined($disk_env{'user.name'})
  735: 	|| !defined($disk_env{'user.domain'})) {
  736:         untie(%disk_env);
  737: 	return undef;
  738:     }
  739: 
  740:     if (ref($userhashref) eq 'HASH') {
  741:         $userhashref->{'name'} = $disk_env{'user.name'};
  742:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  743:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  744:         if ($userhashref->{'lti'}) {
  745:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  746:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  747:         }
  748:     }
  749:     untie(%disk_env);
  750: 
  751:     return $handle;
  752: }
  753: 
  754: sub timed_flock {
  755:     my ($file,$lock_type) = @_;
  756:     my $failed=0;
  757:     eval {
  758: 	local $SIG{__DIE__}='DEFAULT';
  759: 	local $SIG{ALRM}=sub {
  760: 	    $failed=1;
  761: 	    die("failed lock");
  762: 	};
  763: 	alarm(13);
  764: 	flock($file,$lock_type);
  765: 	alarm(0);
  766:     };
  767:     if ($failed) {
  768: 	return undef;
  769:     } else {
  770: 	return 1;
  771:     }
  772: }
  773: 
  774: sub get_sessionfile_vars {
  775:     my ($handle,$lonidsdir,$storearr) = @_;
  776:     my %returnhash;
  777:     unless (ref($storearr) eq 'ARRAY') {
  778:         return %returnhash;
  779:     }
  780:     if (-l "$lonidsdir/$handle.id") {
  781:         my $link = readlink("$lonidsdir/$handle.id");
  782:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  783:             $handle = $1;
  784:         }
  785:     }
  786:     if ((-e "$lonidsdir/$handle.id") &&
  787:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  788:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  789:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  790:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  791:                 flock($idf,LOCK_SH);
  792:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  793:                         &GDBM_READER(),0640)) {
  794:                     foreach my $item (@{$storearr}) {
  795:                         $returnhash{$item} = $disk_env{$item};
  796:                     }
  797:                     untie(%disk_env);
  798:                 }
  799:             }
  800:         }
  801:     }
  802:     return %returnhash;
  803: }
  804: 
  805: # ---------------------------------------------------------- Append Environment
  806: 
  807: sub appenv {
  808:     my ($newenv,$roles) = @_;
  809:     if (ref($newenv) eq 'HASH') {
  810:         foreach my $key (keys(%{$newenv})) {
  811:             my $refused = 0;
  812: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  813:                 $refused = 1;
  814:                 if (ref($roles) eq 'ARRAY') {
  815:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  816:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  817:                         $refused = 0;
  818:                     }
  819:                 }
  820:             }
  821:             if ($refused) {
  822:                 &logthis("<font color=\"blue\">WARNING: ".
  823:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  824:                          .'</font>');
  825: 	        delete($newenv->{$key});
  826:             } else {
  827:                 $env{$key}=$newenv->{$key};
  828:             }
  829:         }
  830:         my $lonids = $perlvar{'lonIDsDir'};
  831:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  832:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  833:             if ($opened
  834: 	        && &timed_flock($env_file,LOCK_EX)
  835: 	        &&
  836: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  837: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  838: 	        while (my ($key,$value) = each(%{$newenv})) {
  839: 	            $disk_env{$key} = $value;
  840: 	        }
  841: 	        untie(%disk_env);
  842:             }
  843:         }
  844:     }
  845:     return 'ok';
  846: }
  847: # ----------------------------------------------------- Delete from Environment
  848: 
  849: sub delenv {
  850:     my ($delthis,$regexp,$roles) = @_;
  851:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  852:         my $refused = 1;
  853:         if (ref($roles) eq 'ARRAY') {
  854:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  855:             if (grep(/^\Q$role\E$/,@{$roles})) {
  856:                 $refused = 0;
  857:             }
  858:         }
  859:         if ($refused) {
  860:             &logthis("<font color=\"blue\">WARNING: ".
  861:                      "Attempt to delete from environment ".$delthis);
  862:             return 'error';
  863:         }
  864:     }
  865:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  866:     if ($opened
  867: 	&& &timed_flock($env_file,LOCK_EX)
  868: 	&&
  869: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  870: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  871: 	foreach my $key (keys(%disk_env)) {
  872: 	    if ($regexp) {
  873:                 if ($key=~/^$delthis/) {
  874:                     delete($env{$key});
  875:                     delete($disk_env{$key});
  876:                 } 
  877:             } else {
  878:                 if ($key=~/^\Q$delthis\E/) {
  879: 		    delete($env{$key});
  880: 		    delete($disk_env{$key});
  881: 	        }
  882:             }
  883: 	}
  884: 	untie(%disk_env);
  885:     }
  886:     return 'ok';
  887: }
  888: 
  889: sub get_env_multiple {
  890:     my ($name) = @_;
  891:     my @values;
  892:     if (defined($env{$name})) {
  893:         # exists is it an array
  894:         if (ref($env{$name})) {
  895:             @values=@{ $env{$name} };
  896:         } else {
  897:             $values[0]=$env{$name};
  898:         }
  899:     }
  900:     return(@values);
  901: }
  902: 
  903: # ------------------------------------------------------------------- Locking
  904: 
  905: sub set_lock {
  906:     my ($text)=@_;
  907:     $locknum++;
  908:     my $id=$$.'-'.$locknum;
  909:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  910:              'session.lock.'.$id => $text});
  911:     return $id;
  912: }
  913: 
  914: sub get_locks {
  915:     my $num=0;
  916:     my %texts=();
  917:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  918:        if ($lock=~/\w/) {
  919:           $num++;
  920:           $texts{$lock}=$env{'session.lock.'.$lock};
  921:        }
  922:    }
  923:    return ($num,%texts);
  924: }
  925: 
  926: sub remove_lock {
  927:     my ($id)=@_;
  928:     my $newlocks='';
  929:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  930:        if (($lock=~/\w/) && ($lock ne $id)) {
  931:           $newlocks.=','.$lock;
  932:        }
  933:     }
  934:     &appenv({'session.locks' => $newlocks});
  935:     &delenv('session.lock.'.$id);
  936: }
  937: 
  938: sub remove_all_locks {
  939:     my $activelocks=$env{'session.locks'};
  940:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  941:        if ($lock=~/\w/) {
  942:           &remove_lock($lock);
  943:        }
  944:     }
  945: }
  946: 
  947: 
  948: # ------------------------------------------ Find out current server userload
  949: sub userload {
  950:     my $numusers=0;
  951:     {
  952: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  953: 	my $filename;
  954: 	my $curtime=time;
  955: 	while ($filename=readdir(LONIDS)) {
  956: 	    next if ($filename eq '.' || $filename eq '..');
  957: 	    next if ($filename =~ /publicuser_\d+\.id/);
  958:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
  959: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  960: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  961: 	}
  962: 	closedir(LONIDS);
  963:     }
  964:     my $userloadpercent=0;
  965:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  966:     if ($maxuserload) {
  967: 	$userloadpercent=100*$numusers/$maxuserload;
  968:     }
  969:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  970:     return $userloadpercent;
  971: }
  972: 
  973: # ------------------------------ Find server with least workload from spare.tab
  974: 
  975: sub spareserver {
  976:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  977:     my $spare_server;
  978:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  979:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  980:                                                      :  $userloadpercent;
  981:     my ($uint_dom,$remotesessions);
  982:     if (($udom ne '') && (&domain($udom) ne '')) {
  983:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  984:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  985:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  986:         $remotesessions = $udomdefaults{'remotesessions'};
  987:     }
  988:     my $spareshash = &this_host_spares($udom);
  989:     if (ref($spareshash) eq 'HASH') {
  990:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  991:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  992:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  993:                                              $try_server));
  994: 	        ($spare_server, $lowest_load) =
  995: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
  996:             }
  997:         }
  998: 
  999:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1000: 
 1001:         if (!$found_server) {
 1002:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1003: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1004:                     next unless (&spare_can_host($udom,$uint_dom,
 1005:                                                  $remotesessions,$try_server));
 1006: 	            ($spare_server, $lowest_load) =
 1007: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1008:                 }
 1009: 	    }
 1010:         }
 1011:     }
 1012: 
 1013:     if (!$want_server_name) {
 1014:         if (defined($spare_server)) {
 1015:             my $hostname = &hostname($spare_server);
 1016:             if (defined($hostname)) {
 1017:                 my $protocol = 'http';
 1018:                 if ($protocol{$spare_server} eq 'https') {
 1019:                     $protocol = $protocol{$spare_server};
 1020:                 }
 1021: 	        $spare_server = $protocol.'://'.$hostname;
 1022:             }
 1023:         }
 1024:     }
 1025:     return $spare_server;
 1026: }
 1027: 
 1028: sub compare_server_load {
 1029:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1030: 
 1031:     if ($required) {
 1032:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1033:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1034:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1035:         if (($major eq '' && $minor eq '') ||
 1036:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1037:             return ($spare_server,$lowest_load);
 1038:         }
 1039:     }
 1040: 
 1041:     my $loadans     = &reply('load',    $try_server);
 1042:     my $userloadans = &reply('userload',$try_server);
 1043: 
 1044:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1045: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1046:     }
 1047: 
 1048:     my $load;
 1049:     if ($loadans =~ /\d/) {
 1050: 	if ($userloadans =~ /\d/) {
 1051: 	    #both are numbers, pick the bigger one
 1052: 	    $load = ($loadans > $userloadans) ? $loadans 
 1053: 		                              : $userloadans;
 1054: 	} else {
 1055: 	    $load = $loadans;
 1056: 	}
 1057:     } else {
 1058: 	$load = $userloadans;
 1059:     }
 1060: 
 1061:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1062: 	$spare_server = $try_server;
 1063: 	$lowest_load  = $load;
 1064:     }
 1065:     return ($spare_server,$lowest_load);
 1066: }
 1067: 
 1068: # --------------------------- ask offload servers if user already has a session
 1069: sub find_existing_session {
 1070:     my ($udom,$uname) = @_;
 1071:     my $spareshash = &this_host_spares($udom);
 1072:     if (ref($spareshash) eq 'HASH') {
 1073:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1074:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1075:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1076:             }
 1077:         }
 1078:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1079:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1080:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1081:             }
 1082:         }
 1083:     }
 1084:     return;
 1085: }
 1086: 
 1087: sub delusersession {
 1088:     my ($lonid,$udom,$uname) = @_;
 1089:     my $uprimary_id = &domain($udom,'primary');
 1090:     my $uintdom = &internet_dom($uprimary_id);
 1091:     my $intdom = &internet_dom($lonid);
 1092:     my $serverhomedom = &host_domain($lonid);
 1093:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1094:         return &reply(join(':','delusersession',
 1095:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1096:     }
 1097:     return;
 1098: }
 1099: 
 1100: # check if user's browser sent load balancer cookie and server still has session
 1101: # and is not overloaded.
 1102: sub check_for_balancer_cookie {
 1103:     my ($r,$update_mtime) = @_;
 1104:     my ($otherserver,$cookie);
 1105:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1106:     if (exists($cookies{'balanceID'})) {
 1107:         my $balid = $cookies{'balanceID'};
 1108:         $cookie=&LONCAPA::clean_handle($balid->value);
 1109:         my $balancedir=$r->dir_config('lonBalanceDir');
 1110:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1111:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1112:                 my ($possudom,$possuname) = ($1,$2);
 1113:                 my $has_session = 0;
 1114:                 if ((&domain($possudom) ne '') &&
 1115:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1116:                     my $try_server;
 1117:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1118:                     if ($opened) {
 1119:                         flock($idf,LOCK_SH);
 1120:                         while (my $line = <$idf>) {
 1121:                             chomp($line);
 1122:                             if (&hostname($line) ne '') {
 1123:                                 $try_server = $line;
 1124:                                 last;
 1125:                             }
 1126:                         }
 1127:                         close($idf);
 1128:                         if (($try_server) &&
 1129:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1130:                             my $lowest_load = 30000;
 1131:                             ($otherserver,$lowest_load) =
 1132:                                 &compare_server_load($try_server,undef,$lowest_load);
 1133:                             if ($otherserver ne '' && $lowest_load < 100) {
 1134:                                 $has_session = 1;
 1135:                             } else {
 1136:                                 undef($otherserver);
 1137:                             }
 1138:                         }
 1139:                     }
 1140:                 }
 1141:                 if ($has_session) {
 1142:                     if ($update_mtime) {
 1143:                         my $atime = my $mtime = time;
 1144:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1145:                     }
 1146:                 } else {
 1147:                     unlink("$balancedir/$cookie.id");
 1148:                 }
 1149:             }
 1150:         }
 1151:     }
 1152:     return ($otherserver,$cookie);
 1153: }
 1154: 
 1155: sub updatebalcookie {
 1156:     my ($cookie,$balancer,$lastentry)=@_;
 1157:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1158:         my ($udom,$uname) = ($1,$2);
 1159:         my $uprimary_id = &domain($udom,'primary');
 1160:         my $uintdom = &internet_dom($uprimary_id);
 1161:         my $intdom = &internet_dom($balancer);
 1162:         my $serverhomedom = &host_domain($balancer);
 1163:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1164:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1165:         }
 1166:     }
 1167:     return;
 1168: }
 1169: 
 1170: sub delbalcookie {
 1171:     my ($cookie,$balancer) =@_;
 1172:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1173:         my ($udom,$uname) = ($1,$2);
 1174:         my $uprimary_id = &domain($udom,'primary');
 1175:         my $uintdom = &internet_dom($uprimary_id);
 1176:         my $intdom = &internet_dom($balancer);
 1177:         my $serverhomedom = &host_domain($balancer);
 1178:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1179:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1180:         }
 1181:     }
 1182: }
 1183: 
 1184: # -------------------------------- ask if server already has a session for user
 1185: sub has_user_session {
 1186:     my ($lonid,$udom,$uname) = @_;
 1187:     my $result = &reply(join(':','userhassession',
 1188: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1189:     return 1 if ($result eq 'ok');
 1190: 
 1191:     return 0;
 1192: }
 1193: 
 1194: # --------- determine least loaded server in a user's domain which allows login
 1195: 
 1196: sub choose_server {
 1197:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1198:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1199:     my %servers = &get_servers($udom);
 1200:     my $lowest_load = 30000;
 1201:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1202:     if ($skiploadbal) {
 1203:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1204:         unless (defined($cached)) {
 1205:             my $cachetime = 60*60*24;
 1206:             my %domconfig =
 1207:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1208:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1209:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1210:                                            $cachetime);
 1211:             }
 1212:         }
 1213:     }
 1214:     foreach my $lonhost (keys(%servers)) {
 1215:         if ($skiploadbal) {
 1216:             if (ref($balancers) eq 'HASH') {
 1217:                 next if (exists($balancers->{$lonhost}));
 1218:             }
 1219:         }
 1220:         my $loginvia;
 1221:         if ($checkloginvia) {
 1222:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1223:             if ($loginvia) {
 1224:                 my ($server,$path) = split(/:/,$loginvia);
 1225:                 ($login_host, $lowest_load) =
 1226:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1227:                 if ($login_host eq $server) {
 1228:                     $portal_path = $path;
 1229:                     $isredirect = 1;
 1230:                 }
 1231:             } else {
 1232:                 ($login_host, $lowest_load) =
 1233:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1234:                 if ($login_host eq $lonhost) {
 1235:                     $portal_path = '';
 1236:                     $isredirect = ''; 
 1237:                 }
 1238:             }
 1239:         } else {
 1240:             ($login_host, $lowest_load) =
 1241:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1242:         }
 1243:     }
 1244:     if ($login_host ne '') {
 1245:         $hostname = &hostname($login_host);
 1246:     }
 1247:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1248: }
 1249: 
 1250: sub get_course_sessions {
 1251:     my ($cnum,$cdom,$lastactivity) = @_;
 1252:     my %servers = &internet_dom_servers($cdom);
 1253:     my %returnhash;
 1254:     foreach my $server (sort(keys(%servers))) {
 1255:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1256:         my @pairs=split(/\&/,$rep);
 1257:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1258:             foreach my $item (@pairs) {
 1259:                 my ($key,$value)=split(/=/,$item,2);
 1260:                 $key = &unescape($key);
 1261:                 next if ($key =~ /^error: 2 /);
 1262:                 if (exists($returnhash{$key})) {
 1263:                     next if ($value < $returnhash{$key});
 1264:                 }
 1265:                 $returnhash{$key}=$value;
 1266:             }
 1267:         }
 1268:     }
 1269:     return %returnhash;
 1270: }
 1271: 
 1272: # --------------------------------------------- Try to change a user's password
 1273: 
 1274: sub changepass {
 1275:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1276:     $currentpass = &escape($currentpass);
 1277:     $newpass     = &escape($newpass);
 1278:     my $lonhost = $perlvar{'lonHostID'};
 1279:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1280: 		       $server);
 1281:     if (! $answer) {
 1282: 	&logthis("No reply on password change request to $server ".
 1283: 		 "by $uname in domain $udom.");
 1284:     } elsif ($answer =~ "^ok") {
 1285:         &logthis("$uname in $udom successfully changed their password ".
 1286: 		 "on $server.");
 1287:     } elsif ($answer =~ "^pwchange_failure") {
 1288: 	&logthis("$uname in $udom was unable to change their password ".
 1289: 		 "on $server.  The action was blocked by either lcpasswd ".
 1290: 		 "or pwchange");
 1291:     } elsif ($answer =~ "^non_authorized") {
 1292:         &logthis("$uname in $udom did not get their password correct when ".
 1293: 		 "attempting to change it on $server.");
 1294:     } elsif ($answer =~ "^auth_mode_error") {
 1295:         &logthis("$uname in $udom attempted to change their password despite ".
 1296: 		 "not being locally or internally authenticated on $server.");
 1297:     } elsif ($answer =~ "^unknown_user") {
 1298:         &logthis("$uname in $udom attempted to change their password ".
 1299: 		 "on $server but were unable to because $server is not ".
 1300: 		 "their home server.");
 1301:     } elsif ($answer =~ "^refused") {
 1302: 	&logthis("$server refused to change $uname in $udom password because ".
 1303: 		 "it was sent an unencrypted request to change the password.");
 1304:     } elsif ($answer =~ "invalid_client") {
 1305:         &logthis("$server refused to change $uname in $udom password because ".
 1306:                  "it was a reset by e-mail originating from an invalid server.");
 1307:     } elsif ($answer =~ "^prioruse") {
 1308:        &logthis("$server refused to change $uname in $udom password because ".
 1309:                 "the password had been used before");
 1310:     }
 1311:     return $answer;
 1312: }
 1313: 
 1314: # ----------------------- Try to determine user's current authentication scheme
 1315: 
 1316: sub queryauthenticate {
 1317:     my ($uname,$udom)=@_;
 1318:     my $uhome=&homeserver($uname,$udom);
 1319:     if (!$uhome) {
 1320: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1321: 	return 'no_host';
 1322:     }
 1323:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1324:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1325: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1326:     }
 1327:     return $answer;
 1328: }
 1329: 
 1330: # --------- Try to authenticate user from domain's lib servers (first this one)
 1331: 
 1332: sub authenticate {
 1333:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1334:     $upass=&escape($upass);
 1335:     $uname= &LONCAPA::clean_username($uname);
 1336:     my $uhome=&homeserver($uname,$udom,1);
 1337:     my $newhome;
 1338:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1339: # Maybe the machine was offline and only re-appeared again recently?
 1340:         &reconlonc();
 1341: # One more
 1342: 	$uhome=&homeserver($uname,$udom,1);
 1343:         if (($uhome eq 'no_host') && $checkdefauth) {
 1344:             if (defined(&domain($udom,'primary'))) {
 1345:                 $newhome=&domain($udom,'primary');
 1346:             }
 1347:             if ($newhome ne '') {
 1348:                 $uhome = $newhome;
 1349:             }
 1350:         }
 1351: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1352: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1353: 	    return 'no_host';
 1354:         }
 1355:     }
 1356:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1357:     if ($answer eq 'authorized') {
 1358:         if ($newhome) {
 1359:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1360:             return 'no_account_on_host'; 
 1361:         } else {
 1362:             &logthis("User $uname at $udom authorized by $uhome");
 1363:             return $uhome;
 1364:         }
 1365:     }
 1366:     if ($answer eq 'non_authorized') {
 1367: 	&logthis("User $uname at $udom rejected by $uhome");
 1368: 	return 'no_host'; 
 1369:     }
 1370:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1371:     return 'no_host';
 1372: }
 1373: 
 1374: sub can_host_session {
 1375:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1376:     my $canhost = 1;
 1377:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1378:     if (ref($remotesessions) eq 'HASH') {
 1379:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1380:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1381:                 $canhost = 0;
 1382:             } else {
 1383:                 $canhost = 1;
 1384:             }
 1385:         }
 1386:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1387:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1388:                 $canhost = 1;
 1389:             } else {
 1390:                 $canhost = 0;
 1391:             }
 1392:         }
 1393:         if ($canhost) {
 1394:             if ($remotesessions->{'version'} ne '') {
 1395:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1396:                 if ($reqmajor ne '' && $reqminor ne '') {
 1397:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1398:                         my $major = $1;
 1399:                         my $minor = $2;
 1400:                         if (($major < $reqmajor ) ||
 1401:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1402:                             $canhost = 0;
 1403:                         }
 1404:                     } else {
 1405:                         $canhost = 0;
 1406:                     }
 1407:                 }
 1408:             }
 1409:         }
 1410:     }
 1411:     if ($canhost) {
 1412:         if (ref($hostedsessions) eq 'HASH') {
 1413:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1414:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1415:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1416:                 if (($uint_dom ne '') && 
 1417:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1418:                     $canhost = 0;
 1419:                 } else {
 1420:                     $canhost = 1;
 1421:                 }
 1422:             }
 1423:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1424:                 if (($uint_dom ne '') && 
 1425:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1426:                     $canhost = 1;
 1427:                 } else {
 1428:                     $canhost = 0;
 1429:                 }
 1430:             }
 1431:         }
 1432:     }
 1433:     return $canhost;
 1434: }
 1435: 
 1436: sub spare_can_host {
 1437:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1438:     my $canhost=1;
 1439:     my $try_server_hostname = &hostname($try_server);
 1440:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1441:     my $serverhomedom = &host_domain($serverhomeID);
 1442:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1443:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1444:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1445:             $canhost = 0;
 1446:         }
 1447:     }
 1448:     if ($canhost) {
 1449:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1450:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1451:                 unless (&shared_institution($udom,$try_server)) {
 1452:                     $canhost = 0;
 1453:                 }
 1454:             }
 1455:         }
 1456:     }
 1457:     if (($canhost) && ($uint_dom)) {
 1458:         my @intdoms;
 1459:         my $internet_names = &get_internet_names($try_server);
 1460:         if (ref($internet_names) eq 'ARRAY') {
 1461:             @intdoms = @{$internet_names};
 1462:         }
 1463:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1464:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1465:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1466:                                          $remotesessions,
 1467:                                          $defdomdefaults{'hostedsessions'});
 1468:         }
 1469:     }
 1470:     return $canhost;
 1471: }
 1472: 
 1473: sub this_host_spares {
 1474:     my ($dom) = @_;
 1475:     my ($dom_in_use,$lonhost_in_use,$result);
 1476:     my @hosts = &current_machine_ids();
 1477:     foreach my $lonhost (@hosts) {
 1478:         if (&host_domain($lonhost) eq $dom) {
 1479:             $dom_in_use = $dom;
 1480:             $lonhost_in_use = $lonhost;
 1481:             last;
 1482:         }
 1483:     }
 1484:     if ($dom_in_use ne '') {
 1485:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1486:     }
 1487:     if (ref($result) ne 'HASH') {
 1488:         $lonhost_in_use = $perlvar{'lonHostID'};
 1489:         $dom_in_use = &host_domain($lonhost_in_use);
 1490:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1491:         if (ref($result) ne 'HASH') {
 1492:             $result = \%spareid;
 1493:         }
 1494:     }
 1495:     return $result;
 1496: }
 1497: 
 1498: sub spares_for_offload  {
 1499:     my ($dom_in_use,$lonhost_in_use) = @_;
 1500:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1501:     if (defined($cached)) {
 1502:         return $result;
 1503:     } else {
 1504:         my $cachetime = 60*60*24;
 1505:         my %domconfig =
 1506:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1507:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1508:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1509:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1510:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1511:                 }
 1512:             }
 1513:         }
 1514:     }
 1515:     return;
 1516: }
 1517: 
 1518: sub get_lonbalancer_config {
 1519:     my ($servers) = @_;
 1520:     my ($currbalancer,$currtargets);
 1521:     if (ref($servers) eq 'HASH') {
 1522:         foreach my $server (keys(%{$servers})) {
 1523:             my %what = (
 1524:                          spareid => 1,
 1525:                          perlvar => 1,
 1526:                        );
 1527:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1528:             if ($result eq 'ok') {
 1529:                 if (ref($returnhash) eq 'HASH') {
 1530:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1531:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1532:                             $currbalancer = $server;
 1533:                             $currtargets = {};
 1534:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1535:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1536:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1537:                                 }
 1538:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1539:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1540:                                 }
 1541:                             }
 1542:                             last;
 1543:                         }
 1544:                     }
 1545:                 }
 1546:             }
 1547:         }
 1548:     }
 1549:     return ($currbalancer,$currtargets);
 1550: }
 1551: 
 1552: sub check_loadbalancing {
 1553:     my ($uname,$udom,$caller) = @_;
 1554:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1555:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1556:     my $lonhost = $perlvar{'lonHostID'};
 1557:     my @hosts = &current_machine_ids();
 1558:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1559:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1560:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1561:     my $serverhomedom = &host_domain($lonhost);
 1562:     my $domneedscache;
 1563:     my $cachetime = 60*60*24;
 1564: 
 1565:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1566:         $dom_in_use = $udom;
 1567:         $homeintdom = 1;
 1568:     } else {
 1569:         $dom_in_use = $serverhomedom;
 1570:     }
 1571:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1572:     unless (defined($cached)) {
 1573:         my %domconfig =
 1574:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1575:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1576:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1577:         } else {
 1578:             $domneedscache = $dom_in_use;
 1579:         }
 1580:     }
 1581:     if (ref($result) eq 'HASH') {
 1582:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1583:             &check_balancer_result($result,@hosts);
 1584:         if ($is_balancer) {
 1585:             if (ref($currrules) eq 'HASH') {
 1586:                 if ($homeintdom) {
 1587:                     if ($uname ne '') {
 1588:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1589:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1590:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1591:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1592:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1593:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1594:                             }
 1595:                         }
 1596:                         if ($rule_in_effect eq '') {
 1597:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1598:                             if ($userenv{'inststatus'} ne '') {
 1599:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1600:                                 my ($othertitle,$usertypes,$types) =
 1601:                                     &Apache::loncommon::sorted_inst_types($udom);
 1602:                                 if (ref($types) eq 'ARRAY') {
 1603:                                     foreach my $type (@{$types}) {
 1604:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1605:                                             if (exists($currrules->{$type})) {
 1606:                                                 $rule_in_effect = $currrules->{$type};
 1607:                                             }
 1608:                                         }
 1609:                                     }
 1610:                                 }
 1611:                             } else {
 1612:                                 if (exists($currrules->{'default'})) {
 1613:                                     $rule_in_effect = $currrules->{'default'};
 1614:                                 }
 1615:                             }
 1616:                         }
 1617:                     } else {
 1618:                         if (exists($currrules->{'default'})) {
 1619:                             $rule_in_effect = $currrules->{'default'};
 1620:                         }
 1621:                     }
 1622:                 } else {
 1623:                     if ($currrules->{'_LC_external'} ne '') {
 1624:                         $rule_in_effect = $currrules->{'_LC_external'};
 1625:                     }
 1626:                 }
 1627:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1628:                                                        $uname,$udom);
 1629:             }
 1630:         }
 1631:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1632:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1633:         unless (defined($cached)) {
 1634:             my %domconfig =
 1635:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1636:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1637:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1638:             } else {
 1639:                 $domneedscache = $serverhomedom;
 1640:             }
 1641:         }
 1642:         if (ref($result) eq 'HASH') {
 1643:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1644:                 &check_balancer_result($result,@hosts);
 1645:             if ($is_balancer) {
 1646:                 if (ref($currrules) eq 'HASH') {
 1647:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1648:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1649:                     }
 1650:                 }
 1651:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1652:                                                        $uname,$udom);
 1653:             }
 1654:         } else {
 1655:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1656:                 $is_balancer = 1;
 1657:                 $offloadto = &this_host_spares($dom_in_use);
 1658:             }
 1659:             unless (defined($cached)) {
 1660:                 $domneedscache = $serverhomedom;
 1661:             }
 1662:         }
 1663:     } else {
 1664:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1665:             $is_balancer = 1;
 1666:             $offloadto = &this_host_spares($dom_in_use);
 1667:         }
 1668:         unless (defined($cached)) {
 1669:             $domneedscache = $serverhomedom;
 1670:         }
 1671:     }
 1672:     if ($domneedscache) {
 1673:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1674:     }
 1675:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1676:         my $lowest_load = 30000;
 1677:         if (ref($offloadto) eq 'HASH') {
 1678:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1679:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1680:                     ($otherserver,$lowest_load) =
 1681:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1682:                 }
 1683:             }
 1684:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1685: 
 1686:             if (!$found_server) {
 1687:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1688:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1689:                         ($otherserver,$lowest_load) =
 1690:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1691:                     }
 1692:                 }
 1693:             }
 1694:         } elsif (ref($offloadto) eq 'ARRAY') {
 1695:             if (@{$offloadto} == 1) {
 1696:                 $otherserver = $offloadto->[0];
 1697:             } elsif (@{$offloadto} > 1) {
 1698:                 foreach my $try_server (@{$offloadto}) {
 1699:                     ($otherserver,$lowest_load) =
 1700:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1701:                 }
 1702:             }
 1703:         }
 1704:         unless ($caller eq 'login') {
 1705:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1706:                 $is_balancer = 0;
 1707:                 if ($uname ne '' && $udom ne '') {
 1708:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1709:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1710:                                  'user.loadbalcheck.time' => time});
 1711:                     }
 1712:                 }
 1713:             }
 1714:         }
 1715:     }
 1716:     if (($is_balancer) && (!$homeintdom)) {
 1717:         undef($setcookie);
 1718:     }
 1719:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1720: }
 1721: 
 1722: sub check_balancer_result {
 1723:     my ($result,@hosts) = @_;
 1724:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1725:     if (ref($result) eq 'HASH') {
 1726:         if ($result->{'lonhost'} ne '') {
 1727:             my $currbalancer = $result->{'lonhost'};
 1728:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1729:                 $is_balancer = 1;
 1730:                 $currtargets = $result->{'targets'};
 1731:                 $currrules = $result->{'rules'};
 1732:             }
 1733:             $dom_balancers = $currbalancer;
 1734:         } else {
 1735:             if (keys(%{$result})) {
 1736:                 foreach my $key (keys(%{$result})) {
 1737:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1738:                         (ref($result->{$key}) eq 'HASH')) {
 1739:                         $is_balancer = 1;
 1740:                         $currrules = $result->{$key}{'rules'};
 1741:                         $currtargets = $result->{$key}{'targets'};
 1742:                         $setcookie = $result->{$key}{'cookie'};
 1743:                         last;
 1744:                     }
 1745:                 }
 1746:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1747:             }
 1748:         }
 1749:     }
 1750:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1751: }
 1752: 
 1753: sub get_loadbalancer_targets {
 1754:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1755:     my $offloadto;
 1756:     if ($rule_in_effect eq 'none') {
 1757:         return [$perlvar{'lonHostID'}];
 1758:     } elsif ($rule_in_effect eq '') {
 1759:         $offloadto = $currtargets;
 1760:     } else {
 1761:         if ($rule_in_effect eq 'homeserver') {
 1762:             my $homeserver = &homeserver($uname,$udom);
 1763:             if ($homeserver ne 'no_host') {
 1764:                 $offloadto = [$homeserver];
 1765:             }
 1766:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1767:             my %domconfig =
 1768:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1769:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1770:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1771:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1772:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1773:                     }
 1774:                 }
 1775:             } else {
 1776:                 my %servers = &internet_dom_servers($udom);
 1777:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1778:                 if (&hostname($remotebalancer) ne '') {
 1779:                     $offloadto = [$remotebalancer];
 1780:                 }
 1781:             }
 1782:         } elsif (&hostname($rule_in_effect) ne '') {
 1783:             $offloadto = [$rule_in_effect];
 1784:         }
 1785:     }
 1786:     return $offloadto;
 1787: }
 1788: 
 1789: sub internet_dom_servers {
 1790:     my ($dom) = @_;
 1791:     my (%uniqservers,%servers);
 1792:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1793:     my @machinedoms = &machine_domains($primaryserver);
 1794:     foreach my $mdom (@machinedoms) {
 1795:         my %currservers = %servers;
 1796:         my %server = &get_servers($mdom);
 1797:         %servers = (%currservers,%server);
 1798:     }
 1799:     my %by_hostname;
 1800:     foreach my $id (keys(%servers)) {
 1801:         push(@{$by_hostname{$servers{$id}}},$id);
 1802:     }
 1803:     foreach my $hostname (sort(keys(%by_hostname))) {
 1804:         if (@{$by_hostname{$hostname}} > 1) {
 1805:             my $match = 0;
 1806:             foreach my $id (@{$by_hostname{$hostname}}) {
 1807:                 if (&host_domain($id) eq $dom) {
 1808:                     $uniqservers{$id} = $hostname;
 1809:                     $match = 1;
 1810:                 }
 1811:             }
 1812:             unless ($match) {
 1813:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1814:             }
 1815:         } else {
 1816:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1817:         }
 1818:     }
 1819:     return %uniqservers;
 1820: }
 1821: 
 1822: sub trusted_domains {
 1823:     my ($cmdtype,$calldom) = @_;
 1824:     my ($trusted,$untrusted);
 1825:     if (&domain($calldom) eq '') {
 1826:         return ($trusted,$untrusted);
 1827:     }
 1828:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1829:         return ($trusted,$untrusted);
 1830:     }
 1831:     my $callprimary = &domain($calldom,'primary');
 1832:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1833:     if ($intcalldom eq '') {
 1834:         return ($trusted,$untrusted);
 1835:     }
 1836: 
 1837:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1838:     unless (defined($cached)) {
 1839:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1840:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1841:         $trustconfig = $domconfig{'trust'};
 1842:     }
 1843:     if (ref($trustconfig)) {
 1844:         my (%possexc,%possinc,@allexc,@allinc); 
 1845:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1846:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1847:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1848:             }
 1849:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1850:                 $possinc{$intcalldom} = 1;
 1851:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1852:             }
 1853:         }
 1854:         if (keys(%possexc)) {
 1855:             if (keys(%possinc)) {
 1856:                 foreach my $key (sort(keys(%possexc))) {
 1857:                     next if ($key eq $intcalldom);
 1858:                     unless ($possinc{$key}) {
 1859:                         push(@allexc,$key);
 1860:                     }
 1861:                 }
 1862:             } else {
 1863:                 @allexc = sort(keys(%possexc));
 1864:             }
 1865:         }
 1866:         if (keys(%possinc)) {
 1867:             $possinc{$intcalldom} = 1;
 1868:             @allinc = sort(keys(%possinc));
 1869:         }
 1870:         if ((@allexc > 0) || (@allinc > 0)) {
 1871:             my %doms_by_intdom;
 1872:             my %allintdoms = &all_host_intdom();
 1873:             my %alldoms = &all_host_domain();
 1874:             foreach my $key (%allintdoms) {
 1875:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1876:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1877:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1878:                     }
 1879:                 } else {
 1880:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1881:                 }
 1882:             }
 1883:             foreach my $exc (@allexc) {
 1884:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1885:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1886:                 }
 1887:             }
 1888:             foreach my $inc (@allinc) {
 1889:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1890:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1891:                 }
 1892:             }
 1893:         }
 1894:     }
 1895:     return ($trusted,$untrusted);
 1896: }
 1897: 
 1898: sub will_trust {
 1899:     my ($cmdtype,$domain,$possdom) = @_;
 1900:     return 1 if ($domain eq $possdom);
 1901:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1902:     my $willtrust; 
 1903:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1904:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1905:             $willtrust = 1;
 1906:         }
 1907:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1908:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1909:             $willtrust = 1;
 1910:         }
 1911:     } else {
 1912:         $willtrust = 1;
 1913:     }
 1914:     return $willtrust;
 1915: }
 1916: 
 1917: # ---------------------- Find the homebase for a user from domain's lib servers
 1918: 
 1919: my %homecache;
 1920: sub homeserver {
 1921:     my ($uname,$udom,$ignoreBadCache)=@_;
 1922:     my $index="$uname:$udom";
 1923: 
 1924:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1925: 
 1926:     my %servers = &get_servers($udom,'library');
 1927:     foreach my $tryserver (keys(%servers)) {
 1928:         next if ($ignoreBadCache ne 'true' && 
 1929: 		 exists($badServerCache{$tryserver}));
 1930: 
 1931: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1932: 	if ($answer eq 'found') {
 1933: 	    delete($badServerCache{$tryserver}); 
 1934: 	    return $homecache{$index}=$tryserver;
 1935: 	} elsif ($answer eq 'no_host') {
 1936: 	    $badServerCache{$tryserver}=1;
 1937: 	}
 1938:     }    
 1939:     return 'no_host';
 1940: }
 1941: 
 1942: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1943: 
 1944: sub idget {
 1945:     my ($udom,$idsref,$namespace)=@_;
 1946:     my %returnhash=();
 1947:     my @ids=(); 
 1948:     if (ref($idsref) eq 'ARRAY') {
 1949:         @ids = @{$idsref};
 1950:     } else {
 1951:         return %returnhash; 
 1952:     }
 1953:     if ($namespace eq '') {
 1954:         $namespace = 'ids';
 1955:     }
 1956:     
 1957:     my %servers = &get_servers($udom,'library');
 1958:     foreach my $tryserver (keys(%servers)) {
 1959: 	my $idlist=join('&', map { &escape($_); } @ids);
 1960: 	if ($namespace eq 'ids') {
 1961: 	    $idlist=~tr/A-Z/a-z/;
 1962: 	}
 1963: 	my $reply;
 1964: 	if ($namespace eq 'ids') {
 1965: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1966: 	} else {
 1967: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1968: 	}
 1969: 	my @answer=();
 1970: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1971: 	    @answer=split(/\&/,$reply);
 1972: 	}                    ;
 1973: 	my $i;
 1974: 	for ($i=0;$i<=$#ids;$i++) {
 1975: 	    if ($answer[$i]) {
 1976: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1977: 	    }
 1978: 	}
 1979:     }
 1980:     return %returnhash;
 1981: }
 1982: 
 1983: # ------------------------------------- Find the IDs behind a list of usernames
 1984: 
 1985: sub idrget {
 1986:     my ($udom,@unames)=@_;
 1987:     my %returnhash=();
 1988:     foreach my $uname (@unames) {
 1989:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1990:     }
 1991:     return %returnhash;
 1992: }
 1993: 
 1994: # Store away a list of names and associated student/employee IDs or clicker IDs
 1995: 
 1996: sub idput {
 1997:     my ($udom,$idsref,$uhom,$namespace)=@_;
 1998:     my %servers=();
 1999:     my %ids=();
 2000:     my %byid = ();
 2001:     if (ref($idsref) eq 'HASH') {
 2002:         %ids=%{$idsref};
 2003:     }
 2004:     if ($namespace eq '') {
 2005:         $namespace = 'ids'; 
 2006:     }
 2007:     foreach my $uname (keys(%ids)) {
 2008: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2009:         if ($uhom eq '') {
 2010:             $uhom=&homeserver($uname,$udom);
 2011:         }
 2012:         if ($uhom ne 'no_host') {
 2013:             my $esc_unam=&escape($uname);
 2014:             if ($namespace eq 'ids') {
 2015:                 my $id=&escape($ids{$uname});
 2016:                 $id=~tr/A-Z/a-z/;
 2017:                 my $esc_unam=&escape($uname);
 2018:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2019:             } else {
 2020:                 my @currids = split(/,/,$ids{$uname});
 2021:                 foreach my $id (@currids) {
 2022:                     $byid{$uhom}{$id} .= $uname.',';
 2023:                 }
 2024:             }
 2025:         }
 2026:     }
 2027:     if ($namespace eq 'clickers') {
 2028:         foreach my $server (keys(%byid)) {
 2029:             if (ref($byid{$server}) eq 'HASH') {
 2030:                 foreach my $id (keys(%{$byid{$server}})) {
 2031:                     $byid{$server} =~ s/,$//;
 2032:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2033:                 }
 2034:             }
 2035:         }
 2036:     }
 2037:     foreach my $server (keys(%servers)) {
 2038:         $servers{$server} =~ s/\&$//;
 2039:         if ($namespace eq 'ids') {     
 2040:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2041:         } else {
 2042:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2043:         }
 2044:     }
 2045: }
 2046: 
 2047: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2048: 
 2049: sub iddel {
 2050:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2051:     my %result=();
 2052:     my %ids=();
 2053:     my %byid = ();
 2054:     if (ref($idshashref) eq 'HASH') {
 2055:         %ids=%{$idshashref};
 2056:     } else {
 2057:         return %result;
 2058:     }
 2059:     if ($namespace eq '') {
 2060:         $namespace = 'ids';
 2061:     }
 2062:     my %servers=();
 2063:     while (my ($id,$unamestr) = each(%ids)) {
 2064:         if ($namespace eq 'ids') {
 2065:             my $uhom = $uhome;
 2066:             if ($uhom eq '') { 
 2067:                 $uhom=&homeserver($unamestr,$udom);
 2068:             }
 2069:             if ($uhom ne 'no_host') {
 2070:                 $servers{$uhom}.='&'.&escape($id);
 2071:             }
 2072:          } else {
 2073:             my @curritems = split(/,/,$ids{$id});
 2074:             foreach my $uname (@curritems) {
 2075:                 my $uhom = $uhome;
 2076:                 if ($uhom eq '') {
 2077:                     $uhom=&homeserver($uname,$udom);
 2078:                 }
 2079:                 if ($uhom ne 'no_host') { 
 2080:                     $byid{$uhom}{$id} .= $uname.',';
 2081:                 }
 2082:             }
 2083:         }
 2084:     }
 2085:     if ($namespace eq 'clickers') {
 2086:         foreach my $server (keys(%byid)) {
 2087:             if (ref($byid{$server}) eq 'HASH') {
 2088:                 foreach my $id (keys(%{$byid{$server}})) {
 2089:                     $byid{$server}{$id} =~ s/,$//;
 2090:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2091:                 }
 2092:             }
 2093:         }
 2094:     }
 2095:     foreach my $server (keys(%servers)) {
 2096:         $servers{$server} =~ s/\&$//;
 2097:         if ($namespace eq 'ids') {
 2098:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2099:         } elsif ($namespace eq 'clickers') {
 2100:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2101:         }
 2102:     }
 2103:     return %result;
 2104: }
 2105: 
 2106: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2107: 
 2108: sub updateclickers {
 2109:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2110:     my %clickers;
 2111:     if (ref($idshashref) eq 'HASH') {
 2112:         %clickers=%{$idshashref};
 2113:     } else {
 2114:         return;
 2115:     }
 2116:     my $items='';
 2117:     foreach my $item (keys(%clickers)) {
 2118:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2119:     }
 2120:     $items=~s/\&$//;
 2121:     my $request = "updateclickers:$udom:$action:$items";
 2122:     if ($critical) {
 2123:         return &critical($request,$uhome);
 2124:     } else {
 2125:         return &reply($request,$uhome);
 2126:     }
 2127: }
 2128: 
 2129: # ------------------------------dump from db file owned by domainconfig user
 2130: sub dump_dom {
 2131:     my ($namespace, $udom, $regexp) = @_;
 2132: 
 2133:     $udom ||= $env{'user.domain'};
 2134: 
 2135:     return () unless $udom;
 2136: 
 2137:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2138: }
 2139: 
 2140: # ------------------------------------------ get items from domain db files   
 2141: 
 2142: sub get_dom {
 2143:     my ($namespace,$storearr,$udom,$uhome)=@_;
 2144:     return if ($udom eq 'public');
 2145:     my $items='';
 2146:     foreach my $item (@$storearr) {
 2147:         $items.=&escape($item).'&';
 2148:     }
 2149:     $items=~s/\&$//;
 2150:     if (!$udom) {
 2151:         $udom=$env{'user.domain'};
 2152:         return if ($udom eq 'public');
 2153:         if (defined(&domain($udom,'primary'))) {
 2154:             $uhome=&domain($udom,'primary');
 2155:         } else {
 2156:             undef($uhome);
 2157:         }
 2158:     } else {
 2159:         if (!$uhome) {
 2160:             if (defined(&domain($udom,'primary'))) {
 2161:                 $uhome=&domain($udom,'primary');
 2162:             }
 2163:         }
 2164:     }
 2165:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2166:         my $rep;
 2167:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2168:             # domain information is hosted on this machine
 2169:             my $cmd = 'getdom';
 2170:             if ($namespace =~ /^enc/) {
 2171:                 $cmd = 'egetdom';
 2172:             }
 2173:             $rep = &LONCAPA::Lond::get_dom("$cmd:$udom:$namespace:$items");
 2174:         } else {
 2175:             if ($namespace =~ /^enc/) {
 2176:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2177:             } else {
 2178:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2179:             }
 2180:         }
 2181:         my %returnhash;
 2182:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2183:             return %returnhash;
 2184:         }
 2185:         my @pairs=split(/\&/,$rep);
 2186:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2187:             return @pairs;
 2188:         }
 2189:         my $i=0;
 2190:         foreach my $item (@$storearr) {
 2191:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2192:             $i++;
 2193:         }
 2194:         return %returnhash;
 2195:     } else {
 2196:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2197:     }
 2198: }
 2199: 
 2200: # -------------------------------------------- put items in domain db files 
 2201: 
 2202: sub put_dom {
 2203:     my ($namespace,$storehash,$udom,$uhome)=@_;
 2204:     if (!$udom) {
 2205:         $udom=$env{'user.domain'};
 2206:         if (defined(&domain($udom,'primary'))) {
 2207:             $uhome=&domain($udom,'primary');
 2208:         } else {
 2209:             undef($uhome);
 2210:         }
 2211:     } else {
 2212:         if (!$uhome) {
 2213:             if (defined(&domain($udom,'primary'))) {
 2214:                 $uhome=&domain($udom,'primary');
 2215:             }
 2216:         }
 2217:     } 
 2218:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2219:         my $items='';
 2220:         foreach my $item (keys(%$storehash)) {
 2221:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2222:         }
 2223:         $items=~s/\&$//;
 2224:         if ($namespace =~ /^enc/) {
 2225:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2226:         } else {
 2227:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2228:         }
 2229:     } else {
 2230:         &logthis("put_dom failed - no homeserver and/or domain");
 2231:     }
 2232: }
 2233: 
 2234: # --------------------- newput for items in db file owned by domainconfig user
 2235: sub newput_dom {
 2236:     my ($namespace,$storehash,$udom) = @_;
 2237:     my $result;
 2238:     if (!$udom) {
 2239:         $udom=$env{'user.domain'};
 2240:     }
 2241:     if ($udom) {
 2242:         my $uname = &get_domainconfiguser($udom);
 2243:         $result = &newput($namespace,$storehash,$udom,$uname);
 2244:     }
 2245:     return $result;
 2246: }
 2247: 
 2248: # --------------------- delete for items in db file owned by domainconfig user
 2249: sub del_dom {
 2250:     my ($namespace,$storearr,$udom)=@_;
 2251:     if (ref($storearr) eq 'ARRAY') {
 2252:         if (!$udom) {
 2253:             $udom=$env{'user.domain'};
 2254:         }
 2255:         if ($udom) {
 2256:             my $uname = &get_domainconfiguser($udom); 
 2257:             return &del($namespace,$storearr,$udom,$uname);
 2258:         }
 2259:     }
 2260: }
 2261: 
 2262: # ----------------------------------construct domainconfig user for a domain 
 2263: sub get_domainconfiguser {
 2264:     my ($udom) = @_;
 2265:     return $udom.'-domainconfig';
 2266: }
 2267: 
 2268: sub retrieve_inst_usertypes {
 2269:     my ($udom) = @_;
 2270:     my (%returnhash,@order);
 2271:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2272:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2273:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2274:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2275:     } else {
 2276:         if (defined(&domain($udom,'primary'))) {
 2277:             my $uhome=&domain($udom,'primary');
 2278:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2279:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2280:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2281:                 return (\%returnhash,\@order);
 2282:             }
 2283:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2284:             my @pairs=split(/\&/,$hashitems);
 2285:             foreach my $item (@pairs) {
 2286:                 my ($key,$value)=split(/=/,$item,2);
 2287:                 $key = &unescape($key);
 2288:                 next if ($key =~ /^error: 2 /);
 2289:                 $returnhash{$key}=&thaw_unescape($value);
 2290:             }
 2291:             my @esc_order = split(/\&/,$orderitems);
 2292:             foreach my $item (@esc_order) {
 2293:                 push(@order,&unescape($item));
 2294:             }
 2295:         } else {
 2296:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2297:         }
 2298:         return (\%returnhash,\@order);
 2299:     }
 2300: }
 2301: 
 2302: sub is_domainimage {
 2303:     my ($url) = @_;
 2304:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2305:         if (&domain($1) ne '') {
 2306:             return '1';
 2307:         }
 2308:     }
 2309:     return;
 2310: }
 2311: 
 2312: sub inst_directory_query {
 2313:     my ($srch) = @_;
 2314:     my $udom = $srch->{'srchdomain'};
 2315:     my %results;
 2316:     my $homeserver = &domain($udom,'primary');
 2317:     my $outcome;
 2318:     if ($homeserver ne '') {
 2319:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2320:             if ($srch->{'srchby'} eq 'email') {
 2321:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2322:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2323:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2324:                     (($major == 2) && ($minor < 12))) {
 2325:                     return;
 2326:                 }
 2327:             }
 2328:         }
 2329: 	my $queryid=&reply("querysend:instdirsearch:".
 2330: 			   &escape($srch->{'srchby'}).':'.
 2331: 			   &escape($srch->{'srchterm'}).':'.
 2332: 			   &escape($srch->{'srchtype'}),$homeserver);
 2333: 	my $host=&hostname($homeserver);
 2334: 	if ($queryid !~/^\Q$host\E\_/) {
 2335: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2336: 	    return;
 2337: 	}
 2338: 	my $response = &get_query_reply($queryid);
 2339: 	my $maxtries = 5;
 2340: 	my $tries = 1;
 2341: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2342: 	    $response = &get_query_reply($queryid);
 2343: 	    $tries ++;
 2344: 	}
 2345: 
 2346:         if (!&error($response) && $response ne 'refused') {
 2347:             if ($response eq 'unavailable') {
 2348:                 $outcome = $response;
 2349:             } else {
 2350:                 $outcome = 'ok';
 2351:                 my @matches = split(/\n/,$response);
 2352:                 foreach my $match (@matches) {
 2353:                     my ($key,$value) = split(/=/,$match);
 2354:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2355:                 }
 2356:             }
 2357:         }
 2358:     }
 2359:     return ($outcome,%results);
 2360: }
 2361: 
 2362: sub usersearch {
 2363:     my ($srch) = @_;
 2364:     my $dom = $srch->{'srchdomain'};
 2365:     my %results;
 2366:     my %libserv = &all_library();
 2367:     my $query = 'usersearch';
 2368:     foreach my $tryserver (keys(%libserv)) {
 2369:         if (&host_domain($tryserver) eq $dom) {
 2370:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2371:                 if ($srch->{'srchby'} eq 'email') {
 2372:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2373:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2374:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2375:                              (($major == 2) && ($minor < 12)));
 2376:                 }
 2377:             }
 2378:             my $host=&hostname($tryserver);
 2379:             my $queryid=
 2380:                 &reply("querysend:".&escape($query).':'.
 2381:                        &escape($srch->{'srchby'}).':'.
 2382:                        &escape($srch->{'srchtype'}).':'.
 2383:                        &escape($srch->{'srchterm'}),$tryserver);
 2384:             if ($queryid !~/^\Q$host\E\_/) {
 2385:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2386:                 next;
 2387:             }
 2388:             my $reply = &get_query_reply($queryid);
 2389:             my $maxtries = 1;
 2390:             my $tries = 1;
 2391:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2392:                 $reply = &get_query_reply($queryid);
 2393:                 $tries ++;
 2394:             }
 2395:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2396:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2397:             } else {
 2398:                 my @matches;
 2399:                 if ($reply =~ /\n/) {
 2400:                     @matches = split(/\n/,$reply);
 2401:                 } else {
 2402:                     @matches = split(/\&/,$reply);
 2403:                 }
 2404:                 foreach my $match (@matches) {
 2405:                     my ($uname,$udom,%userhash);
 2406:                     foreach my $entry (split(/:/,$match)) {
 2407:                         my ($key,$value) =
 2408:                             map {&unescape($_);} split(/=/,$entry);
 2409:                         $userhash{$key} = $value;
 2410:                         if ($key eq 'username') {
 2411:                             $uname = $value;
 2412:                         } elsif ($key eq 'domain') {
 2413:                             $udom = $value;
 2414:                         }
 2415:                     }
 2416:                     $results{$uname.':'.$udom} = \%userhash;
 2417:                 }
 2418:             }
 2419:         }
 2420:     }
 2421:     return %results;
 2422: }
 2423: 
 2424: sub get_instuser {
 2425:     my ($udom,$uname,$id) = @_;
 2426:     my $homeserver = &domain($udom,'primary');
 2427:     my ($outcome,%results);
 2428:     if ($homeserver ne '') {
 2429:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2430:                            &escape($id).':'.&escape($udom),$homeserver);
 2431:         my $host=&hostname($homeserver);
 2432:         if ($queryid !~/^\Q$host\E\_/) {
 2433:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2434:             return;
 2435:         }
 2436:         my $response = &get_query_reply($queryid);
 2437:         my $maxtries = 5;
 2438:         my $tries = 1;
 2439:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2440:             $response = &get_query_reply($queryid);
 2441:             $tries ++;
 2442:         }
 2443:         if (!&error($response) && $response ne 'refused') {
 2444:             if ($response eq 'unavailable') {
 2445:                 $outcome = $response;
 2446:             } else {
 2447:                 $outcome = 'ok';
 2448:                 my @matches = split(/\n/,$response);
 2449:                 foreach my $match (@matches) {
 2450:                     my ($key,$value) = split(/=/,$match);
 2451:                     $results{&unescape($key)} = &thaw_unescape($value);
 2452:                 }
 2453:             }
 2454:         }
 2455:     }
 2456:     my %userinfo;
 2457:     if (ref($results{$uname}) eq 'HASH') {
 2458:         %userinfo = %{$results{$uname}};
 2459:     } 
 2460:     return ($outcome,%userinfo);
 2461: }
 2462: 
 2463: sub get_multiple_instusers {
 2464:     my ($udom,$users,$caller) = @_;
 2465:     my ($outcome,$results);
 2466:     if (ref($users) eq 'HASH') {
 2467:         my $count = keys(%{$users}); 
 2468:         my $requested = &freeze_escape($users);
 2469:         my $homeserver = &domain($udom,'primary');
 2470:         if ($homeserver ne '') {
 2471:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2472:             my $host=&hostname($homeserver);
 2473:             if ($queryid !~/^\Q$host\E\_/) {
 2474:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2475:                          ' for host: '.$homeserver.'in domain '.$udom);
 2476:                 return ($outcome,$results);
 2477:             }
 2478:             my $response = &get_query_reply($queryid);
 2479:             my $maxtries = 5;
 2480:             if ($count > 100) {
 2481:                 $maxtries = 1+int($count/20);
 2482:             }
 2483:             my $tries = 1;
 2484:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2485:                 $response = &get_query_reply($queryid);
 2486:                 $tries ++;
 2487:             }
 2488:             if ($response eq '') {
 2489:                 $results = {};
 2490:                 foreach my $key (keys(%{$users})) {
 2491:                     my ($uname,$id);
 2492:                     if ($caller eq 'id') {
 2493:                         $id = $key;
 2494:                     } else {
 2495:                         $uname = $key;
 2496:                     }
 2497:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2498:                     $outcome = $resp;
 2499:                     if ($resp eq 'ok') {
 2500:                         %{$results} = (%{$results}, %info);
 2501:                     } else {
 2502:                         last;
 2503:                     }
 2504:                 }
 2505:             } elsif(!&error($response) && ($response ne 'refused')) {
 2506:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2507:                     $outcome = $response;
 2508:                 } else {
 2509:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2510:                     if ($outcome eq 'ok') {
 2511:                         $results = &thaw_unescape($userdata); 
 2512:                     }
 2513:                 }
 2514:             }
 2515:         }
 2516:     }
 2517:     return ($outcome,$results);
 2518: }
 2519: 
 2520: sub inst_rulecheck {
 2521:     my ($udom,$uname,$id,$item,$rules) = @_;
 2522:     my %returnhash;
 2523:     if ($udom ne '') {
 2524:         if (ref($rules) eq 'ARRAY') {
 2525:             @{$rules} = map {&escape($_);} (@{$rules});
 2526:             my $rulestr = join(':',@{$rules});
 2527:             my $homeserver=&domain($udom,'primary');
 2528:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2529:                 my $response;
 2530:                 if ($item eq 'username') {                
 2531:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2532:                                               ':'.&escape($uname).':'.$rulestr,
 2533:                                               $homeserver));
 2534:                 } elsif ($item eq 'id') {
 2535:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2536:                                               ':'.&escape($id).':'.$rulestr,
 2537:                                               $homeserver));
 2538:                 } elsif ($item eq 'selfcreate') {
 2539:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2540:                                                &escape($udom).':'.&escape($uname).
 2541:                                               ':'.$rulestr,$homeserver));
 2542:                 }
 2543:                 if ($response ne 'refused') {
 2544:                     my @pairs=split(/\&/,$response);
 2545:                     foreach my $item (@pairs) {
 2546:                         my ($key,$value)=split(/=/,$item,2);
 2547:                         $key = &unescape($key);
 2548:                         next if ($key =~ /^error: 2 /);
 2549:                         $returnhash{$key}=&thaw_unescape($value);
 2550:                     }
 2551:                 }
 2552:             }
 2553:         }
 2554:     }
 2555:     return %returnhash;
 2556: }
 2557: 
 2558: sub inst_userrules {
 2559:     my ($udom,$check) = @_;
 2560:     my (%ruleshash,@ruleorder);
 2561:     if ($udom ne '') {
 2562:         my $homeserver=&domain($udom,'primary');
 2563:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2564:             my $response;
 2565:             if ($check eq 'id') {
 2566:                 $response=&reply('instidrules:'.&escape($udom),
 2567:                                  $homeserver);
 2568:             } elsif ($check eq 'email') {
 2569:                 $response=&reply('instemailrules:'.&escape($udom),
 2570:                                  $homeserver);
 2571:             } else {
 2572:                 $response=&reply('instuserrules:'.&escape($udom),
 2573:                                  $homeserver);
 2574:             }
 2575:             if (($response ne 'refused') && ($response ne 'error') && 
 2576:                 ($response ne 'unknown_cmd') && 
 2577:                 ($response ne 'no_such_host')) {
 2578:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2579:                 my @pairs=split(/\&/,$hashitems);
 2580:                 foreach my $item (@pairs) {
 2581:                     my ($key,$value)=split(/=/,$item,2);
 2582:                     $key = &unescape($key);
 2583:                     next if ($key =~ /^error: 2 /);
 2584:                     $ruleshash{$key}=&thaw_unescape($value);
 2585:                 }
 2586:                 my @esc_order = split(/\&/,$orderitems);
 2587:                 foreach my $item (@esc_order) {
 2588:                     push(@ruleorder,&unescape($item));
 2589:                 }
 2590:             }
 2591:         }
 2592:     }
 2593:     return (\%ruleshash,\@ruleorder);
 2594: }
 2595: 
 2596: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2597: 
 2598: sub get_domain_defaults {
 2599:     my ($domain,$ignore_cache) = @_;
 2600:     return if (($domain eq '') || ($domain eq 'public'));
 2601:     my $cachetime = 60*60*24;
 2602:     unless ($ignore_cache) {
 2603:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2604:         if (defined($cached)) {
 2605:             if (ref($result) eq 'HASH') {
 2606:                 return %{$result};
 2607:             }
 2608:         }
 2609:     }
 2610:     my %domdefaults;
 2611:     my %domconfig =
 2612:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2613:                                   'requestcourses','inststatus',
 2614:                                   'coursedefaults','usersessions',
 2615:                                   'requestauthor','selfenrollment',
 2616:                                   'coursecategories','ssl','autoenroll',
 2617:                                   'trust','helpsettings','wafproxy'],$domain);
 2618:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2619:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2620:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2621:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2622:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2623:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2624:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2625:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2626:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2627:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2628:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2629:     } else {
 2630:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2631:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2632:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2633:     }
 2634:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2635:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2636:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2637:         } else {
 2638:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2639:         }
 2640:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2641:         foreach my $item (@usertools) {
 2642:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2643:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2644:             }
 2645:         }
 2646:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2647:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2648:         }
 2649:     }
 2650:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2651:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2652:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2653:         }
 2654:     }
 2655:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2656:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2657:     }
 2658:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2659:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2660:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2661:         }
 2662:     }
 2663:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2664:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2665:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2666:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2667:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2668:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2669:         }
 2670:         foreach my $type (@coursetypes) {
 2671:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2672:                 unless ($type eq 'community') {
 2673:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2674:                 }
 2675:             }
 2676:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2677:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2678:             }
 2679:             if ($domdefaults{'postsubmit'} eq 'on') {
 2680:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2681:                     $domdefaults{$type.'postsubtimeout'} = 
 2682:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2683:                 }
 2684:             }
 2685:         }
 2686:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2687:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2688:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2689:                 if (@clonecodes) {
 2690:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2691:                 }
 2692:             }
 2693:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2694:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2695:         }
 2696:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2697:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2698:         } 
 2699:     }
 2700:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2701:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2702:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2703:         }
 2704:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2705:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2706:         }
 2707:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2708:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2709:         }
 2710:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2711:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2712:         }
 2713:     }
 2714:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2715:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2716:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2717:                             'approval','limit');
 2718:             foreach my $type (@coursetypes) {
 2719:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2720:                     my @mgrdc = ();
 2721:                     foreach my $item (@settings) {
 2722:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2723:                             push(@mgrdc,$item);
 2724:                         }
 2725:                     }
 2726:                     if (@mgrdc) {
 2727:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2728:                     }
 2729:                 }
 2730:             }
 2731:         }
 2732:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2733:             foreach my $type (@coursetypes) {
 2734:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2735:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2736:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2737:                     }
 2738:                 }
 2739:             }
 2740:         }
 2741:     }
 2742:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2743:         $domdefaults{'catauth'} = 'std';
 2744:         $domdefaults{'catunauth'} = 'std';
 2745:         if ($domconfig{'coursecategories'}{'auth'}) {
 2746:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2747:         }
 2748:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2749:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2750:         }
 2751:     }
 2752:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2753:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2754:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2755:         }
 2756:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2757:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2758:         }
 2759:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2760:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2761:         }
 2762:     }
 2763:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2764:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2765:         foreach my $prefix (@prefixes) {
 2766:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2767:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2768:             }
 2769:         }
 2770:     }
 2771:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2772:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2773:     }
 2774:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2775:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2776:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2777:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2778:         }
 2779:     }
 2780:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2781:         foreach my $item ('ipheader','trusted','vpnint','vpnext') {
 2782:             if ($domconfig{'wafproxy'}{$item}) {
 2783:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2784:             }
 2785:         }
 2786:     } 
 2787:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2788:     return %domdefaults;
 2789: }
 2790: 
 2791: sub get_dom_cats {
 2792:     my ($dom) = @_;
 2793:     return unless (&domain($dom));
 2794:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2795:     unless (defined($cached)) {
 2796:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2797:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2798:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2799:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2800:             } else {
 2801:                 $cats = {};
 2802:             }
 2803:         } else {
 2804:             $cats = {};
 2805:         }
 2806:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
 2807:     }
 2808:     return $cats;
 2809: }
 2810: 
 2811: sub get_dom_instcats {
 2812:     my ($dom) = @_;
 2813:     return unless (&domain($dom));
 2814:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2815:     unless (defined($cached)) {
 2816:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 2817:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 2818:         if ($totcodes > 0) {
 2819:             my $caller = 'global';
 2820:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 2821:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 2822:                 $instcats = {
 2823:                                 codes => \%codes,
 2824:                                 codetitles => \@codetitles,
 2825:                                 cat_titles => \%cat_titles,
 2826:                                 cat_order => \%cat_order,
 2827:                             };
 2828:                 &do_cache_new('instcats',$dom,$instcats,3600);
 2829:             }
 2830:         }
 2831:     }
 2832:     return $instcats;
 2833: }
 2834: 
 2835: sub retrieve_instcodes {
 2836:     my ($coursecodes,$dom) = @_;
 2837:     my $totcodes;
 2838:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 2839:     foreach my $course (keys(%courses)) {
 2840:         if (ref($courses{$course}) eq 'HASH') {
 2841:             if ($courses{$course}{'inst_code'} ne '') {
 2842:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 2843:                 $totcodes ++;
 2844:             }
 2845:         }
 2846:     }
 2847:     return $totcodes;
 2848: }
 2849: 
 2850: sub course_portal_url {
 2851:     my ($cnum,$cdom) = @_;
 2852:     my $chome = &homeserver($cnum,$cdom);
 2853:     my $hostname = &hostname($chome);
 2854:     my $protocol = $protocol{$chome};
 2855:     $protocol = 'http' if ($protocol ne 'https');
 2856:     my %domdefaults = &get_domain_defaults($cdom);
 2857:     my $firsturl;
 2858:     if ($domdefaults{'portal_def'}) {
 2859:         $firsturl = $domdefaults{'portal_def'};
 2860:     } else {
 2861:         $firsturl = $protocol.'://'.$hostname;
 2862:     }
 2863:     return $firsturl;
 2864: }
 2865: 
 2866: # --------------------------------------------- Get domain config for passwords
 2867: 
 2868: sub get_passwdconf {
 2869:     my ($dom) = @_;
 2870:     my (%passwdconf,$gotconf,$lookup);
 2871:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2872:     if (defined($cached)) {
 2873:         if (ref($result) eq 'HASH') {
 2874:             %passwdconf = %{$result};
 2875:             $gotconf = 1;
 2876:         }
 2877:     }
 2878:     unless ($gotconf) {
 2879:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2880:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2881:             %passwdconf = %{$domconfig{'passwords'}};
 2882:         }
 2883:         my $cachetime = 24*60*60;
 2884:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2885:     }
 2886:     return %passwdconf;
 2887: }
 2888: 
 2889: # --------------------------------------------------- Assign a key to a student
 2890: 
 2891: sub assign_access_key {
 2892: #
 2893: # a valid key looks like uname:udom#comments
 2894: # comments are being appended
 2895: #
 2896:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2897:     $kdom=
 2898:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2899:     $knum=
 2900:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2901:     $cdom=
 2902:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2903:     $cnum=
 2904:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2905:     $udom=$env{'user.name'} unless (defined($udom));
 2906:     $uname=$env{'user.domain'} unless (defined($uname));
 2907:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2908:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2909:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2910:                                                   # assigned to this person
 2911:                                                   # - this should not happen,
 2912:                                                   # unless something went wrong
 2913:                                                   # the first time around
 2914: # ready to assign
 2915:         $logentry=$1.'; '.$logentry;
 2916:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2917:                                                  $kdom,$knum) eq 'ok') {
 2918: # key now belongs to user
 2919: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2920:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2921:                 &appenv({'environment.'.$envkey => $ckey});
 2922:                 return 'ok';
 2923:             } else {
 2924:                 return 
 2925:   'error: Count not permanently assign key, will need to be re-entered later.';
 2926: 	    }
 2927:         } else {
 2928:             return 'error: Could not assign key, try again later.';
 2929:         }
 2930:     } elsif (!$existing{$ckey}) {
 2931: # the key does not exist
 2932: 	return 'error: The key does not exist';
 2933:     } else {
 2934: # the key is somebody else's
 2935: 	return 'error: The key is already in use';
 2936:     }
 2937: }
 2938: 
 2939: # ------------------------------------------ put an additional comment on a key
 2940: 
 2941: sub comment_access_key {
 2942: #
 2943: # a valid key looks like uname:udom#comments
 2944: # comments are being appended
 2945: #
 2946:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2947:     $cdom=
 2948:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2949:     $cnum=
 2950:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2951:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2952:     if ($existing{$ckey}) {
 2953:         $existing{$ckey}.='; '.$logentry;
 2954: # ready to assign
 2955:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2956:                                                  $cdom,$cnum) eq 'ok') {
 2957: 	    return 'ok';
 2958:         } else {
 2959: 	    return 'error: Count not store comment.';
 2960:         }
 2961:     } else {
 2962: # the key does not exist
 2963: 	return 'error: The key does not exist';
 2964:     }
 2965: }
 2966: 
 2967: # ------------------------------------------------------ Generate a set of keys
 2968: 
 2969: sub generate_access_keys {
 2970:     my ($number,$cdom,$cnum,$logentry)=@_;
 2971:     $cdom=
 2972:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2973:     $cnum=
 2974:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2975:     unless (&allowed('mky',$cdom)) { return 0; }
 2976:     unless (($cdom) && ($cnum)) { return 0; }
 2977:     if ($number>10000) { return 0; }
 2978:     sleep(2); # make sure don't get same seed twice
 2979:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2980:     my $total=0;
 2981:     for (my $i=1;$i<=$number;$i++) {
 2982:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2983:                   sprintf("%lx",int(100000*rand)).'-'.
 2984:                   sprintf("%lx",int(100000*rand));
 2985:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2986:        $newkey=~s/0/h/g; # and also 0 and O
 2987:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2988:        if ($existing{$newkey}) {
 2989:            $i--;
 2990:        } else {
 2991: 	  if (&put('accesskeys',
 2992:               { $newkey => '# generated '.localtime().
 2993:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 2994:                            '; '.$logentry },
 2995: 		   $cdom,$cnum) eq 'ok') {
 2996:               $total++;
 2997: 	  }
 2998:        }
 2999:     }
 3000:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3001:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3002:     return $total;
 3003: }
 3004: 
 3005: # ------------------------------------------------------- Validate an accesskey
 3006: 
 3007: sub validate_access_key {
 3008:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3009:     $cdom=
 3010:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3011:     $cnum=
 3012:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3013:     $udom=$env{'user.domain'} unless (defined($udom));
 3014:     $uname=$env{'user.name'} unless (defined($uname));
 3015:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3016:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3017: }
 3018: 
 3019: # ------------------------------------- Find the section of student in a course
 3020: sub devalidate_getsection_cache {
 3021:     my ($udom,$unam,$courseid)=@_;
 3022:     my $hashid="$udom:$unam:$courseid";
 3023:     &devalidate_cache_new('getsection',$hashid);
 3024: }
 3025: 
 3026: sub courseid_to_courseurl {
 3027:     my ($courseid) = @_;
 3028:     #already url style courseid
 3029:     return $courseid if ($courseid =~ m{^/});
 3030: 
 3031:     if (exists($env{'course.'.$courseid.'.num'})) {
 3032: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3033: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3034: 	return "/$cdom/$cnum";
 3035:     }
 3036: 
 3037:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 3038:     if (exists($courseinfo{'num'})) {
 3039: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3040:     }
 3041: 
 3042:     return undef;
 3043: }
 3044: 
 3045: sub getsection {
 3046:     my ($udom,$unam,$courseid)=@_;
 3047:     my $cachetime=1800;
 3048: 
 3049:     my $hashid="$udom:$unam:$courseid";
 3050:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3051:     if (defined($cached)) { return $result; }
 3052: 
 3053:     my %Pending; 
 3054:     my %Expired;
 3055:     #
 3056:     # Each role can either have not started yet (pending), be active, 
 3057:     #    or have expired.
 3058:     #
 3059:     # If there is an active role, we are done.
 3060:     #
 3061:     # If there is more than one role which has not started yet, 
 3062:     #     choose the one which will start sooner
 3063:     # If there is one role which has not started yet, return it.
 3064:     #
 3065:     # If there is more than one expired role, choose the one which ended last.
 3066:     # If there is a role which has expired, return it.
 3067:     #
 3068:     $courseid = &courseid_to_courseurl($courseid);
 3069:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3070:     foreach my $key (keys(%roleshash)) {
 3071:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3072:         my $section=$1;
 3073:         if ($key eq $courseid.'_st') { $section=''; }
 3074:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3075:         my $now=time;
 3076:         if (defined($end) && $end && ($now > $end)) {
 3077:             $Expired{$end}=$section;
 3078:             next;
 3079:         }
 3080:         if (defined($start) && $start && ($now < $start)) {
 3081:             $Pending{$start}=$section;
 3082:             next;
 3083:         }
 3084:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3085:     }
 3086:     #
 3087:     # Presumedly there will be few matching roles from the above
 3088:     # loop and the sorting time will be negligible.
 3089:     if (scalar(keys(%Pending))) {
 3090:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3091:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3092:     } 
 3093:     if (scalar(keys(%Expired))) {
 3094:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3095:         my $time = pop(@sorted);
 3096:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3097:     }
 3098:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3099: }
 3100: 
 3101: sub save_cache {
 3102:     &purge_remembered();
 3103:     #&Apache::loncommon::validate_page();
 3104:     undef(%env);
 3105:     undef($env_loaded);
 3106: }
 3107: 
 3108: my $to_remember=-1;
 3109: my %remembered;
 3110: my %accessed;
 3111: my $kicks=0;
 3112: my $hits=0;
 3113: sub make_key {
 3114:     my ($name,$id) = @_;
 3115:     if (length($id) > 65 
 3116: 	&& length(&escape($id)) > 200) {
 3117: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3118:     }
 3119:     return &escape($name.':'.$id);
 3120: }
 3121: 
 3122: sub devalidate_cache_new {
 3123:     my ($name,$id,$debug) = @_;
 3124:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3125:     my $remembered_id=$name.':'.$id;
 3126:     $id=&make_key($name,$id);
 3127:     $memcache->delete($id);
 3128:     delete($remembered{$remembered_id});
 3129:     delete($accessed{$remembered_id});
 3130: }
 3131: 
 3132: sub is_cached_new {
 3133:     my ($name,$id,$debug) = @_;
 3134:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3135:     if (exists($remembered{$remembered_id})) {
 3136: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3137: 	$accessed{$remembered_id}=[&gettimeofday()];
 3138: 	$hits++;
 3139: 	return ($remembered{$remembered_id},1);
 3140:     }
 3141:     $id=&make_key($name,$id);
 3142:     my $value = $memcache->get($id);
 3143:     if (!(defined($value))) {
 3144: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3145: 	return (undef,undef);
 3146:     }
 3147:     if ($value eq '__undef__') {
 3148: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3149: 	$value=undef;
 3150:     }
 3151:     &make_room($remembered_id,$value,$debug);
 3152:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3153:     return ($value,1);
 3154: }
 3155: 
 3156: sub do_cache_new {
 3157:     my ($name,$id,$value,$time,$debug) = @_;
 3158:     my $remembered_id=$name.':'.$id;
 3159:     $id=&make_key($name,$id);
 3160:     my $setvalue=$value;
 3161:     if (!defined($setvalue)) {
 3162: 	$setvalue='__undef__';
 3163:     }
 3164:     if (!defined($time) ) {
 3165: 	$time=600;
 3166:     }
 3167:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3168:     my $result = $memcache->set($id,$setvalue,$time);
 3169:     if (! $result) {
 3170: 	&logthis("caching of id -> $id  failed");
 3171: 	$memcache->disconnect_all();
 3172:     }
 3173:     # need to make a copy of $value
 3174:     &make_room($remembered_id,$value,$debug);
 3175:     return $value;
 3176: }
 3177: 
 3178: sub make_room {
 3179:     my ($remembered_id,$value,$debug)=@_;
 3180: 
 3181:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3182:                                     : $value;
 3183:     if ($to_remember<0) { return; }
 3184:     $accessed{$remembered_id}=[&gettimeofday()];
 3185:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3186:     my $to_kick;
 3187:     my $max_time=0;
 3188:     foreach my $other (keys(%accessed)) {
 3189: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3190: 	    $to_kick=$other;
 3191: 	    $max_time=&tv_interval($accessed{$other});
 3192: 	}
 3193:     }
 3194:     delete($remembered{$to_kick});
 3195:     delete($accessed{$to_kick});
 3196:     $kicks++;
 3197:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3198:     return;
 3199: }
 3200: 
 3201: sub purge_remembered {
 3202:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3203:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3204:     undef(%remembered);
 3205:     undef(%accessed);
 3206: }
 3207: # ------------------------------------- Read an entry from a user's environment
 3208: 
 3209: sub userenvironment {
 3210:     my ($udom,$unam,@what)=@_;
 3211:     my $items;
 3212:     foreach my $item (@what) {
 3213:         $items.=&escape($item).'&';
 3214:     }
 3215:     $items=~s/\&$//;
 3216:     my %returnhash=();
 3217:     my $uhome = &homeserver($unam,$udom);
 3218:     unless ($uhome eq 'no_host') {
 3219:         my @answer=split(/\&/, 
 3220:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3221:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3222:             return %returnhash;
 3223:         }
 3224:         my $i;
 3225:         for ($i=0;$i<=$#what;$i++) {
 3226: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3227:         }
 3228:     }
 3229:     return %returnhash;
 3230: }
 3231: 
 3232: # ---------------------------------------------------------- Get a studentphoto
 3233: sub studentphoto {
 3234:     my ($udom,$unam,$ext) = @_;
 3235:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3236:     if (defined($env{'request.course.id'})) {
 3237:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3238:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3239:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3240:             } else {
 3241:                 my ($result,$perm_reqd)=
 3242: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3243:                 if ($result eq 'ok') {
 3244:                     if (!($perm_reqd eq 'yes')) {
 3245:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3246:                     }
 3247:                 }
 3248:             }
 3249:         }
 3250:     } else {
 3251:         my ($result,$perm_reqd) = 
 3252: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3253:         if ($result eq 'ok') {
 3254:             if (!($perm_reqd eq 'yes')) {
 3255:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3256:             }
 3257:         }
 3258:     }
 3259:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3260: }
 3261: 
 3262: sub retrievestudentphoto {
 3263:     my ($udom,$unam,$ext,$type) = @_;
 3264:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3265:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3266:     if ($ret eq 'ok') {
 3267:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3268:         if ($type eq 'thumbnail') {
 3269:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3270:         }
 3271:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3272:         return $tokenurl;
 3273:     } else {
 3274:         if ($type eq 'thumbnail') {
 3275:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3276:         } else { 
 3277:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3278:         }
 3279:     }
 3280: }
 3281: 
 3282: # -------------------------------------------------------------------- New chat
 3283: 
 3284: sub chatsend {
 3285:     my ($newentry,$anon,$group)=@_;
 3286:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3287:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3288:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3289:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3290: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3291: 		   &escape($newentry)).':'.$group,$chome);
 3292: }
 3293: 
 3294: # ------------------------------------------ Find current version of a resource
 3295: 
 3296: sub getversion {
 3297:     my $fname=&clutter(shift);
 3298:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3299:     return &currentversion(&filelocation('',$fname));
 3300: }
 3301: 
 3302: sub currentversion {
 3303:     my $fname=shift;
 3304:     my $author=$fname;
 3305:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3306:     my ($udom,$uname)=split(/\//,$author);
 3307:     my $home=&homeserver($uname,$udom);
 3308:     if ($home eq 'no_host') { 
 3309:         return -1; 
 3310:     }
 3311:     my $answer=&reply("currentversion:$fname",$home);
 3312:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3313: 	return -1;
 3314:     }
 3315:     return $answer;
 3316: }
 3317: 
 3318: #
 3319: # Return special version number of resource if set by override, empty otherwise
 3320: #
 3321: sub usedversion {
 3322:     my $fname=shift;
 3323:     unless ($fname) { $fname=$env{'request.uri'}; }
 3324:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3325:     if ($urlversion) { return $urlversion; }
 3326:     return '';
 3327: }
 3328: 
 3329: # ----------------------------- Subscribe to a resource, return URL if possible
 3330: 
 3331: sub subscribe {
 3332:     my $fname=shift;
 3333:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3334:     $fname=~s/[\n\r]//g;
 3335:     my $author=$fname;
 3336:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3337:     my ($udom,$uname)=split(/\//,$author);
 3338:     my $home=homeserver($uname,$udom);
 3339:     if ($home eq 'no_host') {
 3340:         return 'not_found';
 3341:     }
 3342:     my $answer=reply("sub:$fname",$home);
 3343:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3344: 	$answer.=' by '.$home;
 3345:     }
 3346:     return $answer;
 3347: }
 3348:     
 3349: # -------------------------------------------------------------- Replicate file
 3350: 
 3351: sub repcopy {
 3352:     my $filename=shift;
 3353:     $filename=~s/\/+/\//g;
 3354:     my $londocroot = $perlvar{'lonDocRoot'};
 3355:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3356:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3357:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3358: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3359: 	return &repcopy_userfile($filename);
 3360:     }
 3361:     $filename=~s/[\n\r]//g;
 3362:     my $transname="$filename.in.transfer";
 3363: # FIXME: this should flock
 3364:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3365:     my $remoteurl=subscribe($filename);
 3366:     if ($remoteurl =~ /^con_lost by/) {
 3367: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3368:            return 'unavailable';
 3369:     } elsif ($remoteurl eq 'not_found') {
 3370: 	   #&logthis("Subscribe returned not_found: $filename");
 3371: 	   return 'not_found';
 3372:     } elsif ($remoteurl =~ /^rejected by/) {
 3373: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3374:            return 'forbidden';
 3375:     } elsif ($remoteurl eq 'directory') {
 3376:            return 'ok';
 3377:     } else {
 3378:         my $author=$filename;
 3379:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3380:         my ($udom,$uname)=split(/\//,$author);
 3381:         my $home=homeserver($uname,$udom);
 3382:         unless ($home eq $perlvar{'lonHostID'}) {
 3383:            my @parts=split(/\//,$filename);
 3384:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3385:            if ($path ne "$londocroot/res") {
 3386:                &logthis("Malconfiguration for replication: $filename");
 3387: 	       return 'bad_request';
 3388:            }
 3389:            my $count;
 3390:            for ($count=5;$count<$#parts;$count++) {
 3391:                $path.="/$parts[$count]";
 3392:                if ((-e $path)!=1) {
 3393: 		   mkdir($path,0777);
 3394:                }
 3395:            }
 3396:            my $request=new HTTP::Request('GET',"$remoteurl");
 3397:            my $response;
 3398:            if ($remoteurl =~ m{/raw/}) {
 3399:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3400:            } else {
 3401:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3402:            }
 3403:            if ($response->is_error()) {
 3404: 	       unlink($transname);
 3405:                my $message=$response->status_line;
 3406:                &logthis("<font color=\"blue\">WARNING:"
 3407:                        ." LWP get: $message: $filename</font>");
 3408:                return 'unavailable';
 3409:            } else {
 3410: 	       if ($remoteurl!~/\.meta$/) {
 3411:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3412:                   my $mresponse;
 3413:                   if ($remoteurl =~ m{/raw/}) {
 3414:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3415:                   } else {
 3416:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3417:                   }
 3418:                   if ($mresponse->is_error()) {
 3419: 		      unlink($filename.'.meta');
 3420:                       &logthis(
 3421:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3422:                   }
 3423: 	       }
 3424:                rename($transname,$filename);
 3425:                return 'ok';
 3426:            }
 3427:        }
 3428:     }
 3429: }
 3430: 
 3431: # ------------------------------------------------- Unsubscribe from a resource
 3432: 
 3433: sub unsubscribe {
 3434:     my ($fname) = @_;
 3435:     my $answer;
 3436:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3437:     $fname=~s/[\n\r]//g;
 3438:     my $author=$fname;
 3439:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3440:     my ($udom,$uname)=split(/\//,$author);
 3441:     my $home=homeserver($uname,$udom);
 3442:     if ($home eq 'no_host') {
 3443:         $answer = 'no_host';
 3444:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3445:         $answer = 'home';
 3446:     } else {
 3447:         my $defdom = $perlvar{'lonDefDomain'};
 3448:         if (&will_trust('content',$defdom,$udom)) {
 3449:             $answer = reply("unsub:$fname",$home);
 3450:         } else {
 3451:             $answer = 'untrusted';
 3452:         }
 3453:     }
 3454:     return $answer;
 3455: }
 3456: 
 3457: # ------------------------------------------------ Get server side include body
 3458: sub ssi_body {
 3459:     my ($filelink,%form)=@_;
 3460:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3461:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3462:     }
 3463:     my $output='';
 3464:     my $response;
 3465:     if ($filelink=~/^https?\:/) {
 3466:        ($output,$response)=&externalssi($filelink);
 3467:     } else {
 3468:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3469:        $filelink .= 'inhibitmenu=yes';
 3470:        ($output,$response)=&ssi($filelink,%form);
 3471:     }
 3472:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3473:     $output=~s/^.*?\<body[^\>]*\>//si;
 3474:     $output=~s/\<\/body\s*\>.*?$//si;
 3475:     if (wantarray) {
 3476:         return ($output, $response);
 3477:     } else {
 3478:         return $output;
 3479:     }
 3480: }
 3481: 
 3482: # --------------------------------------------------------- Server Side Include
 3483: 
 3484: sub absolute_url {
 3485:     my ($host_name,$unalias,$keep_proto) = @_;
 3486:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3487:     if ($host_name eq '') {
 3488: 	$host_name = $ENV{'SERVER_NAME'};
 3489:     }
 3490:     if ($unalias) {
 3491:         my $alias = &get_proxy_alias();
 3492:         if ($alias eq $host_name) {
 3493:             my $lonhost = $perlvar{'lonHostID'};
 3494:             my $hostname = &hostname($lonhost);
 3495:             my $lcproto; 
 3496:             if (($keep_proto) || ($hostname eq '')) {
 3497:                 $lcproto = $protocol;
 3498:             } else {
 3499:                 $lcproto = $protocol{$lonhost};
 3500:                 $lcproto = 'http' if ($lcproto ne 'https');
 3501:                 $lcproto .= '://';
 3502:             }
 3503:             unless ($hostname eq '') {
 3504:                 return $lcproto.$hostname;
 3505:             }
 3506:         }
 3507:     }
 3508:     return $protocol.$host_name;
 3509: }
 3510: 
 3511: #
 3512: #   Server side include.
 3513: # Parameters:
 3514: #  fn     Possibly encrypted resource name/id.
 3515: #  form   Hash that describes how the rendering should be done
 3516: #         and other things.
 3517: # Returns:
 3518: #   Scalar context: The content of the response.
 3519: #   Array context:  2 element list of the content and the full response object.
 3520: #     
 3521: sub ssi {
 3522: 
 3523:     my ($fn,%form)=@_;
 3524:     my ($host,$request,$response);
 3525:     $host = &absolute_url('',1);
 3526: 
 3527:     $form{'no_update_last_known'}=1;
 3528:     &Apache::lonenc::check_encrypt(\$fn);
 3529:     if (%form) {
 3530:       $request=new HTTP::Request('POST',$host.$fn);
 3531:       $request->content(join('&',map { 
 3532:             my $name = escape($_);
 3533:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3534:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3535:             : &escape($form{$_}) );    
 3536:         } keys(%form)));
 3537:     } else {
 3538:       $request=new HTTP::Request('GET',$host.$fn);
 3539:     }
 3540: 
 3541:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3542:     my $lonhost = $perlvar{'lonHostID'};
 3543:     my $islocal;
 3544:     if (($env{'request.course.id'}) &&
 3545:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3546:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3547:         ($form{'grade_symb'} ne '') &&
 3548:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3549:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3550:         $islocal = 1;
 3551:     }
 3552:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3553:                                              '','','',$islocal);
 3554: 
 3555:     if (wantarray) {
 3556: 	return ($response->content, $response);
 3557:     } else {
 3558: 	return $response->content;
 3559:     }
 3560: }
 3561: 
 3562: sub externalssi {
 3563:     my ($url)=@_;
 3564:     my $request=new HTTP::Request('GET',$url);
 3565:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3566:     if (wantarray) {
 3567:         return ($response->content, $response);
 3568:     } else {
 3569:         return $response->content;
 3570:     }
 3571: }
 3572: 
 3573: 
 3574: # If the local copy of a replicated resource is outdated, trigger a  
 3575: # connection from the homeserver to flush the delayed queue. If no update 
 3576: # happens, remove local copies of outdated resource (and corresponding
 3577: # metadata file).
 3578: 
 3579: sub remove_stale_resfile {
 3580:     my ($url) = @_;
 3581:     my $removed;
 3582:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3583:         my $audom = $1;
 3584:         my $auname = $2;
 3585:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3586:             my $homeserver = &homeserver($auname,$audom);
 3587:             unless (($homeserver eq 'no_host') ||
 3588:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3589:                 my $fname = &filelocation('',$url);
 3590:                 if (-e $fname) {
 3591:                     my $hostname = &hostname($homeserver);
 3592:                     if ($hostname) {
 3593:                         my $protocol = $protocol{$homeserver};
 3594:                         $protocol = 'http' if ($protocol ne 'https');
 3595:                         my $uri = &declutter($url);
 3596:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3597:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3598:                         if ($response->is_success()) {
 3599:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3600:                             my $locmodtime = (stat($fname))[9];
 3601:                             if ($locmodtime < $remmodtime) {
 3602:                                 my $stale;
 3603:                                 my $answer = &reply('pong',$homeserver);
 3604:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3605:                                     sleep(0.2);
 3606:                                     $locmodtime = (stat($fname))[9];
 3607:                                     if ($locmodtime < $remmodtime) {
 3608:                                         my $posstransfer = $fname.'.in.transfer';
 3609:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3610:                                             $removed = 1;
 3611:                                         } else {
 3612:                                             $stale = 1;
 3613:                                         }
 3614:                                     } else {
 3615:                                         $removed = 1;
 3616:                                     }
 3617:                                 } else {
 3618:                                     $stale = 1;
 3619:                                 }
 3620:                                 if ($stale) {
 3621:                                     if (unlink($fname)) {
 3622:                                         if ($uri!~/\.meta$/) {
 3623:                                             if (-e $fname.'.meta') {
 3624:                                                 unlink($fname.'.meta');
 3625:                                             }
 3626:                                         }
 3627:                                         my $unsubresult = &unsubscribe($fname);
 3628:                                         unless ($unsubresult eq 'ok') {
 3629:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3630:                                         }
 3631:                                         $removed = 1;
 3632:                                     }
 3633:                                 }
 3634:                             }
 3635:                         }
 3636:                     }
 3637:                 }
 3638:             }
 3639:         }
 3640:     }
 3641:     return $removed;
 3642: }
 3643: 
 3644: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3645: 
 3646: sub allowuploaded {
 3647:     my ($srcurl,$url)=@_;
 3648:     $url=&clutter(&declutter($url));
 3649:     my $dir=$url;
 3650:     $dir=~s/\/[^\/]+$//;
 3651:     my %httpref=();
 3652:     my $httpurl=&hreflocation('',$url);
 3653:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3654:     &Apache::lonnet::appenv(\%httpref);
 3655: }
 3656: 
 3657: #
 3658: # Determine if the current user should be able to edit a particular resource,
 3659: # when viewing in course context.
 3660: # (a) When viewing resource used to determine if "Edit" item is included in 
 3661: #     Functions.
 3662: # (b) When displaying folder contents in course editor, used to determine if
 3663: #     "Edit" link will be displayed alongside resource.
 3664: #
 3665: #  input: six args -- filename (decluttered), course number, course domain,
 3666: #                   url, symb (if registered) and group (if this is a group
 3667: #                   item -- e.g., bulletin board, group page etc.).
 3668: #  output: array of five scalars -- 
 3669: #          $cfile -- url for file editing if editable on current server
 3670: #          $home -- homeserver of resource (i.e., for author if published,
 3671: #                                           or course if uploaded.).
 3672: #          $switchserver --  1 if server switch will be needed.
 3673: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3674: #          $forceview -- 1 if icon/link should be to go to view mode
 3675: #
 3676: 
 3677: sub can_edit_resource {
 3678:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3679:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3680: #
 3681: # For aboutme pages user can only edit his/her own.
 3682: #
 3683:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3684:         my ($sdom,$sname) = ($1,$2);
 3685:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3686:             $home = $env{'user.home'};
 3687:             $cfile = $resurl;
 3688:             if ($env{'form.forceedit'}) {
 3689:                 $forceview = 1;
 3690:             } else {
 3691:                 $forceedit = 1;
 3692:             }
 3693:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3694:         } else {
 3695:             return;
 3696:         }
 3697:     }
 3698: 
 3699:     if ($env{'request.course.id'}) {
 3700:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3701:         if ($group ne '') {
 3702: # if this is a group homepage or group bulletin board, check group privs
 3703:             my $allowed = 0;
 3704:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3705:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3706:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3707:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3708:                     $allowed = 1;
 3709:                 }
 3710:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3711:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3712:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3713:                     $allowed = 1;
 3714:                 }
 3715:             }
 3716:             if ($allowed) {
 3717:                 $home=&homeserver($cnum,$cdom);
 3718:                 if ($env{'form.forceedit'}) {
 3719:                     $forceview = 1;
 3720:                 } else {
 3721:                     $forceedit = 1;
 3722:                 }
 3723:                 $cfile = $resurl;
 3724:             } else {
 3725:                 return;
 3726:             }
 3727:         } else {
 3728:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3729:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3730:                     return;
 3731:                 }
 3732:             } elsif (!$crsedit) {
 3733: #
 3734: # No edit allowed where CC has switched to student role.
 3735: #
 3736:                 return;
 3737:             }
 3738:         }
 3739:     }
 3740: 
 3741:     if ($file ne '') {
 3742:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3743:             if (&is_course_upload($file,$cnum,$cdom)) {
 3744:                 $uploaded = 1;
 3745:                 $incourse = 1;
 3746:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3747:                     $cfile = &hreflocation('',$file);
 3748:                     if ($env{'form.forceedit'}) {
 3749:                         $forceview = 1;
 3750:                     } else {
 3751:                         $forceedit = 1;
 3752:                     }
 3753:                 }
 3754:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3755:                 $incourse = 1;
 3756:                 if ($env{'form.forceedit'}) {
 3757:                     $forceview = 1;
 3758:                 } else {
 3759:                     $forceedit = 1;
 3760:                 }
 3761:                 $cfile = $resurl;
 3762:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3763:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3764:                     $incourse = 1;
 3765:                     if ($env{'form.forceedit'}) {
 3766:                         $forceview = 1;
 3767:                     } else {
 3768:                         $forceedit = 1;
 3769:                     }
 3770:                     $cfile = $resurl;
 3771:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3772:                     $incourse = 1;
 3773:                     $cfile = $resurl.'/smpedit';
 3774:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3775:                     $incourse = 1;
 3776:                     if ($env{'form.forceedit'}) {
 3777:                         $forceview = 1;
 3778:                     } else {
 3779:                         $forceedit = 1;
 3780:                     }
 3781:                     $cfile = $resurl;
 3782:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3783:                     my ($map,$id,$res) = &decode_symb($symb);
 3784:                     if ($map =~ /\.page$/) {
 3785:                         $incourse = 1;
 3786:                         if ($env{'form.forceedit'}) {
 3787:                             $forceview = 1;
 3788:                             $cfile = $map;
 3789:                         } else {
 3790:                             $forceedit = 1;
 3791:                             $cfile =  '/adm/wrapper'.$resurl;
 3792:                         }
 3793:                     }
 3794:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3795:                     $incourse = 1;
 3796:                     if ($env{'form.forceedit'}) {
 3797:                         $forceview = 1;
 3798:                     } else {
 3799:                         $forceedit = 1;
 3800:                     }
 3801:                     $cfile = $resurl;
 3802:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3803:                     $incourse = 1;
 3804:                     if ($env{'form.forceedit'}) {
 3805:                         $forceview = 1;
 3806:                     } else {
 3807:                         $forceedit = 1;
 3808:                     }
 3809:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3810:                 }
 3811:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3812:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3813:                 if (&is_on_map($template)) { 
 3814:                     $incourse = 1;
 3815:                     $forceview = 1;
 3816:                     $cfile = $template;
 3817:                 }
 3818:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3819:                 $incourse = 1;
 3820:                 if ($env{'form.forceedit'}) {
 3821:                     $forceview = 1;
 3822:                 } else {
 3823:                     $forceedit = 1;
 3824:                 }
 3825:                 $cfile = $resurl;
 3826:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3827:                 $incourse = 1;
 3828:                 if ($env{'form.forceedit'}) {
 3829:                     $forceview = 1;
 3830:                 } else {
 3831:                     $forceedit = 1;
 3832:                 }
 3833:                 $cfile = $resurl;
 3834:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3835:                 $incourse = 1;
 3836:                 $forceview = 1;
 3837:                 if ($symb) {
 3838:                     my ($map,$id,$res)=&decode_symb($symb);
 3839:                     $env{'request.symb'} = $symb;
 3840:                     $cfile = &clutter($res);
 3841:                 } else {
 3842:                     $cfile = $env{'form.suppurl'};
 3843:                     my $escfile = &unescape($cfile);
 3844:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3845:                         $cfile = '/adm/wrapper'.$escfile;
 3846:                     } else {
 3847:                         $escfile =~ s{^http://}{};
 3848:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3849:                     }
 3850:                 }
 3851:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3852:                 if ($env{'form.forceedit'}) {
 3853:                     $forceview = 1;
 3854:                 } else {
 3855:                     $forceedit = 1;
 3856:                 }
 3857:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3858:             }
 3859:         }
 3860:         if ($uploaded || $incourse) {
 3861:             $home=&homeserver($cnum,$cdom);
 3862:         } elsif ($file !~ m{/$}) {
 3863:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3864:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3865:             # Check that the user has permission to edit this resource
 3866:             my $setpriv = 1;
 3867:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3868:             if (defined($cfudom)) {
 3869:                 $home=&homeserver($cfuname,$cfudom);
 3870:                 $cfile=$file;
 3871:             }
 3872:         }
 3873:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3874:             (($home ne '') && ($home ne 'no_host'))) {
 3875:             my @ids=&current_machine_ids();
 3876:             unless (grep(/^\Q$home\E$/,@ids)) {
 3877:                 $switchserver=1;
 3878:             }
 3879:         }
 3880:     }
 3881:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3882: }
 3883: 
 3884: sub is_course_upload {
 3885:     my ($file,$cnum,$cdom) = @_;
 3886:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3887:     $uploadpath =~ s{^\/}{};
 3888:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3889:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3890:         return 1;
 3891:     }
 3892:     return;
 3893: }
 3894: 
 3895: sub in_course {
 3896:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3897:     if ($hideprivileged) {
 3898:         my $skipuser;
 3899:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3900:         my @possdoms = ($cdom);  
 3901:         if ($coursehash{'checkforpriv'}) { 
 3902:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3903:         }
 3904:         if (&privileged($uname,$udom,\@possdoms)) {
 3905:             $skipuser = 1;
 3906:             if ($coursehash{'nothideprivileged'}) {
 3907:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3908:                     my $user;
 3909:                     if ($item =~ /:/) {
 3910:                         $user = $item;
 3911:                     } else {
 3912:                         $user = join(':',split(/[\@]/,$item));
 3913:                     }
 3914:                     if ($user eq $uname.':'.$udom) {
 3915:                         undef($skipuser);
 3916:                         last;
 3917:                     }
 3918:                 }
 3919:             }
 3920:             if ($skipuser) {
 3921:                 return 0;
 3922:             }
 3923:         }
 3924:     }
 3925:     $type ||= 'any';
 3926:     if (!defined($cdom) || !defined($cnum)) {
 3927:         my $cid  = $env{'request.course.id'};
 3928:         $cdom = $env{'course.'.$cid.'.domain'};
 3929:         $cnum = $env{'course.'.$cid.'.num'};
 3930:     }
 3931:     my $typesref;
 3932:     if (($type eq 'any') || ($type eq 'all')) {
 3933:         $typesref = ['active','previous','future'];
 3934:     } elsif ($type eq 'previous' || $type eq 'future') {
 3935:         $typesref = [$type];
 3936:     }
 3937:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3938:                               $typesref,undef,[$cdom]);
 3939:     my ($tmp) = keys(%roles);
 3940:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3941:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3942:     if (@course_roles > 0) {
 3943:         return 1;
 3944:     }
 3945:     return 0;
 3946: }
 3947: 
 3948: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3949: # input: action, courseID, current domain, intended
 3950: #        path to file, source of file, instruction to parse file for objects,
 3951: #        ref to hash for embedded objects,
 3952: #        ref to hash for codebase of java objects.
 3953: #        reference to scalar to accommodate mime type determined
 3954: #          from File::MMagic if $parser = parse.
 3955: #
 3956: # output: url to file (if action was uploaddoc), 
 3957: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3958: #
 3959: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3960: # course.
 3961: #
 3962: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3963: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3964: #          course's home server.
 3965: #
 3966: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3967: #          be copied from $source (current location) to 
 3968: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3969: #         and will then be copied to
 3970: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3971: #         course's home server.
 3972: #
 3973: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3974: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3975: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3976: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3977: #         in course's home server.
 3978: #
 3979: 
 3980: sub process_coursefile {
 3981:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3982:         $mimetype)=@_;
 3983:     my $fetchresult;
 3984:     my $home=&homeserver($docuname,$docudom);
 3985:     if ($action eq 'propagate') {
 3986:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3987: 			     $home);
 3988:     } else {
 3989:         my $fpath = '';
 3990:         my $fname = $file;
 3991:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3992:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3993:         my $filepath = &build_filepath($fpath);
 3994:         if ($action eq 'copy') {
 3995:             if ($source eq '') {
 3996:                 $fetchresult = 'no source file';
 3997:                 return $fetchresult;
 3998:             } else {
 3999:                 my $destination = $filepath.'/'.$fname;
 4000:                 rename($source,$destination);
 4001:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4002:                                  $home);
 4003:             }
 4004:         } elsif ($action eq 'uploaddoc') {
 4005:             open(my $fh,'>',$filepath.'/'.$fname);
 4006:             print $fh $env{'form.'.$source};
 4007:             close($fh);
 4008:             if ($parser eq 'parse') {
 4009:                 my $mm = new File::MMagic;
 4010:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4011:                 if ($type eq 'text/html') {
 4012:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4013:                     unless ($parse_result eq 'ok') {
 4014:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4015:                     }
 4016:                 }
 4017:                 if (ref($mimetype)) {
 4018:                     $$mimetype = $type;
 4019:                 } 
 4020:             }
 4021:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4022:                                  $home);
 4023:             if ($fetchresult eq 'ok') {
 4024:                 return '/uploaded/'.$fpath.'/'.$fname;
 4025:             } else {
 4026:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4027:                         ' to host '.$home.': '.$fetchresult);
 4028:                 return '/adm/notfound.html';
 4029:             }
 4030:         }
 4031:     }
 4032:     unless ( $fetchresult eq 'ok') {
 4033:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4034:              ' to host '.$home.': '.$fetchresult);
 4035:     }
 4036:     return $fetchresult;
 4037: }
 4038: 
 4039: sub build_filepath {
 4040:     my ($fpath) = @_;
 4041:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4042:     unless ($fpath eq '') {
 4043:         my @parts=split('/',$fpath);
 4044:         foreach my $part (@parts) {
 4045:             $filepath.= '/'.$part;
 4046:             if ((-e $filepath)!=1) {
 4047:                 mkdir($filepath,0777);
 4048:             }
 4049:         }
 4050:     }
 4051:     return $filepath;
 4052: }
 4053: 
 4054: sub store_edited_file {
 4055:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4056:     my $file = $primary_url;
 4057:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4058:     my $fpath = '';
 4059:     my $fname = $file;
 4060:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4061:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4062:     my $filepath = &build_filepath($fpath);
 4063:     open(my $fh,'>',$filepath.'/'.$fname);
 4064:     print $fh $content;
 4065:     close($fh);
 4066:     my $home=&homeserver($docuname,$docudom);
 4067:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4068: 			  $home);
 4069:     if ($$fetchresult eq 'ok') {
 4070:         return '/uploaded/'.$fpath.'/'.$fname;
 4071:     } else {
 4072:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4073: 		 ' to host '.$home.': '.$$fetchresult);
 4074:         return '/adm/notfound.html';
 4075:     }
 4076: }
 4077: 
 4078: sub clean_filename {
 4079:     my ($fname,$args)=@_;
 4080: # Replace Windows backslashes by forward slashes
 4081:     $fname=~s/\\/\//g;
 4082:     if (!$args->{'keep_path'}) {
 4083:         # Get rid of everything but the actual filename
 4084: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4085:     }
 4086: # Replace spaces by underscores
 4087:     $fname=~s/\s+/\_/g;
 4088: # Transliterate non-ascii text to ascii
 4089:     my $lang = &Apache::lonlocal::current_language();
 4090:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4091: # Replace all other weird characters by nothing
 4092:     $fname=~s{[^/\w\.\-]}{}g;
 4093: # Replace all .\d. sequences with _\d. so they no longer look like version
 4094: # numbers
 4095:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4096: # Replace three or more adjacent underscores with one for consistency 
 4097: # with loncfile::filename_check() so complete url can be extracted by
 4098: # lonnet::decode_symb()
 4099:     $fname=~s/_{3,}/_/g;
 4100:     return $fname;
 4101: }
 4102: 
 4103: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4104: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4105: # image with the same aspect ratio as the original, but with dimensions which do 
 4106: # not exceed $resizewidth and $resizeheight.
 4107:  
 4108: sub resizeImage {
 4109:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4110:     my $ima = Image::Magick->new;
 4111:     my $resized;
 4112:     if (-e $img_path) {
 4113:         $ima->Read($img_path);
 4114:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4115:             my $width = $ima->Get('width');
 4116:             my $height = $ima->Get('height');
 4117:             if ($width > $resizewidth) {
 4118: 	        my $factor = $width/$resizewidth;
 4119:                 my $newheight = $height/$factor;
 4120:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4121:                 $resized = 1;
 4122:             }
 4123:         }
 4124:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4125:             my $width = $ima->Get('width');
 4126:             my $height = $ima->Get('height');
 4127:             if ($height > $resizeheight) {
 4128:                 my $factor = $height/$resizeheight;
 4129:                 my $newwidth = $width/$factor;
 4130:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4131:                 $resized = 1;
 4132:             }
 4133:         }
 4134:         if ($resized) {
 4135:             $ima->Write($img_path);
 4136:         }
 4137:     }
 4138:     return;
 4139: }
 4140: 
 4141: # --------------- Take an uploaded file and put it into the userfiles directory
 4142: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4143: #                    the desired filename is in $env{"form.$formname.filename"}
 4144: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4145: #                                    canceloverwrite, scantron or ''.
 4146: #                   if 'coursedoc': upload to the current course
 4147: #                   if 'existingfile': write file to tmp/overwrites directory 
 4148: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4149: #                   $context is passed as argument to &finishuserfileupload
 4150: #        $subdir - directory in userfile to store the file into
 4151: #        $parser - instruction to parse file for objects ($parser = parse) or
 4152: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4153: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4154: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4155: #        $allfiles - reference to hash for embedded objects
 4156: #        $codebase - reference to hash for codebase of java objects
 4157: #        $desuname - username for permanent storage of uploaded file
 4158: #        $dsetudom - domain for permanaent storage of uploaded file
 4159: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4160: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4161: #        $resizewidth - width (pixels) to which to resize uploaded image
 4162: #        $resizeheight - height (pixels) to which to resize uploaded image
 4163: #        $mimetype - reference to scalar to accommodate mime type determined
 4164: #                    from File::MMagic.
 4165: # 
 4166: # output: url of file in userspace, or error: <message> 
 4167: #             or /adm/notfound.html if failure to upload occurse
 4168: 
 4169: sub userfileupload {
 4170:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4171:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4172:     if (!defined($subdir)) { $subdir='unknown'; }
 4173:     my $fname=$env{'form.'.$formname.'.filename'};
 4174:     $fname=&clean_filename($fname);
 4175:     # See if there is anything left
 4176:     unless ($fname) { return 'error: no uploaded file'; }
 4177:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4178:     if ($fname =~ /^\./) {
 4179:         my ($s,$usec) = &gettimeofday();
 4180:         while (length($usec) < 6) {
 4181:             $usec = '0'.$usec;
 4182:         }
 4183:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4184:     }
 4185:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4186:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4187:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4188:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4189:         my $now = time;
 4190:         my $filepath;
 4191:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4192:              $filepath = 'tmp/helprequests/'.$now;
 4193:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4194:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4195:                          '_'.$env{'user.domain'}.'/pending';
 4196:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4197:             my ($docuname,$docudom);
 4198:             if ($destudom =~ /^$match_domain$/) {
 4199:                 $docudom = $destudom;
 4200:             } else {
 4201:                 $docudom = $env{'user.domain'};
 4202:             }
 4203:             if ($destuname =~ /^$match_username$/) {
 4204:                 $docuname = $destuname;
 4205:             } else {
 4206:                 $docuname = $env{'user.name'};
 4207:             }
 4208:             if (exists($env{'form.group'})) {
 4209:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4210:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4211:             }
 4212:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4213:             if ($context eq 'canceloverwrite') {
 4214:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4215:                 if (-e  $tempfile) {
 4216:                     my @info = stat($tempfile);
 4217:                     if ($info[9] eq $env{'form.timestamp'}) {
 4218:                         unlink($tempfile);
 4219:                     }
 4220:                 }
 4221:                 return;
 4222:             }
 4223:         }
 4224:         # Create the directory if not present
 4225:         my @parts=split(/\//,$filepath);
 4226:         my $fullpath = $perlvar{'lonDaemons'};
 4227:         for (my $i=0;$i<@parts;$i++) {
 4228:             $fullpath .= '/'.$parts[$i];
 4229:             if ((-e $fullpath)!=1) {
 4230:                 mkdir($fullpath,0777);
 4231:             }
 4232:         }
 4233:         open(my $fh,'>',$fullpath.'/'.$fname);
 4234:         print $fh $env{'form.'.$formname};
 4235:         close($fh);
 4236:         if ($context eq 'existingfile') {
 4237:             my @info = stat($fullpath.'/'.$fname);
 4238:             return ($fullpath.'/'.$fname,$info[9]);
 4239:         } else {
 4240:             return $fullpath.'/'.$fname;
 4241:         }
 4242:     }
 4243:     if ($subdir eq 'scantron') {
 4244:         $fname = 'scantron_orig_'.$fname;
 4245:     } else {
 4246:         $fname="$subdir/$fname";
 4247:     }
 4248:     if ($context eq 'coursedoc') {
 4249: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4250: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4251:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4252:             return &finishuserfileupload($docuname,$docudom,
 4253: 					 $formname,$fname,$parser,$allfiles,
 4254: 					 $codebase,$thumbwidth,$thumbheight,
 4255:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4256:         } else {
 4257:             if ($env{'form.folder'}) {
 4258:                 $fname=$env{'form.folder'}.'/'.$fname;
 4259:             }
 4260:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4261: 				       $fname,$formname,$parser,
 4262: 				       $allfiles,$codebase,$mimetype);
 4263:         }
 4264:     } elsif (defined($destuname)) {
 4265:         my $docuname=$destuname;
 4266:         my $docudom=$destudom;
 4267: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4268: 				     $parser,$allfiles,$codebase,
 4269:                                      $thumbwidth,$thumbheight,
 4270:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4271:     } else {
 4272:         my $docuname=$env{'user.name'};
 4273:         my $docudom=$env{'user.domain'};
 4274:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4275:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4276:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4277:         }
 4278: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4279: 				     $parser,$allfiles,$codebase,
 4280:                                      $thumbwidth,$thumbheight,
 4281:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4282:     }
 4283: }
 4284: 
 4285: sub finishuserfileupload {
 4286:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4287:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4288:     my $path=$docudom.'/'.$docuname.'/';
 4289:     my $filepath=$perlvar{'lonDocRoot'};
 4290:   
 4291:     my ($fnamepath,$file,$fetchthumb);
 4292:     $file=$fname;
 4293:     if ($fname=~m|/|) {
 4294:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4295: 	$path.=$fnamepath.'/';
 4296:     }
 4297:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4298:     my $count;
 4299:     for ($count=4;$count<=$#parts;$count++) {
 4300:         $filepath.="/$parts[$count]";
 4301:         if ((-e $filepath)!=1) {
 4302: 	    mkdir($filepath,0777);
 4303:         }
 4304:     }
 4305: 
 4306: # Save the file
 4307:     {
 4308: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4309: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4310: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4311: 	    return '/adm/notfound.html';
 4312: 	}
 4313:         if ($context eq 'overwrite') {
 4314:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4315:             my $target = $filepath.'/'.$file;
 4316:             if (-e $source) {
 4317:                 my @info = stat($source);
 4318:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4319:                     unless (&File::Copy::move($source,$target)) {
 4320:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4321:                         return "Moving from $source failed";
 4322:                     }
 4323:                 } else {
 4324:                     return "Temporary file: $source had unexpected date/time for last modification";
 4325:                 }
 4326:             } else {
 4327:                 return "Temporary file: $source missing";
 4328:             }
 4329:         } elsif (!print FH ($env{'form.'.$formname})) {
 4330: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4331: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4332: 	    return '/adm/notfound.html';
 4333: 	}
 4334: 	close(FH);
 4335:         if ($resizewidth && $resizeheight) {
 4336:             my $mm = new File::MMagic;
 4337:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4338:             if ($mime_type =~ m{^image/}) {
 4339: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4340:             }  
 4341: 	}
 4342:     }
 4343:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4344:         if (ref($mimetype)) {
 4345:             if ($$mimetype eq '') {
 4346:                 my $mm = new File::MMagic;
 4347:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4348:                 $$mimetype = $type;
 4349:             }
 4350:         }
 4351:     }
 4352:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4353:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4354:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4355:                                                        $allfiles,$codebase);
 4356:             unless ($parse_result eq 'ok') {
 4357:                 &logthis('Failed to parse '.$filepath.$file.
 4358: 	   	         ' for embedded media: '.$parse_result); 
 4359:             }
 4360:         }
 4361:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4362:         my $format = $env{'form.scantron_format'};
 4363:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4364:     }
 4365:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4366:         my $input = $filepath.'/'.$file;
 4367:         my $output = $filepath.'/'.'tn-'.$file;
 4368:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4369:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4370:         system({$args[0]} @args);
 4371:         if (-e $filepath.'/'.'tn-'.$file) {
 4372:             $fetchthumb  = 1; 
 4373:         }
 4374:     }
 4375:  
 4376: # Notify homeserver to grep it
 4377: #
 4378:     my $docuhome=&homeserver($docuname,$docudom);	
 4379:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4380:     if ($fetchresult eq 'ok') {
 4381:         if ($fetchthumb) {
 4382:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4383:             if ($thumbresult ne 'ok') {
 4384:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4385:                          $docuhome.': '.$thumbresult);
 4386:             }
 4387:         }
 4388: #
 4389: # Return the URL to it
 4390:         return '/uploaded/'.$path.$file;
 4391:     } else {
 4392:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4393: 		 ': '.$fetchresult);
 4394:         return '/adm/notfound.html';
 4395:     }
 4396: }
 4397: 
 4398: sub extract_embedded_items {
 4399:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4400:     my @state = ();
 4401:     my (%lastids,%related,%shockwave,%flashvars);
 4402:     my %javafiles = (
 4403:                       codebase => '',
 4404:                       code => '',
 4405:                       archive => ''
 4406:                     );
 4407:     my %mediafiles = (
 4408:                       src => '',
 4409:                       movie => '',
 4410:                      );
 4411:     my $p;
 4412:     if ($content) {
 4413:         $p = HTML::LCParser->new($content);
 4414:     } else {
 4415:         $p = HTML::LCParser->new($fullpath);
 4416:     }
 4417:     while (my $t=$p->get_token()) {
 4418: 	if ($t->[0] eq 'S') {
 4419: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4420: 	    push(@state, $tagname);
 4421:             if (lc($tagname) eq 'allow') {
 4422:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4423:             }
 4424: 	    if (lc($tagname) eq 'img') {
 4425: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4426: 	    }
 4427: 	    if (lc($tagname) eq 'a') {
 4428:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4429:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4430:                 }
 4431: 	    }
 4432:             if (lc($tagname) eq 'script') {
 4433:                 my $src;
 4434:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4435:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4436:                 } else {
 4437:                     if ($attr->{'src'} ne '') {
 4438:                         $src = $attr->{'src'};
 4439:                         &add_filetype($allfiles,$src,'src');
 4440:                     }
 4441:                 }
 4442:                 my $text = $p->get_trimmed_text();
 4443:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4444:                     my @swfargs = split(/,/,$1);
 4445:                     foreach my $item (@swfargs) {
 4446:                         $item =~ s/["']//g;
 4447:                         $item =~ s/^\s+//;
 4448:                         $item =~ s/\s+$//;
 4449:                     }
 4450:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4451:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4452:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4453:                         } else {
 4454:                             $related{$swfargs[0]} = [$swfargs[2]];
 4455:                         }
 4456:                     }
 4457:                 }
 4458:             }
 4459:             if (lc($tagname) eq 'link') {
 4460:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4461:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4462:                 }
 4463:             }
 4464: 	    if (lc($tagname) eq 'object' ||
 4465: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4466: 		foreach my $item (keys(%javafiles)) {
 4467: 		    $javafiles{$item} = '';
 4468: 		}
 4469:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4470:                     $lastids{lc($tagname)} = $attr->{'id'};
 4471:                 }
 4472: 	    }
 4473: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4474: 		my $name = lc($attr->{'name'});
 4475: 		foreach my $item (keys(%javafiles)) {
 4476: 		    if ($name eq $item) {
 4477: 			$javafiles{$item} = $attr->{'value'};
 4478: 			last;
 4479: 		    }
 4480: 		}
 4481:                 my $pathfrom;
 4482: 		foreach my $item (keys(%mediafiles)) {
 4483: 		    if ($name eq $item) {
 4484:                         $pathfrom = $attr->{'value'};
 4485:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4486: 			&add_filetype($allfiles,$pathfrom,$name);
 4487: 			last;
 4488: 		    }
 4489: 		}
 4490:                 if ($name eq 'flashvars') {
 4491:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4492:                 }
 4493:                 if ($pathfrom ne '') {
 4494:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4495:                                          $pathfrom);
 4496:                 }
 4497: 	    }
 4498: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4499: 		foreach my $item (keys(%javafiles)) {
 4500: 		    if ($attr->{$item}) {
 4501: 			$javafiles{$item} = $attr->{$item};
 4502: 			last;
 4503: 		    }
 4504: 		}
 4505: 		foreach my $item (keys(%mediafiles)) {
 4506: 		    if ($attr->{$item}) {
 4507: 			&add_filetype($allfiles,$attr->{$item},$item);
 4508: 			last;
 4509: 		    }
 4510: 		}
 4511:                 if (lc($tagname) eq 'embed') {
 4512:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4513:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4514:                                              $attr->{'src'});
 4515:                     }
 4516:                 }
 4517: 	    }
 4518:             if (lc($tagname) eq 'iframe') {
 4519:                 my $src = $attr->{'src'} ;
 4520:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4521:                     &add_filetype($allfiles,$src,'src');
 4522:                 } elsif ($src =~ m{^/}) {
 4523:                     if ($env{'request.course.id'}) {
 4524:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4525:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4526:                         my $url = &hreflocation('',$fullpath);
 4527:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4528:                             my $relpath = $1;
 4529:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4530:                                 &add_filetype($allfiles,$1,'src');
 4531:                             }
 4532:                         }
 4533:                     }
 4534:                 }
 4535:             }
 4536:             if ($t->[4] =~ m{/>$}) {
 4537:                 pop(@state);
 4538:             }
 4539: 	} elsif ($t->[0] eq 'E') {
 4540: 	    my ($tagname) = ($t->[1]);
 4541: 	    if ($javafiles{'codebase'} ne '') {
 4542: 		$javafiles{'codebase'} .= '/';
 4543: 	    }  
 4544: 	    if (lc($tagname) eq 'applet' ||
 4545: 		lc($tagname) eq 'object' ||
 4546: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4547: 		) {
 4548: 		foreach my $item (keys(%javafiles)) {
 4549: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4550: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4551: 			&add_filetype($allfiles,$file,$item);
 4552: 		    }
 4553: 		}
 4554: 	    } 
 4555: 	    pop @state;
 4556: 	}
 4557:     }
 4558:     foreach my $id (sort(keys(%flashvars))) {
 4559:         if ($shockwave{$id} ne '') {
 4560:             my @pairs = split(/\&/,$flashvars{$id});
 4561:             foreach my $pair (@pairs) {
 4562:                 my ($key,$value) = split(/\=/,$pair);
 4563:                 if ($key eq 'thumb') {
 4564:                     &add_filetype($allfiles,$value,$key);
 4565:                 } elsif ($key eq 'content') {
 4566:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4567:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4568:                     if ($ext ne '') {
 4569:                         &add_filetype($allfiles,$path.$value,$ext);
 4570:                     }
 4571:                 }
 4572:             }
 4573:         }
 4574:     }
 4575:     return 'ok';
 4576: }
 4577: 
 4578: sub add_filetype {
 4579:     my ($allfiles,$file,$type)=@_;
 4580:     if (exists($allfiles->{$file})) {
 4581: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4582: 	    push(@{$allfiles->{$file}}, &escape($type));
 4583: 	}
 4584:     } else {
 4585: 	@{$allfiles->{$file}} = (&escape($type));
 4586:     }
 4587: }
 4588: 
 4589: sub embedded_dependency {
 4590:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4591:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4592:         if (($identifier ne '') &&
 4593:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4594:             ($pathfrom ne '')) {
 4595:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4596:             foreach my $dep (@{$related->{$identifier}}) {
 4597:                 &add_filetype($allfiles,$path.$dep,'object');
 4598:             }
 4599:         }
 4600:     }
 4601:     return;
 4602: }
 4603: 
 4604: sub bubblesheet_converter {
 4605:     my ($cdom,$fullpath,$config,$format) = @_;
 4606:     if ((&domain($cdom) ne '') &&
 4607:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4608:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4609:         my (%csvcols,%csvoptions);
 4610:         if (ref($config->{'fields'}) eq 'HASH') {  
 4611:             %csvcols = %{$config->{'fields'}};
 4612:         }
 4613:         if (ref($config->{'options'}) eq 'HASH') {
 4614:             %csvoptions = %{$config->{'options'}};
 4615:         }
 4616:         my %csvbynum = reverse(%csvcols);
 4617:         my %scantronconf = &get_scantron_config($format,$cdom);
 4618:         if (keys(%scantronconf)) {
 4619:             my %bynum = (
 4620:                           $scantronconf{CODEstart} => 'CODEstart',
 4621:                           $scantronconf{IDstart}   => 'IDstart',
 4622:                           $scantronconf{PaperID}   => 'PaperID',
 4623:                           $scantronconf{FirstName} => 'FirstName',
 4624:                           $scantronconf{LastName}  => 'LastName',
 4625:                           $scantronconf{Qstart}    => 'Qstart',
 4626:                         );
 4627:             my @ordered;
 4628:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4629:                 push(@ordered,$bynum{$item});
 4630:             }
 4631:             my %mapstart = (
 4632:                               CODEstart => 'CODE',
 4633:                               IDstart   => 'ID',
 4634:                               PaperID   => 'PaperID',
 4635:                               FirstName => 'FirstName',
 4636:                               LastName  => 'LastName',
 4637:                               Qstart    => 'FirstQuestion',
 4638:                            );
 4639:             my %maplength = (
 4640:                               CODEstart => 'CODElength',
 4641:                               IDstart   => 'IDlength',
 4642:                               PaperID   => 'PaperIDlength',
 4643:                               FirstName => 'FirstNamelength',
 4644:                               LastName  => 'LastNamelength',
 4645:             );
 4646:             if (open(my $fh,'<',$fullpath)) {
 4647:                 my $output;
 4648:                 my %lettdig = &letter_to_digits();
 4649:                 my %diglett = reverse(%lettdig);
 4650:                 my $numletts = scalar(keys(%lettdig));
 4651:                 my $num = 0;
 4652:                 while (my $line=<$fh>) {
 4653:                     $num ++;
 4654:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4655:                     $line =~ s{[\r\n]+$}{};
 4656:                     my %found;
 4657:                     my @values = split(/,/,$line);
 4658:                     my ($qstart,$record);
 4659:                     for (my $i=0; $i<@values; $i++) {
 4660:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4661:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4662:                             if ($values[$i] eq '') {
 4663:                                 $values[$i] = $scantronconf{'Qoff'};
 4664:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4665:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4666:                                     $values[$i] = $lettdig{uc($values[$i])};
 4667:                                 }
 4668:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4669:                                 if ($values[$i] =~ /^[0-9]$/) {
 4670:                                     $values[$i] = $diglett{$values[$i]};
 4671:                                 }
 4672:                             } else {
 4673:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4674:                                     my $digit;
 4675:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4676:                                         $digit = $lettdig{uc($values[$i])}-1;
 4677:                                         if ($values[$i] eq 'J') {
 4678:                                             $digit += $numletts;
 4679:                                         }
 4680:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4681:                                         $digit = $values[$i]-1;
 4682:                                         if ($values[$i] eq '0') {
 4683:                                             $digit += $numletts;
 4684:                                         }
 4685:                                     }
 4686:                                     my $qval='';
 4687:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4688:                                         if ($j == $digit) {
 4689:                                             $qval .= $scantronconf{'Qon'};
 4690:                                         } else {
 4691:                                             $qval .= $scantronconf{'Qoff'};
 4692:                                         }
 4693:                                     }
 4694:                                     $values[$i] = $qval;
 4695:                                 }
 4696:                             }
 4697:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4698:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4699:                             }
 4700:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4701:                             if ($numblank > 0) {
 4702:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4703:                             }
 4704:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4705:                                 $qstart = $i;
 4706:                                 $found{$csvbynum{$i}} = $values[$i];
 4707:                             } else {
 4708:                                 $found{'FirstQuestion'} .= $values[$i];
 4709:                             }
 4710:                         } elsif (exists($csvbynum{$i})) {
 4711:                             if ($csvoptions{'rem'}) {
 4712:                                 $values[$i] =~ s/^\s+//;
 4713:                             }
 4714:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4715:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4716:                                     $values[$i] = '0'.$values[$i];
 4717:                                 }
 4718:                             }
 4719:                             $found{$csvbynum{$i}} = $values[$i];
 4720:                         }
 4721:                     }
 4722:                     foreach my $item (@ordered) {
 4723:                         my $currlength = 1+length($record);
 4724:                         my $numspaces = $scantronconf{$item} - $currlength;
 4725:                         if ($numspaces > 0) {
 4726:                             $record .= (' ' x $numspaces);
 4727:                         }
 4728:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4729:                             unless ($item eq 'Qstart') {
 4730:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4731:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4732:                                 }
 4733:                             }
 4734:                             $record .= $found{$mapstart{$item}};
 4735:                         }
 4736:                     }
 4737:                     $output .= "$record\n";
 4738:                 }
 4739:                 close($fh);
 4740:                 if ($output) {
 4741:                     if (open(my $fh,'>',$fullpath)) {
 4742:                         print $fh $output;
 4743:                         close($fh);
 4744:                     }
 4745:                 }
 4746:             }
 4747:         }
 4748:         return;
 4749:     }
 4750: }
 4751: 
 4752: sub letter_to_digits {
 4753:     my %lettdig = (
 4754:                     A => 1,
 4755:                     B => 2,
 4756:                     C => 3,
 4757:                     D => 4,
 4758:                     E => 5,
 4759:                     F => 6,
 4760:                     G => 7,
 4761:                     H => 8,
 4762:                     I => 9,
 4763:                     J => 0,
 4764:                   );
 4765:     return %lettdig;
 4766: }
 4767: 
 4768: sub get_scantron_config {
 4769:     my ($which,$cdom) = @_;
 4770:     my @lines = &get_scantronformat_file($cdom);
 4771:     my %config;
 4772:     #FIXME probably should move to XML it has already gotten a bit much now
 4773:     foreach my $line (@lines) {
 4774:         my ($name,$descrip)=split(/:/,$line);
 4775:         if ($name ne $which ) { next; }
 4776:         chomp($line);
 4777:         my @config=split(/:/,$line);
 4778:         $config{'name'}=$config[0];
 4779:         $config{'description'}=$config[1];
 4780:         $config{'CODElocation'}=$config[2];
 4781:         $config{'CODEstart'}=$config[3];
 4782:         $config{'CODElength'}=$config[4];
 4783:         $config{'IDstart'}=$config[5];
 4784:         $config{'IDlength'}=$config[6];
 4785:         $config{'Qstart'}=$config[7];
 4786:         $config{'Qlength'}=$config[8];
 4787:         $config{'Qoff'}=$config[9];
 4788:         $config{'Qon'}=$config[10];
 4789:         $config{'PaperID'}=$config[11];
 4790:         $config{'PaperIDlength'}=$config[12];
 4791:         $config{'FirstName'}=$config[13];
 4792:         $config{'FirstNamelength'}=$config[14];
 4793:         $config{'LastName'}=$config[15];
 4794:         $config{'LastNamelength'}=$config[16];
 4795:         $config{'BubblesPerRow'}=$config[17];
 4796:         last;
 4797:     }
 4798:     return %config;
 4799: }
 4800: 
 4801: sub get_scantronformat_file {
 4802:     my ($cdom) = @_;
 4803:     if ($cdom eq '') {
 4804:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4805:     }
 4806:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4807:     my $gottab = 0;
 4808:     my @lines;
 4809:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4810:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4811:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4812:             if ($formatfile ne '-1') {
 4813:                 @lines = split("\n",$formatfile,-1);
 4814:                 $gottab = 1;
 4815:             }
 4816:         }
 4817:     }
 4818:     if (!$gottab) {
 4819:         my $confname = $cdom.'-domainconfig';
 4820:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4821:         my $formatfile = &getfile($default);
 4822:         if ($formatfile ne '-1') {
 4823:             @lines = split("\n",$formatfile,-1);
 4824:             $gottab = 1;
 4825:         }
 4826:     }
 4827:     if (!$gottab) {
 4828:         my @domains = &current_machine_domains();
 4829:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4830:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4831:                 @lines = <$fh>;
 4832:                 close($fh);
 4833:             }
 4834:         } else {
 4835:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4836:                 @lines = <$fh>;
 4837:                 close($fh);
 4838:             }
 4839:         }
 4840:     }
 4841:     return @lines;
 4842: }
 4843: 
 4844: sub removeuploadedurl {
 4845:     my ($url)=@_;	
 4846:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4847:     return &removeuserfile($uname,$udom,$fname);
 4848: }
 4849: 
 4850: sub removeuserfile {
 4851:     my ($docuname,$docudom,$fname)=@_;
 4852:     my $home=&homeserver($docuname,$docudom);    
 4853:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4854:     if ($result eq 'ok') {	
 4855:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4856:             my $metafile = $fname.'.meta';
 4857:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4858: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4859:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4860:             my $sqlresult = 
 4861:                 &update_portfolio_table($docuname,$docudom,$file,
 4862:                                         'portfolio_metadata',$group,
 4863:                                         'delete');
 4864:         }
 4865:     }
 4866:     return $result;
 4867: }
 4868: 
 4869: sub mkdiruserfile {
 4870:     my ($docuname,$docudom,$dir)=@_;
 4871:     my $home=&homeserver($docuname,$docudom);
 4872:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4873: }
 4874: 
 4875: sub renameuserfile {
 4876:     my ($docuname,$docudom,$old,$new)=@_;
 4877:     my $home=&homeserver($docuname,$docudom);
 4878:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4879:                         &escape("$old").':'.&escape("$new"),$home);
 4880:     if ($result eq 'ok') {
 4881:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4882:             my $oldmeta = $old.'.meta';
 4883:             my $newmeta = $new.'.meta';
 4884:             my $metaresult = 
 4885:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4886: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4887:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4888:             my $sqlresult = 
 4889:                 &update_portfolio_table($docuname,$docudom,$file,
 4890:                                         'portfolio_metadata',$group,
 4891:                                         'delete');
 4892:         }
 4893:     }
 4894:     return $result;
 4895: }
 4896: 
 4897: # ------------------------------------------------------------------------- Log
 4898: 
 4899: sub log {
 4900:     my ($dom,$nam,$hom,$what)=@_;
 4901:     return critical("log:$dom:$nam:$what",$hom);
 4902: }
 4903: 
 4904: # ------------------------------------------------------------------ Course Log
 4905: #
 4906: # This routine flushes several buffers of non-mission-critical nature
 4907: #
 4908: 
 4909: sub flushcourselogs {
 4910:     &logthis('Flushing log buffers');
 4911: #
 4912: # course logs
 4913: # This is a log of all transactions in a course, which can be used
 4914: # for data mining purposes
 4915: #
 4916: # It also collects the courseid database, which lists last transaction
 4917: # times and course titles for all courseids
 4918: #
 4919:     my %courseidbuffer=();
 4920:     foreach my $crsid (keys(%courselogs)) {
 4921:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4922: 		          &escape($courselogs{$crsid}),
 4923: 		          $coursehombuf{$crsid}) eq 'ok') {
 4924: 	    delete $courselogs{$crsid};
 4925:         } else {
 4926:             &logthis('Failed to flush log buffer for '.$crsid);
 4927:             if (length($courselogs{$crsid})>40000) {
 4928:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4929:                         " exceeded maximum size, deleting.</font>");
 4930:                delete $courselogs{$crsid};
 4931:             }
 4932:         }
 4933:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4934:             'description' => $coursedescrbuf{$crsid},
 4935:             'inst_code'    => $courseinstcodebuf{$crsid},
 4936:             'type'        => $coursetypebuf{$crsid},
 4937:             'owner'       => $courseownerbuf{$crsid},
 4938:         };
 4939:     }
 4940: #
 4941: # Write course id database (reverse lookup) to homeserver of courses 
 4942: # Is used in pickcourse
 4943: #
 4944:     foreach my $crs_home (keys(%courseidbuffer)) {
 4945:         my $response = &courseidput(&host_domain($crs_home),
 4946:                                     $courseidbuffer{$crs_home},
 4947:                                     $crs_home,'timeonly');
 4948:     }
 4949: #
 4950: # File accesses
 4951: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4952: #
 4953:     foreach my $entry (keys(%accesshash)) {
 4954:         if ($entry =~ /___count$/) {
 4955:             my ($dom,$name);
 4956:             ($dom,$name,undef)=
 4957: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4958:             if (! defined($dom) || $dom eq '' || 
 4959:                 ! defined($name) || $name eq '') {
 4960:                 my $cid = $env{'request.course.id'};
 4961:                 $dom  = $env{'request.'.$cid.'.domain'};
 4962:                 $name = $env{'request.'.$cid.'.num'};
 4963:             }
 4964:             my $value = $accesshash{$entry};
 4965:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4966:             my %temphash=($url => $value);
 4967:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4968:             if ($result eq 'ok') {
 4969:                 delete $accesshash{$entry};
 4970:             }
 4971:         } else {
 4972:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4973:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4974:             my %temphash=($entry => $accesshash{$entry});
 4975:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4976:                 delete $accesshash{$entry};
 4977:             }
 4978:         }
 4979:     }
 4980: #
 4981: # Roles
 4982: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4983: #
 4984:     foreach my $entry (keys(%userrolehash)) {
 4985:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4986: 	    split(/\:/,$entry);
 4987:         if (&Apache::lonnet::put('nohist_userroles',
 4988:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4989:                 $rudom,$runame) eq 'ok') {
 4990: 	    delete $userrolehash{$entry};
 4991:         }
 4992:     }
 4993: #
 4994: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 4995: #
 4996:     my %domrolebuffer = ();
 4997:     foreach my $entry (keys(%domainrolehash)) {
 4998:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 4999:         if ($domrolebuffer{$rudom}) {
 5000:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5001:                       '='.&escape($domainrolehash{$entry});
 5002:         } else {
 5003:             $domrolebuffer{$rudom}.=&escape($entry).
 5004:                       '='.&escape($domainrolehash{$entry});
 5005:         }
 5006:         delete $domainrolehash{$entry};
 5007:     }
 5008:     foreach my $dom (keys(%domrolebuffer)) {
 5009: 	my %servers;
 5010: 	if (defined(&domain($dom,'primary'))) {
 5011: 	    my $primary=&domain($dom,'primary');
 5012: 	    my $hostname=&hostname($primary);
 5013: 	    $servers{$primary} = $hostname;
 5014: 	} else { 
 5015: 	    %servers = &get_servers($dom,'library');
 5016: 	}
 5017: 	foreach my $tryserver (keys(%servers)) {
 5018: 	    if (&reply('domroleput:'.$dom.':'.
 5019: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5020: 		last;
 5021: 	    } else {  
 5022: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5023: 	    }
 5024:         }
 5025:     }
 5026:     $dumpcount++;
 5027: }
 5028: 
 5029: sub courselog {
 5030:     my $what=shift;
 5031:     $what=time.':'.$what;
 5032:     unless ($env{'request.course.id'}) { return ''; }
 5033:     $coursedombuf{$env{'request.course.id'}}=
 5034:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5035:     $coursenumbuf{$env{'request.course.id'}}=
 5036:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5037:     $coursehombuf{$env{'request.course.id'}}=
 5038:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5039:     $coursedescrbuf{$env{'request.course.id'}}=
 5040:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5041:     $courseinstcodebuf{$env{'request.course.id'}}=
 5042:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5043:     $courseownerbuf{$env{'request.course.id'}}=
 5044:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5045:     $coursetypebuf{$env{'request.course.id'}}=
 5046:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5047:     if (defined $courselogs{$env{'request.course.id'}}) {
 5048: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5049:     } else {
 5050: 	$courselogs{$env{'request.course.id'}}.=$what;
 5051:     }
 5052:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5053: 	&flushcourselogs();
 5054:     }
 5055: }
 5056: 
 5057: sub courseacclog {
 5058:     my $fnsymb=shift;
 5059:     unless ($env{'request.course.id'}) { return ''; }
 5060:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5061:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5062:         $what.=':POST';
 5063:         # FIXME: Probably ought to escape things....
 5064: 	foreach my $key (keys(%env)) {
 5065:             if ($key=~/^form\.(.*)/) {
 5066:                 my $formitem = $1;
 5067:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5068:                     $what.=':'.$formitem.'='.$env{$key};
 5069:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5070:                     if ($formitem eq 'proctorpassword') {
 5071:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5072:                     } else {
 5073:                         $what.=':'.$formitem.'='.$env{$key};
 5074:                     }
 5075:                 }
 5076:             }
 5077:         }
 5078:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5079:         # FIXME: We should not be depending on a form parameter that someone
 5080:         # editing lonsearchcat.pm might change in the future.
 5081:         if ($env{'form.phase'} eq 'course_search') {
 5082:             $what.= ':POST';
 5083:             # FIXME: Probably ought to escape things....
 5084:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5085:                                  'crsdiscuss') {
 5086:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5087:             }
 5088:         }
 5089:     }
 5090:     &courselog($what);
 5091: }
 5092: 
 5093: sub countacc {
 5094:     my $url=&declutter(shift);
 5095:     return if (! defined($url) || $url eq '');
 5096:     unless ($env{'request.course.id'}) { return ''; }
 5097: #
 5098: # Mark that this url was used in this course
 5099: #
 5100:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5101: #
 5102: # Increase the access count for this resource in this child process
 5103: #
 5104:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5105:     $accesshash{$key}++;
 5106: }
 5107: 
 5108: sub linklog {
 5109:     my ($from,$to)=@_;
 5110:     $from=&declutter($from);
 5111:     $to=&declutter($to);
 5112:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5113:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5114: }
 5115: 
 5116: sub statslog {
 5117:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5118:     if ($users<2) { return; }
 5119:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5120:             'course'       => $env{'request.course.id'},
 5121:             'sections'     => '"all"',
 5122:             'num_students' => $users,
 5123:             'part'         => $part,
 5124:             'symb'         => $symb,
 5125:             'mean_tries'   => $av_attempts,
 5126:             'deg_of_diff'  => $degdiff});
 5127:     foreach my $key (keys(%dynstore)) {
 5128:         $accesshash{$key}=$dynstore{$key};
 5129:     }
 5130: }
 5131:   
 5132: sub userrolelog {
 5133:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5134:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5135:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5136:        $userrolehash
 5137:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5138:                     =$tend.':'.$tstart;
 5139:     }
 5140:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5141:        $userrolehash
 5142:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5143:                     =$tend.':'.$tstart;
 5144:     }
 5145:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5146:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5147:        $domainrolehash
 5148:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5149:                     = $tend.':'.$tstart;
 5150:     }
 5151: }
 5152: 
 5153: sub courserolelog {
 5154:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5155:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5156:         my $cdom = $1;
 5157:         my $cnum = $2;
 5158:         my $sec = $3;
 5159:         my $namespace = 'rolelog';
 5160:         my %storehash = (
 5161:                            role    => $trole,
 5162:                            start   => $tstart,
 5163:                            end     => $tend,
 5164:                            selfenroll => $selfenroll,
 5165:                            context    => $context,
 5166:                         );
 5167:         if ($trole eq 'gr') {
 5168:             $namespace = 'groupslog';
 5169:             $storehash{'group'} = $sec;
 5170:         } else {
 5171:             $storehash{'section'} = $sec;
 5172:         }
 5173:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5174:                    $domain,$cnum,$cdom);
 5175:         if (($trole ne 'st') || ($sec ne '')) {
 5176:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5177:         }
 5178:     }
 5179:     return;
 5180: }
 5181: 
 5182: sub domainrolelog {
 5183:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5184:     if ($area =~ m{^/($match_domain)/$}) {
 5185:         my $cdom = $1;
 5186:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 5187:         my $namespace = 'rolelog';
 5188:         my %storehash = (
 5189:                            role    => $trole,
 5190:                            start   => $tstart,
 5191:                            end     => $tend,
 5192:                            context => $context,
 5193:                         );
 5194:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5195:                    $domain,$domconfiguser,$cdom);
 5196:     }
 5197:     return;
 5198: 
 5199: }
 5200: 
 5201: sub coauthorrolelog {
 5202:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5203:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5204:         my $audom = $1;
 5205:         my $auname = $2;
 5206:         my $namespace = 'rolelog';
 5207:         my %storehash = (
 5208:                            role    => $trole,
 5209:                            start   => $tstart,
 5210:                            end     => $tend,
 5211:                            context => $context,
 5212:                         );
 5213:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5214:                    $domain,$auname,$audom);
 5215:     }
 5216:     return;
 5217: }
 5218: 
 5219: sub get_course_adv_roles {
 5220:     my ($cid,$codes) = @_;
 5221:     $cid=$env{'request.course.id'} unless (defined($cid));
 5222:     my %coursehash=&coursedescription($cid);
 5223:     my $crstype = &Apache::loncommon::course_type($cid);
 5224:     my %nothide=();
 5225:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5226:         if ($user !~ /:/) {
 5227: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5228:         } else {
 5229:             $nothide{$user}=1;
 5230:         }
 5231:     }
 5232:     my @possdoms = ($coursehash{'domain'});
 5233:     if ($coursehash{'checkforpriv'}) {
 5234:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5235:     }
 5236:     my %returnhash=();
 5237:     my %dumphash=
 5238:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5239:     my $now=time;
 5240:     my %privileged;
 5241:     foreach my $entry (keys(%dumphash)) {
 5242: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5243:         if (($tstart) && ($tstart<0)) { next; }
 5244:         if (($tend) && ($tend<$now)) { next; }
 5245:         if (($tstart) && ($now<$tstart)) { next; }
 5246:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5247: 	if ($username eq '' || $domain eq '') { next; }
 5248:         if ((&privileged($username,$domain,\@possdoms)) &&
 5249:             (!$nothide{$username.':'.$domain})) { next; }
 5250: 	if ($role eq 'cr') { next; }
 5251:         if ($codes) {
 5252:             if ($section) { $role .= ':'.$section; }
 5253:             if ($returnhash{$role}) {
 5254:                 $returnhash{$role}.=','.$username.':'.$domain;
 5255:             } else {
 5256:                 $returnhash{$role}=$username.':'.$domain;
 5257:             }
 5258:         } else {
 5259:             my $key=&plaintext($role,$crstype);
 5260:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5261:             if ($returnhash{$key}) {
 5262: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5263:             } else {
 5264:                 $returnhash{$key}=$username.':'.$domain;
 5265:             }
 5266:         }
 5267:     }
 5268:     return %returnhash;
 5269: }
 5270: 
 5271: sub get_my_roles {
 5272:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5273:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5274:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5275:     my (%dumphash,%nothide);
 5276:     if ($context eq 'userroles') {
 5277:         %dumphash = &dump('roles',$udom,$uname);
 5278:     } else {
 5279:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5280:         if ($hidepriv) {
 5281:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5282:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5283:                 if ($user !~ /:/) {
 5284:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5285:                 } else {
 5286:                     $nothide{$user} = 1;
 5287:                 }
 5288:             }
 5289:         }
 5290:     }
 5291:     my %returnhash=();
 5292:     my $now=time;
 5293:     my %privileged;
 5294:     foreach my $entry (keys(%dumphash)) {
 5295:         my ($role,$tend,$tstart);
 5296:         if ($context eq 'userroles') {
 5297:             next if ($entry =~ /^rolesdef/);
 5298: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5299:         } else {
 5300:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5301:         }
 5302:         if (($tstart) && ($tstart<0)) { next; }
 5303:         my $status = 'active';
 5304:         if (($tend) && ($tend<=$now)) {
 5305:             $status = 'previous';
 5306:         } 
 5307:         if (($tstart) && ($now<$tstart)) {
 5308:             $status = 'future';
 5309:         }
 5310:         if (ref($types) eq 'ARRAY') {
 5311:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5312:                 next;
 5313:             } 
 5314:         } else {
 5315:             if ($status ne 'active') {
 5316:                 next;
 5317:             }
 5318:         }
 5319:         my ($rolecode,$username,$domain,$section,$area);
 5320:         if ($context eq 'userroles') {
 5321:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5322:             (undef,$domain,$username,$section) = split(/\//,$area);
 5323:         } else {
 5324:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5325:         }
 5326:         if (ref($roledoms) eq 'ARRAY') {
 5327:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5328:                 next;
 5329:             }
 5330:         }
 5331:         if (ref($roles) eq 'ARRAY') {
 5332:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5333:                 if ($role =~ /^cr\//) {
 5334:                     if (!grep(/^cr$/,@{$roles})) {
 5335:                         next;
 5336:                     }
 5337:                 } elsif ($role =~ /^gr\//) {
 5338:                     if (!grep(/^gr$/,@{$roles})) {
 5339:                         next;
 5340:                     }
 5341:                 } else {
 5342:                     next;
 5343:                 }
 5344:             }
 5345:         }
 5346:         if ($hidepriv) {
 5347:             my @privroles = ('dc','su');
 5348:             if ($context eq 'userroles') {
 5349:                 next if (grep(/^\Q$role\E$/,@privroles));
 5350:             } else {
 5351:                 my $possdoms = [$domain];
 5352:                 if (ref($roledoms) eq 'ARRAY') {
 5353:                    push(@{$possdoms},@{$roledoms}); 
 5354:                 }
 5355:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5356:                     if (!$nothide{$username.':'.$domain}) {
 5357:                         next;
 5358:                     }
 5359:                 }
 5360:             }
 5361:         }
 5362:         if ($withsec) {
 5363:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5364:                 $tstart.':'.$tend;
 5365:         } else {
 5366:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5367:         }
 5368:     }
 5369:     return %returnhash;
 5370: }
 5371: 
 5372: sub get_all_adhocroles {
 5373:     my ($dom) = @_;
 5374:     my @roles_by_num = ();
 5375:     my %domdefaults = &get_domain_defaults($dom);
 5376:     my (%description,%access_in_dom,%access_info);
 5377:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5378:         my $count = 0;
 5379:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5380:         my %ordered;
 5381:         foreach my $role (sort(keys(%domcurrent))) {
 5382:             my ($order,$desc,$access_in_dom);
 5383:             if (ref($domcurrent{$role}) eq 'HASH') {
 5384:                 $order = $domcurrent{$role}{'order'};
 5385:                 $desc = $domcurrent{$role}{'desc'};
 5386:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5387:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5388:             }
 5389:             if ($order eq '') {
 5390:                 $order = $count;
 5391:             }
 5392:             $ordered{$order} = $role;
 5393:             if ($desc ne '') {
 5394:                 $description{$role} = $desc;
 5395:             } else {
 5396:                 $description{$role}= $role;
 5397:             }
 5398:             $count++;
 5399:         }
 5400:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5401:             push(@roles_by_num,$ordered{$item});
 5402:         }
 5403:     }
 5404:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5405: }
 5406: 
 5407: sub get_my_adhocroles {
 5408:     my ($cid,$checkreg) = @_;
 5409:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5410:     if ($env{'request.course.id'} eq $cid) {
 5411:         $cdom = $env{'course.'.$cid.'.domain'};
 5412:         $cnum = $env{'course.'.$cid.'.num'};
 5413:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5414:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5415:         $cdom = $1;
 5416:         $cnum = $2;
 5417:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5418:                                      $cdom,$cnum);
 5419:     }
 5420:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5421:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5422:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5423:         if ($rosterhash{$user} ne '') {
 5424:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5425:             return ([],{}) if ($type eq 'auto');
 5426:         }
 5427:     }
 5428:     if (($cdom ne '') && ($cnum ne ''))  {
 5429:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5430:             my $then=$env{'user.login.time'};
 5431:             my $update=$env{'user.update.time'};
 5432:             if (!$update) {
 5433:                 $update = $then;
 5434:             }
 5435:             my @liveroles;
 5436:             foreach my $role ('dh','da') {
 5437:                 if ($env{"user.role.$role./$cdom/"}) {
 5438:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5439:                     my $limit = $update;
 5440:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5441:                         $limit = $then;
 5442:                     }
 5443:                     my $activerole = 1;
 5444:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5445:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5446:                     if ($activerole) {
 5447:                         push(@liveroles,$role);
 5448:                     }
 5449:                 }
 5450:             }
 5451:             if (@liveroles) {
 5452:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5453:                     my ($accessref,$accessinfo,%access_in_dom);
 5454:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5455:                     if (ref($roles_by_num) eq 'ARRAY') {
 5456:                         if (@{$roles_by_num}) {
 5457:                             my %settings;
 5458:                             if ($env{'request.course.id'} eq $cid) {
 5459:                                 foreach my $envkey (keys(%env)) {
 5460:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5461:                                         $settings{$1} = $env{$envkey};
 5462:                                     }
 5463:                                 }
 5464:                             } else {
 5465:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5466:                             }
 5467:                             my %setincrs;
 5468:                             if ($settings{'internal.adhocaccess'}) {
 5469:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5470:                             }
 5471:                             my @statuses;
 5472:                             if ($env{'environment.inststatus'}) {
 5473:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5474:                             }
 5475:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5476:                             if (ref($accessref) eq 'HASH') {
 5477:                                 %access_in_dom = %{$accessref};
 5478:                             }
 5479:                             foreach my $role (@{$roles_by_num}) {
 5480:                                 my ($curraccess,@okstatus,@personnel);
 5481:                                 if ($setincrs{$role}) {
 5482:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5483:                                     if ($curraccess eq 'status') {
 5484:                                         @okstatus = split(/\&/,$rest);
 5485:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5486:                                         @personnel = split(/\&/,$rest);
 5487:                                     }
 5488:                                 } else {
 5489:                                     $curraccess = $access_in_dom{$role};
 5490:                                     if (ref($accessinfo) eq 'HASH') {
 5491:                                         if ($curraccess eq 'status') {
 5492:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5493:                                                 @okstatus = @{$accessinfo->{$role}};
 5494:                                             }
 5495:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5496:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5497:                                                 @personnel = @{$accessinfo->{$role}};
 5498:                                             }
 5499:                                         }
 5500:                                     }
 5501:                                 }
 5502:                                 if ($curraccess eq 'none') {
 5503:                                     next;
 5504:                                 } elsif ($curraccess eq 'all') {
 5505:                                     push(@possroles,$role);
 5506:                                 } elsif ($curraccess eq 'dh') {
 5507:                                     if (grep(/^dh$/,@liveroles)) {
 5508:                                         push(@possroles,$role);
 5509:                                     } else {
 5510:                                         next;
 5511:                                     }
 5512:                                 } elsif ($curraccess eq 'da') {
 5513:                                     if (grep(/^da$/,@liveroles)) {
 5514:                                         push(@possroles,$role);
 5515:                                     } else {
 5516:                                         next;
 5517:                                     }
 5518:                                 } elsif ($curraccess eq 'status') {
 5519:                                     if (@okstatus) {
 5520:                                         if (!@statuses) {
 5521:                                             if (grep(/^default$/,@okstatus)) {
 5522:                                                 push(@possroles,$role);
 5523:                                             }
 5524:                                         } else {
 5525:                                             foreach my $status (@okstatus) {
 5526:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5527:                                                     push(@possroles,$role);
 5528:                                                     last;
 5529:                                                 }
 5530:                                             }
 5531:                                         }
 5532:                                     }
 5533:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5534:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5535:                                         if ($curraccess eq 'exc') {
 5536:                                             push(@possroles,$role);
 5537:                                         }
 5538:                                     } elsif ($curraccess eq 'inc') {
 5539:                                         push(@possroles,$role);
 5540:                                     }
 5541:                                 }
 5542:                             }
 5543:                         }
 5544:                     }
 5545:                 }
 5546:             }
 5547:         }
 5548:     }
 5549:     unless (ref($description) eq 'HASH') {
 5550:         if (ref($roles_by_num) eq 'ARRAY') {
 5551:             my %desc;
 5552:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5553:             $description = \%desc;
 5554:         } else {
 5555:             $description = {};
 5556:         }
 5557:     }
 5558:     return (\@possroles,$description);
 5559: }
 5560: 
 5561: # ----------------------------------------------------- Frontpage Announcements
 5562: #
 5563: #
 5564: 
 5565: sub postannounce {
 5566:     my ($server,$text)=@_;
 5567:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5568:     unless ($text=~/\w/) { $text=''; }
 5569:     return &reply('setannounce:'.&escape($text),$server);
 5570: }
 5571: 
 5572: sub getannounce {
 5573: 
 5574:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5575: 	my $announcement='';
 5576: 	while (my $line = <$fh>) { $announcement .= $line; }
 5577: 	close($fh);
 5578: 	if ($announcement=~/\w/) { 
 5579: 	    return 
 5580:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5581:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5582: 	} else {
 5583: 	    return '';
 5584: 	}
 5585:     } else {
 5586: 	return '';
 5587:     }
 5588: }
 5589: 
 5590: # ---------------------------------------------------------- Course ID routines
 5591: # Deal with domain's nohist_courseid.db files
 5592: #
 5593: 
 5594: sub courseidput {
 5595:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5596:     return unless (ref($storehash) eq 'HASH');
 5597:     my $outcome;
 5598:     if ($caller eq 'timeonly') {
 5599:         my $cids = '';
 5600:         foreach my $item (keys(%$storehash)) {
 5601:             $cids.=&escape($item).'&';
 5602:         }
 5603:         $cids=~s/\&$//;
 5604:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5605:                           $coursehome);       
 5606:     } else {
 5607:         my $items = '';
 5608:         foreach my $item (keys(%$storehash)) {
 5609:             $items.= &escape($item).'='.
 5610:                      &freeze_escape($$storehash{$item}).'&';
 5611:         }
 5612:         $items=~s/\&$//;
 5613:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5614:                           $coursehome);
 5615:     }
 5616:     if ($outcome eq 'unknown_cmd') {
 5617:         my $what;
 5618:         foreach my $cid (keys(%$storehash)) {
 5619:             $what .= &escape($cid).'=';
 5620:             foreach my $item ('description','inst_code','owner','type') {
 5621:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5622:             }
 5623:             $what =~ s/\:$/&/;
 5624:         }
 5625:         $what =~ s/\&$//;  
 5626:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5627:     } else {
 5628:         return $outcome;
 5629:     }
 5630: }
 5631: 
 5632: sub courseiddump {
 5633:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5634:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5635:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5636:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5637:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5638:     my $as_hash = 1;
 5639:     my %returnhash;
 5640:     if (!$domfilter) { $domfilter=''; }
 5641:     my %libserv = &all_library();
 5642:     foreach my $tryserver (keys(%libserv)) {
 5643:         if ( (  $hostidflag == 1 
 5644: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5645: 	     || (!defined($hostidflag)) ) {
 5646: 
 5647: 	    if (($domfilter eq '') ||
 5648: 		(&host_domain($tryserver) eq $domfilter)) {
 5649:                 my $rep;
 5650:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5651:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5652:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5653:                                 &escape($descfilter), &escape($instcodefilter), 
 5654:                                 &escape($ownerfilter), &escape($coursefilter),
 5655:                                 &escape($typefilter), &escape($regexp_ok), 
 5656:                                 $as_hash, &escape($selfenrollonly), 
 5657:                                 &escape($catfilter), $showhidden, $caller, 
 5658:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5659:                                 &escape($createdbefore), &escape($createdafter), 
 5660:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5661:                                 $reqcrsdom,&escape($reqinstcode))));
 5662:                 } else {
 5663:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5664:                              $sincefilter.':'.&escape($descfilter).':'.
 5665:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5666:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5667:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5668:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5669:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5670:                              &escape($cc_clone).':'.$cloneonly.':'.
 5671:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5672:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5673:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5674:                 }
 5675:                      
 5676:                 my @pairs=split(/\&/,$rep);
 5677:                 foreach my $item (@pairs) {
 5678:                     my ($key,$value)=split(/\=/,$item,2);
 5679:                     $key = &unescape($key);
 5680:                     next if ($key =~ /^error: 2 /);
 5681:                     my $result = &thaw_unescape($value);
 5682:                     if (ref($result) eq 'HASH') {
 5683:                         $returnhash{$key}=$result;
 5684:                     } else {
 5685:                         my @responses = split(/:/,$value);
 5686:                         my @items = ('description','inst_code','owner','type');
 5687:                         for (my $i=0; $i<@responses; $i++) {
 5688:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5689:                         }
 5690:                     }
 5691:                 }
 5692:             }
 5693:         }
 5694:     }
 5695:     return %returnhash;
 5696: }
 5697: 
 5698: sub courselastaccess {
 5699:     my ($cdom,$cnum,$hostidref) = @_;
 5700:     my %returnhash;
 5701:     if ($cdom && $cnum) {
 5702:         my $chome = &homeserver($cnum,$cdom);
 5703:         if ($chome ne 'no_host') {
 5704:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5705:             &extract_lastaccess(\%returnhash,$rep);
 5706:         }
 5707:     } else {
 5708:         if (!$cdom) { $cdom=''; }
 5709:         my %libserv = &all_library();
 5710:         foreach my $tryserver (keys(%libserv)) {
 5711:             if (ref($hostidref) eq 'ARRAY') {
 5712:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5713:             } 
 5714:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5715:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5716:                 &extract_lastaccess(\%returnhash,$rep);
 5717:             }
 5718:         }
 5719:     }
 5720:     return %returnhash;
 5721: }
 5722: 
 5723: sub extract_lastaccess {
 5724:     my ($returnhash,$rep) = @_;
 5725:     if (ref($returnhash) eq 'HASH') {
 5726:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5727:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5728:                  $rep eq '') {
 5729:             my @pairs=split(/\&/,$rep);
 5730:             foreach my $item (@pairs) {
 5731:                 my ($key,$value)=split(/\=/,$item,2);
 5732:                 $key = &unescape($key);
 5733:                 next if ($key =~ /^error: 2 /);
 5734:                 $returnhash->{$key} = &thaw_unescape($value);
 5735:             }
 5736:         }
 5737:     }
 5738:     return;
 5739: }
 5740: 
 5741: # ---------------------------------------------------------- DC e-mail
 5742: 
 5743: sub dcmailput {
 5744:     my ($domain,$msgid,$message,$server)=@_;
 5745:     my $status = &Apache::lonnet::critical(
 5746:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5747:        &escape($message),$server);
 5748:     return $status;
 5749: }
 5750: 
 5751: sub dcmaildump {
 5752:     my ($dom,$startdate,$enddate,$senders) = @_;
 5753:     my %returnhash=();
 5754: 
 5755:     if (defined(&domain($dom,'primary'))) {
 5756:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5757:                                                          &escape($enddate).':';
 5758: 	my @esc_senders=map { &escape($_)} @$senders;
 5759: 	$cmd.=&escape(join('&',@esc_senders));
 5760: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5761:             my ($key,$value) = split(/\=/,$line,2);
 5762:             if (($key) && ($value)) {
 5763:                 $returnhash{&unescape($key)} = &unescape($value);
 5764:             }
 5765:         }
 5766:     }
 5767:     return %returnhash;
 5768: }
 5769: # ---------------------------------------------------------- Domain roles
 5770: 
 5771: sub get_domain_roles {
 5772:     my ($dom,$roles,$startdate,$enddate)=@_;
 5773:     if ((!defined($startdate)) || ($startdate eq '')) {
 5774:         $startdate = '.';
 5775:     }
 5776:     if ((!defined($enddate)) || ($enddate eq '')) {
 5777:         $enddate = '.';
 5778:     }
 5779:     my $rolelist;
 5780:     if (ref($roles) eq 'ARRAY') {
 5781:         $rolelist = join('&',@{$roles});
 5782:     }
 5783:     my %personnel = ();
 5784: 
 5785:     my %servers = &get_servers($dom,'library');
 5786:     foreach my $tryserver (keys(%servers)) {
 5787: 	%{$personnel{$tryserver}}=();
 5788: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5789: 					    &escape($startdate).':'.
 5790: 					    &escape($enddate).':'.
 5791: 					    &escape($rolelist), $tryserver))) {
 5792: 	    my ($key,$value) = split(/\=/,$line,2);
 5793: 	    if (($key) && ($value)) {
 5794: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5795: 	    }
 5796: 	}
 5797:     }
 5798:     return %personnel;
 5799: }
 5800: 
 5801: sub get_active_domroles {
 5802:     my ($dom,$roles) = @_;
 5803:     return () unless (ref($roles) eq 'ARRAY');
 5804:     my $now = time;
 5805:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5806:     my %domroles;
 5807:     foreach my $server (keys(%dompersonnel)) {
 5808:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5809:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5810:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5811:         }
 5812:     }
 5813:     return %domroles;
 5814: }
 5815: 
 5816: # ----------------------------------------------------------- Interval timing 
 5817: 
 5818: {
 5819: # Caches needed for speedup of navmaps
 5820: # We don't want to cache this for very long at all (5 seconds at most)
 5821: # 
 5822: # The user for whom we cache
 5823: my $cachedkey='';
 5824: # The cached times for this user
 5825: my %cachedtimes=();
 5826: # When this was last done
 5827: my $cachedtime='';
 5828: 
 5829: sub load_all_first_access {
 5830:     my ($uname,$udom,$ignorecache)=@_;
 5831:     if (($cachedkey eq $uname.':'.$udom) &&
 5832:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5833:         (!$ignorecache)) {
 5834:         return;
 5835:     }
 5836:     $cachedtime=time;
 5837:     $cachedkey=$uname.':'.$udom;
 5838:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5839: }
 5840: 
 5841: sub get_first_access {
 5842:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5843:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5844:     if ($argsymb) { $symb=$argsymb; }
 5845:     my ($map,$id,$res)=&decode_symb($symb);
 5846:     if ($argmap) { $map = $argmap; }
 5847:     if ($type eq 'course') {
 5848: 	$res='course';
 5849:     } elsif ($type eq 'map') {
 5850: 	$res=&symbread($map);
 5851:     } else {
 5852: 	$res=$symb;
 5853:     }
 5854:     &load_all_first_access($uname,$udom,$ignorecache);
 5855:     return $cachedtimes{"$courseid\0$res"};
 5856: }
 5857: 
 5858: sub set_first_access {
 5859:     my ($type,$interval)=@_;
 5860:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5861:     my ($map,$id,$res)=&decode_symb($symb);
 5862:     if ($type eq 'course') {
 5863: 	$res='course';
 5864:     } elsif ($type eq 'map') {
 5865: 	$res=&symbread($map);
 5866:     } else {
 5867: 	$res=$symb;
 5868:     }
 5869:     $cachedkey='';
 5870:     my $firstaccess=&get_first_access($type,$symb,$map);
 5871:     if ($firstaccess) {
 5872:         &logthis("First access time already set ($firstaccess) when attempting ".
 5873:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5874:                  "in $courseid");
 5875:         return 'already_set';
 5876:     } else {
 5877:         my $start = time;
 5878: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5879:                           $udom,$uname);
 5880:         if ($putres eq 'ok') {
 5881:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5882:                  $udom,$uname); 
 5883:             &appenv(
 5884:                      {
 5885:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5886:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5887:                      }
 5888:                   );
 5889:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5890:                 $cachedtimes{"$courseid\0$res"} = $start;
 5891:             }
 5892:         } elsif ($putres ne 'refused') {
 5893:             &logthis("Result: $putres when attempting to set first access time ".
 5894:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5895:         }
 5896:         return $putres;
 5897:     }
 5898:     return 'already_set';
 5899: }
 5900: }
 5901: 
 5902: # --------------------------------------------- Set Expire Date for Spreadsheet
 5903: 
 5904: sub expirespread {
 5905:     my ($uname,$udom,$stype,$usymb)=@_;
 5906:     my $cid=$env{'request.course.id'}; 
 5907:     if ($cid) {
 5908:        my $now=time;
 5909:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5910:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5911:                             $env{'course.'.$cid.'.num'}.
 5912: 	        	    ':nohist_expirationdates:'.
 5913:                             &escape($key).'='.$now,
 5914:                             $env{'course.'.$cid.'.home'})
 5915:     }
 5916:     return 'ok';
 5917: }
 5918: 
 5919: # ----------------------------------------------------- Devalidate Spreadsheets
 5920: 
 5921: sub devalidate {
 5922:     my ($symb,$uname,$udom)=@_;
 5923:     my $cid=$env{'request.course.id'}; 
 5924:     if ($cid) {
 5925:         # delete the stored spreadsheets for
 5926:         # - the student level sheet of this user in course's homespace
 5927:         # - the assessment level sheet for this resource 
 5928:         #   for this user in user's homespace
 5929: 	# - current conditional state info
 5930: 	my $key=$uname.':'.$udom.':';
 5931:         my $status=
 5932: 	    &del('nohist_calculatedsheets',
 5933: 		 [$key.'studentcalc:'],
 5934: 		 $env{'course.'.$cid.'.domain'},
 5935: 		 $env{'course.'.$cid.'.num'})
 5936: 		.' '.
 5937: 	    &del('nohist_calculatedsheets_'.$cid,
 5938: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5939:         unless ($status eq 'ok ok') {
 5940:            &logthis('Could not devalidate spreadsheet '.
 5941:                     $uname.' at '.$udom.' for '.
 5942: 		    $symb.': '.$status);
 5943:         }
 5944: 	&delenv('user.state.'.$cid);
 5945:     }
 5946: }
 5947: 
 5948: sub get_scalar {
 5949:     my ($string,$end) = @_;
 5950:     my $value;
 5951:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5952: 	$value = $1;
 5953:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5954: 	$value = $1;
 5955:     }
 5956:     return &unescape($value);
 5957: }
 5958: 
 5959: sub array2str {
 5960:   my (@array) = @_;
 5961:   my $result=&arrayref2str(\@array);
 5962:   $result=~s/^__ARRAY_REF__//;
 5963:   $result=~s/__END_ARRAY_REF__$//;
 5964:   return $result;
 5965: }
 5966: 
 5967: sub arrayref2str {
 5968:   my ($arrayref) = @_;
 5969:   my $result='__ARRAY_REF__';
 5970:   foreach my $elem (@$arrayref) {
 5971:     if(ref($elem) eq 'ARRAY') {
 5972:       $result.=&arrayref2str($elem).'&';
 5973:     } elsif(ref($elem) eq 'HASH') {
 5974:       $result.=&hashref2str($elem).'&';
 5975:     } elsif(ref($elem)) {
 5976:       #print("Got a ref of ".(ref($elem))." skipping.");
 5977:     } else {
 5978:       $result.=&escape($elem).'&';
 5979:     }
 5980:   }
 5981:   $result=~s/\&$//;
 5982:   $result .= '__END_ARRAY_REF__';
 5983:   return $result;
 5984: }
 5985: 
 5986: sub hash2str {
 5987:   my (%hash) = @_;
 5988:   my $result=&hashref2str(\%hash);
 5989:   $result=~s/^__HASH_REF__//;
 5990:   $result=~s/__END_HASH_REF__$//;
 5991:   return $result;
 5992: }
 5993: 
 5994: sub hashref2str {
 5995:   my ($hashref)=@_;
 5996:   my $result='__HASH_REF__';
 5997:   foreach my $key (sort(keys(%$hashref))) {
 5998:     if (ref($key) eq 'ARRAY') {
 5999:       $result.=&arrayref2str($key).'=';
 6000:     } elsif (ref($key) eq 'HASH') {
 6001:       $result.=&hashref2str($key).'=';
 6002:     } elsif (ref($key)) {
 6003:       $result.='=';
 6004:       #print("Got a ref of ".(ref($key))." skipping.");
 6005:     } else {
 6006: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6007:     }
 6008: 
 6009:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6010:       $result.=&arrayref2str($hashref->{$key}).'&';
 6011:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6012:       $result.=&hashref2str($hashref->{$key}).'&';
 6013:     } elsif(ref($hashref->{$key})) {
 6014:        $result.='&';
 6015:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6016:     } else {
 6017:       $result.=&escape($hashref->{$key}).'&';
 6018:     }
 6019:   }
 6020:   $result=~s/\&$//;
 6021:   $result .= '__END_HASH_REF__';
 6022:   return $result;
 6023: }
 6024: 
 6025: sub str2hash {
 6026:     my ($string)=@_;
 6027:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6028:     return %$hash;
 6029: }
 6030: 
 6031: sub str2hashref {
 6032:   my ($string) = @_;
 6033: 
 6034:   my %hash;
 6035: 
 6036:   if($string !~ /^__HASH_REF__/) {
 6037:       if (! ($string eq '' || !defined($string))) {
 6038: 	  $hash{'error'}='Not hash reference';
 6039:       }
 6040:       return (\%hash, $string);
 6041:   }
 6042: 
 6043:   $string =~ s/^__HASH_REF__//;
 6044: 
 6045:   while($string !~ /^__END_HASH_REF__/) {
 6046:       #key
 6047:       my $key='';
 6048:       if($string =~ /^__HASH_REF__/) {
 6049:           ($key, $string)=&str2hashref($string);
 6050:           if(defined($key->{'error'})) {
 6051:               $hash{'error'}='Bad data';
 6052:               return (\%hash, $string);
 6053:           }
 6054:       } elsif($string =~ /^__ARRAY_REF__/) {
 6055:           ($key, $string)=&str2arrayref($string);
 6056:           if($key->[0] eq 'Array reference error') {
 6057:               $hash{'error'}='Bad data';
 6058:               return (\%hash, $string);
 6059:           }
 6060:       } else {
 6061:           $string =~ s/^(.*?)=//;
 6062: 	  $key=&unescape($1);
 6063:       }
 6064:       $string =~ s/^=//;
 6065: 
 6066:       #value
 6067:       my $value='';
 6068:       if($string =~ /^__HASH_REF__/) {
 6069:           ($value, $string)=&str2hashref($string);
 6070:           if(defined($value->{'error'})) {
 6071:               $hash{'error'}='Bad data';
 6072:               return (\%hash, $string);
 6073:           }
 6074:       } elsif($string =~ /^__ARRAY_REF__/) {
 6075:           ($value, $string)=&str2arrayref($string);
 6076:           if($value->[0] eq 'Array reference error') {
 6077:               $hash{'error'}='Bad data';
 6078:               return (\%hash, $string);
 6079:           }
 6080:       } else {
 6081: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6082:       }
 6083:       $string =~ s/^&//;
 6084: 
 6085:       $hash{$key}=$value;
 6086:   }
 6087: 
 6088:   $string =~ s/^__END_HASH_REF__//;
 6089: 
 6090:   return (\%hash, $string);
 6091: }
 6092: 
 6093: sub str2array {
 6094:     my ($string)=@_;
 6095:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6096:     return @$array;
 6097: }
 6098: 
 6099: sub str2arrayref {
 6100:   my ($string) = @_;
 6101:   my @array;
 6102: 
 6103:   if($string !~ /^__ARRAY_REF__/) {
 6104:       if (! ($string eq '' || !defined($string))) {
 6105: 	  $array[0]='Array reference error';
 6106:       }
 6107:       return (\@array, $string);
 6108:   }
 6109: 
 6110:   $string =~ s/^__ARRAY_REF__//;
 6111: 
 6112:   while($string !~ /^__END_ARRAY_REF__/) {
 6113:       my $value='';
 6114:       if($string =~ /^__HASH_REF__/) {
 6115:           ($value, $string)=&str2hashref($string);
 6116:           if(defined($value->{'error'})) {
 6117:               $array[0] ='Array reference error';
 6118:               return (\@array, $string);
 6119:           }
 6120:       } elsif($string =~ /^__ARRAY_REF__/) {
 6121:           ($value, $string)=&str2arrayref($string);
 6122:           if($value->[0] eq 'Array reference error') {
 6123:               $array[0] ='Array reference error';
 6124:               return (\@array, $string);
 6125:           }
 6126:       } else {
 6127: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6128:       }
 6129:       $string =~ s/^&//;
 6130: 
 6131:       push(@array, $value);
 6132:   }
 6133: 
 6134:   $string =~ s/^__END_ARRAY_REF__//;
 6135: 
 6136:   return (\@array, $string);
 6137: }
 6138: 
 6139: # -------------------------------------------------------------------Temp Store
 6140: 
 6141: sub tmpreset {
 6142:   my ($symb,$namespace,$domain,$stuname) = @_;
 6143:   if (!$symb) {
 6144:     $symb=&symbread();
 6145:     if (!$symb) { $symb= $env{'request.url'}; }
 6146:   }
 6147:   $symb=escape($symb);
 6148: 
 6149:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6150:   $namespace=~s/\//\_/g;
 6151:   $namespace=~s/\W//g;
 6152: 
 6153:   if (!$domain) { $domain=$env{'user.domain'}; }
 6154:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6155:   if ($domain eq 'public' && $stuname eq 'public') {
 6156:       $stuname=&get_requestor_ip();
 6157:   }
 6158:   my $path=LONCAPA::tempdir();
 6159:   my %hash;
 6160:   if (tie(%hash,'GDBM_File',
 6161: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6162: 	  &GDBM_WRCREAT(),0640)) {
 6163:     foreach my $key (keys(%hash)) {
 6164:       if ($key=~ /:$symb/) {
 6165: 	delete($hash{$key});
 6166:       }
 6167:     }
 6168:   }
 6169: }
 6170: 
 6171: sub tmpstore {
 6172:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6173: 
 6174:   if (!$symb) {
 6175:     $symb=&symbread();
 6176:     if (!$symb) { $symb= $env{'request.url'}; }
 6177:   }
 6178:   $symb=escape($symb);
 6179: 
 6180:   if (!$namespace) {
 6181:     # I don't think we would ever want to store this for a course.
 6182:     # it seems this will only be used if we don't have a course.
 6183:     #$namespace=$env{'request.course.id'};
 6184:     #if (!$namespace) {
 6185:       $namespace=$env{'request.state'};
 6186:     #}
 6187:   }
 6188:   $namespace=~s/\//\_/g;
 6189:   $namespace=~s/\W//g;
 6190:   if (!$domain) { $domain=$env{'user.domain'}; }
 6191:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6192:   if ($domain eq 'public' && $stuname eq 'public') {
 6193:       $stuname=&get_requestor_ip();
 6194:   }
 6195:   my $now=time;
 6196:   my %hash;
 6197:   my $path=LONCAPA::tempdir();
 6198:   if (tie(%hash,'GDBM_File',
 6199: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6200: 	  &GDBM_WRCREAT(),0640)) {
 6201:     $hash{"version:$symb"}++;
 6202:     my $version=$hash{"version:$symb"};
 6203:     my $allkeys=''; 
 6204:     foreach my $key (keys(%$storehash)) {
 6205:       $allkeys.=$key.':';
 6206:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6207:     }
 6208:     $hash{"$version:$symb:timestamp"}=$now;
 6209:     $allkeys.='timestamp';
 6210:     $hash{"$version:keys:$symb"}=$allkeys;
 6211:     if (untie(%hash)) {
 6212:       return 'ok';
 6213:     } else {
 6214:       return "error:$!";
 6215:     }
 6216:   } else {
 6217:     return "error:$!";
 6218:   }
 6219: }
 6220: 
 6221: # -----------------------------------------------------------------Temp Restore
 6222: 
 6223: sub tmprestore {
 6224:   my ($symb,$namespace,$domain,$stuname) = @_;
 6225: 
 6226:   if (!$symb) {
 6227:     $symb=&symbread();
 6228:     if (!$symb) { $symb= $env{'request.url'}; }
 6229:   }
 6230:   $symb=escape($symb);
 6231: 
 6232:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6233: 
 6234:   if (!$domain) { $domain=$env{'user.domain'}; }
 6235:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6236:   if ($domain eq 'public' && $stuname eq 'public') {
 6237:       $stuname=&get_requestor_ip();
 6238:   }
 6239:   my %returnhash;
 6240:   $namespace=~s/\//\_/g;
 6241:   $namespace=~s/\W//g;
 6242:   my %hash;
 6243:   my $path=LONCAPA::tempdir();
 6244:   if (tie(%hash,'GDBM_File',
 6245: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6246: 	  &GDBM_READER(),0640)) {
 6247:     my $version=$hash{"version:$symb"};
 6248:     $returnhash{'version'}=$version;
 6249:     my $scope;
 6250:     for ($scope=1;$scope<=$version;$scope++) {
 6251:       my $vkeys=$hash{"$scope:keys:$symb"};
 6252:       my @keys=split(/:/,$vkeys);
 6253:       my $key;
 6254:       $returnhash{"$scope:keys"}=$vkeys;
 6255:       foreach $key (@keys) {
 6256: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6257: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6258:       }
 6259:     }
 6260:     if (!(untie(%hash))) {
 6261:       return "error:$!";
 6262:     }
 6263:   } else {
 6264:     return "error:$!";
 6265:   }
 6266:   return %returnhash;
 6267: }
 6268: 
 6269: # ----------------------------------------------------------------------- Store
 6270: 
 6271: sub store {
 6272:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6273:     my $home='';
 6274: 
 6275:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6276: 
 6277:     $symb=&symbclean($symb);
 6278:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6279: 
 6280:     if (!$domain) { $domain=$env{'user.domain'}; }
 6281:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6282: 
 6283:     &devalidate($symb,$stuname,$domain);
 6284: 
 6285:     $symb=escape($symb);
 6286:     if (!$namespace) { 
 6287:        unless ($namespace=$env{'request.course.id'}) { 
 6288:           return ''; 
 6289:        } 
 6290:     }
 6291:     if (!$home) { $home=$env{'user.home'}; }
 6292: 
 6293:     $$storehash{'ip'}=&get_requestor_ip();
 6294:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6295: 
 6296:     my $namevalue='';
 6297:     foreach my $key (keys(%$storehash)) {
 6298:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6299:     }
 6300:     $namevalue=~s/\&$//;
 6301:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6302:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6303: }
 6304: 
 6305: # -------------------------------------------------------------- Critical Store
 6306: 
 6307: sub cstore {
 6308:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6309:     my $home='';
 6310: 
 6311:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6312: 
 6313:     $symb=&symbclean($symb);
 6314:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6315: 
 6316:     if (!$domain) { $domain=$env{'user.domain'}; }
 6317:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6318: 
 6319:     &devalidate($symb,$stuname,$domain);
 6320: 
 6321:     $symb=escape($symb);
 6322:     if (!$namespace) { 
 6323:        unless ($namespace=$env{'request.course.id'}) { 
 6324:           return ''; 
 6325:        } 
 6326:     }
 6327:     if (!$home) { $home=$env{'user.home'}; }
 6328: 
 6329:     $$storehash{'ip'}=&get_requestor_ip();
 6330:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6331: 
 6332:     my $namevalue='';
 6333:     foreach my $key (keys(%$storehash)) {
 6334:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6335:     }
 6336:     $namevalue=~s/\&$//;
 6337:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6338:     return critical
 6339:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6340: }
 6341: 
 6342: # --------------------------------------------------------------------- Restore
 6343: 
 6344: sub restore {
 6345:     my ($symb,$namespace,$domain,$stuname) = @_;
 6346:     my $home='';
 6347: 
 6348:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6349: 
 6350:     if (!$symb) {
 6351:         return if ($namespace eq 'courserequests');
 6352:         unless ($symb=escape(&symbread())) { return ''; }
 6353:     } else {
 6354:         unless ($namespace eq 'courserequests') {
 6355:             $symb=&escape(&symbclean($symb));
 6356:         }
 6357:     }
 6358:     if (!$namespace) { 
 6359:        unless ($namespace=$env{'request.course.id'}) { 
 6360:           return ''; 
 6361:        } 
 6362:     }
 6363:     if (!$domain) { $domain=$env{'user.domain'}; }
 6364:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6365:     if (!$home) { $home=$env{'user.home'}; }
 6366:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6367: 
 6368:     my %returnhash=();
 6369:     foreach my $line (split(/\&/,$answer)) {
 6370: 	my ($name,$value)=split(/\=/,$line);
 6371:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6372:     }
 6373:     my $version;
 6374:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6375:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6376:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6377:        }
 6378:     }
 6379:     return %returnhash;
 6380: }
 6381: 
 6382: # ---------------------------------------------------------- Course Description
 6383: #
 6384: #  
 6385: 
 6386: sub coursedescription {
 6387:     my ($courseid,$args)=@_;
 6388:     $courseid=~s/^\///;
 6389:     $courseid=~s/\_/\//g;
 6390:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6391:     my $chome=&homeserver($cnum,$cdomain);
 6392:     my $normalid=$cdomain.'_'.$cnum;
 6393:     # need to always cache even if we get errors otherwise we keep 
 6394:     # trying and trying and trying to get the course description.
 6395:     my %envhash=();
 6396:     my %returnhash=();
 6397:     
 6398:     my $expiretime=600;
 6399:     if ($env{'request.course.id'} eq $normalid) {
 6400: 	$expiretime=120;
 6401:     }
 6402: 
 6403:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6404:     if (!$args->{'freshen_cache'}
 6405: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6406: 	foreach my $key (keys(%env)) {
 6407: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6408: 	    my ($setting) = $1;
 6409: 	    $returnhash{$setting} = $env{$key};
 6410: 	}
 6411: 	return %returnhash;
 6412:     }
 6413: 
 6414:     # get the data again
 6415: 
 6416:     if (!$args->{'one_time'}) {
 6417: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6418:     }
 6419: 
 6420:     if ($chome ne 'no_host') {
 6421:        %returnhash=&dump('environment',$cdomain,$cnum);
 6422:        if (!exists($returnhash{'con_lost'})) {
 6423: 	   my $username = $env{'user.name'}; # Defult username
 6424: 	   if(defined $args->{'user'}) {
 6425: 	       $username = $args->{'user'};
 6426: 	   }
 6427:            $returnhash{'home'}= $chome;
 6428: 	   $returnhash{'domain'} = $cdomain;
 6429: 	   $returnhash{'num'} = $cnum;
 6430:            if (!defined($returnhash{'type'})) {
 6431:                $returnhash{'type'} = 'Course';
 6432:            }
 6433:            while (my ($name,$value) = each %returnhash) {
 6434:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6435:            }
 6436:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6437:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6438: 	       $username.'_'.$cdomain.'_'.$cnum;
 6439:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6440:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6441:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6442:        }
 6443:     }
 6444:     if (!$args->{'one_time'}) {
 6445: 	&appenv(\%envhash);
 6446:     }
 6447:     return %returnhash;
 6448: }
 6449: 
 6450: sub update_released_required {
 6451:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6452:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6453:         $cid = $env{'request.course.id'};
 6454:         $cdom = $env{'course.'.$cid.'.domain'};
 6455:         $cnum = $env{'course.'.$cid.'.num'};
 6456:         $chome = $env{'course.'.$cid.'.home'};
 6457:     }
 6458:     if ($needsrelease) {
 6459:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6460:         my $needsupdate;
 6461:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6462:             $needsupdate = 1;
 6463:         } else {
 6464:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6465:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6466:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6467:                 $needsupdate = 1;
 6468:             }
 6469:         }
 6470:         if ($needsupdate) {
 6471:             my %needshash = (
 6472:                              'internal.releaserequired' => $needsrelease,
 6473:                             );
 6474:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6475:             if ($putresult eq 'ok') {
 6476:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6477:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6478:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6479:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6480:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6481:                 }
 6482:             }
 6483:         }
 6484:     }
 6485:     return;
 6486: }
 6487: 
 6488: # -------------------------------------------------See if a user is privileged
 6489: 
 6490: sub privileged {
 6491:     my ($username,$domain,$possdomains,$possroles)=@_;
 6492:     my $now = time;
 6493:     my $roles;
 6494:     if (ref($possroles) eq 'ARRAY') {
 6495:         $roles = $possroles; 
 6496:     } else {
 6497:         $roles = ['dc','su'];
 6498:     }
 6499:     if (ref($possdomains) eq 'ARRAY') {
 6500:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6501:         foreach my $dom (@{$possdomains}) {
 6502:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6503:                 (ref($privileged{$dom}) eq 'HASH')) {
 6504:                 foreach my $role (@{$roles}) {
 6505:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6506:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6507:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6508:                             return 1 unless (($end && $end < $now) ||
 6509:                                              ($start && $start > $now));
 6510:                         }
 6511:                     }
 6512:                 }
 6513:             }
 6514:         }
 6515:     } else {
 6516:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6517:         my $now = time;
 6518: 
 6519:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6520:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6521:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6522:                 return 1 unless ($tend && $tend < $now) 
 6523:                         or ($tstart && $tstart > $now);
 6524:             }
 6525:         }
 6526:     }
 6527:     return 0;
 6528: }
 6529: 
 6530: sub privileged_by_domain {
 6531:     my ($domains,$roles) = @_;
 6532:     my %privileged = ();
 6533:     my $cachetime = 60*60*24;
 6534:     my $now = time;
 6535:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6536:         return %privileged;
 6537:     }
 6538:     foreach my $dom (@{$domains}) {
 6539:         next if (ref($privileged{$dom}) eq 'HASH');
 6540:         my $needroles;
 6541:         foreach my $role (@{$roles}) {
 6542:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6543:             if (defined($cached)) {
 6544:                 if (ref($result) eq 'HASH') {
 6545:                     $privileged{$dom}{$role} = $result;
 6546:                 }
 6547:             } else {
 6548:                 $needroles = 1;
 6549:             }
 6550:         }
 6551:         if ($needroles) {
 6552:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6553:             $privileged{$dom} = {};
 6554:             foreach my $server (keys(%dompersonnel)) {
 6555:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6556:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6557:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6558:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6559:                         next if ($end && $end < $now);
 6560:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6561:                             $dompersonnel{$server}{$item};
 6562:                     }
 6563:                 }
 6564:             }
 6565:             if (ref($privileged{$dom}) eq 'HASH') {
 6566:                 foreach my $role (@{$roles}) {
 6567:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6568:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6569:                     } else {
 6570:                         my %hash = ();
 6571:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6572:                     }
 6573:                 }
 6574:             }
 6575:         }
 6576:     }
 6577:     return %privileged;
 6578: }
 6579: 
 6580: # -------------------------------------------------------- Get user privileges
 6581: 
 6582: sub rolesinit {
 6583:     my ($domain, $username) = @_;
 6584:     my %userroles = ('user.login.time' => time);
 6585:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6586: 
 6587:     # firstaccess and timerinterval are related to timed maps/resources. 
 6588:     # also, blocking can be triggered by an activating timer
 6589:     # it's saved in the user's %env.
 6590:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6591:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6592:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6593:         %timerintchk, %timerintenv);
 6594: 
 6595:     foreach my $key (keys(%firstaccess)) {
 6596:         my ($cid, $rest) = split(/\0/, $key);
 6597:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6598:     }
 6599: 
 6600:     foreach my $key (keys(%timerinterval)) {
 6601:         my ($cid,$rest) = split(/\0/,$key);
 6602:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6603:     }
 6604: 
 6605:     my %allroles=();
 6606:     my %allgroups=();
 6607: 
 6608:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6609:         my $role = $rolesdump{$area};
 6610:         $area =~ s/\_\w\w$//;
 6611: 
 6612:         my ($trole, $tend, $tstart, $group_privs);
 6613: 
 6614:         if ($role =~ /^cr/) {
 6615:         # Custom role, defined by a user 
 6616:         # e.g., user.role.cr/msu/smith/mynewrole
 6617:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6618:                 $trole = $1;
 6619:                 ($tend, $tstart) = split('_', $2);
 6620:             } else {
 6621:                 $trole = $role;
 6622:             }
 6623:         } elsif ($role =~ m|^gr/|) {
 6624:         # Role of member in a group, defined within a course/community
 6625:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6626:             ($trole, $tend, $tstart) = split(/_/, $role);
 6627:             next if $tstart eq '-1';
 6628:             ($trole, $group_privs) = split(/\//, $trole);
 6629:             $group_privs = &unescape($group_privs);
 6630:         } else {
 6631:         # Just a normal role, defined in roles.tab
 6632:             ($trole, $tend, $tstart) = split(/_/,$role);
 6633:         }
 6634: 
 6635:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6636:                  $username);
 6637:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6638: 
 6639:         # role expired or not available yet?
 6640:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6641:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6642: 
 6643:         next if $area eq '' or $trole eq '';
 6644: 
 6645:         my $spec = "$trole.$area";
 6646:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6647: 
 6648:         if ($trole =~ /^cr\//) {
 6649:         # Custom role, defined by a user
 6650:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6651:         } elsif ($trole eq 'gr') {
 6652:         # Role of a member in a group, defined within a course/community
 6653:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6654:             next;
 6655:         } else {
 6656:         # Normal role, defined in roles.tab
 6657:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6658:         }
 6659: 
 6660:         my $cid = $tdomain.'_'.$trest;
 6661:         unless ($firstaccchk{$cid}) {
 6662:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6663:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6664:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6665:                         $coursetimerstarts{$cid}{$item}; 
 6666:                 }
 6667:             }
 6668:             $firstaccchk{$cid} = 1;
 6669:         }
 6670:         unless ($timerintchk{$cid}) {
 6671:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6672:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6673:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6674:                        $coursetimerintervals{$cid}{$item};
 6675:                 }
 6676:             }
 6677:             $timerintchk{$cid} = 1;
 6678:         }
 6679:     }
 6680: 
 6681:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6682:                                                           \%allroles, \%allgroups);
 6683:     $env{'user.adv'} = $userroles{'user.adv'};
 6684:     $env{'user.rar'} = $userroles{'user.rar'};
 6685: 
 6686:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6687: }
 6688: 
 6689: sub set_arearole {
 6690:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6691:     unless ($nolog) {
 6692: # log the associated role with the area
 6693:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6694:     }
 6695:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6696: }
 6697: 
 6698: sub custom_roleprivs {
 6699:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6700:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6701:     my $homsvr = &homeserver($rauthor,$rdomain);
 6702:     if (&hostname($homsvr) ne '') {
 6703:         my ($rdummy,$roledef)=
 6704:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6705:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6706:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6707:             if (defined($syspriv)) {
 6708:                 if ($trest =~ /^$match_community$/) {
 6709:                     $syspriv =~ s/bre\&S//; 
 6710:                 }
 6711:                 $$allroles{'cm./'}.=':'.$syspriv;
 6712:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6713:             }
 6714:             if ($tdomain ne '') {
 6715:                 if (defined($dompriv)) {
 6716:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6717:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6718:                 }
 6719:                 if (($trest ne '') && (defined($coursepriv))) {
 6720:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6721:                         my $rolename = $1;
 6722:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6723:                     }
 6724:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6725:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6726:                 }
 6727:             }
 6728:         }
 6729:     }
 6730: }
 6731: 
 6732: sub course_adhocrole_privs {
 6733:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6734:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6735:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6736:         my (%currprivs,%storeprivs);
 6737:         foreach my $item (split(/:/,$coursepriv)) {
 6738:             my ($priv,$restrict) = split(/\&/,$item);
 6739:             $currprivs{$priv} = $restrict;
 6740:         }
 6741:         my (%possadd,%possremove,%full);
 6742:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6743:             my ($priv,$restrict)=split(/\&/,$item);
 6744:             $full{$priv} = $restrict;
 6745:         }
 6746:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6747:              next if ($item eq '');
 6748:              my ($rule,$rest) = split(/=/,$item);
 6749:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6750:              foreach my $priv (split(/:/,$rest)) {
 6751:                  if ($priv ne '') {
 6752:                      if ($rule eq 'off') {
 6753:                          $possremove{$priv} = 1;
 6754:                      } else {
 6755:                          $possadd{$priv} = 1;
 6756:                      }
 6757:                  }
 6758:              }
 6759:          }
 6760:          foreach my $priv (sort(keys(%full))) {
 6761:              if (exists($currprivs{$priv})) {
 6762:                  unless (exists($possremove{$priv})) {
 6763:                      $storeprivs{$priv} = $currprivs{$priv};
 6764:                  }
 6765:              } elsif (exists($possadd{$priv})) {
 6766:                  $storeprivs{$priv} = $full{$priv};
 6767:              }
 6768:          }
 6769:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6770:      }
 6771:      return $coursepriv;
 6772: }
 6773: 
 6774: sub group_roleprivs {
 6775:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6776:     my $access = 1;
 6777:     my $now = time;
 6778:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6779:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6780:     if ($access) {
 6781:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6782:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6783:     }
 6784: }
 6785: 
 6786: sub standard_roleprivs {
 6787:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6788:     if (defined($pr{$trole.':s'})) {
 6789:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6790:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6791:     }
 6792:     if ($tdomain ne '') {
 6793:         if (defined($pr{$trole.':d'})) {
 6794:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6795:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6796:         }
 6797:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6798:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6799:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6800:         }
 6801:     }
 6802: }
 6803: 
 6804: sub set_userprivs {
 6805:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6806:     my $author=0;
 6807:     my $adv=0;
 6808:     my $rar=0;
 6809:     my %grouproles = ();
 6810:     if (keys(%{$allgroups}) > 0) {
 6811:         my @groupkeys; 
 6812:         foreach my $role (keys(%{$allroles})) {
 6813:             push(@groupkeys,$role);
 6814:         }
 6815:         if (ref($groups_roles) eq 'HASH') {
 6816:             foreach my $key (keys(%{$groups_roles})) {
 6817:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6818:                     push(@groupkeys,$key);
 6819:                 }
 6820:             }
 6821:         }
 6822:         if (@groupkeys > 0) {
 6823:             foreach my $role (@groupkeys) {
 6824:                 my ($trole,$area,$sec,$extendedarea);
 6825:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6826:                     $trole = $1;
 6827:                     $area = $2;
 6828:                     $sec = $3;
 6829:                     $extendedarea = $area.$sec;
 6830:                     if (exists($$allgroups{$area})) {
 6831:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6832:                             my $spec = $trole.'.'.$extendedarea;
 6833:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6834:                                                 $$allgroups{$area}{$group};
 6835:                         }
 6836:                     }
 6837:                 }
 6838:             }
 6839:         }
 6840:     }
 6841:     foreach my $group (keys(%grouproles)) {
 6842:         $$allroles{$group} = $grouproles{$group};
 6843:     }
 6844:     foreach my $role (keys(%{$allroles})) {
 6845:         my %thesepriv;
 6846:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6847:         foreach my $item (split(/:/,$$allroles{$role})) {
 6848:             if ($item ne '') {
 6849:                 my ($privilege,$restrictions)=split(/&/,$item);
 6850:                 if ($restrictions eq '') {
 6851:                     $thesepriv{$privilege}='F';
 6852:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6853:                     $thesepriv{$privilege}.=$restrictions;
 6854:                 }
 6855:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6856:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6857:             }
 6858:         }
 6859:         my $thesestr='';
 6860:         foreach my $priv (sort(keys(%thesepriv))) {
 6861: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6862: 	}
 6863:         $userroles->{'user.priv.'.$role} = $thesestr;
 6864:     }
 6865:     return ($author,$adv,$rar);
 6866: }
 6867: 
 6868: sub role_status {
 6869:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6870:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6871:         my ($one,$two) = split(m{\./},$rolekey,2);
 6872:         (undef,undef,$$role) = split(/\./,$one,3);
 6873:         unless (!defined($$role) || $$role eq '') {
 6874:             $$where = '/'.$two;
 6875:             $$trolecode=$$role.'.'.$$where;
 6876:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6877:             $$tstatus='is';
 6878:             if ($$tstart && $$tstart>$update) {
 6879:                 $$tstatus='future';
 6880:                 if ($$tstart<$now) {
 6881:                     if ($$tstart && $$tstart>$refresh) {
 6882:                         if (($$where ne '') && ($$role ne '')) {
 6883:                             my (%allroles,%allgroups,$group_privs,
 6884:                                 %groups_roles,@rolecodes);
 6885:                             my %userroles = (
 6886:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6887:                             );
 6888:                             @rolecodes = ('cm'); 
 6889:                             my $spec=$$role.'.'.$$where;
 6890:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6891:                             if ($$role =~ /^cr\//) {
 6892:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6893:                                 push(@rolecodes,'cr');
 6894:                             } elsif ($$role eq 'gr') {
 6895:                                 push(@rolecodes,$$role);
 6896:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6897:                                                     $env{'user.name'});
 6898:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6899:                                 (undef,my $group_privs) = split(/\//,$trole);
 6900:                                 $group_privs = &unescape($group_privs);
 6901:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6902:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6903:                                 &get_groups_roles($tdomain,$trest,
 6904:                                                   \%course_roles,\@rolecodes,
 6905:                                                   \%groups_roles);
 6906:                             } else {
 6907:                                 push(@rolecodes,$$role);
 6908:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6909:                             }
 6910:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6911:                                                                    \%groups_roles);
 6912:                             &appenv(\%userroles,\@rolecodes);
 6913:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6914:                         }
 6915:                     }
 6916:                     $$tstatus = 'is';
 6917:                 }
 6918:             }
 6919:             if ($$tend) {
 6920:                 if ($$tend<$update) {
 6921:                     $$tstatus='expired';
 6922:                 } elsif ($$tend<$now) {
 6923:                     $$tstatus='will_not';
 6924:                 }
 6925:             }
 6926:         }
 6927:     }
 6928: }
 6929: 
 6930: sub get_groups_roles {
 6931:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6932:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6933:                   (ref($rolecodes) eq 'ARRAY') && 
 6934:                   (ref($groups_roles) eq 'HASH')); 
 6935:     if (keys(%{$cdom_courseroles}) > 0) {
 6936:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6937:         if ($cdom ne '' && $cnum ne '') {
 6938:             foreach my $key (keys(%{$cdom_courseroles})) {
 6939:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6940:                     my $crsrole = $1;
 6941:                     my $crssec = $2;
 6942:                     if ($crsrole =~ /^cr/) {
 6943:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6944:                             push(@{$rolecodes},'cr');
 6945:                         }
 6946:                     } else {
 6947:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6948:                             push(@{$rolecodes},$crsrole);
 6949:                         }
 6950:                     }
 6951:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6952:                     if ($crssec ne '') {
 6953:                         $rolekey .= "/$crssec";
 6954:                     }
 6955:                     $rolekey .= './';
 6956:                     $groups_roles->{$rolekey} = $rolecodes;
 6957:                 }
 6958:             }
 6959:         }
 6960:     }
 6961:     return;
 6962: }
 6963: 
 6964: sub delete_env_groupprivs {
 6965:     my ($where,$courseroles,$possroles) = @_;
 6966:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6967:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6968:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6969:         %{$courseroles->{$udom}} =
 6970:             &get_my_roles('','','userroles',['active'],
 6971:                           $possroles,[$udom],1);
 6972:     }
 6973:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6974:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6975:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6976:             my $area = '/'.$cdom.'/'.$cnum;
 6977:             my $privkey = "user.priv.$crsrole.$area";
 6978:             if ($crssec ne '') {
 6979:                 $privkey .= '/'.$crssec;
 6980:             }
 6981:             $privkey .= ".$area/$group";
 6982:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6983:         }
 6984:     }
 6985:     return;
 6986: }
 6987: 
 6988: sub check_adhoc_privs {
 6989:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6990:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6991:     if ($sec) {
 6992:         $cckey .= '/'.$sec;
 6993:     } 
 6994:     my $setprivs;
 6995:     if ($env{$cckey}) {
 6996:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 6997:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 6998:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 6999:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7000:             $setprivs = 1;
 7001:         }
 7002:     } else {
 7003:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7004:         $setprivs = 1;
 7005:     }
 7006:     return $setprivs;
 7007: }
 7008: 
 7009: sub set_adhoc_privileges {
 7010: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7011:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7012:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7013:     if ($sec ne '') {
 7014:         $area .= '/'.$sec;
 7015:     }
 7016:     my $spec = $role.'.'.$area;
 7017:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7018:                                   $env{'user.name'},1);
 7019:     my %rolehash = ();
 7020:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7021:         my $rolename = $1;
 7022:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7023:         my %domdef = &get_domain_defaults($dcdom);
 7024:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7025:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7026:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7027:             }
 7028:         }
 7029:     } else {
 7030:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7031:     }
 7032:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7033:     &appenv(\%userroles,[$role,'cm']);
 7034:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7035:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7036:             ($caller eq 'tiny')) {
 7037:         &appenv( {'request.role'        => $spec,
 7038:                   'request.role.domain' => $dcdom,
 7039:                   'request.course.sec'  => $sec,
 7040:                  }
 7041:                );
 7042:         my $tadv=0;
 7043:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7044:         &appenv({'request.role.adv'    => $tadv});
 7045:     }
 7046: }
 7047: 
 7048: # --------------------------------------------------------------- get interface
 7049: 
 7050: sub get {
 7051:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7052:    my $items='';
 7053:    foreach my $item (@$storearr) {
 7054:        $items.=&escape($item).'&';
 7055:    }
 7056:    $items=~s/\&$//;
 7057:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7058:    if (!$uname) { $uname=$env{'user.name'}; }
 7059:    my $uhome=&homeserver($uname,$udomain);
 7060: 
 7061:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7062:    my @pairs=split(/\&/,$rep);
 7063:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7064:      return @pairs;
 7065:    }
 7066:    my %returnhash=();
 7067:    my $i=0;
 7068:    foreach my $item (@$storearr) {
 7069:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7070:       $i++;
 7071:    }
 7072:    return %returnhash;
 7073: }
 7074: 
 7075: # --------------------------------------------------------------- del interface
 7076: 
 7077: sub del {
 7078:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7079:    my $items='';
 7080:    foreach my $item (@$storearr) {
 7081:        $items.=&escape($item).'&';
 7082:    }
 7083: 
 7084:    $items=~s/\&$//;
 7085:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7086:    if (!$uname) { $uname=$env{'user.name'}; }
 7087:    my $uhome=&homeserver($uname,$udomain);
 7088:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7089: }
 7090: 
 7091: # -------------------------------------------------------------- dump interface
 7092: 
 7093: sub unserialize {
 7094:     my ($rep, $escapedkeys) = @_;
 7095: 
 7096:     return {} if $rep =~ /^error/;
 7097: 
 7098:     my %returnhash=();
 7099: 	foreach my $item (split(/\&/,$rep)) {
 7100: 	    my ($key, $value) = split(/=/, $item, 2);
 7101: 	    $key = unescape($key) unless $escapedkeys;
 7102: 	    next if $key =~ /^error: 2 /;
 7103: 	    $returnhash{$key} = &thaw_unescape($value);
 7104: 	}
 7105:     #return %returnhash;
 7106:     return \%returnhash;
 7107: }        
 7108: 
 7109: # see Lond::dump_with_regexp
 7110: # if $escapedkeys hash keys won't get unescaped.
 7111: sub dump {
 7112:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 7113:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7114:     if (!$uname) { $uname=$env{'user.name'}; }
 7115:     my $uhome=&homeserver($uname,$udomain);
 7116: 
 7117:     if ($regexp) {
 7118:         $regexp=&escape($regexp);
 7119:     } else {
 7120:         $regexp='.';
 7121:     }
 7122:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7123:         # user is hosted on this machine
 7124:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7125:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7126:         return %{unserialize($reply, $escapedkeys)};
 7127:     }
 7128:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7129:     my @pairs=split(/\&/,$rep);
 7130:     my %returnhash=();
 7131:     if (!($rep =~ /^error/ )) {
 7132: 	foreach my $item (@pairs) {
 7133: 	    my ($key,$value)=split(/=/,$item,2);
 7134:         $key = unescape($key) unless $escapedkeys;
 7135:         #$key = &unescape($key);
 7136: 	    next if ($key =~ /^error: 2 /);
 7137: 	    $returnhash{$key}=&thaw_unescape($value);
 7138: 	}
 7139:     }
 7140:     return %returnhash;
 7141: }
 7142: 
 7143: 
 7144: # --------------------------------------------------------- dumpstore interface
 7145: 
 7146: sub dumpstore {
 7147:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7148:    # same as dump but keys must be escaped. They may contain colon separated
 7149:    # lists of values that may themself contain colons (e.g. symbs).
 7150:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7151: }
 7152: 
 7153: # -------------------------------------------------------------- keys interface
 7154: 
 7155: sub getkeys {
 7156:    my ($namespace,$udomain,$uname)=@_;
 7157:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7158:    if (!$uname) { $uname=$env{'user.name'}; }
 7159:    my $uhome=&homeserver($uname,$udomain);
 7160:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7161:    my @keyarray=();
 7162:    foreach my $key (split(/\&/,$rep)) {
 7163:       next if ($key =~ /^error: 2 /);
 7164:       push(@keyarray,&unescape($key));
 7165:    }
 7166:    return @keyarray;
 7167: }
 7168: 
 7169: # --------------------------------------------------------------- currentdump
 7170: sub currentdump {
 7171:    my ($courseid,$sdom,$sname)=@_;
 7172:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7173:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7174:    $sname    = $env{'user.name'}         if (! defined($sname));
 7175:    my $uhome = &homeserver($sname,$sdom);
 7176:    my $rep;
 7177: 
 7178:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7179:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7180:                    $courseid)));
 7181:    } else {
 7182:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7183:    }
 7184: 
 7185:    return if ($rep =~ /^(error:|no_such_host)/);
 7186:    #
 7187:    my %returnhash=();
 7188:    #
 7189:    if ($rep eq 'unknown_cmd') {
 7190:        # an old lond will not know currentdump
 7191:        # Do a dump and make it look like a currentdump
 7192:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7193:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7194:        my %hash = @tmp;
 7195:        @tmp=();
 7196:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7197:    } else {
 7198:        my @pairs=split(/\&/,$rep);
 7199:        foreach my $pair (@pairs) {
 7200:            my ($key,$value)=split(/=/,$pair,2);
 7201:            my ($symb,$param) = split(/:/,$key);
 7202:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7203:                                                         &thaw_unescape($value);
 7204:        }
 7205:    }
 7206:    return %returnhash;
 7207: }
 7208: 
 7209: sub convert_dump_to_currentdump{
 7210:     my %hash = %{shift()};
 7211:     my %returnhash;
 7212:     # Code ripped from lond, essentially.  The only difference
 7213:     # here is the unescaping done by lonnet::dump().  Conceivably
 7214:     # we might run in to problems with parameter names =~ /^v\./
 7215:     while (my ($key,$value) = each(%hash)) {
 7216:         my ($v,$symb,$param) = split(/:/,$key);
 7217: 	$symb  = &unescape($symb);
 7218: 	$param = &unescape($param);
 7219:         next if ($v eq 'version' || $symb eq 'keys');
 7220:         next if (exists($returnhash{$symb}) &&
 7221:                  exists($returnhash{$symb}->{$param}) &&
 7222:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7223:         $returnhash{$symb}->{$param}=$value;
 7224:         $returnhash{$symb}->{'v.'.$param}=$v;
 7225:     }
 7226:     #
 7227:     # Remove all of the keys in the hashes which keep track of
 7228:     # the version of the parameter.
 7229:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7230:         # use a foreach because we are going to delete from the hash.
 7231:         foreach my $key (keys(%$param_hash)) {
 7232:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7233:         }
 7234:     }
 7235:     return \%returnhash;
 7236: }
 7237: 
 7238: # ------------------------------------------------------ critical inc interface
 7239: 
 7240: sub cinc {
 7241:     return &inc(@_,'critical');
 7242: }
 7243: 
 7244: # --------------------------------------------------------------- inc interface
 7245: 
 7246: sub inc {
 7247:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7248:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7249:     if (!$uname) { $uname=$env{'user.name'}; }
 7250:     my $uhome=&homeserver($uname,$udomain);
 7251:     my $items='';
 7252:     if (! ref($store)) {
 7253:         # got a single value, so use that instead
 7254:         $items = &escape($store).'=&';
 7255:     } elsif (ref($store) eq 'SCALAR') {
 7256:         $items = &escape($$store).'=&';        
 7257:     } elsif (ref($store) eq 'ARRAY') {
 7258:         $items = join('=&',map {&escape($_);} @{$store});
 7259:     } elsif (ref($store) eq 'HASH') {
 7260:         while (my($key,$value) = each(%{$store})) {
 7261:             $items.= &escape($key).'='.&escape($value).'&';
 7262:         }
 7263:     }
 7264:     $items=~s/\&$//;
 7265:     if ($critical) {
 7266: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7267:     } else {
 7268: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7269:     }
 7270: }
 7271: 
 7272: # --------------------------------------------------------------- put interface
 7273: 
 7274: sub put {
 7275:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7276:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7277:    if (!$uname) { $uname=$env{'user.name'}; }
 7278:    my $uhome=&homeserver($uname,$udomain);
 7279:    my $items='';
 7280:    foreach my $item (keys(%$storehash)) {
 7281:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7282:    }
 7283:    $items=~s/\&$//;
 7284:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7285: }
 7286: 
 7287: # ------------------------------------------------------------ newput interface
 7288: 
 7289: sub newput {
 7290:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7291:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7292:    if (!$uname) { $uname=$env{'user.name'}; }
 7293:    my $uhome=&homeserver($uname,$udomain);
 7294:    my $items='';
 7295:    foreach my $key (keys(%$storehash)) {
 7296:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7297:    }
 7298:    $items=~s/\&$//;
 7299:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7300: }
 7301: 
 7302: # ---------------------------------------------------------  putstore interface
 7303: 
 7304: sub putstore {
 7305:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7306:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7307:    if (!$uname) { $uname=$env{'user.name'}; }
 7308:    my $uhome=&homeserver($uname,$udomain);
 7309:    my $items='';
 7310:    foreach my $key (keys(%$storehash)) {
 7311:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7312:    }
 7313:    $items=~s/\&$//;
 7314:    my $esc_symb=&escape($symb);
 7315:    my $esc_v=&escape($version);
 7316:    my $reply =
 7317:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7318: 	      $uhome);
 7319:    if (($tolog) && ($reply eq 'ok')) {
 7320:        my $namevalue='';
 7321:        foreach my $key (keys(%{$storehash})) {
 7322:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7323:        }
 7324:        my $ip = &get_requestor_ip();
 7325:        $namevalue .= 'ip='.&escape($ip).
 7326:                      '&host='.&escape($perlvar{'lonHostID'}).
 7327:                      '&version='.$esc_v.
 7328:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7329:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7330:    }
 7331:    if ($reply eq 'unknown_cmd') {
 7332:        # gfall back to way things use to be done
 7333:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7334: 			    $uname);
 7335:    }
 7336:    return $reply;
 7337: }
 7338: 
 7339: sub old_putstore {
 7340:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7341:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7342:     if (!$uname) { $uname=$env{'user.name'}; }
 7343:     my $uhome=&homeserver($uname,$udomain);
 7344:     my %newstorehash;
 7345:     foreach my $item (keys(%$storehash)) {
 7346: 	my $key = $version.':'.&escape($symb).':'.$item;
 7347: 	$newstorehash{$key} = $storehash->{$item};
 7348:     }
 7349:     my $items='';
 7350:     my %allitems = ();
 7351:     foreach my $item (keys(%newstorehash)) {
 7352: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7353: 	    my $key = $1.':keys:'.$2;
 7354: 	    $allitems{$key} .= $3.':';
 7355: 	}
 7356: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7357:     }
 7358:     foreach my $item (keys(%allitems)) {
 7359: 	$allitems{$item} =~ s/\:$//;
 7360: 	$items.= $item.'='.$allitems{$item}.'&';
 7361:     }
 7362:     $items=~s/\&$//;
 7363:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7364: }
 7365: 
 7366: # ------------------------------------------------------ critical put interface
 7367: 
 7368: sub cput {
 7369:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7370:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7371:    if (!$uname) { $uname=$env{'user.name'}; }
 7372:    my $uhome=&homeserver($uname,$udomain);
 7373:    my $items='';
 7374:    foreach my $item (keys(%$storehash)) {
 7375:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7376:    }
 7377:    $items=~s/\&$//;
 7378:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7379: }
 7380: 
 7381: # -------------------------------------------------------------- eget interface
 7382: 
 7383: sub eget {
 7384:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7385:    my $items='';
 7386:    foreach my $item (@$storearr) {
 7387:        $items.=&escape($item).'&';
 7388:    }
 7389:    $items=~s/\&$//;
 7390:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7391:    if (!$uname) { $uname=$env{'user.name'}; }
 7392:    my $uhome=&homeserver($uname,$udomain);
 7393:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7394:    my @pairs=split(/\&/,$rep);
 7395:    my %returnhash=();
 7396:    my $i=0;
 7397:    foreach my $item (@$storearr) {
 7398:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7399:       $i++;
 7400:    }
 7401:    return %returnhash;
 7402: }
 7403: 
 7404: # ------------------------------------------------------------ tmpput interface
 7405: sub tmpput {
 7406:     my ($storehash,$server,$context)=@_;
 7407:     my $items='';
 7408:     foreach my $item (keys(%$storehash)) {
 7409: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7410:     }
 7411:     $items=~s/\&$//;
 7412:     if (defined($context)) {
 7413:         $items .= ':'.&escape($context);
 7414:     }
 7415:     return &reply("tmpput:$items",$server);
 7416: }
 7417: 
 7418: # ------------------------------------------------------------ tmpget interface
 7419: sub tmpget {
 7420:     my ($token,$server)=@_;
 7421:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7422:     my $rep=&reply("tmpget:$token",$server);
 7423:     my %returnhash;
 7424:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7425:         return %returnhash;
 7426:     }
 7427:     foreach my $item (split(/\&/,$rep)) {
 7428: 	my ($key,$value)=split(/=/,$item);
 7429: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7430:     }
 7431:     return %returnhash;
 7432: }
 7433: 
 7434: # ------------------------------------------------------------ tmpdel interface
 7435: sub tmpdel {
 7436:     my ($token,$server)=@_;
 7437:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7438:     return &reply("tmpdel:$token",$server);
 7439: }
 7440: 
 7441: # ------------------------------------------------------------ get_timebased_id 
 7442: 
 7443: sub get_timebased_id {
 7444:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7445:         $maxtries) = @_;
 7446:     my ($newid,$error,$dellock);
 7447:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7448:         return ('','ok','invalid call to get suffix');
 7449:     }
 7450: 
 7451: # set defaults for any optional args for which values were not supplied
 7452:     if ($who eq '') {
 7453:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7454:     }
 7455:     if (!$locktries) {
 7456:         $locktries = 3;
 7457:     }
 7458:     if (!$maxtries) {
 7459:         $maxtries = 10;
 7460:     }
 7461:     
 7462:     if (($cdom eq '') || ($cnum eq '')) {
 7463:         if ($env{'request.course.id'}) {
 7464:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7465:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7466:         }
 7467:         if (($cdom eq '') || ($cnum eq '')) {
 7468:             return ('','ok','call to get suffix not in course context');
 7469:         }
 7470:     }
 7471: 
 7472: # construct locking item
 7473:     my $lockhash = {
 7474:                       $prefix."\0".'locked_'.$keyid => $who,
 7475:                    };
 7476:     my $tries = 0;
 7477: 
 7478: # attempt to get lock on nohist_$namespace file
 7479:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7480:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7481:         $tries ++;
 7482:         sleep 1;
 7483:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7484:     }
 7485: 
 7486: # attempt to get unique identifier, based on current timestamp
 7487:     if ($gotlock eq 'ok') {
 7488:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7489:         my $id = time;
 7490:         $newid = $id;
 7491:         if ($idtype eq 'addcode') {
 7492:             $newid .= &sixnum_code();
 7493:         }
 7494:         my $idtries = 0;
 7495:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7496:             if ($idtype eq 'concat') {
 7497:                 $newid = $id.$idtries;
 7498:             } elsif ($idtype eq 'addcode') {
 7499:                 $newid = $newid.&sixnum_code();
 7500:             } else {
 7501:                 $newid ++;
 7502:             }
 7503:             $idtries ++;
 7504:         }
 7505:         if (!exists($inuse{$prefix."\0".$newid})) {
 7506:             my %new_item =  (
 7507:                               $prefix."\0".$newid => $who,
 7508:                             );
 7509:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7510:                                                  $cdom,$cnum);
 7511:             if ($putresult ne 'ok') {
 7512:                 undef($newid);
 7513:                 $error = 'error saving new item: '.$putresult;
 7514:             }
 7515:         } else {
 7516:              undef($newid);
 7517:              $error = ('error: no unique suffix available for the new item ');
 7518:         }
 7519: #  remove lock
 7520:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7521:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7522:     } else {
 7523:         $error = "error: could not obtain lockfile\n";
 7524:         $dellock = 'ok';
 7525:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7526:             $dellock = 'nolock';
 7527:         }
 7528:     }
 7529:     return ($newid,$dellock,$error);
 7530: }
 7531: 
 7532: sub sixnum_code {
 7533:     my $code;
 7534:     for (0..6) {
 7535:         $code .= int( rand(9) );
 7536:     }
 7537:     return $code;
 7538: }
 7539: 
 7540: # -------------------------------------------------- portfolio access checking
 7541: 
 7542: sub portfolio_access {
 7543:     my ($requrl,$clientip) = @_;
 7544:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7545:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7546:     if ($result) {
 7547:         my %setters;
 7548:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7549:             my ($startblock,$endblock) =
 7550:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 7551:             if ($startblock && $endblock) {
 7552:                 return 'B';
 7553:             }
 7554:         } else {
 7555:             my ($startblock,$endblock) =
 7556:                 &Apache::loncommon::blockcheck(\%setters,'port');
 7557:             if ($startblock && $endblock) {
 7558:                 return 'B';
 7559:             }
 7560:         }
 7561:     }
 7562:     if ($result eq 'ok') {
 7563:        return 'F';
 7564:     } elsif ($result =~ /^[^:]+:guest_/) {
 7565:        return 'A';
 7566:     }
 7567:     return '';
 7568: }
 7569: 
 7570: sub get_portfolio_access {
 7571:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7572: 
 7573:     if (!ref($access_hash)) {
 7574: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7575: 	my %access_controls = &get_access_controls($current_perms,$group,
 7576: 						   $file_name);
 7577: 	$access_hash = $access_controls{$file_name};
 7578:     }
 7579: 
 7580:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7581:     my $now = time;
 7582:     if (ref($access_hash) eq 'HASH') {
 7583:         foreach my $key (keys(%{$access_hash})) {
 7584:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7585:             if ($start > $now) {
 7586:                 next;
 7587:             }
 7588:             if ($end && $end<$now) {
 7589:                 next;
 7590:             }
 7591:             if ($scope eq 'public') {
 7592:                 $public = $key;
 7593:                 last;
 7594:             } elsif ($scope eq 'guest') {
 7595:                 $guest = $key;
 7596:             } elsif ($scope eq 'domains') {
 7597:                 push(@domains,$key);
 7598:             } elsif ($scope eq 'users') {
 7599:                 push(@users,$key);
 7600:             } elsif ($scope eq 'course') {
 7601:                 push(@courses,$key);
 7602:             } elsif ($scope eq 'group') {
 7603:                 push(@groups,$key);
 7604:             } elsif ($scope eq 'ip') {
 7605:                 push(@ips,$key);
 7606:             }
 7607:         }
 7608:         if ($public) {
 7609:             return 'ok';
 7610:         } elsif (@ips > 0) {
 7611:             my $allowed;
 7612:             foreach my $ipkey (@ips) {
 7613:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7614:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7615:                         $allowed = 1;
 7616:                         last; 
 7617:                     }
 7618:                 }
 7619:             }
 7620:             if ($allowed) {
 7621:                 return 'ok';
 7622:             }
 7623:         }
 7624:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7625:             if ($guest) {
 7626:                 return $guest;
 7627:             }
 7628:         } else {
 7629:             if (@domains > 0) {
 7630:                 foreach my $domkey (@domains) {
 7631:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7632:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7633:                             return 'ok';
 7634:                         }
 7635:                     }
 7636:                 }
 7637:             }
 7638:             if (@users > 0) {
 7639:                 foreach my $userkey (@users) {
 7640:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7641:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7642:                             if (ref($item) eq 'HASH') {
 7643:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7644:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7645:                                     return 'ok';
 7646:                                 }
 7647:                             }
 7648:                         }
 7649:                     } 
 7650:                 }
 7651:             }
 7652:             my %roleshash;
 7653:             my @courses_and_groups = @courses;
 7654:             push(@courses_and_groups,@groups); 
 7655:             if (@courses_and_groups > 0) {
 7656:                 my (%allgroups,%allroles); 
 7657:                 my ($start,$end,$role,$sec,$group);
 7658:                 foreach my $envkey (%env) {
 7659:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7660:                         my $cid = $2.'_'.$3; 
 7661:                         if ($1 eq 'gr') {
 7662:                             $group = $4;
 7663:                             $allgroups{$cid}{$group} = $env{$envkey};
 7664:                         } else {
 7665:                             if ($4 eq '') {
 7666:                                 $sec = 'none';
 7667:                             } else {
 7668:                                 $sec = $4;
 7669:                             }
 7670:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7671:                         }
 7672:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7673:                         my $cid = $2.'_'.$3;
 7674:                         if ($4 eq '') {
 7675:                             $sec = 'none';
 7676:                         } else {
 7677:                             $sec = $4;
 7678:                         }
 7679:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7680:                     }
 7681:                 }
 7682:                 if (keys(%allroles) == 0) {
 7683:                     return;
 7684:                 }
 7685:                 foreach my $key (@courses_and_groups) {
 7686:                     my %content = %{$$access_hash{$key}};
 7687:                     my $cnum = $content{'number'};
 7688:                     my $cdom = $content{'domain'};
 7689:                     my $cid = $cdom.'_'.$cnum;
 7690:                     if (!exists($allroles{$cid})) {
 7691:                         next;
 7692:                     }    
 7693:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7694:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7695:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7696:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7697:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7698:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7699:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7700:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7701:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7702:                                         if (grep/^all$/,@sections) {
 7703:                                             return 'ok';
 7704:                                         } else {
 7705:                                             if (grep/^$sec$/,@sections) {
 7706:                                                 return 'ok';
 7707:                                             }
 7708:                                         }
 7709:                                     }
 7710:                                 }
 7711:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7712:                                     if (grep/^none$/,@groups) {
 7713:                                         return 'ok';
 7714:                                     }
 7715:                                 } else {
 7716:                                     if (grep/^all$/,@groups) {
 7717:                                         return 'ok';
 7718:                                     } 
 7719:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7720:                                         if (grep/^$group$/,@groups) {
 7721:                                             return 'ok';
 7722:                                         }
 7723:                                     }
 7724:                                 } 
 7725:                             }
 7726:                         }
 7727:                     }
 7728:                 }
 7729:             }
 7730:             if ($guest) {
 7731:                 return $guest;
 7732:             }
 7733:         }
 7734:     }
 7735:     return;
 7736: }
 7737: 
 7738: sub course_group_datechecker {
 7739:     my ($dates,$now,$status) = @_;
 7740:     my ($start,$end) = split(/\./,$dates);
 7741:     if (!$start && !$end) {
 7742:         return 'ok';
 7743:     }
 7744:     if (grep/^active$/,@{$status}) {
 7745:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7746:             return 'ok';
 7747:         }
 7748:     }
 7749:     if (grep/^previous$/,@{$status}) {
 7750:         if ($end > $now ) {
 7751:             return 'ok';
 7752:         }
 7753:     }
 7754:     if (grep/^future$/,@{$status}) {
 7755:         if ($start > $now) {
 7756:             return 'ok';
 7757:         }
 7758:     }
 7759:     return; 
 7760: }
 7761: 
 7762: sub parse_portfolio_url {
 7763:     my ($url) = @_;
 7764: 
 7765:     my ($type,$udom,$unum,$group,$file_name);
 7766:     
 7767:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7768: 	$type = 1;
 7769:         $udom = $1;
 7770:         $unum = $2;
 7771:         $file_name = $3;
 7772:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7773: 	$type = 2;
 7774:         $udom = $1;
 7775:         $unum = $2;
 7776:         $group = $3;
 7777:         $file_name = $3.'/'.$4;
 7778:     }
 7779:     if (wantarray) {
 7780: 	return ($type,$udom,$unum,$file_name,$group);
 7781:     }
 7782:     return $type;
 7783: }
 7784: 
 7785: sub is_portfolio_url {
 7786:     my ($url) = @_;
 7787:     return scalar(&parse_portfolio_url($url));
 7788: }
 7789: 
 7790: sub is_portfolio_file {
 7791:     my ($file) = @_;
 7792:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7793:         return 1;
 7794:     }
 7795:     return;
 7796: }
 7797: 
 7798: sub usertools_access {
 7799:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7800:     my ($access,%tools);
 7801:     if ($context eq '') {
 7802:         $context = 'tools';
 7803:     }
 7804:     if ($context eq 'requestcourses') {
 7805:         %tools = (
 7806:                       official   => 1,
 7807:                       unofficial => 1,
 7808:                       community  => 1,
 7809:                       textbook   => 1,
 7810:                       placement  => 1,
 7811:                       lti        => 1,
 7812:                  );
 7813:     } elsif ($context eq 'requestauthor') {
 7814:         %tools = (
 7815:                       requestauthor => 1,
 7816:                  );
 7817:     } else {
 7818:         %tools = (
 7819:                       aboutme   => 1,
 7820:                       blog      => 1,
 7821:                       webdav    => 1,
 7822:                       portfolio => 1,
 7823:                  );
 7824:     }
 7825:     return if (!defined($tools{$tool}));
 7826: 
 7827:     if (($udom eq '') || ($uname eq '')) {
 7828:         $udom = $env{'user.domain'};
 7829:         $uname = $env{'user.name'};
 7830:     }
 7831: 
 7832:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7833:         if ($action ne 'reload') {
 7834:             if ($context eq 'requestcourses') {
 7835:                 return $env{'environment.canrequest.'.$tool};
 7836:             } elsif ($context eq 'requestauthor') {
 7837:                 return $env{'environment.canrequest.author'};
 7838:             } else {
 7839:                 return $env{'environment.availabletools.'.$tool};
 7840:             }
 7841:         }
 7842:     }
 7843: 
 7844:     my ($toolstatus,$inststatus,$envkey);
 7845:     if ($context eq 'requestauthor') {
 7846:         $envkey = $context; 
 7847:     } else {
 7848:         $envkey = $context.'.'.$tool;
 7849:     }
 7850: 
 7851:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7852:          ($action ne 'reload')) {
 7853:         $toolstatus = $env{'environment.'.$envkey};
 7854:         $inststatus = $env{'environment.inststatus'};
 7855:     } else {
 7856:         if (ref($userenvref) eq 'HASH') {
 7857:             $toolstatus = $userenvref->{$envkey};
 7858:             $inststatus = $userenvref->{'inststatus'};
 7859:         } else {
 7860:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7861:             $toolstatus = $userenv{$envkey};
 7862:             $inststatus = $userenv{'inststatus'};
 7863:         }
 7864:     }
 7865: 
 7866:     if ($toolstatus ne '') {
 7867:         if ($toolstatus) {
 7868:             $access = 1;
 7869:         } else {
 7870:             $access = 0;
 7871:         }
 7872:         return $access;
 7873:     }
 7874: 
 7875:     my ($is_adv,%domdef);
 7876:     if (ref($is_advref) eq 'HASH') {
 7877:         $is_adv = $is_advref->{'is_adv'};
 7878:     } else {
 7879:         $is_adv = &is_advanced_user($udom,$uname);
 7880:     }
 7881:     if (ref($domdefref) eq 'HASH') {
 7882:         %domdef = %{$domdefref};
 7883:     } else {
 7884:         %domdef = &get_domain_defaults($udom);
 7885:     }
 7886:     if (ref($domdef{$tool}) eq 'HASH') {
 7887:         if ($is_adv) {
 7888:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7889:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7890:                     $access = 1;
 7891:                 } else {
 7892:                     $access = 0;
 7893:                 }
 7894:                 return $access;
 7895:             }
 7896:         }
 7897:         if ($inststatus ne '') {
 7898:             my ($hasaccess,$hasnoaccess);
 7899:             foreach my $affiliation (split(/:/,$inststatus)) {
 7900:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7901:                     if ($domdef{$tool}{$affiliation}) {
 7902:                         $hasaccess = 1;
 7903:                     } else {
 7904:                         $hasnoaccess = 1;
 7905:                     }
 7906:                 }
 7907:             }
 7908:             if ($hasaccess || $hasnoaccess) {
 7909:                 if ($hasaccess) {
 7910:                     $access = 1;
 7911:                 } elsif ($hasnoaccess) {
 7912:                     $access = 0; 
 7913:                 }
 7914:                 return $access;
 7915:             }
 7916:         } else {
 7917:             if ($domdef{$tool}{'default'} ne '') {
 7918:                 if ($domdef{$tool}{'default'}) {
 7919:                     $access = 1;
 7920:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7921:                     $access = 0;
 7922:                 }
 7923:                 return $access;
 7924:             }
 7925:         }
 7926:     } else {
 7927:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7928:             $access = 1;
 7929:         } else {
 7930:             $access = 0;
 7931:         }
 7932:         return $access;
 7933:     }
 7934: }
 7935: 
 7936: sub is_course_owner {
 7937:     my ($cdom,$cnum,$udom,$uname) = @_;
 7938:     if (($udom eq '') || ($uname eq '')) {
 7939:         $udom = $env{'user.domain'};
 7940:         $uname = $env{'user.name'};
 7941:     }
 7942:     unless (($udom eq '') || ($uname eq '')) {
 7943:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7944:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7945:                 return 1;
 7946:             } else {
 7947:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7948:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7949:                     return 1;
 7950:                 }
 7951:             }
 7952:         }
 7953:     }
 7954:     return;
 7955: }
 7956: 
 7957: sub is_advanced_user {
 7958:     my ($udom,$uname) = @_;
 7959:     if ($udom ne '' && $uname ne '') {
 7960:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7961:             if (wantarray) {
 7962:                 return ($env{'user.adv'},$env{'user.author'});
 7963:             } else {
 7964:                 return $env{'user.adv'};
 7965:             }
 7966:         }
 7967:     }
 7968:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7969:     my %allroles;
 7970:     my ($is_adv,$is_author);
 7971:     foreach my $role (keys(%roleshash)) {
 7972:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7973:         my $area = '/'.$tdomain.'/'.$trest;
 7974:         if ($sec ne '') {
 7975:             $area .= '/'.$sec;
 7976:         }
 7977:         if (($area ne '') && ($trole ne '')) {
 7978:             my $spec=$trole.'.'.$area;
 7979:             if ($trole =~ /^cr\//) {
 7980:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7981:             } elsif ($trole ne 'gr') {
 7982:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7983:             }
 7984:             if ($trole eq 'au') {
 7985:                 $is_author = 1;
 7986:             }
 7987:         }
 7988:     }
 7989:     foreach my $role (keys(%allroles)) {
 7990:         last if ($is_adv);
 7991:         foreach my $item (split(/:/,$allroles{$role})) {
 7992:             if ($item ne '') {
 7993:                 my ($privilege,$restrictions)=split(/&/,$item);
 7994:                 if ($privilege eq 'adv') {
 7995:                     $is_adv = 1;
 7996:                     last;
 7997:                 }
 7998:             }
 7999:         }
 8000:     }
 8001:     if (wantarray) {
 8002:         return ($is_adv,$is_author);
 8003:     }
 8004:     return $is_adv;
 8005: }
 8006: 
 8007: sub check_can_request {
 8008:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8009:     my $canreq = 0;
 8010:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8011:         $uname = $env{'user.name'};
 8012:         $udom = $env{'user.domain'};
 8013:     }
 8014:     my ($types,$typename) = &Apache::loncommon::course_types();
 8015:     my @options = ('approval','validate','autolimit');
 8016:     my $optregex = join('|',@options);
 8017:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8018:         foreach my $type (@{$types}) {
 8019:             if (&usertools_access($uname,$udom,$type,undef,
 8020:                                   'requestcourses')) {
 8021:                 $canreq ++;
 8022:                 if (ref($request_domains) eq 'HASH') {
 8023:                     push(@{$request_domains->{$type}},$udom);
 8024:                 }
 8025:                 if ($dom eq $udom) {
 8026:                     $can_request->{$type} = 1;
 8027:                 }
 8028:             }
 8029:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8030:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8031:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8032:                 if (@curr > 0) {
 8033:                     foreach my $item (@curr) {
 8034:                         if (ref($request_domains) eq 'HASH') {
 8035:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8036:                             if ($otherdom ne '') {
 8037:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 8038:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8039:                                         push(@{$request_domains->{$type}},$otherdom);
 8040:                                     }
 8041:                                 } else {
 8042:                                     push(@{$request_domains->{$type}},$otherdom);
 8043:                                 }
 8044:                             }
 8045:                         }
 8046:                     }
 8047:                     unless ($dom eq $env{'user.domain'}) {
 8048:                         $canreq ++;
 8049:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8050:                             $can_request->{$type} = 1;
 8051:                         }
 8052:                     }
 8053:                 }
 8054:             }
 8055:         }
 8056:     }
 8057:     return $canreq;
 8058: }
 8059: 
 8060: # ---------------------------------------------- Custom access rule evaluation
 8061: 
 8062: sub customaccess {
 8063:     my ($priv,$uri)=@_;
 8064:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8065:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8066:     $udom = &LONCAPA::clean_domain($udom);
 8067:     $ucrs = &LONCAPA::clean_username($ucrs);
 8068:     my $access=0;
 8069:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8070: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8071: 	if ($type eq 'user') {
 8072: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8073: 		my ($tdom,$tuname)=split(m{/},$scope);
 8074: 		if ($tdom) {
 8075: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8076: 		}
 8077: 		if ($tuname) {
 8078: 		    if ($tuname ne $env{'user.name'}) { next; }
 8079: 		}
 8080: 		$access=($effect eq 'allow');
 8081: 		last;
 8082: 	    }
 8083: 	} else {
 8084: 	    if ($role) {
 8085: 		if ($role ne $urole) { next; }
 8086: 	    }
 8087: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8088: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8089: 		if ($tdom) {
 8090: 		    if ($tdom ne $udom) { next; }
 8091: 		}
 8092: 		if ($tcrs) {
 8093: 		    if ($tcrs ne $ucrs) { next; }
 8094: 		}
 8095: 		if ($tsec) {
 8096: 		    if ($tsec ne $usec) { next; }
 8097: 		}
 8098: 		$access=($effect eq 'allow');
 8099: 		last;
 8100: 	    }
 8101: 	    if ($realm eq '' && $role eq '') {
 8102: 		$access=($effect eq 'allow');
 8103: 	    }
 8104: 	}
 8105:     }
 8106:     return $access;
 8107: }
 8108: 
 8109: # ------------------------------------------------- Check for a user privilege
 8110: 
 8111: sub allowed {
 8112:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache)=@_;
 8113:     my $ver_orguri=$uri;
 8114:     $uri=&deversion($uri);
 8115:     my $orguri=$uri;
 8116:     $uri=&declutter($uri);
 8117: 
 8118:     if ($priv eq 'evb') {
 8119: # Evade communication block restrictions for specified role in a course
 8120:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8121:             return $1;
 8122:         } else {
 8123:             return;
 8124:         }
 8125:     }
 8126: 
 8127:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8128: # Free bre access to adm and meta resources
 8129:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8130: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8131: 	&& ($priv eq 'bre')) {
 8132: 	return 'F';
 8133:     }
 8134: 
 8135: # Free bre access to user's own portfolio contents
 8136:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8137:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8138: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8139:         my %setters;
 8140:         my ($startblock,$endblock) = 
 8141:             &Apache::loncommon::blockcheck(\%setters,'port');
 8142:         if ($startblock && $endblock) {
 8143:             return 'B';
 8144:         } else {
 8145:             return 'F';
 8146:         }
 8147:     }
 8148: 
 8149: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8150:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8151:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8152:         if (exists($env{'request.course.id'})) {
 8153:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8154:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8155:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8156:                 my $courseprivid=$env{'request.course.id'};
 8157:                 $courseprivid=~s/\_/\//;
 8158:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8159:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8160:                     return $1; 
 8161:                 } else {
 8162:                     if ($env{'request.course.sec'}) {
 8163:                         $courseprivid.='/'.$env{'request.course.sec'};
 8164:                     }
 8165:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8166:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8167:                         return $2;
 8168:                     }
 8169:                 }
 8170:             }
 8171:         }
 8172:     }
 8173: 
 8174: # Free bre to public access
 8175: 
 8176:     if ($priv eq 'bre') {
 8177:         my $copyright;
 8178:         unless ($uri =~ /ext\.tool/) {
 8179:             $copyright=&metadata($uri,'copyright');
 8180:         }
 8181: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8182:            return 'F'; 
 8183:         }
 8184:         if ($copyright eq 'priv') {
 8185:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8186: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8187: 		return '';
 8188:             }
 8189:         }
 8190:         if ($copyright eq 'domain') {
 8191:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8192: 	    unless (($env{'user.domain'} eq $1) ||
 8193:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8194: 		return '';
 8195:             }
 8196:         }
 8197:         if ($env{'request.role'}=~ /li\.\//) {
 8198:             # Library role, so allow browsing of resources in this domain.
 8199:             return 'F';
 8200:         }
 8201:         if ($copyright eq 'custom') {
 8202: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8203:         }
 8204:     }
 8205:     # Domain coordinator is trying to create a course
 8206:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8207:         # uri is the requested domain in this case.
 8208:         # comparison to 'request.role.domain' shows if the user has selected
 8209:         # a role of dc for the domain in question.
 8210:         return 'F' if ($uri eq $env{'request.role.domain'});
 8211:     }
 8212: 
 8213:     my $thisallowed='';
 8214:     my $statecond=0;
 8215:     my $courseprivid='';
 8216: 
 8217:     my $ownaccess;
 8218:     # Community Coordinator or Assistant Co-author browsing resource space.
 8219:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8220:         if ($uri eq '') {
 8221:             $ownaccess = 1;
 8222:         } else {
 8223:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8224:                 my $udom = $env{'user.domain'};
 8225:                 my $uname = $env{'user.name'};
 8226:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8227:                     $ownaccess = 1;
 8228:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8229:                     unless ($uri =~ m{\.\./}) {
 8230:                         $ownaccess = 1;
 8231:                     }
 8232:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8233:                     my $now = time;
 8234:                     if ($uri =~ m{^([^/]+)/?$}) {
 8235:                         my $adom = $1;
 8236:                         foreach my $key (keys(%env)) {
 8237:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8238:                                 my ($start,$end) = split('.',$env{$key});
 8239:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8240:                                     $ownaccess = 1;
 8241:                                     last;
 8242:                                 }
 8243:                             }
 8244:                         }
 8245:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8246:                         my $adom = $1;
 8247:                         my $aname = $2;
 8248:                         foreach my $role ('ca','aa') { 
 8249:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8250:                                 my ($start,$end) =
 8251:                                     split('.',$env{"user.role.$role./$adom/$aname"});
 8252:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8253:                                     $ownaccess = 1;
 8254:                                     last;
 8255:                                 }
 8256:                             }
 8257:                         }
 8258:                     }
 8259:                 }
 8260:             }
 8261:         }
 8262:     }
 8263: 
 8264: # Course
 8265: 
 8266:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8267:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8268:             $thisallowed.=$1;
 8269:         }
 8270:     }
 8271: 
 8272: # Domain
 8273: 
 8274:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8275:        =~/\Q$priv\E\&([^\:]*)/) {
 8276:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8277:             $thisallowed.=$1;
 8278:         }
 8279:     }
 8280: 
 8281: # User who is not author or co-author might still be able to edit
 8282: # resource of an author in the domain (e.g., if Domain Coordinator).
 8283:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8284:         (&allowed('mdc',$env{'request.course.id'}))) {
 8285:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8286:             $thisallowed.=$1;
 8287:         }
 8288:     }
 8289: 
 8290: # Course: uri itself is a course
 8291:     my $courseuri=$uri;
 8292:     $courseuri=~s/\_(\d)/\/$1/;
 8293:     $courseuri=~s/^([^\/])/\/$1/;
 8294: 
 8295:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8296:        =~/\Q$priv\E\&([^\:]*)/) {
 8297:         if ($priv eq 'mip') {
 8298:             my $rem = $1;
 8299:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8300:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8301:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8302:                 if ($cdom ne '') {
 8303:                     my %passwdconf = &get_passwdconf($cdom);
 8304:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8305:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8306:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8307:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8308:                                 unless (@inststatuses) {
 8309:                                     @inststatuses = ('default');
 8310:                                 }
 8311:                                 foreach my $status (@inststatuses) {
 8312:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8313:                                         $thisallowed.=$rem;
 8314:                                     }
 8315:                                 }
 8316:                             }
 8317:                         }
 8318:                     }
 8319:                 }
 8320:             }
 8321:         } else {
 8322:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8323:                 $thisallowed.=$1;
 8324:             }
 8325:         }
 8326:     }
 8327: 
 8328: # URI is an uploaded document for this course, default permissions don't matter
 8329: # not allowing 'edit' access (editupload) to uploaded course docs
 8330:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8331: 	$thisallowed='';
 8332:         my ($match)=&is_on_map($uri);
 8333:         if ($match) {
 8334:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8335:                   =~/\Q$priv\E\&([^\:]*)/) {
 8336:                 my $value = $1;
 8337:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8338:                 if ($deeplinkblock) {
 8339:                     $thisallowed='D';
 8340:                 } elsif ($noblockcheck) {
 8341:                     $thisallowed.=$value;
 8342:                 } else {
 8343:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8344:                     if (@blockers > 0) {
 8345:                         $thisallowed = 'B';
 8346:                     } else {
 8347:                         $thisallowed.=$value;
 8348:                     }
 8349:                 }
 8350:             }
 8351:         } else {
 8352:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8353:             if ($refuri) {
 8354:                 if ($refuri =~ m|^/adm/|) {
 8355:                     $thisallowed='F';
 8356:                 } else {
 8357:                     $refuri=&declutter($refuri);
 8358:                     my ($match) = &is_on_map($refuri);
 8359:                     if ($match) {
 8360:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8361:                         if ($deeplinkblock) {
 8362:                             $thisallowed='D';
 8363:                         } elsif ($noblockcheck) {
 8364:                             $thisallowed='F';
 8365:                         } else {
 8366:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8367:                             if (@blockers > 0) {
 8368:                                 $thisallowed = 'B';
 8369:                             } else {
 8370:                                 $thisallowed='F';
 8371:                             }
 8372:                         }
 8373:                     }
 8374:                 }
 8375:             }
 8376:         }
 8377:     }
 8378: 
 8379:     if ($priv eq 'bre'
 8380: 	&& $thisallowed ne 'F' 
 8381: 	&& $thisallowed ne '2'
 8382: 	&& &is_portfolio_url($uri)) {
 8383: 	$thisallowed = &portfolio_access($uri,$clientip);
 8384:     }
 8385: 
 8386: # Full access at system, domain or course-wide level? Exit.
 8387:     if ($thisallowed=~/F/) {
 8388: 	return 'F';
 8389:     }
 8390: 
 8391: # If this is generating or modifying users, exit with special codes
 8392: 
 8393:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8394: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8395: 	    my ($audom,$auname)=split('/',$uri);
 8396: # no author name given, so this just checks on the general right to make a co-author in this domain
 8397: 	    unless ($auname) { return $thisallowed; }
 8398: # an author name is given, so we are about to actually make a co-author for a certain account
 8399: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8400: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8401: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8402: 	}
 8403: 	return $thisallowed;
 8404:     }
 8405: #
 8406: # Gathered so far: system, domain and course wide privileges
 8407: #
 8408: # Course: See if uri or referer is an individual resource that is part of 
 8409: # the course
 8410: 
 8411:     if ($env{'request.course.id'}) {
 8412: 
 8413: # If this is modifying password (internal auth) domains must match for user and user's role.
 8414: 
 8415:         if ($priv eq 'mip') {
 8416:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8417:                 return $thisallowed;
 8418:             } else {
 8419:                 return '';
 8420:             }
 8421:         }
 8422: 
 8423:        $courseprivid=$env{'request.course.id'};
 8424:        if ($env{'request.course.sec'}) {
 8425:           $courseprivid.='/'.$env{'request.course.sec'};
 8426:        }
 8427:        $courseprivid=~s/\_/\//;
 8428:        my $checkreferer=1;
 8429:        my ($match,$cond)=&is_on_map($uri);
 8430:        if ($match) {
 8431:            $statecond=$cond;
 8432:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8433:                =~/\Q$priv\E\&([^\:]*)/) {
 8434:                my $value = $1;
 8435:                if ($priv eq 'bre') {
 8436:                    if ($noblockcheck) {
 8437:                        $thisallowed.=$value;
 8438:                    } else {
 8439:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8440:                        if (@blockers > 0) {
 8441:                            $thisallowed = 'B';
 8442:                        } else {
 8443:                            $thisallowed.=$value;
 8444:                        }
 8445:                    }
 8446:                } else {
 8447:                    $thisallowed.=$value;
 8448:                }
 8449:                $checkreferer=0;
 8450:            }
 8451:        }
 8452: 
 8453:        if ($checkreferer) {
 8454: 	  my $refuri=$env{'httpref.'.$orguri};
 8455:             unless ($refuri) {
 8456:                 foreach my $key (keys(%env)) {
 8457: 		    if ($key=~/^httpref\..*\*/) {
 8458: 			my $pattern=$key;
 8459:                         $pattern=~s/^httpref\.\/res\///;
 8460:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8461:                         $pattern=~s/\//\\\//g;
 8462:                         if ($orguri=~/$pattern/) {
 8463: 			    $refuri=$env{$key};
 8464:                         }
 8465:                     }
 8466:                 }
 8467:             }
 8468: 
 8469:          if ($refuri) { 
 8470: 	  $refuri=&declutter($refuri);
 8471:           my ($match,$cond)=&is_on_map($refuri);
 8472:             if ($match) {
 8473:               my $refstatecond=$cond;
 8474:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8475:                   =~/\Q$priv\E\&([^\:]*)/) {
 8476:                   my $value = $1;
 8477:                   if ($priv eq 'bre') {
 8478:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8479:                       if ($deeplinkblock) {
 8480:                           $thisallowed = 'D';
 8481:                       } elsif ($noblockcheck) {
 8482:                           $thisallowed.=$value;
 8483:                       } else {
 8484:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8485:                           if (@blockers > 0) {
 8486:                               $thisallowed = 'B';
 8487:                           } else {
 8488:                               $thisallowed.=$value;
 8489:                           }
 8490:                       }
 8491:                   } else {
 8492:                       $thisallowed.=$value;
 8493:                   }
 8494:                   $uri=$refuri;
 8495:                   $statecond=$refstatecond;
 8496:               }
 8497:           }
 8498:         }
 8499:        }
 8500:    }
 8501: 
 8502: #
 8503: # Gathered now: all privileges that could apply, and condition number
 8504: # 
 8505: #
 8506: # Full or no access?
 8507: #
 8508: 
 8509:     if ($thisallowed=~/F/) {
 8510: 	return 'F';
 8511:     }
 8512: 
 8513:     unless ($thisallowed) {
 8514:         return '';
 8515:     }
 8516: 
 8517: # Restrictions exist, deal with them
 8518: #
 8519: #   C:according to course preferences
 8520: #   R:according to resource settings
 8521: #   L:unless locked
 8522: #   X:according to user session state
 8523: #
 8524: 
 8525: # Possibly locked functionality, check all courses
 8526: # Locks might take effect only after 10 minutes cache expiration for other
 8527: # courses, and 2 minutes for current course
 8528: 
 8529:     my $envkey;
 8530:     if ($thisallowed=~/L/) {
 8531:         foreach $envkey (keys(%env)) {
 8532:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8533:                my $courseid=$2;
 8534:                my $roleid=$1.'.'.$2;
 8535:                $courseid=~s/^\///;
 8536:                my $expiretime=600;
 8537:                if ($env{'request.role'} eq $roleid) {
 8538: 		  $expiretime=120;
 8539:                }
 8540: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8541:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8542:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8543: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8544:                }
 8545:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8546:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8547: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8548:                        &log($env{'user.domain'},$env{'user.name'},
 8549:                             $env{'user.home'},
 8550:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8551:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8552:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8553: 		       return '';
 8554:                    }
 8555:                }
 8556:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8557:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8558: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 8559:                        &log($env{'user.domain'},$env{'user.name'},
 8560:                             $env{'user.home'},
 8561:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8562:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8563:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8564: 		       return '';
 8565:                    }
 8566:                }
 8567: 	   }
 8568:        }
 8569:     }
 8570: 
 8571: #
 8572: # Rest of the restrictions depend on selected course
 8573: #
 8574: 
 8575:     unless ($env{'request.course.id'}) {
 8576: 	if ($thisallowed eq 'A') {
 8577: 	    return 'A';
 8578:         } elsif ($thisallowed eq 'B') {
 8579:             return 'B';
 8580: 	} else {
 8581: 	    return '1';
 8582: 	}
 8583:     }
 8584: 
 8585: #
 8586: # Now user is definitely in a course
 8587: #
 8588: 
 8589: 
 8590: # Course preferences
 8591: 
 8592:    if ($thisallowed=~/C/) {
 8593:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8594:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8595:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8596: 	   =~/\Q$rolecode\E/) {
 8597: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8598: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8599: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8600: 			$env{'request.course.id'});
 8601: 	   }
 8602:            return '';
 8603:        }
 8604: 
 8605:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8606: 	   =~/\Q$unamedom\E/) {
 8607: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8608: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8609: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8610: 			$env{'request.course.id'});
 8611: 	   }
 8612:            return '';
 8613:        }
 8614:    }
 8615: 
 8616: # Resource preferences
 8617: 
 8618:    if ($thisallowed=~/R/) {
 8619:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8620:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8621: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8622: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8623: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8624: 	   }
 8625: 	   return '';
 8626:        }
 8627:    }
 8628: 
 8629: # Restricted by state or randomout?
 8630: 
 8631:    if ($thisallowed=~/X/) {
 8632:       if ($env{'acc.randomout'}) {
 8633: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8634:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8635:             return ''; 
 8636:          }
 8637:       }
 8638:       if (&condval($statecond)) {
 8639: 	 return '2';
 8640:       } else {
 8641:          return '';
 8642:       }
 8643:    }
 8644: 
 8645:     if ($thisallowed eq 'A') {
 8646: 	return 'A';
 8647:     } elsif ($thisallowed eq 'B') {
 8648:         return 'B';
 8649:     } elsif ($thisallowed eq 'D') {
 8650:         return 'D';
 8651:     }
 8652:    return 'F';
 8653: }
 8654: 
 8655: # ------------------------------------------- Check construction space access
 8656: 
 8657: sub constructaccess {
 8658:     my ($url,$setpriv)=@_;
 8659: 
 8660: # We do not allow editing of previous versions of files
 8661:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8662: 
 8663: # Get username and domain from URL
 8664:     my ($ownername,$ownerdomain,$ownerhome);
 8665: 
 8666:     ($ownerdomain,$ownername) =
 8667:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8668: 
 8669: # The URL does not really point to any authorspace, forget it
 8670:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8671: 
 8672: # Now we need to see if the user has access to the authorspace of
 8673: # $ownername at $ownerdomain
 8674: 
 8675:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8676: # Real author for this?
 8677:        $ownerhome = $env{'user.home'};
 8678:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8679:           return ($ownername,$ownerdomain,$ownerhome);
 8680:        }
 8681:     } else {
 8682: # Co-author for this?
 8683:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8684:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8685:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8686:             return ($ownername,$ownerdomain,$ownerhome);
 8687:         }
 8688:         if ($env{'request.course.id'}) {
 8689:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8690:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8691:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8692:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8693:                     return ($ownername,$ownerdomain,$ownerhome);
 8694:                 }
 8695:             }
 8696:         }
 8697:     }
 8698: 
 8699: # We don't have any access right now. If we are not possibly going to do anything about this,
 8700: # we might as well leave
 8701:    unless ($setpriv) { return ''; }
 8702: 
 8703: # Backdoor access?
 8704:     my $allowed=&allowed('eco',$ownerdomain);
 8705: # Nope
 8706:     unless ($allowed) { return ''; }
 8707: # Looks like we may have access, but could be locked by the owner of the construction space
 8708:     if ($allowed eq 'U') {
 8709:         my %blocked=&get('environment',['domcoord.author'],
 8710:                          $ownerdomain,$ownername);
 8711: # Is blocked by owner
 8712:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8713:     }
 8714:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8715: # Grant temporary access
 8716:         my $then=$env{'user.login.time'};
 8717:         my $update=$env{'user.update.time'};
 8718:         if (!$update) { $update = $then; }
 8719:         my $refresh=$env{'user.refresh.time'};
 8720:         if (!$refresh) { $refresh = $update; }
 8721:         my $now = time;
 8722:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8723:                            $now,'ca','constructaccess');
 8724:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8725:         return($ownername,$ownerdomain,$ownerhome);
 8726:     }
 8727: # No business here
 8728:     return '';
 8729: }
 8730: 
 8731: # ----------------------------------------------------------- Content Blocking
 8732: 
 8733: {
 8734: # Caches for faster Course Contents display where content blocking
 8735: # is in operation (i.e., interval param set) for timed quiz.
 8736: #
 8737: # User for whom data are being temporarily cached.
 8738: my $cacheduser='';
 8739: # Course for which data are being temporarily cached.
 8740: my $cachedcid='';
 8741: # Cached blockers for this user (a hash of blocking items). 
 8742: my %cachedblockers=();
 8743: # When the data were last cached.
 8744: my $cachedlast='';
 8745: 
 8746: sub load_all_blockers {
 8747:     my ($uname,$udom)=@_;
 8748:     if (($uname ne '') && ($udom ne '')) { 
 8749:         if (($cacheduser eq $uname.':'.$udom) &&
 8750:             ($cachedcid eq $env{'request.course.id'}) &&
 8751:             (abs($cachedlast-time)<5)) {
 8752:             return;
 8753:         }
 8754:     }
 8755:     $cachedlast=time;
 8756:     $cacheduser=$uname.':'.$udom;
 8757:     $cachedcid=$env{'request.course.id'};
 8758:     %cachedblockers = &get_commblock_resources();
 8759:     return;
 8760: }
 8761: 
 8762: sub get_comm_blocks {
 8763:     my ($cdom,$cnum) = @_;
 8764:     if ($cdom eq '' || $cnum eq '') {
 8765:         return unless ($env{'request.course.id'});
 8766:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8767:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8768:     }
 8769:     my %commblocks;
 8770:     my $hashid=$cdom.'_'.$cnum;
 8771:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8772:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8773:         %commblocks = %{$blocksref};
 8774:     } else {
 8775:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8776:         my $cachetime = 600;
 8777:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8778:     }
 8779:     return %commblocks;
 8780: }
 8781: 
 8782: sub get_commblock_resources {
 8783:     my ($blocks) = @_;
 8784:     my %blockers = ();
 8785:     return %blockers unless ($env{'request.course.id'});
 8786:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8787:     my %commblocks;
 8788:     if (ref($blocks) eq 'HASH') {
 8789:         %commblocks = %{$blocks};
 8790:     } else {
 8791:         %commblocks = &get_comm_blocks();
 8792:     }
 8793:     return %blockers unless (keys(%commblocks) > 0); 
 8794:     my $navmap = Apache::lonnavmaps::navmap->new();
 8795:     return %blockers unless (ref($navmap));
 8796:     my $now = time;
 8797:     foreach my $block (keys(%commblocks)) {
 8798:         if ($block =~ /^(\d+)____(\d+)$/) {
 8799:             my ($start,$end) = ($1,$2);
 8800:             if ($start <= $now && $end >= $now) {
 8801:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8802:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8803:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8804:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8805:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8806:                             }
 8807:                         }
 8808:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8809:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8810:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8811:                             }
 8812:                         }
 8813:                     }
 8814:                 }
 8815:             }
 8816:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8817:             my $item = $1;
 8818:             my @to_test;
 8819:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8820:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8821:                     my @interval;
 8822:                     my $type = 'map';
 8823:                     if ($item eq 'course') {
 8824:                         $type = 'course';
 8825:                         @interval=&EXT("resource.0.interval");
 8826:                     } else {
 8827:                         if ($item =~ /___\d+___/) {
 8828:                             $type = 'resource';
 8829:                             @interval=&EXT("resource.0.interval",$item);
 8830:                             if (ref($navmap)) {                        
 8831:                                 my $res = $navmap->getBySymb($item); 
 8832:                                 push(@to_test,$res);
 8833:                             }
 8834:                         } else {
 8835:                             my $mapsymb = &symbread($item,1);
 8836:                             if ($mapsymb) {
 8837:                                 if (ref($navmap)) {
 8838:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8839:                                     if (ref($mapres)) {
 8840:                                         my $first = $mapres->map_start();
 8841:                                         my $finish = $mapres->map_finish();
 8842:                                         my $it = $navmap->getIterator($first,$finish,undef,0,0);
 8843:                                         if (ref($it)) {
 8844:                                             my $res;
 8845:                                             while ($res = $it->next(undef,1)) {
 8846:                                                 next unless (ref($res));
 8847:                                                 my $symb = $res->symb();
 8848:                                                 next if (($symb eq $mapsymb) || ($symb eq ''));
 8849:                                                 @interval=&EXT("resource.0.interval",$symb);
 8850:                                                 if ($interval[1] eq 'map') {
 8851:                                                     if ($res->answerable()) {
 8852:                                                         push(@to_test,$res);
 8853:                                                         last;
 8854:                                                     }
 8855:                                                 }
 8856:                                             }
 8857:                                         }
 8858:                                     }
 8859:                                 }
 8860:                             }
 8861:                         }
 8862:                     }
 8863:                     if ($interval[0] =~ /^(\d+)/) {
 8864:                         my $timelimit = $1; 
 8865:                         my $first_access;
 8866:                         if ($type eq 'resource') {
 8867:                             $first_access=&get_first_access($interval[1],$item);
 8868:                         } elsif ($type eq 'map') {
 8869:                             $first_access=&get_first_access($interval[1],undef,$item);
 8870:                         } else {
 8871:                             $first_access=&get_first_access($interval[1]);
 8872:                         }
 8873:                         if ($first_access) {
 8874:                             my $timesup = $first_access+$timelimit;
 8875:                             if ($timesup > $now) {
 8876:                                 my $activeblock;
 8877:                                 foreach my $res (@to_test) {
 8878:                                     if ($res->answerable()) {
 8879:                                         $activeblock = 1;
 8880:                                         last;
 8881:                                     }
 8882:                                 }
 8883:                                 if ($activeblock) {
 8884:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8885:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8886:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8887:                                          }
 8888:                                     }
 8889:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8890:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8891:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8892:                                         }
 8893:                                     }
 8894:                                 }
 8895:                             }
 8896:                         }
 8897:                     }
 8898:                 }
 8899:             }
 8900:         }
 8901:     }
 8902:     return %blockers;
 8903: }
 8904: 
 8905: sub has_comm_blocking {
 8906:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 8907:     my @blockers;
 8908:     return unless ($env{'request.course.id'});
 8909:     return unless ($priv eq 'bre');
 8910:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8911:     return if ($env{'request.state'} eq 'construct');
 8912:     my %blockinfo;
 8913:     if (ref($blocks) eq 'HASH') {
 8914:         %blockinfo = &get_commblock_resources($blocks);
 8915:     } else {
 8916:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 8917:         %blockinfo = %cachedblockers;
 8918:     }
 8919:     return unless (keys(%blockinfo) > 0);
 8920:     my (%possibles,@symbs);
 8921:     if (!$symb) {
 8922:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 8923:     }
 8924:     if ($symb) {
 8925:         @symbs = ($symb);
 8926:     } elsif (keys(%possibles)) { 
 8927:         @symbs = keys(%possibles);
 8928:     }
 8929:     my $noblock;
 8930:     foreach my $symb (@symbs) {
 8931:         last if ($noblock);
 8932:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8933:         foreach my $block (keys(%blockinfo)) {
 8934:             if ($block =~ /^firstaccess____(.+)$/) {
 8935:                 my $item = $1;
 8936:                 unless ($blocked) {
 8937:                     if (($item eq $map) || ($item eq $symb)) {
 8938:                         $noblock = 1;
 8939:                         last;
 8940:                     }
 8941:                 }
 8942:             }
 8943:             if (ref($blockinfo{$block}) eq 'HASH') {
 8944:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 8945:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 8946:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8947:                             push(@blockers,$block);
 8948:                         }
 8949:                     }
 8950:                 }
 8951:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 8952:                     if ($blockinfo{$block}{'maps'}{$map}) {
 8953:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8954:                             push(@blockers,$block);
 8955:                         }
 8956:                     }
 8957:                 }
 8958:             }
 8959:         }
 8960:     }
 8961:     unless ($noblock) { 
 8962:         return @blockers;
 8963:     }
 8964:     return;
 8965: }
 8966: }
 8967: 
 8968: sub deeplink_check {
 8969:     my ($priv,$symb,$uri) = @_;
 8970:     return unless ($env{'request.course.id'});
 8971:     return unless ($priv eq 'bre');
 8972:     return if ($env{'request.state'} eq 'construct');
 8973:     return if ($env{'request.role.adv'});
 8974:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8975:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8976:     my (%possibles,@symbs);
 8977:     if (!$symb) {
 8978:         $symb = &symbread($uri,1,1,1,\%possibles);
 8979:     }
 8980:     if ($symb) {
 8981:         @symbs = ($symb);
 8982:     } elsif (keys(%possibles)) {
 8983:         @symbs = keys(%possibles);
 8984:     }
 8985: 
 8986:     my ($login,$switchrole,$allow);
 8987:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
 8988:         my $key = $1;
 8989:         my $tinyurl;
 8990:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
 8991:         if (defined($cached)) {
 8992:              $tinyurl = $result;
 8993:         } else {
 8994:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
 8995:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
 8996:              if ($currtiny{$key} ne '') {
 8997:                  $tinyurl = $currtiny{$key};
 8998:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
 8999:              }
 9000:         }
 9001:         if ($tinyurl ne '') {
 9002:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
 9003:             if ($cnumreq eq $cnum) {
 9004:                 $login = $posslogin;
 9005:             } else {
 9006:                 $switchrole = 1;
 9007:             }
 9008:         }
 9009:     }
 9010:     foreach my $symb (@symbs) {
 9011:         last if ($allow);
 9012:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9013:         if ($deeplink eq '') {
 9014:             $allow = 1;
 9015:         } else {
 9016:             my ($listed,$scope,$access) = split(/,/,$deeplink);
 9017:             if ($access eq 'any') {
 9018:                 $allow = 1;
 9019:             } elsif ($login) {
 9020:                 if ($access eq 'only') {
 9021:                     if ($scope eq 'res') {
 9022:                         if ($symb eq $login) {
 9023:                             $allow = 1;
 9024:                         }
 9025:                     } elsif ($scope eq 'map') {
 9026: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
 9027:                     } elsif ($scope eq 'rec') {
 9028: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
 9029:                     }
 9030:                 } else {
 9031:                     my ($acctype,$item) = split(/:/,$access);
 9032:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
 9033:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
 9034:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 9035:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
 9036:                                 $allow = 1;
 9037:                             }
 9038:                         }
 9039:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9040:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9041:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 9042:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
 9043:                                 $allow = 1;
 9044:                             }
 9045:                         }
 9046:                     }
 9047:                 }
 9048:             }
 9049:         }
 9050:     }
 9051:     return if ($allow);
 9052:     return 1;
 9053: }
 9054: 
 9055: # -------------------------------- Deversion and split uri into path an filename   
 9056: 
 9057: #
 9058: #   Removes the version from a URI and
 9059: #   splits it in to its filename and path to the filename.
 9060: #   Seems like File::Basename could have done this more clearly.
 9061: #   Parameters:
 9062: #      $uri   - input URI
 9063: #   Returns:
 9064: #     Two element list consisting of 
 9065: #     $pathname  - the URI up to and excluding the trailing /
 9066: #     $filename  - The part of the URI following the last /
 9067: #  NOTE:
 9068: #    Another realization of this is simply:
 9069: #    use File::Basename;
 9070: #    ...
 9071: #    $uri = shift;
 9072: #    $filename = basename($uri);
 9073: #    $path     = dirname($uri);
 9074: #    return ($filename, $path);
 9075: #
 9076: #     The implementation below is probably faster however.
 9077: #
 9078: sub split_uri_for_cond {
 9079:     my $uri=&deversion(&declutter(shift));
 9080:     my @uriparts=split(/\//,$uri);
 9081:     my $filename=pop(@uriparts);
 9082:     my $pathname=join('/',@uriparts);
 9083:     return ($pathname,$filename);
 9084: }
 9085: # --------------------------------------------------- Is a resource on the map?
 9086: 
 9087: sub is_on_map {
 9088:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9089:     #Trying to find the conditional for the file
 9090:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9091: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9092:     if ($match) {
 9093: 	return (1,$1);
 9094:     } else {
 9095: 	return (0,0);
 9096:     }
 9097: }
 9098: 
 9099: # --------------------------------------------------------- Get symb from alias
 9100: 
 9101: sub get_symb_from_alias {
 9102:     my $symb=shift;
 9103:     my ($map,$resid,$url)=&decode_symb($symb);
 9104: # Already is a symb
 9105:     if ($url) { return $symb; }
 9106: # Must be an alias
 9107:     my $aliassymb='';
 9108:     my %bighash;
 9109:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9110:                             &GDBM_READER(),0640)) {
 9111:         my $rid=$bighash{'mapalias_'.$symb};
 9112: 	if ($rid) {
 9113: 	    my ($mapid,$resid)=split(/\./,$rid);
 9114: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9115: 				    $resid,$bighash{'src_'.$rid});
 9116: 	}
 9117:         untie %bighash;
 9118:     }
 9119:     return $aliassymb;
 9120: }
 9121: 
 9122: # ----------------------------------------------------------------- Define Role
 9123: 
 9124: sub definerole {
 9125:   if (allowed('mcr','/')) {
 9126:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9127:     foreach my $role (split(':',$sysrole)) {
 9128: 	my ($crole,$cqual)=split(/\&/,$role);
 9129:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9130:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9131: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9132:                return "refused:s:$crole&$cqual"; 
 9133:             }
 9134:         }
 9135:     }
 9136:     foreach my $role (split(':',$domrole)) {
 9137: 	my ($crole,$cqual)=split(/\&/,$role);
 9138:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9139:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9140: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9141:                return "refused:d:$crole&$cqual"; 
 9142:             }
 9143:         }
 9144:     }
 9145:     foreach my $role (split(':',$courole)) {
 9146: 	my ($crole,$cqual)=split(/\&/,$role);
 9147:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9148:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9149: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9150:                return "refused:c:$crole&$cqual"; 
 9151:             }
 9152:         }
 9153:     }
 9154:     my $uhome;
 9155:     if (($uname ne '') && ($udom ne '')) {
 9156:         $uhome = &homeserver($uname,$udom);
 9157:         return $uhome if ($uhome eq 'no_host');
 9158:     } else {
 9159:         $uname = $env{'user.name'};
 9160:         $udom = $env{'user.domain'};
 9161:         $uhome = $env{'user.home'};
 9162:     }
 9163:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9164:                 "$udom:$uname:rolesdef_$rolename=".
 9165:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9166:     return reply($command,$uhome);
 9167:   } else {
 9168:     return 'refused';
 9169:   }
 9170: }
 9171: 
 9172: # ---------------- Make a metadata query against the network of library servers
 9173: 
 9174: sub metadata_query {
 9175:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9176:     my %rhash;
 9177:     my %libserv = &all_library();
 9178:     my @server_list = (defined($server_array) ? @$server_array
 9179:                                               : keys(%libserv) );
 9180:     for my $server (@server_list) {
 9181:         my $domains = ''; 
 9182:         if (ref($domains_hash) eq 'HASH') {
 9183:             $domains = $domains_hash->{$server}; 
 9184:         }
 9185: 	unless ($custom or $customshow) {
 9186: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9187: 	    $rhash{$server}=$reply;
 9188: 	}
 9189: 	else {
 9190: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9191: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9192: 			     $server);
 9193: 	    $rhash{$server}=$reply;
 9194: 	}
 9195:     }
 9196:     return \%rhash;
 9197: }
 9198: 
 9199: # ----------------------------------------- Send log queries and wait for reply
 9200: 
 9201: sub log_query {
 9202:     my ($uname,$udom,$query,%filters)=@_;
 9203:     my $uhome=&homeserver($uname,$udom);
 9204:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9205:     my $uhost=&hostname($uhome);
 9206:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9207:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9208:                        $uhome);
 9209:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9210:     return get_query_reply($queryid);
 9211: }
 9212: 
 9213: # -------------------------- Update MySQL table for portfolio file
 9214: 
 9215: sub update_portfolio_table {
 9216:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9217:     if ($group ne '') {
 9218:         $file_name =~s /^\Q$group\E//;
 9219:     }
 9220:     my $homeserver = &homeserver($uname,$udom);
 9221:     my $queryid=
 9222:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9223:                ':'.&escape($file_name).':'.$action,$homeserver);
 9224:     my $reply = &get_query_reply($queryid);
 9225:     return $reply;
 9226: }
 9227: 
 9228: # -------------------------- Update MySQL allusers table
 9229: 
 9230: sub update_allusers_table {
 9231:     my ($uname,$udom,$names) = @_;
 9232:     my $homeserver = &homeserver($uname,$udom);
 9233:     my $queryid=
 9234:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9235:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9236:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9237:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9238:                'generation='.&escape($names->{'generation'}).'%%'.
 9239:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9240:                'id='.&escape($names->{'id'}),$homeserver);
 9241:     return;
 9242: }
 9243: 
 9244: # ------- Request retrieval of institutional classlists for course(s)
 9245: 
 9246: sub fetch_enrollment_query {
 9247:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9248:     my ($homeserver,$sleep,$loopmax);
 9249:     my $maxtries = 1;
 9250:     if ($context eq 'automated') {
 9251:         $homeserver = $perlvar{'lonHostID'};
 9252:         $sleep = 2;
 9253:         $loopmax = 100;
 9254:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9255:     } else {
 9256:         $homeserver = &homeserver($cnum,$dom);
 9257:     }
 9258:     my $host=&hostname($homeserver);
 9259:     my $cmd = '';
 9260:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9261:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9262:     }
 9263:     $cmd =~ s/%%$//;
 9264:     $cmd = &escape($cmd);
 9265:     my $query = 'fetchenrollment';
 9266:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9267:     unless ($queryid=~/^\Q$host\E\_/) { 
 9268:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9269:         return 'error: '.$queryid;
 9270:     }
 9271:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9272:     my $tries = 1;
 9273:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9274:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9275:         $tries ++;
 9276:     }
 9277:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9278:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9279:     } else {
 9280:         my @responses = split(/:/,$reply);
 9281:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9282:             foreach my $line (@responses) {
 9283:                 my ($key,$value) = split(/=/,$line,2);
 9284:                 $$replyref{$key} = $value;
 9285:             }
 9286:         } else {
 9287:             my $pathname = LONCAPA::tempdir();
 9288:             foreach my $line (@responses) {
 9289:                 my ($key,$value) = split(/=/,$line);
 9290:                 $$replyref{$key} = $value;
 9291:                 if ($value > 0) {
 9292:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9293:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9294:                         my $destname = $pathname.'/'.$filename;
 9295:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9296:                         if ($xml_classlist =~ /^error/) {
 9297:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9298:                         } else {
 9299:                             if ( open(FILE,">",$destname) ) {
 9300:                                 print FILE &unescape($xml_classlist);
 9301:                                 close(FILE);
 9302:                             } else {
 9303:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9304:                             }
 9305:                         }
 9306:                     }
 9307:                 }
 9308:             }
 9309:         }
 9310:         return 'ok';
 9311:     }
 9312:     return 'error';
 9313: }
 9314: 
 9315: sub get_query_reply {
 9316:     my ($queryid,$sleep,$loopmax) = @_;;
 9317:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9318:         $sleep = 0.2;
 9319:     }
 9320:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9321:         $loopmax = 100;
 9322:     }
 9323:     my $replyfile=LONCAPA::tempdir().$queryid;
 9324:     my $reply='';
 9325:     for (1..$loopmax) {
 9326: 	sleep($sleep);
 9327:         if (-e $replyfile.'.end') {
 9328: 	    if (open(my $fh,"<",$replyfile)) {
 9329: 		$reply = join('',<$fh>);
 9330: 		close($fh);
 9331: 	   } else { return 'error: reply_file_error'; }
 9332:            return &unescape($reply);
 9333: 	}
 9334:     }
 9335:     return 'timeout:'.$queryid;
 9336: }
 9337: 
 9338: sub courselog_query {
 9339: #
 9340: # possible filters:
 9341: # url: url or symb
 9342: # username
 9343: # domain
 9344: # action: view, submit, grade
 9345: # start: timestamp
 9346: # end: timestamp
 9347: #
 9348:     my (%filters)=@_;
 9349:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9350:     if ($filters{'url'}) {
 9351: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9352:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9353:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9354:     }
 9355:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9356:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9357:     return &log_query($cname,$cdom,'courselog',%filters);
 9358: }
 9359: 
 9360: sub userlog_query {
 9361: #
 9362: # possible filters:
 9363: # action: log check role
 9364: # start: timestamp
 9365: # end: timestamp
 9366: #
 9367:     my ($uname,$udom,%filters)=@_;
 9368:     return &log_query($uname,$udom,'userlog',%filters);
 9369: }
 9370: 
 9371: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9372: 
 9373: sub auto_run {
 9374:     my ($cnum,$cdom) = @_;
 9375:     my $response = 0;
 9376:     my $settings;
 9377:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9378:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9379:         $settings = $domconfig{'autoenroll'};
 9380:         if ($settings->{'run'} eq '1') {
 9381:             $response = 1;
 9382:         }
 9383:     } else {
 9384:         my $homeserver;
 9385:         if (&is_course($cdom,$cnum)) {
 9386:             $homeserver = &homeserver($cnum,$cdom);
 9387:         } else {
 9388:             $homeserver = &domain($cdom,'primary');
 9389:         }
 9390:         if ($homeserver ne 'no_host') {
 9391:             $response = &reply('autorun:'.$cdom,$homeserver);
 9392:         }
 9393:     }
 9394:     return $response;
 9395: }
 9396: 
 9397: sub auto_get_sections {
 9398:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9399:     my $homeserver;
 9400:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9401:         $homeserver = &homeserver($cnum,$cdom);
 9402:     }
 9403:     if (!defined($homeserver)) { 
 9404:         if ($cdom =~ /^$match_domain$/) {
 9405:             $homeserver = &domain($cdom,'primary');
 9406:         }
 9407:     }
 9408:     my @secs;
 9409:     if (defined($homeserver)) {
 9410:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9411:         unless ($response eq 'refused') {
 9412:             @secs = split(/:/,$response);
 9413:         }
 9414:     }
 9415:     return @secs;
 9416: }
 9417: 
 9418: sub auto_new_course {
 9419:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9420:     my $homeserver = &homeserver($cnum,$cdom);
 9421:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9422:     return $response;
 9423: }
 9424: 
 9425: sub auto_validate_courseID {
 9426:     my ($cnum,$cdom,$inst_course_id) = @_;
 9427:     my $homeserver = &homeserver($cnum,$cdom);
 9428:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9429:     return $response;
 9430: }
 9431: 
 9432: sub auto_validate_instcode {
 9433:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9434:     my ($homeserver,$response);
 9435:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9436:         $homeserver = &homeserver($cnum,$cdom);
 9437:     }
 9438:     if (!defined($homeserver)) {
 9439:         if ($cdom =~ /^$match_domain$/) {
 9440:             $homeserver = &domain($cdom,'primary');
 9441:         }
 9442:     }
 9443:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9444:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9445:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9446:     return ($outcome,$description,$defaultcredits);
 9447: }
 9448: 
 9449: sub auto_validate_inst_crosslist {
 9450:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
 9451:     my ($homeserver,$response);
 9452:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9453:         $homeserver = &homeserver($cnum,$cdom);
 9454:     }
 9455:     if (!defined($homeserver)) {
 9456:         if ($cdom =~ /^$match_domain$/) {
 9457:             $homeserver = &domain($cdom,'primary');
 9458:         }
 9459:     }
 9460:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
 9461:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
 9462:                          &escape($instcode).':'.&escape($inst_xlist).':'.
 9463:                          &escape($coowner),$homeserver);
 9464:     }
 9465:     return $response;
 9466: }
 9467: 
 9468: sub auto_create_password {
 9469:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9470:     my ($homeserver,$response);
 9471:     my $create_passwd = 0;
 9472:     my $authchk = '';
 9473:     if ($udom =~ /^$match_domain$/) {
 9474:         $homeserver = &domain($udom,'primary');
 9475:     }
 9476:     if ($homeserver eq '') {
 9477:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9478:             $homeserver = &homeserver($cnum,$cdom);
 9479:         }
 9480:     }
 9481:     if ($homeserver eq '') {
 9482:         $authchk = 'nodomain';
 9483:     } else {
 9484:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9485:         if ($response eq 'refused') {
 9486:             $authchk = 'refused';
 9487:         } else {
 9488:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9489:         }
 9490:     }
 9491:     return ($authparam,$create_passwd,$authchk);
 9492: }
 9493: 
 9494: sub auto_photo_permission {
 9495:     my ($cnum,$cdom,$students) = @_;
 9496:     my $homeserver = &homeserver($cnum,$cdom);
 9497:     my ($outcome,$perm_reqd,$conditions) = 
 9498: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9499:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9500: 	return (undef,undef);
 9501:     }
 9502:     return ($outcome,$perm_reqd,$conditions);
 9503: }
 9504: 
 9505: sub auto_checkphotos {
 9506:     my ($uname,$udom,$pid) = @_;
 9507:     my $homeserver = &homeserver($uname,$udom);
 9508:     my ($result,$resulttype);
 9509:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9510: 				   &escape($uname).':'.&escape($pid),
 9511: 				   $homeserver));
 9512:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9513: 	return (undef,undef);
 9514:     }
 9515:     if ($outcome) {
 9516:         ($result,$resulttype) = split(/:/,$outcome);
 9517:     } 
 9518:     return ($result,$resulttype);
 9519: }
 9520: 
 9521: sub auto_photochoice {
 9522:     my ($cnum,$cdom) = @_;
 9523:     my $homeserver = &homeserver($cnum,$cdom);
 9524:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9525: 						       &escape($cdom),
 9526: 						       $homeserver)));
 9527:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9528: 	return (undef,undef);
 9529:     }
 9530:     return ($update,$comment);
 9531: }
 9532: 
 9533: sub auto_photoupdate {
 9534:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9535:     my $homeserver = &homeserver($cnum,$dom);
 9536:     my $host=&hostname($homeserver);
 9537:     my $cmd = '';
 9538:     my $maxtries = 1;
 9539:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9540:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9541:     }
 9542:     $cmd =~ s/%%$//;
 9543:     $cmd = &escape($cmd);
 9544:     my $query = 'institutionalphotos';
 9545:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9546:     unless ($queryid=~/^\Q$host\E\_/) {
 9547:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9548:         return 'error: '.$queryid;
 9549:     }
 9550:     my $reply = &get_query_reply($queryid);
 9551:     my $tries = 1;
 9552:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9553:         $reply = &get_query_reply($queryid);
 9554:         $tries ++;
 9555:     }
 9556:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9557:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9558:     } else {
 9559:         my @responses = split(/:/,$reply);
 9560:         my $outcome = shift(@responses); 
 9561:         foreach my $item (@responses) {
 9562:             my ($key,$value) = split(/=/,$item);
 9563:             $$photo{$key} = $value;
 9564:         }
 9565:         return $outcome;
 9566:     }
 9567:     return 'error';
 9568: }
 9569: 
 9570: sub auto_instcode_format {
 9571:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9572: 	$cat_order) = @_;
 9573:     my $courses = '';
 9574:     my @homeservers;
 9575:     if ($caller eq 'global') {
 9576: 	my %servers = &get_servers($codedom,'library');
 9577: 	foreach my $tryserver (keys(%servers)) {
 9578: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9579: 		push(@homeservers,$tryserver);
 9580: 	    }
 9581:         }
 9582:     } elsif ($caller eq 'requests') {
 9583:         if ($codedom =~ /^$match_domain$/) {
 9584:             my $chome = &domain($codedom,'primary');
 9585:             unless ($chome eq 'no_host') {
 9586:                 push(@homeservers,$chome);
 9587:             }
 9588:         }
 9589:     } else {
 9590:         push(@homeservers,&homeserver($caller,$codedom));
 9591:     }
 9592:     foreach my $code (keys(%{$instcodes})) {
 9593:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9594:     }
 9595:     chop($courses);
 9596:     my $ok_response = 0;
 9597:     my $response;
 9598:     while (@homeservers > 0 && $ok_response == 0) {
 9599:         my $server = shift(@homeservers); 
 9600:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9601:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9602:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9603: 		split(/:/,$response);
 9604:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9605:             push(@{$codetitles},&str2array($codetitles_str));
 9606:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9607:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9608:             $ok_response = 1;
 9609:         }
 9610:     }
 9611:     if ($ok_response) {
 9612:         return 'ok';
 9613:     } else {
 9614:         return $response;
 9615:     }
 9616: }
 9617: 
 9618: sub auto_instcode_defaults {
 9619:     my ($domain,$returnhash,$code_order) = @_;
 9620:     my @homeservers;
 9621: 
 9622:     my %servers = &get_servers($domain,'library');
 9623:     foreach my $tryserver (keys(%servers)) {
 9624: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9625: 	    push(@homeservers,$tryserver);
 9626: 	}
 9627:     }
 9628: 
 9629:     my $response;
 9630:     foreach my $server (@homeservers) {
 9631:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9632:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9633: 	
 9634: 	foreach my $pair (split(/\&/,$response)) {
 9635: 	    my ($name,$value)=split(/\=/,$pair);
 9636: 	    if ($name eq 'code_order') {
 9637: 		@{$code_order} = split(/\&/,&unescape($value));
 9638: 	    } else {
 9639: 		$returnhash->{&unescape($name)}=&unescape($value);
 9640: 	    }
 9641: 	}
 9642: 	return 'ok';
 9643:     }
 9644: 
 9645:     return $response;
 9646: }
 9647: 
 9648: sub auto_possible_instcodes {
 9649:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9650:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9651:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9652:         return;
 9653:     }
 9654:     my (@homeservers,$uhome);
 9655:     if (defined(&domain($domain,'primary'))) {
 9656:         $uhome=&domain($domain,'primary');
 9657:         push(@homeservers,&domain($domain,'primary'));
 9658:     } else {
 9659:         my %servers = &get_servers($domain,'library');
 9660:         foreach my $tryserver (keys(%servers)) {
 9661:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9662:                 push(@homeservers,$tryserver);
 9663:             }
 9664:         }
 9665:     }
 9666:     my $response;
 9667:     foreach my $server (@homeservers) {
 9668:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9669:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9670:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9671:             split(':',$response);
 9672:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9673:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9674:         foreach my $item (split('&',$cat_title)) {   
 9675:             my ($name,$value)=split('=',$item);
 9676:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9677:         }
 9678:         foreach my $item (split('&',$cat_order)) {
 9679:             my ($name,$value)=split('=',$item);
 9680:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9681:         }
 9682:         return 'ok';
 9683:     }
 9684:     return $response;
 9685: }
 9686: 
 9687: sub auto_courserequest_checks {
 9688:     my ($dom) = @_;
 9689:     my ($homeserver,%validations);
 9690:     if ($dom =~ /^$match_domain$/) {
 9691:         $homeserver = &domain($dom,'primary');
 9692:     }
 9693:     unless ($homeserver eq 'no_host') {
 9694:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9695:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9696:             my @items = split(/&/,$response);
 9697:             foreach my $item (@items) {
 9698:                 my ($key,$value) = split('=',$item);
 9699:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9700:             }
 9701:         }
 9702:     }
 9703:     return %validations; 
 9704: }
 9705: 
 9706: sub auto_courserequest_validation {
 9707:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9708:     my ($homeserver,$response);
 9709:     if ($dom =~ /^$match_domain$/) {
 9710:         $homeserver = &domain($dom,'primary');
 9711:     }
 9712:     unless ($homeserver eq 'no_host') {
 9713:         my $customdata;
 9714:         if (ref($custominfo) eq 'HASH') {
 9715:             $customdata = &freeze_escape($custominfo);
 9716:         }
 9717:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9718:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9719:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9720:                                     $customdata,$homeserver));
 9721:     }
 9722:     return $response;
 9723: }
 9724: 
 9725: sub auto_validate_class_sec {
 9726:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9727:     my $homeserver = &homeserver($cnum,$cdom);
 9728:     my $ownerlist;
 9729:     if (ref($owners) eq 'ARRAY') {
 9730:         $ownerlist = join(',',@{$owners});
 9731:     } else {
 9732:         $ownerlist = $owners;
 9733:     }
 9734:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9735:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9736:     return $response;
 9737: }
 9738: 
 9739: sub auto_validate_instclasses {
 9740:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9741:     my ($homeserver,%validations);
 9742:     $homeserver = &homeserver($cnum,$cdom);
 9743:     unless ($homeserver eq 'no_host') {
 9744:         my $ownerlist;
 9745:         if (ref($owners) eq 'ARRAY') {
 9746:             $ownerlist = join(',',@{$owners});
 9747:         } else {
 9748:             $ownerlist = $owners;
 9749:         }
 9750:         if (ref($classesref) eq 'HASH') {
 9751:             my $classes = &freeze_escape($classesref);
 9752:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9753:                                 ':'.$cdom.':'.$classes,$homeserver);
 9754:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9755:                 my @items = split(/&/,$response);
 9756:                 foreach my $item (@items) {
 9757:                     my ($key,$value) = split('=',$item);
 9758:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9759:                 }
 9760:             }
 9761:         }
 9762:     }
 9763:     return %validations;
 9764: }
 9765: 
 9766: sub auto_crsreq_update {
 9767:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9768:         $code,$accessstart,$accessend,$inbound) = @_;
 9769:     my ($homeserver,%crsreqresponse);
 9770:     if ($cdom =~ /^$match_domain$/) {
 9771:         $homeserver = &domain($cdom,'primary');
 9772:     }
 9773:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9774:         my $info;
 9775:         if (ref($inbound) eq 'HASH') {
 9776:             $info = &freeze_escape($inbound);
 9777:         }
 9778:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9779:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9780:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9781:                             &escape($title).':'.&escape($code).':'.
 9782:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9783:                             $homeserver);
 9784:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9785:             my @items = split(/&/,$response);
 9786:             foreach my $item (@items) {
 9787:                 my ($key,$value) = split('=',$item);
 9788:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9789:             }
 9790:         }
 9791:     }
 9792:     return \%crsreqresponse;
 9793: }
 9794: 
 9795: sub auto_export_grades {
 9796:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9797:     my ($homeserver,%exportresponse);
 9798:     if ($cdom =~ /^$match_domain$/) {
 9799:         $homeserver = &domain($cdom,'primary');
 9800:     }
 9801:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9802:         my $info;
 9803:         if (ref($inforef) eq 'HASH') {
 9804:             $info = &freeze_escape($inforef);
 9805:         }
 9806:         if (ref($gradesref) eq 'HASH') {
 9807:             my $grades = &freeze_escape($gradesref);
 9808:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9809:                                 $info.':'.$grades,$homeserver);
 9810:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9811:                 my @items = split(/&/,$response);
 9812:                 foreach my $item (@items) {
 9813:                     my ($key,$value) = split('=',$item);
 9814:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9815:                 }
 9816:             }
 9817:         }
 9818:     }
 9819:     return \%exportresponse;
 9820: }
 9821: 
 9822: sub check_instcode_cloning {
 9823:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9824:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9825:         return;
 9826:     }
 9827:     my $canclone;
 9828:     if (@{$code_order} > 0) {
 9829:         my $instcoderegexp ='^';
 9830:         my @clonecodes = split(/\&/,$cloner);
 9831:         foreach my $item (@{$code_order}) {
 9832:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9833:                 foreach my $pair (@clonecodes) {
 9834:                     my ($key,$val) = split(/\=/,$pair,2);
 9835:                     $val = &unescape($val);
 9836:                     if ($key eq $item) {
 9837:                         $instcoderegexp .= '('.$val.')';
 9838:                         last;
 9839:                     }
 9840:                 }
 9841:             } else {
 9842:                 $instcoderegexp .= $codedefaults->{$item};
 9843:             }
 9844:         }
 9845:         $instcoderegexp .= '$';
 9846:         my (@from,@to);
 9847:         eval {
 9848:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9849:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9850:         };
 9851:         if ((@from > 0) && (@to > 0)) {
 9852:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9853:             if (!@diffs) {
 9854:                 $canclone = 1;
 9855:             }
 9856:         }
 9857:     }
 9858:     return $canclone;
 9859: }
 9860: 
 9861: sub default_instcode_cloning {
 9862:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9863:     my (%codedefaults,@code_order,$canclone);
 9864:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9865:         %codedefaults = %{$codedefaultsref};
 9866:         @code_order = @{$codeorderref};
 9867:     } elsif ($clonedom) {
 9868:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 9869:     }
 9870:     if (($domdefclone) && (@code_order)) {
 9871:         my @clonecodes = split(/\+/,$domdefclone);
 9872:         my $instcoderegexp ='^';
 9873:         foreach my $item (@code_order) {
 9874:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 9875:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 9876:             } else {
 9877:                 $instcoderegexp .= $codedefaults{$item};
 9878:             }
 9879:         }
 9880:         $instcoderegexp .= '$';
 9881:         my (@from,@to);
 9882:         eval {
 9883:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9884:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 9885:         };
 9886:         if ((@from > 0) && (@to > 0)) {
 9887:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9888:             if (!@diffs) {
 9889:                 $canclone = 1;
 9890:             }
 9891:         }
 9892:     }
 9893:     return $canclone;
 9894: }
 9895: 
 9896: # ------------------------------------------------------- Course Group routines
 9897: 
 9898: sub get_coursegroups {
 9899:     my ($cdom,$cnum,$group,$namespace) = @_;
 9900:     return(&dump($namespace,$cdom,$cnum,$group));
 9901: }
 9902: 
 9903: sub modify_coursegroup {
 9904:     my ($cdom,$cnum,$groupsettings) = @_;
 9905:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9906: }
 9907: 
 9908: sub toggle_coursegroup_status {
 9909:     my ($cdom,$cnum,$group,$action) = @_;
 9910:     my ($from_namespace,$to_namespace);
 9911:     if ($action eq 'delete') {
 9912:         $from_namespace = 'coursegroups';
 9913:         $to_namespace = 'deleted_groups';
 9914:     } else {
 9915:         $from_namespace = 'deleted_groups';
 9916:         $to_namespace = 'coursegroups';
 9917:     }
 9918:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 9919:     if (my $tmp = &error(%curr_group)) {
 9920:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 9921:         return ('read error',$tmp);
 9922:     } else {
 9923:         my %savedsettings = %curr_group; 
 9924:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 9925:         my $deloutcome;
 9926:         if ($result eq 'ok') {
 9927:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 9928:         } else {
 9929:             return ('write error',$result);
 9930:         }
 9931:         if ($deloutcome eq 'ok') {
 9932:             return 'ok';
 9933:         } else {
 9934:             return ('delete error',$deloutcome);
 9935:         }
 9936:     }
 9937: }
 9938: 
 9939: sub modify_group_roles {
 9940:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
 9941:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 9942:     my $role = 'gr/'.&escape($userprivs);
 9943:     my ($uname,$udom) = split(/:/,$user);
 9944:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
 9945:     if ($result eq 'ok') {
 9946:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 9947:     }
 9948:     return $result;
 9949: }
 9950: 
 9951: sub modify_coursegroup_membership {
 9952:     my ($cdom,$cnum,$membership) = @_;
 9953:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 9954:     return $result;
 9955: }
 9956: 
 9957: sub get_active_groups {
 9958:     my ($udom,$uname,$cdom,$cnum) = @_;
 9959:     my $now = time;
 9960:     my %groups = ();
 9961:     foreach my $key (keys(%env)) {
 9962:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 9963:             my ($start,$end) = split(/\./,$env{$key});
 9964:             if (($end!=0) && ($end<$now)) { next; }
 9965:             if (($start!=0) && ($start>$now)) { next; }
 9966:             if ($1 eq $cdom && $2 eq $cnum) {
 9967:                 $groups{$3} = $env{$key} ;
 9968:             }
 9969:         }
 9970:     }
 9971:     return %groups;
 9972: }
 9973: 
 9974: sub get_group_membership {
 9975:     my ($cdom,$cnum,$group) = @_;
 9976:     return(&dump('groupmembership',$cdom,$cnum,$group));
 9977: }
 9978: 
 9979: sub get_users_groups {
 9980:     my ($udom,$uname,$courseid) = @_;
 9981:     my @usersgroups;
 9982:     my $cachetime=1800;
 9983: 
 9984:     my $hashid="$udom:$uname:$courseid";
 9985:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 9986:     if (defined($cached)) {
 9987:         @usersgroups = split(/:/,$grouplist);
 9988:     } else {  
 9989:         $grouplist = '';
 9990:         my $courseurl = &courseid_to_courseurl($courseid);
 9991:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 9992:         my $access_end = $env{'course.'.$courseid.
 9993:                               '.default_enrollment_end_date'};
 9994:         my $now = time;
 9995:         foreach my $key (keys(%roleshash)) {
 9996:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 9997:                 my $group = $1;
 9998:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 9999:                     my $start = $2;
10000:                     my $end = $1;
10001:                     if ($start == -1) { next; } # deleted from group
10002:                     if (($start!=0) && ($start>$now)) { next; }
10003:                     if (($end!=0) && ($end<$now)) {
10004:                         if ($access_end && $access_end < $now) {
10005:                             if ($access_end - $end < 86400) {
10006:                                 push(@usersgroups,$group);
10007:                             }
10008:                         }
10009:                         next;
10010:                     }
10011:                     push(@usersgroups,$group);
10012:                 }
10013:             }
10014:         }
10015:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10016:         $grouplist = join(':',@usersgroups);
10017:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10018:     }
10019:     return @usersgroups;
10020: }
10021: 
10022: sub devalidate_getgroups_cache {
10023:     my ($udom,$uname,$cdom,$cnum)=@_;
10024:     my $courseid = $cdom.'_'.$cnum;
10025: 
10026:     my $hashid="$udom:$uname:$courseid";
10027:     &devalidate_cache_new('getgroups',$hashid);
10028: }
10029: 
10030: # ------------------------------------------------------------------ Plain Text
10031: 
10032: sub plaintext {
10033:     my ($short,$type,$cid,$forcedefault) = @_;
10034:     if ($short =~ m{^cr/}) {
10035: 	return (split('/',$short))[-1];
10036:     }
10037:     if (!defined($cid)) {
10038:         $cid = $env{'request.course.id'};
10039:     }
10040:     my %rolenames = (
10041:                       Course    => 'std',
10042:                       Community => 'alt1',
10043:                       Placement => 'std',
10044:                     );
10045:     if ($cid ne '') {
10046:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10047:             unless ($forcedefault) {
10048:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10049:                 &Apache::lonlocal::mt_escape(\$roletext);
10050:                 return &Apache::lonlocal::mt($roletext);
10051:             }
10052:         }
10053:     }
10054:     if ((defined($type)) && (defined($rolenames{$type})) &&
10055:         (defined($rolenames{$type})) && 
10056:         (defined($prp{$short}{$rolenames{$type}}))) {
10057:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10058:     } elsif ($cid ne '') {
10059:         my $crstype = $env{'course.'.$cid.'.type'};
10060:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10061:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10062:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10063:         }
10064:     }
10065:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10066: }
10067: 
10068: # ----------------------------------------------------------------- Assign Role
10069: 
10070: sub assignrole {
10071:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10072:         $context)=@_;
10073:     my $mrole;
10074:     if ($role =~ /^cr\//) {
10075:         my $cwosec=$url;
10076:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10077: 	unless (&allowed('ccr',$cwosec)) {
10078:            my $refused = 1;
10079:            if ($context eq 'requestcourses') {
10080:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10081:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10082:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10083:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10084:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10085:                            if ($crsenv{'internal.courseowner'} eq
10086:                                $env{'user.name'}.':'.$env{'user.domain'}) {
10087:                                $refused = '';
10088:                            }
10089:                        }
10090:                    }
10091:                }
10092:            }
10093:            if ($refused) {
10094:                &logthis('Refused custom assignrole: '.
10095:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10096:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10097:                return 'refused';
10098:            }
10099:         }
10100:         $mrole='cr';
10101:     } elsif ($role =~ /^gr\//) {
10102:         my $cwogrp=$url;
10103:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10104:         unless (&allowed('mdg',$cwogrp)) {
10105:             &logthis('Refused group assignrole: '.
10106:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10107:                     $env{'user.name'}.' at '.$env{'user.domain'});
10108:             return 'refused';
10109:         }
10110:         $mrole='gr';
10111:     } else {
10112:         my $cwosec=$url;
10113:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10114:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10115:             my $refused;
10116:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10117:                 if (!(&allowed('c'.$role,$url))) {
10118:                     $refused = 1;
10119:                 }
10120:             } else {
10121:                 $refused = 1;
10122:             }
10123:             if ($refused) {
10124:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10125:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10126:                     my %crsenv;
10127:                     if ($role eq 'cc' || $role eq 'co') {
10128:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10129:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10130:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10131:                                 if ($crsenv{'internal.courseowner'} eq 
10132:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10133:                                     $refused = '';
10134:                                 }
10135:                             }
10136:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10137:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10138:                                 if ($crsenv{'internal.courseowner'} eq 
10139:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10140:                                     $refused = '';
10141:                                 }
10142:                             }
10143:                         }
10144:                     }
10145:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10146:                     if ($role eq 'st') {
10147:                         $refused = '';
10148:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10149:                         $refused = '';
10150:                     }
10151:                 } elsif ($context eq 'requestcourses') {
10152:                     my @possroles = ('st','ta','ep','in','cc','co');
10153:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10154:                         my $wrongcc;
10155:                         if ($cnum =~ /^$match_community$/) {
10156:                             $wrongcc = 1 if ($role eq 'cc');
10157:                         } else {
10158:                             $wrongcc = 1 if ($role eq 'co');
10159:                         }
10160:                         unless ($wrongcc) {
10161:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10162:                             if ($crsenv{'internal.courseowner'} eq 
10163:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10164:                                 $refused = '';
10165:                             }
10166:                         }
10167:                     }
10168:                 } elsif ($context eq 'requestauthor') {
10169:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10170:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10171:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10172:                             $refused = '';
10173:                         } else {
10174:                             my %domdefaults = &get_domain_defaults($udom);
10175:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10176:                                 my $checkbystatus;
10177:                                 if ($env{'user.adv'}) { 
10178:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10179:                                     if ($disposition eq 'automatic') {
10180:                                         $refused = '';
10181:                                     } elsif ($disposition eq '') {
10182:                                         $checkbystatus = 1;
10183:                                     } 
10184:                                 } else {
10185:                                     $checkbystatus = 1;
10186:                                 }
10187:                                 if ($checkbystatus) {
10188:                                     if ($env{'environment.inststatus'}) {
10189:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10190:                                         foreach my $type (@inststatuses) {
10191:                                             if (($type ne '') &&
10192:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10193:                                                 $refused = '';
10194:                                             }
10195:                                         }
10196:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10197:                                         $refused = '';
10198:                                     }
10199:                                 }
10200:                             }
10201:                         }
10202:                     }
10203:                 }
10204:                 if ($refused) {
10205:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10206:                              ' '.$role.' '.$end.' '.$start.' by '.
10207: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10208:                     return 'refused';
10209:                 }
10210:             }
10211:         } elsif ($role eq 'au') {
10212:             if ($url ne '/'.$udom.'/') {
10213:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10214:                          ' to assign author role for '.$uname.':'.$udom.
10215:                          ' in domain: '.$url.' refused (wrong domain).');
10216:                 return 'refused';
10217:             }
10218:         }
10219:         $mrole=$role;
10220:     }
10221:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10222:                 "$udom:$uname:$url".'_'."$mrole=$role";
10223:     if ($end) { $command.='_'.$end; }
10224:     if ($start) {
10225: 	if ($end) { 
10226:            $command.='_'.$start; 
10227:         } else {
10228:            $command.='_0_'.$start;
10229:         }
10230:     }
10231:     my $origstart = $start;
10232:     my $origend = $end;
10233:     my $delflag;
10234: # actually delete
10235:     if ($deleteflag) {
10236: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10237: # modify command to delete the role
10238:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10239:                 "$udom:$uname:$url".'_'."$mrole";
10240: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10241: # set start and finish to negative values for userrolelog
10242:            $start=-1;
10243:            $end=-1;
10244:            $delflag = 1;
10245:         }
10246:     }
10247: # send command
10248:     my $answer=&reply($command,&homeserver($uname,$udom));
10249: # log new user role if status is ok
10250:     if ($answer eq 'ok') {
10251: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10252:         if (($role eq 'cc') || ($role eq 'in') ||
10253:             ($role eq 'ep') || ($role eq 'ad') ||
10254:             ($role eq 'ta') || ($role eq 'st') ||
10255:             ($role=~/^cr/) || ($role eq 'gr') ||
10256:             ($role eq 'co')) {
10257: # for course roles, perform group memberships changes triggered by role change.
10258:             unless ($role =~ /^gr/) {
10259:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10260:                                                  $origstart,$selfenroll,$context);
10261:             }
10262:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10263:                            $selfenroll,$context);
10264:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10265:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10266:                  ($role eq 'da')) {
10267:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10268:                            $context);
10269:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10270:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10271:                              $context); 
10272:         }
10273:         if ($role eq 'cc') {
10274:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10275:         }
10276:     }
10277:     return $answer;
10278: }
10279: 
10280: sub autoupdate_coowners {
10281:     my ($url,$end,$start,$uname,$udom) = @_;
10282:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10283:     if (($cdom ne '') && ($cnum ne '')) {
10284:         my $now = time;
10285:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10286:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10287:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10288:             my $instcode = $coursehash{'internal.coursecode'};
10289:             my $xlists = $coursehash{'internal.crosslistings'};
10290:             if ($instcode ne '') {
10291:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10292:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10293:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10294:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10295:                         unless ($result eq 'valid') {
10296:                             if ($xlists ne '') {
10297:                                 foreach my $xlist (split(',',$xlists)) {
10298:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
10299:                                     $result =
10300:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10301:                                                                       $inst_crosslist,$uname.':'.$udom);
10302:                                     last if ($result eq 'valid');
10303:                                 }
10304:                             }
10305:                         }
10306:                         if ($result eq 'valid') {
10307:                             if ($coursehash{'internal.co-owners'}) {
10308:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10309:                                     push(@newcoowners,$coowner);
10310:                                 }
10311:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10312:                                     push(@newcoowners,$uname.':'.$udom);
10313:                                 }
10314:                                 @newcoowners = sort(@newcoowners);
10315:                             } else {
10316:                                 push(@newcoowners,$uname.':'.$udom);
10317:                             }
10318:                         } elsif ($coursehash{'internal.co-owners'}) {
10319:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10320:                                 unless ($coowner eq $uname.':'.$udom) {
10321:                                     push(@newcoowners,$coowner);
10322:                                 }
10323:                             }
10324:                             unless (@newcoowners > 0) {
10325:                                 $delcoowners = 1;
10326:                                 $coowners = '';
10327:                             }
10328:                         }
10329:                         if (@newcoowners || $delcoowners) {
10330:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10331:                                             $delcoowners,@newcoowners);
10332:                         }
10333:                     }
10334:                 }
10335:             }
10336:         }
10337:     }
10338: }
10339: 
10340: sub store_coowners {
10341:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10342:     my $cid = $cdom.'_'.$cnum;
10343:     my ($coowners,$delresult,$putresult);
10344:     if (@newcoowners) {
10345:         $coowners = join(',',@newcoowners);
10346:         my %coownershash = (
10347:                             'internal.co-owners' => $coowners,
10348:                            );
10349:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10350:         if ($putresult eq 'ok') {
10351:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10352:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10353:             }
10354:         }
10355:     }
10356:     if ($delcoowners) {
10357:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10358:         if ($delresult eq 'ok') {
10359:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10360:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10361:             }
10362:         }
10363:     }
10364:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10365:         my %crsinfo =
10366:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10367:         if (ref($crsinfo{$cid}) eq 'HASH') {
10368:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10369:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10370:         }
10371:     }
10372: }
10373: 
10374: # -------------------------------------------------- Modify user authentication
10375: # Overrides without validation
10376: 
10377: sub modifyuserauth {
10378:     my ($udom,$uname,$umode,$upass)=@_;
10379:     my $uhome=&homeserver($uname,$udom);
10380:     my $allowed;
10381:     if (&allowed('mau',$udom)) {
10382:         $allowed = 1;
10383:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10384:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10385:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10386:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10387:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10388:         if (($cdom ne '') && ($cnum ne '')) {
10389:             my $is_owner = &is_course_owner($cdom,$cnum);
10390:             if ($is_owner) {
10391:                 $allowed = 1;
10392:             }
10393:         }
10394:     }
10395:     unless ($allowed) { return 'refused'; }
10396:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10397:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10398:              ' in domain '.$env{'request.role.domain'});  
10399:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10400: 		     &escape($upass),$uhome);
10401:     my $ip = &get_requestor_ip();
10402:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10403:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10404:          '(Remote '.$ip.'): '.$reply);
10405:     &log($udom,,$uname,$uhome,
10406:         'Authentication changed by '.$env{'user.domain'}.', '.
10407:                                      $env{'user.name'}.', '.$umode.
10408:          '(Remote '.$ip.'): '.$reply);
10409:     unless ($reply eq 'ok') {
10410:         &logthis('Authentication mode error: '.$reply);
10411: 	return 'error: '.$reply;
10412:     }   
10413:     return 'ok';
10414: }
10415: 
10416: # --------------------------------------------------------------- Modify a user
10417: 
10418: sub modifyuser {
10419:     my ($udom,    $uname, $uid,
10420:         $umode,   $upass, $first,
10421:         $middle,  $last,  $gene,
10422:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10423:     $udom= &LONCAPA::clean_domain($udom);
10424:     $uname=&LONCAPA::clean_username($uname);
10425:     my $showcandelete = 'none';
10426:     if (ref($candelete) eq 'ARRAY') {
10427:         if (@{$candelete} > 0) {
10428:             $showcandelete = join(', ',@{$candelete});
10429:         }
10430:     }
10431:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10432:              $umode.', '.$first.', '.$middle.', '.
10433: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10434:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10435:                                      ' desiredhome not specified'). 
10436:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10437:              ' in domain '.$env{'request.role.domain'});
10438:     my $uhome=&homeserver($uname,$udom,'true');
10439:     my $newuser;
10440:     if ($uhome eq 'no_host') {
10441:         $newuser = 1;
10442:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10443:                 ($umode eq 'lti')) {
10444:             return 'error: more information needed to create new user';
10445:         }
10446:     }
10447: # ----------------------------------------------------------------- Create User
10448:     if (($uhome eq 'no_host') && 
10449: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10450:         my $unhome='';
10451:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10452:             $unhome = $desiredhome;
10453: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10454: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10455:         } else { # load balancing routine for determining $unhome
10456:             my $loadm=10000000;
10457: 	    my %servers = &get_servers($udom,'library');
10458: 	    foreach my $tryserver (keys(%servers)) {
10459: 		my $answer=reply('load',$tryserver);
10460: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10461: 		    $loadm=$answer;
10462: 		    $unhome=$tryserver;
10463: 		}
10464: 	    }
10465:         }
10466:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10467: 	    return 'error: unable to find a home server for '.$uname.
10468:                    ' in domain '.$udom;
10469:         }
10470:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10471:                          &escape($upass),$unhome);
10472: 	unless ($reply eq 'ok') {
10473:             return 'error: '.$reply;
10474:         }   
10475:         $uhome=&homeserver($uname,$udom,'true');
10476:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10477: 	    return 'error: unable verify users home machine.';
10478:         }
10479:     }   # End of creation of new user
10480: # ---------------------------------------------------------------------- Add ID
10481:     if ($uid) {
10482:        $uid=~tr/A-Z/a-z/;
10483:        my %uidhash=&idrget($udom,$uname);
10484:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10485:          && (!$forceid)) {
10486: 	  unless ($uid eq $uidhash{$uname}) {
10487: 	      return 'error: user id "'.$uid.'" does not match '.
10488:                   'current user id "'.$uidhash{$uname}.'".';
10489:           }
10490:        } else {
10491: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10492:        }
10493:     }
10494: # -------------------------------------------------------------- Add names, etc
10495:     my @tmp=&get('environment',
10496: 		   ['firstname','middlename','lastname','generation','id',
10497:                     'permanentemail','inststatus'],
10498: 		   $udom,$uname);
10499:     my (%names,%oldnames);
10500:     if ($tmp[0] =~ m/^error:.*/) { 
10501:         %names=(); 
10502:     } else {
10503:         %names = @tmp;
10504:         %oldnames = %names;
10505:     }
10506: #
10507: # If name, email and/or uid are blank (e.g., because an uploaded file
10508: # of users did not contain them), do not overwrite existing values
10509: # unless field is in $candelete array ref.  
10510: #
10511: 
10512:     my @fields = ('firstname','middlename','lastname','generation',
10513:                   'permanentemail','id');
10514:     my %newvalues;
10515:     if (ref($candelete) eq 'ARRAY') {
10516:         foreach my $field (@fields) {
10517:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10518:                 if ($field eq 'firstname') {
10519:                     $names{$field} = $first;
10520:                 } elsif ($field eq 'middlename') {
10521:                     $names{$field} = $middle;
10522:                 } elsif ($field eq 'lastname') {
10523:                     $names{$field} = $last;
10524:                 } elsif ($field eq 'generation') { 
10525:                     $names{$field} = $gene;
10526:                 } elsif ($field eq 'permanentemail') {
10527:                     $names{$field} = $email;
10528:                 } elsif ($field eq 'id') {
10529:                     $names{$field}  = $uid;
10530:                 }
10531:             }
10532:         }
10533:     }
10534:     if ($first)  { $names{'firstname'}  = $first; }
10535:     if (defined($middle)) { $names{'middlename'} = $middle; }
10536:     if ($last)   { $names{'lastname'}   = $last; }
10537:     if (defined($gene))   { $names{'generation'} = $gene; }
10538:     if ($email) {
10539:        $email=~s/[^\w\@\.\-\,]//gs;
10540:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10541:     }
10542:     if ($uid) { $names{'id'}  = $uid; }
10543:     if (defined($inststatus)) {
10544:         $names{'inststatus'} = '';
10545:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10546:         if (ref($usertypes) eq 'HASH') {
10547:             my @okstatuses; 
10548:             foreach my $item (split(/:/,$inststatus)) {
10549:                 if (defined($usertypes->{$item})) {
10550:                     push(@okstatuses,$item);  
10551:                 }
10552:             }
10553:             if (@okstatuses) {
10554:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10555:             }
10556:         }
10557:     }
10558:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10559:                  $umode.', '.$first.', '.$middle.', '.
10560:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10561:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10562:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10563:     } else {
10564:         $logmsg .= ' during self creation';
10565:     }
10566:     my $changed;
10567:     if ($newuser) {
10568:         $changed = 1;
10569:     } else {
10570:         foreach my $field (@fields) {
10571:             if ($names{$field} ne $oldnames{$field}) {
10572:                 $changed = 1;
10573:                 last;
10574:             }
10575:         }
10576:     }
10577:     unless ($changed) {
10578:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10579:         &logthis($logmsg);
10580:         return 'ok';
10581:     }
10582:     my $reply = &put('environment', \%names, $udom,$uname);
10583:     if ($reply ne 'ok') { 
10584:         return 'error: '.$reply;
10585:     }
10586:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10587:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10588:     }
10589:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10590:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10591:     $logmsg = 'Success modifying user '.$logmsg;
10592:     &logthis($logmsg);
10593:     return 'ok';
10594: }
10595: 
10596: # -------------------------------------------------------------- Modify student
10597: 
10598: sub modifystudent {
10599:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10600:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10601:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10602:     if (!$cid) {
10603: 	unless ($cid=$env{'request.course.id'}) {
10604: 	    return 'not_in_class';
10605: 	}
10606:     }
10607: # --------------------------------------------------------------- Make the user
10608:     my $reply=&modifyuser
10609: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10610:          $desiredhome,$email,$inststatus);
10611:     unless ($reply eq 'ok') { return $reply; }
10612:     # This will cause &modify_student_enrollment to get the uid from the
10613:     # student's environment
10614:     $uid = undef if (!$forceid);
10615:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10616:                                         $gene,$usec,$end,$start,$type,$locktype,
10617:                                         $cid,$selfenroll,$context,$credits,$instsec);
10618:     return $reply;
10619: }
10620: 
10621: sub modify_student_enrollment {
10622:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10623:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10624:     my ($cdom,$cnum,$chome);
10625:     if (!$cid) {
10626: 	unless ($cid=$env{'request.course.id'}) {
10627: 	    return 'not_in_class';
10628: 	}
10629: 	$cdom=$env{'course.'.$cid.'.domain'};
10630: 	$cnum=$env{'course.'.$cid.'.num'};
10631:     } else {
10632: 	($cdom,$cnum)=split(/_/,$cid);
10633:     }
10634:     $chome=$env{'course.'.$cid.'.home'};
10635:     if (!$chome) {
10636: 	$chome=&homeserver($cnum,$cdom);
10637:     }
10638:     if (!$chome) { return 'unknown_course'; }
10639:     # Make sure the user exists
10640:     my $uhome=&homeserver($uname,$udom);
10641:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10642: 	return 'error: no such user';
10643:     }
10644:     # Get student data if we were not given enough information
10645:     if (!defined($first)  || $first  eq '' || 
10646:         !defined($last)   || $last   eq '' || 
10647:         !defined($uid)    || $uid    eq '' || 
10648:         !defined($middle) || $middle eq '' || 
10649:         !defined($gene)   || $gene   eq '') {
10650:         # They did not supply us with enough data to enroll the student, so
10651:         # we need to pick up more information.
10652:         my %tmp = &get('environment',
10653:                        ['firstname','middlename','lastname', 'generation','id']
10654:                        ,$udom,$uname);
10655: 
10656:         #foreach my $key (keys(%tmp)) {
10657:         #    &logthis("key $key = ".$tmp{$key});
10658:         #}
10659:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10660:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10661:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10662:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10663:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10664:     }
10665:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10666:     my $user = "$uname:$udom";
10667:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10668:     my $reply=cput('classlist',
10669: 		   {$user => 
10670: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10671: 		   $cdom,$cnum);
10672:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10673:         &devalidate_getsection_cache($udom,$uname,$cid);
10674:     } else { 
10675: 	return 'error: '.$reply;
10676:     }
10677:     # Add student role to user
10678:     my $uurl='/'.$cid;
10679:     $uurl=~s/\_/\//g;
10680:     if ($usec) {
10681: 	$uurl.='/'.$usec;
10682:     }
10683:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10684:                              $selfenroll,$context);
10685:     if ($result ne 'ok') {
10686:         if ($old_entry{$user} ne '') {
10687:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10688:         } else {
10689:             $reply = &del('classlist',[$user],$cdom,$cnum);
10690:         }
10691:     }
10692:     return $result; 
10693: }
10694: 
10695: sub format_name {
10696:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10697:     my $name;
10698:     if ($first ne 'lastname') {
10699: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10700:     } else {
10701: 	if ($lastname=~/\S/) {
10702: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10703: 	    $name=~s/\s+,/,/;
10704: 	} else {
10705: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10706: 	}
10707:     }
10708:     $name=~s/^\s+//;
10709:     $name=~s/\s+$//;
10710:     $name=~s/\s+/ /g;
10711:     return $name;
10712: }
10713: 
10714: # ------------------------------------------------- Write to course preferences
10715: 
10716: sub writecoursepref {
10717:     my ($courseid,%prefs)=@_;
10718:     $courseid=~s/^\///;
10719:     $courseid=~s/\_/\//g;
10720:     my ($cdomain,$cnum)=split(/\//,$courseid);
10721:     my $chome=homeserver($cnum,$cdomain);
10722:     if (($chome eq '') || ($chome eq 'no_host')) { 
10723: 	return 'error: no such course';
10724:     }
10725:     my $cstring='';
10726:     foreach my $pref (keys(%prefs)) {
10727: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10728:     }
10729:     $cstring=~s/\&$//;
10730:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10731: }
10732: 
10733: # ---------------------------------------------------------- Make/modify course
10734: 
10735: sub createcourse {
10736:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10737:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
10738:     $url=&declutter($url);
10739:     my $cid='';
10740:     if ($context eq 'requestcourses') {
10741:         my $can_create = 0;
10742:         my ($ownername,$ownerdom) = split(':',$course_owner);
10743:         if ($udom eq $ownerdom) {
10744:             my $reload;
10745:             if (($callercontext eq 'auto') &&
10746:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
10747:                 $reload = 'reload';
10748:             }
10749:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
10750:                                   $context)) {
10751:                 $can_create = 1;
10752:             }
10753:         } else {
10754:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10755:                                            $category);
10756:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10757:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10758:                 if (@curr > 0) {
10759:                     my @options = qw(approval validate autolimit);
10760:                     my $optregex = join('|',@options);
10761:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10762:                         $can_create = 1;
10763:                     }
10764:                 }
10765:             }
10766:         }
10767:         if ($can_create) {
10768:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10769:                 unless (&allowed('ccc',$udom)) {
10770:                     return 'refused'; 
10771:                 }
10772:             }
10773:         } else {
10774:             return 'refused';
10775:         }
10776:     } elsif (!&allowed('ccc',$udom)) {
10777:         return 'refused';
10778:     }
10779: # --------------------------------------------------------------- Get Unique ID
10780:     my $uname;
10781:     if ($cnum =~ /^$match_courseid$/) {
10782:         my $chome=&homeserver($cnum,$udom,'true');
10783:         if (($chome eq '') || ($chome eq 'no_host')) {
10784:             $uname = $cnum;
10785:         } else {
10786:             $uname = &generate_coursenum($udom,$crstype);
10787:         }
10788:     } else {
10789:         $uname = &generate_coursenum($udom,$crstype);
10790:     }
10791:     return $uname if ($uname =~ /^error/);
10792: # -------------------------------------------------- Check supplied server name
10793:     if (!defined($course_server)) {
10794:         if (defined(&domain($udom,'primary'))) {
10795:             $course_server = &domain($udom,'primary');
10796:         } else {
10797:             $course_server = $env{'user.home'}; 
10798:         }
10799:     }
10800:     my %host_servers =
10801:         &Apache::lonnet::get_servers($udom,'library');
10802:     unless ($host_servers{$course_server}) {
10803:         return 'error: invalid home server for course: '.$course_server;
10804:     }
10805: # ------------------------------------------------------------- Make the course
10806:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10807:                       $course_server);
10808:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10809:     my $uhome=&homeserver($uname,$udom,'true');
10810:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10811: 	return 'error: no such course';
10812:     }
10813: # ----------------------------------------------------------------- Course made
10814: # log existence
10815:     my $now = time;
10816:     my $newcourse = {
10817:                     $udom.'_'.$uname => {
10818:                                      description => $description,
10819:                                      inst_code   => $inst_code,
10820:                                      owner       => $course_owner,
10821:                                      type        => $crstype,
10822:                                      creator     => $env{'user.name'}.':'.
10823:                                                     $env{'user.domain'},
10824:                                      created     => $now,
10825:                                      context     => $context,
10826:                                                 },
10827:                     };
10828:     &courseidput($udom,$newcourse,$uhome,'notime');
10829: # set toplevel url
10830:     my $topurl=$url;
10831:     unless ($nonstandard) {
10832: # ------------------------------------------ For standard courses, make top url
10833:         my $mapurl=&clutter($url);
10834:         if ($mapurl eq '/res/') { $mapurl=''; }
10835:         $env{'form.initmap'}=(<<ENDINITMAP);
10836: <map>
10837: <resource id="1" type="start"></resource>
10838: <resource id="2" src="$mapurl"></resource>
10839: <resource id="3" type="finish"></resource>
10840: <link index="1" from="1" to="2"></link>
10841: <link index="2" from="2" to="3"></link>
10842: </map>
10843: ENDINITMAP
10844:         $topurl=&declutter(
10845:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10846:                           );
10847:     }
10848: # ----------------------------------------------------------- Write preferences
10849:     &writecoursepref($udom.'_'.$uname,
10850:                      ('description'              => $description,
10851:                       'url'                      => $topurl,
10852:                       'internal.creator'         => $env{'user.name'}.':'.
10853:                                                     $env{'user.domain'},
10854:                       'internal.created'         => $now,
10855:                       'internal.creationcontext' => $context)
10856:                     );
10857:     return '/'.$udom.'/'.$uname;
10858: }
10859: 
10860: # ------------------------------------------------------------------- Create ID
10861: sub generate_coursenum {
10862:     my ($udom,$crstype) = @_;
10863:     my $domdesc = &domain($udom);
10864:     return 'error: invalid domain' if ($domdesc eq '');
10865:     my $first;
10866:     if ($crstype eq 'Community') {
10867:         $first = '0';
10868:     } else {
10869:         $first = int(1+rand(9)); 
10870:     } 
10871:     my $uname=$first.
10872:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10873:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
10874:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10875: # ----------------------------------------------- Make sure that does not exist
10876:     my $uhome=&homeserver($uname,$udom,'true');
10877:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
10878:         if ($crstype eq 'Community') {
10879:             $first = '0';
10880:         } else {
10881:             $first = int(1+rand(9));
10882:         }
10883:         $uname=$first.
10884:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10885:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
10886:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10887:         $uhome=&homeserver($uname,$udom,'true');
10888:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
10889:             return 'error: unable to generate unique course-ID';
10890:         }
10891:     }
10892:     return $uname;
10893: }
10894: 
10895: sub is_course {
10896:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
10897:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
10898: 
10899:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
10900:     my $uhome=&homeserver($cnum,$cdom);
10901:     my $iscourse;
10902:     if (grep { $_ eq $uhome } current_machine_ids()) {
10903:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
10904:     } else {
10905:         my $hashid = $cdom.':'.$cnum;
10906:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
10907:         unless (defined($cached)) {
10908:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
10909:                                         $cnum,undef,undef,'.');
10910:             $iscourse = 0;
10911:             if (exists($courses{$cdom.'_'.$cnum})) {
10912:                 $iscourse = 1;
10913:             }
10914:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
10915:         }
10916:     }
10917:     return unless ($iscourse);
10918:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
10919: }
10920: 
10921: sub store_userdata {
10922:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
10923:     my $result;
10924:     if ($datakey ne '') {
10925:         if (ref($storehash) eq 'HASH') {
10926:             if ($udom eq '' || $uname eq '') {
10927:                 $udom = $env{'user.domain'};
10928:                 $uname = $env{'user.name'};
10929:             }
10930:             my $uhome=&homeserver($uname,$udom);
10931:             if (($uhome eq '') || ($uhome eq 'no_host')) {
10932:                 $result = 'error: no_host';
10933:             } else {
10934:                 $storehash->{'ip'} = &get_requestor_ip();
10935:                 $storehash->{'host'} = $perlvar{'lonHostID'};
10936: 
10937:                 my $namevalue='';
10938:                 foreach my $key (keys(%{$storehash})) {
10939:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
10940:                 }
10941:                 $namevalue=~s/\&$//;
10942:                 unless ($namespace eq 'courserequests') {
10943:                     $datakey = &escape($datakey);
10944:                 }
10945:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
10946:                                   $namevalue,$uhome);
10947:             }
10948:         } else {
10949:             $result = 'error: data to store was not a hash reference'; 
10950:         }
10951:     } else {
10952:         $result= 'error: invalid requestkey'; 
10953:     }
10954:     return $result;
10955: }
10956: 
10957: # ---------------------------------------------------------- Assign Custom Role
10958: 
10959: sub assigncustomrole {
10960:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10961:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10962:                        $end,$start,$deleteflag,$selfenroll,$context);
10963: }
10964: 
10965: # ----------------------------------------------------------------- Revoke Role
10966: 
10967: sub revokerole {
10968:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10969:     my $now=time;
10970:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10971: }
10972: 
10973: # ---------------------------------------------------------- Revoke Custom Role
10974: 
10975: sub revokecustomrole {
10976:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10977:     my $now=time;
10978:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10979:            $deleteflag,$selfenroll,$context);
10980: }
10981: 
10982: # ------------------------------------------------------------ Disk usage
10983: sub diskusage {
10984:     my ($udom,$uname,$directorypath,$getpropath)=@_;
10985:     $directorypath =~ s/\/$//;
10986:     my $listing=&reply('du2:'.&escape($directorypath).':'
10987:                        .&escape($getpropath).':'.&escape($uname).':'
10988:                        .&escape($udom),homeserver($uname,$udom));
10989:     if ($listing eq 'unknown_cmd') {
10990:         if ($getpropath) {
10991:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
10992:         }
10993:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10994:     }
10995:     return $listing;
10996: }
10997: 
10998: sub is_locked {
10999:     my ($file_name, $domain, $user, $which) = @_;
11000:     my @check;
11001:     my $is_locked;
11002:     push (@check,$file_name);
11003:     my %locked = &get('file_permissions',\@check,
11004: 		      $env{'user.domain'},$env{'user.name'});
11005:     my ($tmp)=keys(%locked);
11006:     if ($tmp=~/^error:/) { undef(%locked); }
11007:     
11008:     if (ref($locked{$file_name}) eq 'ARRAY') {
11009:         $is_locked = 'false';
11010:         foreach my $entry (@{$locked{$file_name}}) {
11011:            if (ref($entry) eq 'ARRAY') {
11012:                $is_locked = 'true';
11013:                if (ref($which) eq 'ARRAY') {
11014:                    push(@{$which},$entry);
11015:                } else {
11016:                    last;
11017:                }
11018:            }
11019:        }
11020:     } else {
11021:         $is_locked = 'false';
11022:     }
11023:     return $is_locked;
11024: }
11025: 
11026: sub declutter_portfile {
11027:     my ($file) = @_;
11028:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11029:     return $file;
11030: }
11031: 
11032: # ------------------------------------------------------------- Mark as Read Only
11033: 
11034: sub mark_as_readonly {
11035:     my ($domain,$user,$files,$what) = @_;
11036:     my %current_permissions = &dump('file_permissions',$domain,$user);
11037:     my ($tmp)=keys(%current_permissions);
11038:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11039:     foreach my $file (@{$files}) {
11040: 	$file = &declutter_portfile($file);
11041:         push(@{$current_permissions{$file}},$what);
11042:     }
11043:     &put('file_permissions',\%current_permissions,$domain,$user);
11044:     return;
11045: }
11046: 
11047: # ------------------------------------------------------------Save Selected Files
11048: 
11049: sub save_selected_files {
11050:     my ($user, $path, @files) = @_;
11051:     my $filename = $user."savedfiles";
11052:     my @other_files = &files_not_in_path($user, $path);
11053:     open (OUT,'>',LONCAPA::tempdir().$filename);
11054:     foreach my $file (@files) {
11055:         print (OUT $env{'form.currentpath'}.$file."\n");
11056:     }
11057:     foreach my $file (@other_files) {
11058:         print (OUT $file."\n");
11059:     }
11060:     close (OUT);
11061:     return 'ok';
11062: }
11063: 
11064: sub clear_selected_files {
11065:     my ($user) = @_;
11066:     my $filename = $user."savedfiles";
11067:     open (OUT,'>',LONCAPA::tempdir().$filename);
11068:     print (OUT undef);
11069:     close (OUT);
11070:     return ("ok");    
11071: }
11072: 
11073: sub files_in_path {
11074:     my ($user, $path) = @_;
11075:     my $filename = $user."savedfiles";
11076:     my %return_files;
11077:     open (IN,'<',LONCAPA::tempdir().$filename);
11078:     while (my $line_in = <IN>) {
11079:         chomp ($line_in);
11080:         my @paths_and_file = split (m!/!, $line_in);
11081:         my $file_part = pop (@paths_and_file);
11082:         my $path_part = join ('/', @paths_and_file);
11083:         $path_part.='/';
11084:         my $path_and_file = $path_part.$file_part;
11085:         if ($path_part eq $path) {
11086:             $return_files{$file_part}= 'selected';
11087:         }
11088:     }
11089:     close (IN);
11090:     return (\%return_files);
11091: }
11092: 
11093: # called in portfolio select mode, to show files selected NOT in current directory
11094: sub files_not_in_path {
11095:     my ($user, $path) = @_;
11096:     my $filename = $user."savedfiles";
11097:     my @return_files;
11098:     my $path_part;
11099:     open(IN, '<',LONCAPA::tempdir().$filename);
11100:     while (my $line = <IN>) {
11101:         #ok, I know it's clunky, but I want it to work
11102:         my @paths_and_file = split(m|/|, $line);
11103:         my $file_part = pop(@paths_and_file);
11104:         chomp($file_part);
11105:         my $path_part = join('/', @paths_and_file);
11106:         $path_part .= '/';
11107:         my $path_and_file = $path_part.$file_part;
11108:         if ($path_part ne $path) {
11109:             push(@return_files, ($path_and_file));
11110:         }
11111:     }
11112:     close(OUT);
11113:     return (@return_files);
11114: }
11115: 
11116: #------------------------------Submitted/Handedback Portfolio Files Versioning
11117:  
11118: sub portfiles_versioning {
11119:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11120:     my $portfolio_root = '/userfiles/portfolio';
11121:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11122:     foreach my $file (@{$portfiles}) {
11123:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11124:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11125:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11126:         my $getpropath = 1;
11127:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11128:                                              $stu_name,$getpropath);
11129:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11130:         my $new_answer = 
11131:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11132:         if ($new_answer ne 'problem getting file') {
11133:             push(@{$versioned_portfiles}, $directory.$new_answer);
11134:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11135:                               [$symb,$env{'request.course.id'},'graded']);
11136:         }
11137:     }
11138: }
11139: 
11140: sub get_next_version {
11141:     my ($answer_name, $answer_ext, $dir_list) = @_;
11142:     my $version;
11143:     if (ref($dir_list) eq 'ARRAY') {
11144:         foreach my $row (@{$dir_list}) {
11145:             my ($file) = split(/\&/,$row,2);
11146:             my ($file_name,$file_version,$file_ext) =
11147:                 &file_name_version_ext($file);
11148:             if (($file_name eq $answer_name) &&
11149:                 ($file_ext eq $answer_ext)) {
11150:                      # gets here if filename and extension match,
11151:                      # regardless of version
11152:                 if ($file_version ne '') {
11153:                     # a versioned file is found  so save it for later
11154:                     if ($file_version > $version) {
11155:                         $version = $file_version;
11156:                     }
11157:                 }
11158:             }
11159:         }
11160:     }
11161:     $version ++;
11162:     return($version);
11163: }
11164: 
11165: sub version_selected_portfile {
11166:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11167:     my ($answer_name,$answer_ver,$answer_ext) =
11168:         &file_name_version_ext($file_name);
11169:     my $new_answer;
11170:     $env{'form.copy'} =
11171:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11172:     if($env{'form.copy'} eq '-1') {
11173:         $new_answer = 'problem getting file';
11174:     } else {
11175:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11176:         my $copy_result = 
11177:             &finishuserfileupload($stu_name,$domain,'copy',
11178:                                   '/portfolio'.$directory.$new_answer);
11179:     }
11180:     undef($env{'form.copy'});
11181:     return ($new_answer);
11182: }
11183: 
11184: sub file_name_version_ext {
11185:     my ($file)=@_;
11186:     my @file_parts = split(/\./, $file);
11187:     my ($name,$version,$ext);
11188:     if (@file_parts > 1) {
11189:         $ext=pop(@file_parts);
11190:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11191:             $version=pop(@file_parts);
11192:         }
11193:         $name=join('.',@file_parts);
11194:     } else {
11195:         $name=join('.',@file_parts);
11196:     }
11197:     return($name,$version,$ext);
11198: }
11199: 
11200: #----------------------------------------------Get portfolio file permissions
11201: 
11202: sub get_portfile_permissions {
11203:     my ($domain,$user) = @_;
11204:     my %current_permissions = &dump('file_permissions',$domain,$user);
11205:     my ($tmp)=keys(%current_permissions);
11206:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11207:     return \%current_permissions;
11208: }
11209: 
11210: #---------------------------------------------Get portfolio file access controls
11211: 
11212: sub get_access_controls {
11213:     my ($current_permissions,$group,$file) = @_;
11214:     my %access;
11215:     my $real_file = $file;
11216:     $file =~ s/\.meta$//;
11217:     if (defined($file)) {
11218:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11219:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11220:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11221:             }
11222:         }
11223:     } else {
11224:         foreach my $key (keys(%{$current_permissions})) {
11225:             if ($key =~ /\0accesscontrol$/) {
11226:                 if (defined($group)) {
11227:                     if ($key !~ m-^\Q$group\E/-) {
11228:                         next;
11229:                     }
11230:                 }
11231:                 my ($fullpath) = split(/\0/,$key);
11232:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11233:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11234:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11235:                     }
11236:                 }
11237:             }
11238:         }
11239:     }
11240:     return %access;
11241: }
11242: 
11243: sub modify_access_controls {
11244:     my ($file_name,$changes,$domain,$user)=@_;
11245:     my ($outcome,$deloutcome);
11246:     my %store_permissions;
11247:     my %new_values;
11248:     my %new_control;
11249:     my %translation;
11250:     my @deletions = ();
11251:     my $now = time;
11252:     if (exists($$changes{'activate'})) {
11253:         if (ref($$changes{'activate'}) eq 'HASH') {
11254:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11255:             my $numnew = scalar(@newitems);
11256:             for (my $i=0; $i<$numnew; $i++) {
11257:                 my $newkey = $newitems[$i];
11258:                 my $newid = &Apache::loncommon::get_cgi_id();
11259:                 if ($newkey =~ /^\d+:/) { 
11260:                     $newkey =~ s/^(\d+)/$newid/;
11261:                     $translation{$1} = $newid;
11262:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11263:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11264:                     $translation{$1} = $newid;
11265:                 }
11266:                 $new_values{$file_name."\0".$newkey} = 
11267:                                           $$changes{'activate'}{$newitems[$i]};
11268:                 $new_control{$newkey} = $now;
11269:             }
11270:         }
11271:     }
11272:     my %todelete;
11273:     my %changed_items;
11274:     foreach my $action ('delete','update') {
11275:         if (exists($$changes{$action})) {
11276:             if (ref($$changes{$action}) eq 'HASH') {
11277:                 foreach my $key (keys(%{$$changes{$action}})) {
11278:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11279:                     if ($action eq 'delete') { 
11280:                         $todelete{$itemnum} = 1;
11281:                     } else {
11282:                         $changed_items{$itemnum} = $key;
11283:                     }
11284:                 }
11285:             }
11286:         }
11287:     }
11288:     # get lock on access controls for file.
11289:     my $lockhash = {
11290:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11291:                                                        ':'.$env{'user.domain'},
11292:                    }; 
11293:     my $tries = 0;
11294:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11295:    
11296:     while (($gotlock ne 'ok') && $tries < 10) {
11297:         $tries ++;
11298:         sleep(0.1);
11299:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11300:     }
11301:     if ($gotlock eq 'ok') {
11302:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11303:         my ($tmp)=keys(%curr_permissions);
11304:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11305:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11306:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11307:             if (ref($curr_controls) eq 'HASH') {
11308:                 foreach my $control_item (keys(%{$curr_controls})) {
11309:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11310:                     if (defined($todelete{$itemnum})) {
11311:                         push(@deletions,$file_name."\0".$control_item);
11312:                     } else {
11313:                         if (defined($changed_items{$itemnum})) {
11314:                             $new_control{$changed_items{$itemnum}} = $now;
11315:                             push(@deletions,$file_name."\0".$control_item);
11316:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11317:                         } else {
11318:                             $new_control{$control_item} = $$curr_controls{$control_item};
11319:                         }
11320:                     }
11321:                 }
11322:             }
11323:         }
11324:         my ($group);
11325:         if (&is_course($domain,$user)) {
11326:             ($group,my $file) = split(/\//,$file_name,2);
11327:         }
11328:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11329:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11330:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11331:         #  remove lock
11332:         my @del_lock = ($file_name."\0".'locked_access_records');
11333:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11334:         my $sqlresult =
11335:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11336:                                     $group);
11337:     } else {
11338:         $outcome = "error: could not obtain lockfile\n";  
11339:     }
11340:     return ($outcome,$deloutcome,\%new_values,\%translation);
11341: }
11342: 
11343: sub make_public_indefinitely {
11344:     my (@requrl) = @_;
11345:     return &automated_portfile_access('public',\@requrl);
11346: }
11347: 
11348: sub automated_portfile_access {
11349:     my ($accesstype,$addsref,$delsref,$info) = @_;
11350:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11351:         return 'invalid';
11352:     }
11353:     my %urls;
11354:     if (ref($addsref) eq 'ARRAY') {
11355:         foreach my $requrl (@{$addsref}) {
11356:             if (&is_portfolio_url($requrl)) {
11357:                 unless (exists($urls{$requrl})) {
11358:                     $urls{$requrl} = 'add';
11359:                 }
11360:             }
11361:         }
11362:     }
11363:     if (ref($delsref) eq 'ARRAY') {
11364:         foreach my $requrl (@{$delsref}) { 
11365:             if (&is_portfolio_url($requrl)) {
11366:                 unless (exists($urls{$requrl})) {
11367:                     $urls{$requrl} = 'delete'; 
11368:                 }
11369:             }
11370:         }
11371:     }
11372:     unless (keys(%urls)) {
11373:         return 'invalid';
11374:     }
11375:     my $ip;
11376:     if ($accesstype eq 'ip') {
11377:         if (ref($info) eq 'HASH') {
11378:             if ($info->{'ip'} ne '') {
11379:                 $ip = $info->{'ip'};
11380:             }
11381:         }
11382:         if ($ip eq '') {
11383:             return 'invalid';
11384:         }
11385:     }
11386:     my $errors;
11387:     my $now = time;
11388:     my %current_perms;
11389:     foreach my $requrl (sort(keys(%urls))) {
11390:         my $action;
11391:         if ($urls{$requrl} eq 'add') {
11392:             $action = 'activate';
11393:         } else {
11394:             $action = 'none';
11395:         }
11396:         my $aclnum = 0;
11397:         my (undef,$udom,$unum,$file_name,$group) =
11398:             &parse_portfolio_url($requrl);
11399:         unless (exists($current_perms{$unum.':'.$udom})) {
11400:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11401:         }
11402:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11403:                                                    $group,$file_name);
11404:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11405:             my ($num,$scope,$end,$start) = 
11406:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11407:             if ($scope eq $accesstype) {
11408:                 if (($start <= $now) && ($end == 0)) {
11409:                     if ($accesstype eq 'ip') {
11410:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11411:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11412:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11413:                                     if ($urls{$requrl} eq 'add') {
11414:                                         $action = 'none';
11415:                                         last;
11416:                                     } else {
11417:                                         $action = 'delete';
11418:                                         $aclnum = $num;
11419:                                         last;
11420:                                     }
11421:                                 }
11422:                             }
11423:                         }
11424:                     } elsif ($accesstype eq 'public') {
11425:                         if ($urls{$requrl} eq 'add') {
11426:                             $action = 'none';
11427:                             last;
11428:                         } else {
11429:                             $action = 'delete';
11430:                             $aclnum = $num;
11431:                             last;
11432:                         }
11433:                     }
11434:                 } elsif ($accesstype eq 'public') {
11435:                     $action = 'update';
11436:                     $aclnum = $num;
11437:                     last;
11438:                 }
11439:             }
11440:         }
11441:         if ($action eq 'none') {
11442:             next;
11443:         } else {
11444:             my %changes;
11445:             my $newend = 0;
11446:             my $newstart = $now;
11447:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11448:             $changes{$action}{$newkey} = {
11449:                 type => $accesstype,
11450:                 time => {
11451:                     start => $newstart,
11452:                     end   => $newend,
11453:                 },
11454:             };
11455:             if ($accesstype eq 'ip') {
11456:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11457:             }
11458:             my ($outcome,$deloutcome,$new_values,$translation) =
11459:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11460:             unless ($outcome eq 'ok') {
11461:                 $errors .= $outcome.' ';
11462:             }
11463:         }
11464:     }
11465:     if ($errors) {
11466:         $errors =~ s/\s$//;
11467:         return $errors;
11468:     } else {
11469:         return 'ok';
11470:     }
11471: }
11472: 
11473: #------------------------------------------------------Get Marked as Read Only
11474: 
11475: sub get_marked_as_readonly {
11476:     my ($domain,$user,$what,$group) = @_;
11477:     my $current_permissions = &get_portfile_permissions($domain,$user);
11478:     my @readonly_files;
11479:     my $cmp1=$what;
11480:     if (ref($what)) { $cmp1=join('',@{$what}) };
11481:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11482:         if (defined($group)) {
11483:             if ($file_name !~ m-^\Q$group\E/-) {
11484:                 next;
11485:             }
11486:         }
11487:         if (ref($value) eq "ARRAY"){
11488:             foreach my $stored_what (@{$value}) {
11489:                 my $cmp2=$stored_what;
11490:                 if (ref($stored_what) eq 'ARRAY') {
11491:                     $cmp2=join('',@{$stored_what});
11492:                 }
11493:                 if ($cmp1 eq $cmp2) {
11494:                     push(@readonly_files, $file_name);
11495:                     last;
11496:                 } elsif (!defined($what)) {
11497:                     push(@readonly_files, $file_name);
11498:                     last;
11499:                 }
11500:             }
11501:         }
11502:     }
11503:     return @readonly_files;
11504: }
11505: #-----------------------------------------------------------Get Marked as Read Only Hash
11506: 
11507: sub get_marked_as_readonly_hash {
11508:     my ($current_permissions,$group,$what) = @_;
11509:     my %readonly_files;
11510:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11511:         if (defined($group)) {
11512:             if ($file_name !~ m-^\Q$group\E/-) {
11513:                 next;
11514:             }
11515:         }
11516:         if (ref($value) eq "ARRAY"){
11517:             foreach my $stored_what (@{$value}) {
11518:                 if (ref($stored_what) eq 'ARRAY') {
11519:                     foreach my $lock_descriptor(@{$stored_what}) {
11520:                         if ($lock_descriptor eq 'graded') {
11521:                             $readonly_files{$file_name} = 'graded';
11522:                         } elsif ($lock_descriptor eq 'handback') {
11523:                             $readonly_files{$file_name} = 'handback';
11524:                         } else {
11525:                             if (!exists($readonly_files{$file_name})) {
11526:                                 $readonly_files{$file_name} = 'locked';
11527:                             }
11528:                         }
11529:                     }
11530:                 } 
11531:             }
11532:         } 
11533:     }
11534:     return %readonly_files;
11535: }
11536: # ------------------------------------------------------------ Unmark as Read Only
11537: 
11538: sub unmark_as_readonly {
11539:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11540:     # for portfolio submissions, $what contains [$symb,$crsid] 
11541:     my ($domain,$user,$what,$file_name,$group) = @_;
11542:     $file_name = &declutter_portfile($file_name);
11543:     my $symb_crs = $what;
11544:     if (ref($what)) { $symb_crs=join('',@$what); }
11545:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11546:     my ($tmp)=keys(%current_permissions);
11547:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11548:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11549:     foreach my $file (@readonly_files) {
11550: 	my $clean_file = &declutter_portfile($file);
11551: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11552: 	my $current_locks = $current_permissions{$file};
11553:         my @new_locks;
11554:         my @del_keys;
11555:         if (ref($current_locks) eq "ARRAY"){
11556:             foreach my $locker (@{$current_locks}) {
11557:                 my $compare=$locker;
11558:                 if (ref($locker) eq 'ARRAY') {
11559:                     $compare=join('',@{$locker});
11560:                     if ($compare ne $symb_crs) {
11561:                         push(@new_locks, $locker);
11562:                     }
11563:                 }
11564:             }
11565:             if (scalar(@new_locks) > 0) {
11566:                 $current_permissions{$file} = \@new_locks;
11567:             } else {
11568:                 push(@del_keys, $file);
11569:                 &del('file_permissions',\@del_keys, $domain, $user);
11570:                 delete($current_permissions{$file});
11571:             }
11572:         }
11573:     }
11574:     &put('file_permissions',\%current_permissions,$domain,$user);
11575:     return;
11576: }
11577: 
11578: # ------------------------------------------------------------ Directory lister
11579: 
11580: sub dirlist {
11581:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11582:     $uri=~s/^\///;
11583:     $uri=~s/\/$//;
11584:     my ($udom, $uname);
11585:     if ($getuserdir) {
11586:         $udom = $userdomain;
11587:         $uname = $username;
11588:     } else {
11589:         (undef,$udom,$uname)=split(/\//,$uri);
11590:         if(defined($userdomain)) {
11591:             $udom = $userdomain;
11592:         }
11593:         if(defined($username)) {
11594:             $uname = $username;
11595:         }
11596:     }
11597:     my ($dirRoot,$listing,@listing_results);
11598: 
11599:     $dirRoot = $perlvar{'lonDocRoot'};
11600:     if (defined($getpropath)) {
11601:         $dirRoot = &propath($udom,$uname);
11602:         $dirRoot =~ s/\/$//;
11603:     } elsif (defined($getuserdir)) {
11604:         my $subdir=$uname.'__';
11605:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11606:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11607:                    ."/$udom/$subdir/$uname";
11608:     } elsif (defined($alternateRoot)) {
11609:         $dirRoot = $alternateRoot;
11610:     }
11611: 
11612:     if($udom) {
11613:         if($uname) {
11614:             my $uhome = &homeserver($uname,$udom);
11615:             if ($uhome eq 'no_host') {
11616:                 return ([],'no_host');
11617:             }
11618:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11619:                               .$getuserdir.':'.&escape($dirRoot)
11620:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11621:             if ($listing eq 'unknown_cmd') {
11622:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11623:             } else {
11624:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11625:             }
11626:             if ($listing eq 'unknown_cmd') {
11627:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11628:                 @listing_results = split(/:/,$listing);
11629:             } else {
11630:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11631:             }
11632:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11633:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11634:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11635:                 return ([],$listing);
11636:             } else {
11637:                 return (\@listing_results);
11638:             }
11639:         } elsif(!$alternateRoot) {
11640:             my (%allusers,%listerror);
11641: 	    my %servers = &get_servers($udom,'library');
11642:  	    foreach my $tryserver (keys(%servers)) {
11643:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11644:                                   &escape($udom),$tryserver);
11645:                 if ($listing eq 'unknown_cmd') {
11646: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11647: 				      $udom, $tryserver);
11648:                 } else {
11649:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11650:                 }
11651: 		if ($listing eq 'unknown_cmd') {
11652: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11653: 				      $udom, $tryserver);
11654: 		    @listing_results = split(/:/,$listing);
11655: 		} else {
11656: 		    @listing_results =
11657: 			map { &unescape($_); } split(/:/,$listing);
11658: 		}
11659:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11660:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11661:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11662:                     $listerror{$tryserver} = $listing;
11663:                 } else {
11664: 		    foreach my $line (@listing_results) {
11665: 			my ($entry) = split(/&/,$line,2);
11666: 			$allusers{$entry} = 1;
11667: 		    }
11668: 		}
11669:             }
11670:             my @alluserslist=();
11671:             foreach my $user (sort(keys(%allusers))) {
11672:                 push(@alluserslist,$user.'&user');
11673:             }
11674: 
11675:             if (!%listerror) {
11676:                 # no errors
11677:                 return (\@alluserslist);
11678:             } elsif (scalar(keys(%servers)) == 1) {
11679:                 # one library server, one error 
11680:                 my ($key) = keys(%listerror);
11681:                 return (\@alluserslist, $listerror{$key});
11682:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11683:                 # con_lost indicates that we might miss data from at least one
11684:                 # library server
11685:                 return (\@alluserslist, 'con_lost');
11686:             } else {
11687:                 # multiple library servers and no con_lost -> data should be
11688:                 # complete. 
11689:                 return (\@alluserslist);
11690:             }
11691: 
11692:         } else {
11693:             return ([],'missing username');
11694:         }
11695:     } elsif(!defined($getpropath)) {
11696:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11697:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11698:         return (\@all_domains);
11699:     } else {
11700:         return ([],'missing domain');
11701:     }
11702: }
11703: 
11704: # --------------------------------------------- GetFileTimestamp
11705: # This function utilizes dirlist and returns the date stamp for
11706: # when it was last modified.  It will also return an error of -1
11707: # if an error occurs
11708: 
11709: sub GetFileTimestamp {
11710:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11711:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11712:     $studentName   = &LONCAPA::clean_username($studentName);
11713:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11714:                                     undef,$getuserdir);
11715:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11716:         return -1;
11717:     }
11718:     if (ref($fileref) eq 'ARRAY') {
11719:         my @stats = split('&',$fileref->[0]);
11720:         # @stats contains first the filename, then the stat output
11721:         return $stats[10]; # so this is 10 instead of 9.
11722:     } else {
11723:         return -1;
11724:     }
11725: }
11726: 
11727: sub stat_file {
11728:     my ($uri) = @_;
11729:     $uri = &clutter_with_no_wrapper($uri);
11730: 
11731:     my ($udom,$uname,$file);
11732:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11733: 	($udom,$uname,$file) =
11734: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11735: 	$file = 'userfiles/'.$file;
11736:     }
11737:     if ($uri =~ m-^/res/-) {
11738: 	($udom,$uname) = 
11739: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11740: 	$file = $uri;
11741:     }
11742: 
11743:     if (!$udom || !$uname || !$file) {
11744: 	# unable to handle the uri
11745: 	return ();
11746:     }
11747:     my $getpropath;
11748:     if ($file =~ /^userfiles\//) {
11749:         $getpropath = 1;
11750:     }
11751:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11752:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11753:         return ();
11754:     } else {
11755:         if (ref($listref) eq 'ARRAY') {
11756:             my @stats = split('&',$listref->[0]);
11757: 	    shift(@stats); #filename is first
11758: 	    return @stats;
11759:         }
11760:     }
11761:     return ();
11762: }
11763: 
11764: # --------------------------------------------------------- recursedirs
11765: # Recursive function to traverse either a specific user's Authoring Space
11766: # or corresponding Published Resource Space, and populate the hash ref:
11767: # $dirhashref with URLs of all directories, and if $filehashref hash
11768: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11769: # or .rights files in resource space, and .meta, .save, .log, and .bak
11770: # files in Authoring Space.
11771: #
11772: # Inputs:
11773: #
11774: # $is_home - true if current server is home server for user's space
11775: # $context - either: priv, or res respectively for Authoring or Resource Space.
11776: # $docroot - Document root (i.e., /home/httpd/html
11777: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11778: # $relpath - Current path (relative to top level).
11779: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11780: # $filehashref - reference to hash to populate with URLs of files (Optional)
11781: #
11782: # Returns: nothing
11783: #
11784: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11785: #
11786: # Currently used by interface/londocs.pm to create linked select boxes for
11787: # directory and filename to import a Course "Author" resource into a course, and
11788: # also to create linked select boxes for Authoring Space and Directory to choose
11789: # save location for creation of a new "standard" problem from the Course Editor.
11790: #
11791: 
11792: sub recursedirs {
11793:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11794:     return unless (ref($dirhashref) eq 'HASH');
11795:     my $currpath = $docroot.$toppath;
11796:     if ($relpath) {
11797:         $currpath .= "/$relpath";
11798:     }
11799:     my $savefile;
11800:     if (ref($filehashref)) {
11801:         $savefile = 1;
11802:     }
11803:     if ($is_home) {
11804:         if (opendir(my $dirh,$currpath)) {
11805:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11806:                 next if ($item eq '');
11807:                 if (-d "$currpath/$item") {
11808:                     my $newpath;
11809:                     if ($relpath) {
11810:                         $newpath = "$relpath/$item";
11811:                     } else {
11812:                         $newpath = $item;
11813:                     }
11814:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11815:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11816:                 } elsif ($savefile) {
11817:                     if ($context eq 'priv') {
11818:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11819:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11820:                         }
11821:                     } else {
11822:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11823:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11824:                         }
11825:                     }
11826:                 }
11827:             }
11828:             closedir($dirh);
11829:         }
11830:     } else {
11831:         my ($dirlistref,$listerror) =
11832:             &dirlist($toppath.$relpath);
11833:         my @dir_lines;
11834:         my $dirptr=16384;
11835:         if (ref($dirlistref) eq 'ARRAY') {
11836:             foreach my $dir_line (sort
11837:                               {
11838:                                   my ($afile)=split('&',$a,2);
11839:                                   my ($bfile)=split('&',$b,2);
11840:                                   return (lc($afile) cmp lc($bfile));
11841:                               } (@{$dirlistref})) {
11842:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11843:                     split(/\&/,$dir_line,16);
11844:                 $item =~ s/\s+$//;
11845:                 next if (($item =~ /^\.\.?$/) || ($obs));
11846:                 if ($dirptr&$testdir) {
11847:                     my $newpath;
11848:                     if ($relpath) {
11849:                         $newpath = "$relpath/$item";
11850:                     } else {
11851:                         $relpath = '/';
11852:                         $newpath = $item;
11853:                     }
11854:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11855:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11856:                 } elsif ($savefile) {
11857:                     if ($context eq 'priv') {
11858:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11859:                             $filehashref->{$relpath}{$item} = 1;
11860:                         }
11861:                     } else {
11862:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11863:                             $filehashref->{$relpath}{$item} = 1;
11864:                         }
11865:                     }
11866:                 }
11867:             }
11868:         }
11869:     }
11870:     return;
11871: }
11872: 
11873: # -------------------------------------------------------- Value of a Condition
11874: 
11875: # gets the value of a specific preevaluated condition
11876: #    stored in the string  $env{user.state.<cid>}
11877: # or looks up a condition reference in the bighash and if if hasn't
11878: # already been evaluated recurses into docondval to get the value of
11879: # the condition, then memoizing it to 
11880: #   $env{user.state.<cid>.<condition>}
11881: sub directcondval {
11882:     my $number=shift;
11883:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
11884: 	&Apache::lonuserstate::evalstate();
11885:     }
11886:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
11887: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
11888:     } elsif ($number =~ /^_/) {
11889: 	my $sub_condition;
11890: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11891: 		&GDBM_READER(),0640)) {
11892: 	    $sub_condition=$bighash{'conditions'.$number};
11893: 	    untie(%bighash);
11894: 	}
11895: 	my $value = &docondval($sub_condition);
11896: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11897: 	return $value;
11898:     }
11899:     if ($env{'user.state.'.$env{'request.course.id'}}) {
11900:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11901:     } else {
11902:        return 2;
11903:     }
11904: }
11905: 
11906: # get the collection of conditions for this resource
11907: sub condval {
11908:     my $condidx=shift;
11909:     my $allpathcond='';
11910:     foreach my $cond (split(/\|/,$condidx)) {
11911: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
11912: 	    $allpathcond.=
11913: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
11914: 	}
11915:     }
11916:     $allpathcond=~s/\|$//;
11917:     return &docondval($allpathcond);
11918: }
11919: 
11920: #evaluates an expression of conditions
11921: sub docondval {
11922:     my ($allpathcond) = @_;
11923:     my $result=0;
11924:     if ($env{'request.course.id'}
11925: 	&& defined($allpathcond)) {
11926: 	my $operand='|';
11927: 	my @stack;
11928: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
11929: 	    if ($chunk eq '(') {
11930: 		push @stack,($operand,$result);
11931: 	    } elsif ($chunk eq ')') {
11932: 		my $before=pop @stack;
11933: 		if (pop @stack eq '&') {
11934: 		    $result=$result>$before?$before:$result;
11935: 		} else {
11936: 		    $result=$result>$before?$result:$before;
11937: 		}
11938: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
11939: 		$operand=$chunk;
11940: 	    } else {
11941: 		my $new=directcondval($chunk);
11942: 		if ($operand eq '&') {
11943: 		    $result=$result>$new?$new:$result;
11944: 		} else {
11945: 		    $result=$result>$new?$result:$new;
11946: 		}
11947: 	    }
11948: 	}
11949:     }
11950:     return $result;
11951: }
11952: 
11953: # ---------------------------------------------------- Devalidate courseresdata
11954: 
11955: sub devalidatecourseresdata {
11956:     my ($coursenum,$coursedomain)=@_;
11957:     my $hashid=$coursenum.':'.$coursedomain;
11958:     &devalidate_cache_new('courseres',$hashid);
11959: }
11960: 
11961: 
11962: # --------------------------------------------------- Course Resourcedata Query
11963: #
11964: #  Parameters:
11965: #      $coursenum    - Number of the course.
11966: #      $coursedomain - Domain at which the course was created.
11967: #  Returns:
11968: #     A hash of the course parameters along (I think) with timestamps
11969: #     and version info.
11970: 
11971: sub get_courseresdata {
11972:     my ($coursenum,$coursedomain)=@_;
11973:     my $coursehom=&homeserver($coursenum,$coursedomain);
11974:     my $hashid=$coursenum.':'.$coursedomain;
11975:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
11976:     my %dumpreply;
11977:     unless (defined($cached)) {
11978: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11979: 	$result=\%dumpreply;
11980: 	my ($tmp) = keys(%dumpreply);
11981: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11982: 	    &do_cache_new('courseres',$hashid,$result,600);
11983: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11984: 	    return $tmp;
11985: 	} elsif ($tmp =~ /^(error)/) {
11986: 	    $result=undef;
11987: 	    &do_cache_new('courseres',$hashid,$result,600);
11988: 	}
11989:     }
11990:     return $result;
11991: }
11992: 
11993: sub devalidateuserresdata {
11994:     my ($uname,$udom)=@_;
11995:     my $hashid="$udom:$uname";
11996:     &devalidate_cache_new('userres',$hashid);
11997: }
11998: 
11999: sub get_userresdata {
12000:     my ($uname,$udom)=@_;
12001:     #most student don\'t have any data set, check if there is some data
12002:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12003: 
12004:     my $hashid="$udom:$uname";
12005:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12006:     if (!defined($cached)) {
12007: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12008: 	$result=\%resourcedata;
12009: 	&do_cache_new('userres',$hashid,$result,600);
12010:     }
12011:     my ($tmp)=keys(%$result);
12012:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12013: 	return $result;
12014:     }
12015:     #error 2 occurs when the .db doesn't exist
12016:     if ($tmp!~/error: 2 /) {
12017:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12018: 	    &logthis("<font color=\"blue\">WARNING:".
12019: 		     " Trying to get resource data for ".
12020: 		     $uname." at ".$udom.": ".
12021: 		     $tmp."</font>");
12022:         }
12023:     } elsif ($tmp=~/error: 2 /) {
12024: 	#&EXT_cache_set($udom,$uname);
12025: 	&do_cache_new('userres',$hashid,undef,600);
12026: 	undef($tmp); # not really an error so don't send it back
12027:     }
12028:     return $tmp;
12029: }
12030: #----------------------------------------------- resdata - return resource data
12031: #  Purpose:
12032: #    Return resource data for either users or for a course.
12033: #  Parameters:
12034: #     $name      - Course/user name.
12035: #     $domain    - Name of the domain the user/course is registered on.
12036: #     $type      - Type of thing $name is (must be 'course' or 'user')
12037: #     $mapp      - decluttered URL of enclosing map  
12038: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12039: #     $recurseup - Ref to array of map URLs, starting with map containing
12040: #                  $mapp up through hierarchy of nested maps to top level map.  
12041: #     $courseid  - CourseID (first part of param identifier).
12042: #     $modifier  - Middle part of param identifier.
12043: #     $what      - Last part of param identifier.
12044: #     @which     - Array of names of resources desired.
12045: #  Returns:
12046: #     The value of the first reasource in @which that is found in the
12047: #     resource hash.
12048: #  Exceptional Conditions:
12049: #     If the $type passed in is not valid (not the string 'course' or 
12050: #     'user', an undefined  reference is returned.
12051: #     If none of the resources are found, an undef is returned
12052: sub resdata {
12053:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12054:         $modifier,$what,@which)=@_;
12055:     my $result;
12056:     if ($type eq 'course') {
12057: 	$result=&get_courseresdata($name,$domain);
12058:     } elsif ($type eq 'user') {
12059: 	$result=&get_userresdata($name,$domain);
12060:     }
12061:     if (!ref($result)) { return $result; }    
12062:     foreach my $item (@which) {
12063:         if ($item->[1] eq 'course') {
12064:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12065:                 unless ($$recursed) {
12066:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12067:                     $$recursed = 1;
12068:                 }
12069:                 foreach my $item (@${recurseup}) {
12070:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12071:                     last if (defined($result->{$norecursechk}));
12072:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12073:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12074:                 }
12075:             }
12076:         }
12077:         if (defined($result->{$item->[0]})) {
12078: 	    return [$result->{$item->[0]},$item->[1]];
12079: 	}
12080:     }
12081:     return undef;
12082: }
12083: 
12084: sub get_domain_lti {
12085:     my ($cdom,$context) = @_;
12086:     my ($name,%lti);
12087:     if ($context eq 'consumer') {
12088:         $name = 'ltitools';
12089:     } elsif ($context eq 'provider') {
12090:         $name = 'lti';
12091:     } else {
12092:         return %lti;
12093:     }
12094:     my ($result,$cached)=&is_cached_new($name,$cdom);
12095:     if (defined($cached)) {
12096:         if (ref($result) eq 'HASH') {
12097:             %lti = %{$result};
12098:         }
12099:     } else {
12100:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12101:         if (ref($domconfig{$name}) eq 'HASH') {
12102:             %lti = %{$domconfig{$name}};
12103:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
12104:             if (ref($encdomconfig{$name}) eq 'HASH') {
12105:                 foreach my $id (keys(%lti)) {
12106:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
12107:                         foreach my $item ('key','secret') {
12108:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
12109:                         }
12110:                     }
12111:                 }
12112:             }
12113:         }
12114:         my $cachetime = 24*60*60;
12115:         &do_cache_new($name,$cdom,\%lti,$cachetime);
12116:     }
12117:     return %lti;
12118: }
12119: 
12120: sub get_numsuppfiles {
12121:     my ($cnum,$cdom,$ignorecache)=@_;
12122:     my $hashid=$cnum.':'.$cdom;
12123:     my ($suppcount,$cached);
12124:     unless ($ignorecache) {
12125:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
12126:     }
12127:     unless (defined($cached)) {
12128:         my $chome=&homeserver($cnum,$cdom);
12129:         unless ($chome eq 'no_host') {
12130:             ($suppcount,my $supptools,my $errors) = (0,0,0);
12131:             my $suppmap = 'supplemental.sequence';
12132:             ($suppcount,$supptools,$errors) =
12133:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
12134:                                                          $supptools,$errors);
12135:         }
12136:         &do_cache_new('suppcount',$hashid,$suppcount,600);
12137:     }
12138:     return $suppcount;
12139: }
12140: 
12141: #
12142: # EXT resource caching routines
12143: #
12144: 
12145: {
12146: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12147: #
12148: # The course for which we cache
12149: my $cachedmapkey='';
12150: # The cached recursive maps for this course
12151: my %cachedmaps=();
12152: # When this was last done
12153: my $cachedmaptime='';
12154: 
12155: sub clear_EXT_cache_status {
12156:     &delenv('cache.EXT.');
12157: }
12158: 
12159: sub EXT_cache_status {
12160:     my ($target_domain,$target_user) = @_;
12161:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12162:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12163:         # We know already the user has no data
12164:         return 1;
12165:     } else {
12166:         return 0;
12167:     }
12168: }
12169: 
12170: sub EXT_cache_set {
12171:     my ($target_domain,$target_user) = @_;
12172:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12173:     #&appenv({$cachename => time});
12174: }
12175: 
12176: # --------------------------------------------------------- Value of a Variable
12177: sub EXT {
12178: 
12179:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
12180:     unless ($varname) { return ''; }
12181:     #get real user name/domain, courseid and symb
12182:     my $courseid;
12183:     my $publicuser;
12184:     if ($symbparm) {
12185: 	$symbparm=&get_symb_from_alias($symbparm);
12186:     }
12187:     if (!($uname && $udom)) {
12188:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12189:       if (!$symbparm) {	$symbparm=$cursymb; }
12190:     } else {
12191: 	$courseid=$env{'request.course.id'};
12192:     }
12193:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12194:     my $rest;
12195:     if (defined($therest[0])) {
12196:        $rest=join('.',@therest);
12197:     } else {
12198:        $rest='';
12199:     }
12200: 
12201:     my $qualifierrest=$qualifier;
12202:     if ($rest) { $qualifierrest.='.'.$rest; }
12203:     my $spacequalifierrest=$space;
12204:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12205:     if ($realm eq 'user') {
12206: # --------------------------------------------------------------- user.resource
12207: 	if ($space eq 'resource') {
12208: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12209: 		  || defined($Apache::lonhomework::parsing_a_task))
12210: 		 &&
12211: 		 ($symbparm eq &symbread()) ) {	
12212: 		# if we are in the middle of processing the resource the
12213: 		# get the value we are planning on committing
12214:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12215:                     return $Apache::lonhomework::results{$qualifierrest};
12216:                 } else {
12217:                     return $Apache::lonhomework::history{$qualifierrest};
12218:                 }
12219: 	    } else {
12220: 		my %restored;
12221: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12222: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12223: 		} else {
12224: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12225: 		}
12226: 		return $restored{$qualifierrest};
12227: 	    }
12228: # ----------------------------------------------------------------- user.access
12229:         } elsif ($space eq 'access') {
12230: 	    # FIXME - not supporting calls for a specific user
12231:             return &allowed($qualifier,$rest);
12232: # ------------------------------------------ user.preferences, user.environment
12233:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12234: 	    if (($uname eq $env{'user.name'}) &&
12235: 		($udom eq $env{'user.domain'})) {
12236: 		return $env{join('.',('environment',$qualifierrest))};
12237: 	    } else {
12238: 		my %returnhash;
12239: 		if (!$publicuser) {
12240: 		    %returnhash=&userenvironment($udom,$uname,
12241: 						 $qualifierrest);
12242: 		}
12243: 		return $returnhash{$qualifierrest};
12244: 	    }
12245: # ----------------------------------------------------------------- user.course
12246:         } elsif ($space eq 'course') {
12247: 	    # FIXME - not supporting calls for a specific user
12248:             return $env{join('.',('request.course',$qualifier))};
12249: # ------------------------------------------------------------------- user.role
12250:         } elsif ($space eq 'role') {
12251: 	    # FIXME - not supporting calls for a specific user
12252:             my ($role,$where)=split(/\./,$env{'request.role'});
12253:             if ($qualifier eq 'value') {
12254: 		return $role;
12255:             } elsif ($qualifier eq 'extent') {
12256:                 return $where;
12257:             }
12258: # ----------------------------------------------------------------- user.domain
12259:         } elsif ($space eq 'domain') {
12260:             return $udom;
12261: # ------------------------------------------------------------------- user.name
12262:         } elsif ($space eq 'name') {
12263:             return $uname;
12264: # ---------------------------------------------------- Any other user namespace
12265:         } else {
12266: 	    my %reply;
12267: 	    if (!$publicuser) {
12268: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12269: 	    }
12270: 	    return $reply{$qualifierrest};
12271:         }
12272:     } elsif ($realm eq 'query') {
12273: # ---------------------------------------------- pull stuff out of query string
12274:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12275: 						[$spacequalifierrest]);
12276: 	return $env{'form.'.$spacequalifierrest}; 
12277:    } elsif ($realm eq 'request') {
12278: # ------------------------------------------------------------- request.browser
12279:         if ($space eq 'browser') {
12280:             return $env{'browser.'.$qualifier};
12281: # ------------------------------------------------------------ request.filename
12282:         } else {
12283:             return $env{'request.'.$spacequalifierrest};
12284:         }
12285:     } elsif ($realm eq 'course') {
12286: # ---------------------------------------------------------- course.description
12287:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12288:     } elsif ($realm eq 'resource') {
12289: 
12290: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12291: 	    if (!$symbparm) { $symbparm=&symbread(); }
12292: 	}
12293: 
12294:         if ($qualifier eq '') {
12295: 	    if ($space eq 'title') {
12296: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12297: 	        return &gettitle($symbparm);
12298: 	    }
12299: 	
12300: 	    if ($space eq 'map') {
12301: 	        my ($map) = &decode_symb($symbparm);
12302: 	        return &symbread($map);
12303: 	    }
12304:             if ($space eq 'maptitle') {
12305:                 my ($map) = &decode_symb($symbparm);
12306:                 return &gettitle($map);
12307:             }
12308: 	    if ($space eq 'filename') {
12309: 	        if ($symbparm) {
12310: 		    return &clutter((&decode_symb($symbparm))[2]);
12311: 	        }
12312: 	        return &hreflocation('',$env{'request.filename'});
12313: 	    }
12314: 
12315:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12316:                 if ($space eq 'visibleparts') {
12317:                     my $navmap = Apache::lonnavmaps::navmap->new();
12318:                     my $item;
12319:                     if (ref($navmap)) {
12320:                         my $res = $navmap->getBySymb($symbparm);
12321:                         my $parts = $res->parts();
12322:                         if (ref($parts) eq 'ARRAY') {
12323:                             $item = join(',',@{$parts});
12324:                         }
12325:                         undef($navmap);
12326:                     }
12327:                     return $item;
12328:                 }
12329:             }
12330:         }
12331: 
12332: 	my ($section, $group, @groups, @recurseup, $recursed);
12333: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12334:         if (($courseid eq '') && ($cid)) {
12335:             $courseid = $cid;
12336:         }
12337: 	if (($symbparm && $courseid) && 
12338: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12339: 
12340: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12341: 
12342: # ----------------------------------------------------- Cascading lookup scheme
12343: 	    my $symbp=$symbparm;
12344: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12345: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12346:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12347: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12348: 	    if (($env{'user.name'} eq $uname) &&
12349: 		($env{'user.domain'} eq $udom)) {
12350: 		$section=$env{'request.course.sec'};
12351:                 @groups = split(/:/,$env{'request.course.groups'});  
12352:                 @groups=&sort_course_groups($courseid,@groups); 
12353: 	    } else {
12354: 		if (! defined($usection)) {
12355: 		    $section=&getsection($udom,$uname,$courseid);
12356: 		} else {
12357: 		    $section = $usection;
12358: 		}
12359:                 @groups = &get_users_groups($udom,$uname,$courseid);
12360: 	    }
12361: 
12362: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12363: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12364:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12365: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12366: 
12367: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12368: 	    my $courselevelr=$courseid.'.'.$symbparm;
12369:             $courseleveli=$courseid.'.'.$recurseparm;
12370: 	    $courselevelm=$courseid.'.'.$mapparm;
12371: 
12372: # ----------------------------------------------------------- first, check user
12373: 
12374: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12375:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12376: 				       ([$courselevelr,'resource'],
12377: 					[$courselevelm,'map'     ],
12378:                                         [$courseleveli,'map'     ],
12379: 					[$courselevel, 'course'  ]));
12380: 	    if (defined($userreply)) { return &get_reply($userreply); }
12381: 
12382: # ------------------------------------------------ second, check some of course
12383:             my $coursereply;
12384:             if (@groups > 0) {
12385:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12386:                                        $recurseparm,$mapparm,$spacequalifierrest,
12387:                                        $mapp,\$recursed,\@recurseup);
12388:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12389:             }
12390: 
12391: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12392: 				  $env{'course.'.$courseid.'.domain'},
12393: 				  'course',$mapp,\$recursed,\@recurseup,
12394:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12395: 				  ([$seclevelr,   'resource'],
12396: 				   [$seclevelm,   'map'     ],
12397:                                    [$secleveli,   'map'     ],
12398: 				   [$seclevel,    'course'  ],
12399: 				   [$courselevelr,'resource']));
12400: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12401: 
12402: # ------------------------------------------------------ third, check map parms
12403: 	    my %parmhash=();
12404: 	    my $thisparm='';
12405: 	    if (tie(%parmhash,'GDBM_File',
12406: 		    $env{'request.course.fn'}.'_parms.db',
12407: 		    &GDBM_READER(),0640)) {
12408: 		$thisparm=$parmhash{$symbparm};
12409: 		untie(%parmhash);
12410: 	    }
12411: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12412: 	}
12413: # ------------------------------------------ fourth, look in resource metadata
12414:  
12415:         my $what = $spacequalifierrest;
12416: 	$what=~s/\./\_/;
12417: 	my $filename;
12418: 	if (!$symbparm) { $symbparm=&symbread(); }
12419: 	if ($symbparm) {
12420: 	    $filename=(&decode_symb($symbparm))[2];
12421: 	} else {
12422: 	    $filename=$env{'request.filename'};
12423: 	}
12424:         my $toolsymb;
12425:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12426:             $toolsymb = $symbparm;
12427:         }
12428: 	my $metadata=&metadata($filename,$what,$toolsymb);
12429: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12430: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12431: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12432: 
12433: # ----------------------------------------------- fifth, look in rest of course
12434: 	if ($symbparm && defined($courseid) && 
12435: 	    $courseid eq $env{'request.course.id'}) {
12436: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12437: 				     $env{'course.'.$courseid.'.domain'},
12438: 				     'course',$mapp,\$recursed,\@recurseup,
12439:                                      $courseid,'.',$spacequalifierrest,
12440: 				     ([$courselevelm,'map'   ],
12441:                                       [$courseleveli,'map'   ],
12442: 				      [$courselevel, 'course']));
12443: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12444: 	}
12445: # ------------------------------------------------------------------ Cascade up
12446: 	unless ($space eq '0') {
12447: 	    my @parts=split(/_/,$space);
12448: 	    my $id=pop(@parts);
12449: 	    my $part=join('_',@parts);
12450: 	    if ($part eq '') { $part='0'; }
12451: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12452: 				 $symbparm,$udom,$uname,$section,1);
12453: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12454: 	}
12455: 	if ($recurse) { return undef; }
12456: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12457: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12458: # ---------------------------------------------------- Any other user namespace
12459:     } elsif ($realm eq 'environment') {
12460: # ----------------------------------------------------------------- environment
12461: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12462: 	    return $env{'environment.'.$spacequalifierrest};
12463: 	} else {
12464: 	    if ($uname eq 'anonymous' && $udom eq '') {
12465: 		return '';
12466: 	    }
12467: 	    my %returnhash=&userenvironment($udom,$uname,
12468: 					    $spacequalifierrest);
12469: 	    return $returnhash{$spacequalifierrest};
12470: 	}
12471:     } elsif ($realm eq 'system') {
12472: # ----------------------------------------------------------------- system.time
12473: 	if ($space eq 'time') {
12474: 	    return time;
12475:         }
12476:     } elsif ($realm eq 'server') {
12477: # ----------------------------------------------------------------- system.time
12478: 	if ($space eq 'name') {
12479: 	    return $ENV{'SERVER_NAME'};
12480:         }
12481:     } elsif ($realm eq 'client') {
12482:         if ($space eq 'remote_addr') {
12483:             return &get_requestor_ip();
12484:         }
12485:     }
12486:     return '';
12487: }
12488: 
12489: sub get_reply {
12490:     my ($reply_value) = @_;
12491:     if (ref($reply_value) eq 'ARRAY') {
12492:         if (wantarray) {
12493: 	    return @$reply_value;
12494:         }
12495:         return $reply_value->[0];
12496:     } else {
12497:         return $reply_value;
12498:     }
12499: }
12500: 
12501: sub check_group_parms {
12502:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12503:         $recursed,$recurseupref) = @_;
12504:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12505:                   [$what,'course']);
12506:     my $coursereply;
12507:     foreach my $group (@{$groups}) {
12508:         my @groupitems = ();
12509:         foreach my $level (@levels) {
12510:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12511:              push(@groupitems,[$item,$level->[1]]);
12512:         }
12513:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12514:                                    $env{'course.'.$courseid.'.domain'},
12515:                                    'course',$mapp,$recursed,$recurseupref,
12516:                                    $courseid,'.['.$group.'].',$what,
12517:                                    @groupitems);
12518:         last if (defined($coursereply));
12519:     }
12520:     return $coursereply;
12521: }
12522: 
12523: sub get_map_hierarchy {
12524:     my ($mapname,$courseid) = @_;
12525:     my @recurseup = ();
12526:     if ($mapname) {
12527:         if (($cachedmapkey eq $courseid) &&
12528:             (abs($cachedmaptime-time)<5)) {
12529:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12530:                 return @{$cachedmaps{$mapname}};
12531:             }
12532:         }
12533:         my $navmap = Apache::lonnavmaps::navmap->new();
12534:         if (ref($navmap)) {
12535:             @recurseup = $navmap->recurseup_maps($mapname);
12536:             undef($navmap);
12537:             $cachedmaps{$mapname} = \@recurseup;
12538:             $cachedmaptime=time;
12539:             $cachedmapkey=$courseid;
12540:         }
12541:     }
12542:     return @recurseup;
12543: }
12544: 
12545: }
12546: 
12547: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12548:     my ($courseid,@groups) = @_;
12549:     @groups = sort(@groups);
12550:     return @groups;
12551: }
12552: 
12553: sub packages_tab_default {
12554:     my ($uri,$varname,$toolsymb)=@_;
12555:     my (undef,$part,$name)=split(/\./,$varname);
12556: 
12557:     my (@extension,@specifics,$do_default);
12558:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12559: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12560: 	if ($pack_type eq 'default') {
12561: 	    $do_default=1;
12562: 	} elsif ($pack_type eq 'extension') {
12563: 	    push(@extension,[$package,$pack_type,$pack_part]);
12564: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12565: 	    # only look at packages defaults for packages that this id is
12566: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12567: 	}
12568:     }
12569:     # first look for a package that matches the requested part id
12570:     foreach my $package (@specifics) {
12571: 	my (undef,$pack_type,$pack_part)=@{$package};
12572: 	next if ($pack_part ne $part);
12573: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12574: 	    return $packagetab{"$pack_type&$name&default"};
12575: 	}
12576:     }
12577:     # look for any possible matching non extension_ package
12578:     foreach my $package (@specifics) {
12579: 	my (undef,$pack_type,$pack_part)=@{$package};
12580: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12581: 	    return $packagetab{"$pack_type&$name&default"};
12582: 	}
12583: 	if ($pack_type eq 'part') { $pack_part='0'; }
12584: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12585: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12586: 	}
12587:     }
12588:     # look for any posible extension_ match
12589:     foreach my $package (@extension) {
12590: 	my ($package,$pack_type)=@{$package};
12591: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12592: 	    return $packagetab{"$pack_type&$name&default"};
12593: 	}
12594: 	if (defined($packagetab{$package."&$name&default"})) {
12595: 	    return $packagetab{$package."&$name&default"};
12596: 	}
12597:     }
12598:     # look for a global default setting
12599:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12600: 	return $packagetab{"default&$name&default"};
12601:     }
12602:     return undef;
12603: }
12604: 
12605: sub add_prefix_and_part {
12606:     my ($prefix,$part)=@_;
12607:     my $keyroot;
12608:     if (defined($prefix) && $prefix !~ /^__/) {
12609: 	# prefix that has a part already
12610: 	$keyroot=$prefix;
12611:     } elsif (defined($prefix)) {
12612: 	# prefix that is missing a part
12613: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12614:     } else {
12615: 	# no prefix at all
12616: 	if (defined($part)) { $keyroot='_'.$part; }
12617:     }
12618:     return $keyroot;
12619: }
12620: 
12621: # ---------------------------------------------------------------- Get metadata
12622: 
12623: my %metaentry;
12624: my %importedpartids;
12625: my %importedrespids;
12626: sub metadata {
12627:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12628:     $uri=&declutter($uri);
12629:     # if it is a non metadata possible uri return quickly
12630:     if (($uri eq '') || 
12631: 	(($uri =~ m|^/*adm/|) && 
12632: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12633:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12634: 	return undef;
12635:     }
12636:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12637: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12638: 	return undef;
12639:     }
12640:     my $filename=$uri;
12641:     $uri=~s/\.meta$//;
12642: #
12643: # Is the metadata already cached?
12644: # Look at timestamp of caching
12645: # Everything is cached by the main uri, libraries are never directly cached
12646: #
12647:     if (!defined($liburi)) {
12648: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12649: 	if (defined($cached)) { return $result->{':'.$what}; }
12650:     }
12651: 
12652: #
12653: # If the uri is for an external tool the file from
12654: # which metadata should be retrieved depends on whether
12655: # the tool had been configured to be gradable (set in the Course
12656: # Editor or Resource Editor).
12657: #
12658: # If a valid symb has been included as the third arg in the call
12659: # to &metadata() that can be used to retrieve the value of
12660: # parameter_0_gradable set for the resource, and included in the
12661: # uploaded map containing the tool. The value is retrieved via
12662: # &EXT(), if a valid symb is available.  Otherwise the value of
12663: # gradable in the exttool_$marker.db file for the tool instance
12664: # is retrieved via &get().
12665: #
12666: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12667: # hiddenresource and encrypturl (during course initialization)
12668: # the map-level parameter for resource.0.gradable included in the 
12669: # uploaded map containing the tool will not yet have been stored
12670: # in the user_course_parms.db file for the user's session, so in 
12671: # this case fall back to retrieving gradable status from the
12672: # exttool_$marker.db file.
12673: #
12674: # In order to avoid an infinite loop, &metadata() will return
12675: # before a call to &EXT(), if the uri is for an external tool
12676: # and the $what for which metadata is being requested is
12677: # parameter_0_gradable or 0_gradable.
12678: #
12679: 
12680:     if ($uri =~ /ext\.tool$/) {
12681:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12682:             return;
12683:         } else {
12684:             my ($checked,$use_passback);
12685:             if ($toolsymb ne '') {
12686:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12687:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12688:                     $checked = 1;
12689:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12690:                         $use_passback = 1;
12691:                     }
12692:                 }
12693:             }
12694:             unless ($checked) {
12695:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12696:                 $marker=~s/\D//g;
12697:                 if ($marker) {
12698:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12699:                     $use_passback = $toolsettings{'gradable'};
12700:                 }
12701:             }
12702:             if ($use_passback) {
12703:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12704:             } else {
12705:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12706:             }
12707:         }
12708:     }
12709: 
12710:     {
12711: # Imported parts would go here
12712:         my @origfiletagids=();
12713:         my $importedparts=0;
12714: 
12715: # Imported responseids would go here
12716:         my $importedresponses=0;
12717: #
12718: # Is this a recursive call for a library?
12719: #
12720: #	if (! exists($metacache{$uri})) {
12721: #	    $metacache{$uri}={};
12722: #	}
12723: 	my $cachetime = 60*60;
12724:         if ($liburi) {
12725: 	    $liburi=&declutter($liburi);
12726:             $filename=$liburi;
12727:         } else {
12728: 	    &devalidate_cache_new('meta',$uri);
12729: 	    undef(%metaentry);
12730: 	}
12731:         my %metathesekeys=();
12732:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12733: 	my $metastring;
12734: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12735: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12736: 	    $metastring = 
12737: 		&Apache::lonnet::ssi_body($which,
12738: 					  ('grade_target' => 'meta'));
12739: 	    $cachetime = 1; # only want this cached in the child not long term
12740: 	} elsif (($uri !~ m -^(editupload)/-) && 
12741:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12742: 	    my $file=&filelocation('',&clutter($filename));
12743: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12744: 	    $metastring=&getfile($file);
12745: 	}
12746:         my $parser=HTML::LCParser->new(\$metastring);
12747:         my $token;
12748:         undef %metathesekeys;
12749:         while ($token=$parser->get_token) {
12750: 	    if ($token->[0] eq 'S') {
12751: 		if (defined($token->[2]->{'package'})) {
12752: #
12753: # This is a package - get package info
12754: #
12755: 		    my $package=$token->[2]->{'package'};
12756: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12757: 		    if (defined($token->[2]->{'id'})) { 
12758: 			$keyroot.='_'.$token->[2]->{'id'}; 
12759: 		    }
12760: 		    if ($metaentry{':packages'}) {
12761: 			$metaentry{':packages'}.=','.$package.$keyroot;
12762: 		    } else {
12763: 			$metaentry{':packages'}=$package.$keyroot;
12764: 		    }
12765: 		    foreach my $pack_entry (keys(%packagetab)) {
12766: 			my $part=$keyroot;
12767: 			$part=~s/^\_//;
12768: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12769: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12770: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12771: 			    # ignore package.tab specified default values
12772:                             # here &package_tab_default() will fetch those
12773: 			    if ($subp eq 'default') { next; }
12774: 			    my $value=$packagetab{$pack_entry};
12775: 			    my $unikey;
12776: 			    if ($pack =~ /_0$/) {
12777: 				$unikey='parameter_0_'.$name;
12778: 				$part=0;
12779: 			    } else {
12780: 				$unikey='parameter'.$keyroot.'_'.$name;
12781: 			    }
12782: 			    if ($subp eq 'display') {
12783: 				$value.=' [Part: '.$part.']';
12784: 			    }
12785: 			    $metaentry{':'.$unikey.'.part'}=$part;
12786: 			    $metathesekeys{$unikey}=1;
12787: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12788: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12789: 			    }
12790: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12791: 				$metaentry{':'.$unikey}=
12792: 				    $metaentry{':'.$unikey.'.default'};
12793: 			    }
12794: 			}
12795: 		    }
12796: 		} else {
12797: #
12798: # This is not a package - some other kind of start tag
12799: #
12800: 		    my $entry=$token->[1];
12801: 		    my $unikey='';
12802: 
12803: 		    if ($entry eq 'import') {
12804: #
12805: # Importing a library here
12806: #
12807:                         my $location=$parser->get_text('/import');
12808:                         my $dir=$filename;
12809:                         $dir=~s|[^/]*$||;
12810:                         $location=&filelocation($dir,$location);
12811: 
12812:                         my $importid=$token->[2]->{'id'};
12813:                         my $importmode=$token->[2]->{'importmode'};
12814: #
12815: # Check metadata for imported file to
12816: # see if it contained response items
12817: #
12818:                         my ($origfile,@libfilekeys);
12819:                         my %currmetaentry = %metaentry;
12820:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12821:                                                            $depthcount+1));
12822:                         if (grep(/^responseorder$/,@libfilekeys)) {
12823:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12824:                                                              undef,$depthcount+1);
12825:                             if ($libresponseorder ne '') {
12826:                                 if ($#origfiletagids<0) {
12827:                                     undef(%importedrespids);
12828:                                     undef(%importedpartids);
12829:                                 }
12830:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12831:                                 if (@respids) {
12832:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12833:                                 }
12834:                                 if ($importedrespids{$importid} ne '') {
12835:                                     $importedresponses = 1;
12836: # We need to get the original file and the imported file to get the response order correct
12837: # Load and inspect original file
12838:                                     if ($#origfiletagids<0) {
12839:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12840:                                         $origfile=&getfile($origfilelocation);
12841:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12842:                                     }
12843:                                 }
12844:                             }
12845:                         }
12846: # Do not overwrite contents of %metaentry hash for resource itself with 
12847: # hash populated for imported library file
12848:                         %metaentry = %currmetaentry;
12849:                         undef(%currmetaentry);
12850:                         if ($importmode eq 'part') {
12851: # Import as part(s)
12852:                            $importedparts=1;
12853: # We need to get the original file and the imported file to get the part order correct
12854: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
12855: # Load and inspect original file if we didn't do that already
12856:                            if ($#origfiletagids<0) {
12857:                                undef(%importedrespids);
12858:                                undef(%importedpartids);
12859:                                if ($origfile eq '') {
12860:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12861:                                    $origfile=&getfile($origfilelocation);
12862:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12863:                                }
12864:                            }
12865:                            my @impfilepartids;
12866: # If <partorder> tag is included in metadata for the imported file
12867: # get the parts in the imported file from that.
12868:                            if (grep(/^partorder$/,@libfilekeys)) {
12869:                                %currmetaentry = %metaentry;
12870:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12871:                                                             $depthcount+1);
12872:                                %metaentry = %currmetaentry;
12873:                                undef(%currmetaentry);
12874:                                if ($libpartorder ne '') {
12875:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
12876:                                }
12877:                            } else {
12878: # If no <partorder> tag available, load and inspect imported file
12879:                                my $impfile=&getfile($location);
12880:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12881:                            }
12882:                            if ($#impfilepartids>=0) {
12883: # This problem had parts
12884:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
12885:                            } else {
12886: # Importing by turning a single problem into a problem part
12887: # It gets the import-tags ID as part-ID
12888:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
12889:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
12890:                            }
12891:                         } else {
12892: # Import as problem or as normal import
12893:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12894:                             unless ($importmode eq 'problem') {
12895: # Normal import
12896:                                 if (defined($token->[2]->{'id'})) {
12897:                                     $unikey.='_'.$token->[2]->{'id'};
12898:                                 }
12899:                             }
12900: # Check metadata for imported file to
12901: # see if it contained parts
12902:                             if (grep(/^partorder$/,@libfilekeys)) {
12903:                                 %currmetaentry = %metaentry;
12904:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12905:                                                              $depthcount+1);
12906:                                 %metaentry = %currmetaentry;
12907:                                 undef(%currmetaentry);
12908:                                 if ($libpartorder ne '') {
12909:                                     $importedparts = 1;
12910:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
12911:                                 }
12912:                             }
12913:                         }
12914: 			if ($depthcount<20) {
12915: 			    my $metadata = 
12916: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
12917: 					  $depthcount+1);
12918: 			    foreach my $meta (split(',',$metadata)) {
12919: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
12920: 				$metathesekeys{$meta}=1;
12921: 			    }
12922:                         }
12923: 		    } else {
12924: #
12925: # Not importing, some other kind of non-package, non-library start tag
12926: # 
12927:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
12928:                         if (defined($token->[2]->{'id'})) {
12929:                             $unikey.='_'.$token->[2]->{'id'};
12930:                         }
12931: 			if (defined($token->[2]->{'name'})) { 
12932: 			    $unikey.='_'.$token->[2]->{'name'}; 
12933: 			}
12934: 			$metathesekeys{$unikey}=1;
12935: 			foreach my $param (@{$token->[3]}) {
12936: 			    $metaentry{':'.$unikey.'.'.$param} =
12937: 				$token->[2]->{$param};
12938: 			}
12939: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
12940: 			my $default=$metaentry{':'.$unikey.'.default'};
12941: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
12942: 		 # only ws inside the tag, and not in default, so use default
12943: 		 # as value
12944: 			    $metaentry{':'.$unikey}=$default;
12945: 			} elsif ( $internaltext =~ /\S/ ) {
12946: 		  # something interesting inside the tag
12947: 			    $metaentry{':'.$unikey}=$internaltext;
12948: 			} else {
12949: 		  # no interesting values, don't set a default
12950: 			}
12951: # end of not-a-package not-a-library import
12952: 		    }
12953: # end of not-a-package start tag
12954: 		}
12955: # the next is the end of "start tag"
12956: 	    }
12957: 	}
12958: 	my ($extension) = ($uri =~ /\.(\w+)$/);
12959: 	$extension = lc($extension);
12960: 	if ($extension eq 'htm') { $extension='html'; }
12961: 
12962: 	foreach my $key (keys(%packagetab)) {
12963: 	    #no specific packages #how's our extension
12964: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
12965: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
12966: 					 \%metathesekeys);
12967: 	}
12968: 
12969: 	if (!exists($metaentry{':packages'})
12970: 	    || $packagetab{"import_defaults&extension_$extension"}) {
12971: 	    foreach my $key (keys(%packagetab)) {
12972: 		#no specific packages well let's get default then
12973: 		if ($key!~/^default&/) { next; }
12974: 		&metadata_create_package_def($uri,$key,'default',
12975: 					     \%metathesekeys);
12976: 	    }
12977: 	}
12978: # are there custom rights to evaluate
12979: 	if ($metaentry{':copyright'} eq 'custom') {
12980: 
12981:     #
12982:     # Importing a rights file here
12983:     #
12984: 	    unless ($depthcount) {
12985: 		my $location=$metaentry{':customdistributionfile'};
12986: 		my $dir=$filename;
12987: 		$dir=~s|[^/]*$||;
12988: 		$location=&filelocation($dir,$location);
12989: 		my $rights_metadata =
12990: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
12991: 			      $depthcount+1);
12992: 		foreach my $rights (split(',',$rights_metadata)) {
12993: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
12994: 		    $metathesekeys{$rights}=1;
12995: 		}
12996: 	    }
12997: 	}
12998: 	# uniqifiy package listing
12999: 	my %seen;
13000: 	my @uniq_packages =
13001: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13002: 	$metaentry{':packages'} = join(',',@uniq_packages);
13003: 
13004:         if (($importedresponses) || ($importedparts)) {
13005:             if ($importedparts) {
13006: # We had imported parts and need to rebuild partorder
13007:                 $metaentry{':partorder'}='';
13008:                 $metathesekeys{'partorder'}=1;
13009:             }
13010:             if ($importedresponses) {
13011: # We had imported responses and need to rebuil responseorder
13012:                 $metaentry{':responseorder'}='';
13013:                 $metathesekeys{'responseorder'}=1;
13014:             }
13015:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
13016:                 my $origid = $origfiletagids[$index+1];
13017:                 if ($origfiletagids[$index] eq 'part') {
13018: # Original part, part of the problem
13019:                     if ($importedparts) {
13020:                         $metaentry{':partorder'}.=','.$origid;
13021:                     }
13022:                 } elsif ($origfiletagids[$index] eq 'import') {
13023:                     if ($importedparts) {
13024: # We have imported parts at this position
13025:                         if ($importedpartids{$origid} ne '') {
13026:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
13027:                         }
13028:                     }
13029:                     if ($importedresponses) {
13030: # We have imported responses at this position
13031:                         if ($importedrespids{$origid} ne '') {
13032:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
13033:                         }
13034:                     }
13035:                 } else {
13036: # Original response item, part of the problem
13037:                     if ($importedresponses) {
13038:                         $metaentry{':responseorder'}.=','.$origid;
13039:                     }
13040:                 }
13041:             }
13042:             if ($importedparts) {
13043:                 $metaentry{':partorder'}=~s/^\,//;
13044:             }
13045:             if ($importedresponses) {
13046:                 $metaentry{':responseorder'}=~s/^\,//;
13047:             }
13048:         }
13049: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
13050: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
13051: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
13052:         unless ($liburi) {
13053: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
13054:         }
13055: # this is the end of "was not already recently cached
13056:     }
13057:     return $metaentry{':'.$what};
13058: }
13059: 
13060: sub metadata_create_package_def {
13061:     my ($uri,$key,$package,$metathesekeys)=@_;
13062:     my ($pack,$name,$subp)=split(/\&/,$key);
13063:     if ($subp eq 'default') { next; }
13064:     
13065:     if (defined($metaentry{':packages'})) {
13066: 	$metaentry{':packages'}.=','.$package;
13067:     } else {
13068: 	$metaentry{':packages'}=$package;
13069:     }
13070:     my $value=$packagetab{$key};
13071:     my $unikey;
13072:     $unikey='parameter_0_'.$name;
13073:     $metaentry{':'.$unikey.'.part'}=0;
13074:     $$metathesekeys{$unikey}=1;
13075:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13076: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
13077:     }
13078:     if (defined($metaentry{':'.$unikey.'.default'})) {
13079: 	$metaentry{':'.$unikey}=
13080: 	    $metaentry{':'.$unikey.'.default'};
13081:     }
13082: }
13083: 
13084: sub metadata_generate_part0 {
13085:     my ($metadata,$metacache,$uri) = @_;
13086:     my %allnames;
13087:     foreach my $metakey (keys(%$metadata)) {
13088: 	if ($metakey=~/^parameter\_(.*)/) {
13089: 	  my $part=$$metacache{':'.$metakey.'.part'};
13090: 	  my $name=$$metacache{':'.$metakey.'.name'};
13091: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
13092: 	    $allnames{$name}=$part;
13093: 	  }
13094: 	}
13095:     }
13096:     foreach my $name (keys(%allnames)) {
13097:       $$metadata{"parameter_0_$name"}=1;
13098:       my $key=":parameter_0_$name";
13099:       $$metacache{"$key.part"}='0';
13100:       $$metacache{"$key.name"}=$name;
13101:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
13102: 					   $allnames{$name}.'_'.$name.
13103: 					   '.type'};
13104:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
13105: 			     '.display'};
13106:       my $expr='[Part: '.$allnames{$name}.']';
13107:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
13108:       $$metacache{"$key.display"}=$olddis;
13109:     }
13110: }
13111: 
13112: # ------------------------------------------------------ Devalidate title cache
13113: 
13114: sub devalidate_title_cache {
13115:     my ($url)=@_;
13116:     if (!$env{'request.course.id'}) { return; }
13117:     my $symb=&symbread($url);
13118:     if (!$symb) { return; }
13119:     my $key=$env{'request.course.id'}."\0".$symb;
13120:     &devalidate_cache_new('title',$key);
13121: }
13122: 
13123: # ------------------------------------------------- Get the title of a course
13124: 
13125: sub current_course_title {
13126:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13127: }
13128: # ------------------------------------------------- Get the title of a resource
13129: 
13130: sub gettitle {
13131:     my $urlsymb=shift;
13132:     my $symb=&symbread($urlsymb);
13133:     if ($symb) {
13134: 	my $key=$env{'request.course.id'}."\0".$symb;
13135: 	my ($result,$cached)=&is_cached_new('title',$key);
13136: 	if (defined($cached)) { 
13137: 	    return $result;
13138: 	}
13139: 	my ($map,$resid,$url)=&decode_symb($symb);
13140: 	my $title='';
13141: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13142: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13143: 	} else {
13144: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13145: 		    &GDBM_READER(),0640)) {
13146: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13147: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13148: 		untie(%bighash);
13149: 	    }
13150: 	}
13151: 	$title=~s/\&colon\;/\:/gs;
13152: 	if ($title) {
13153: # Remember both $symb and $title for dynamic metadata
13154:             $accesshash{$symb.'___crstitle'}=$title;
13155:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13156: # Cache this title and then return it
13157: 	    return &do_cache_new('title',$key,$title,600);
13158: 	}
13159: 	$urlsymb=$url;
13160:     }
13161:     my $title=&metadata($urlsymb,'title');
13162:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13163:     return $title;
13164: }
13165: 
13166: sub get_slot {
13167:     my ($which,$cnum,$cdom)=@_;
13168:     if (!$cnum || !$cdom) {
13169: 	(undef,my $courseid)=&whichuser();
13170: 	$cdom=$env{'course.'.$courseid.'.domain'};
13171: 	$cnum=$env{'course.'.$courseid.'.num'};
13172:     }
13173:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13174:     my %slotinfo;
13175:     if (exists($remembered{$key})) {
13176: 	$slotinfo{$which} = $remembered{$key};
13177:     } else {
13178: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13179: 	&Apache::lonhomework::showhash(%slotinfo);
13180: 	my ($tmp)=keys(%slotinfo);
13181: 	if ($tmp=~/^error:/) { return (); }
13182: 	$remembered{$key} = $slotinfo{$which};
13183:     }
13184:     if (ref($slotinfo{$which}) eq 'HASH') {
13185: 	return %{$slotinfo{$which}};
13186:     }
13187:     return $slotinfo{$which};
13188: }
13189: 
13190: sub get_reservable_slots {
13191:     my ($cnum,$cdom,$uname,$udom) = @_;
13192:     my $now = time;
13193:     my $reservable_info;
13194:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13195:     if (exists($remembered{$key})) {
13196:         $reservable_info = $remembered{$key};
13197:     } else {
13198:         my %resv;
13199:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13200:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13201:         $reservable_info = \%resv;
13202:         $remembered{$key} = $reservable_info;
13203:     }
13204:     return $reservable_info;
13205: }
13206: 
13207: sub get_course_slots {
13208:     my ($cnum,$cdom) = @_;
13209:     my $hashid=$cnum.':'.$cdom;
13210:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13211:     if (defined($cached)) {
13212:         if (ref($result) eq 'HASH') {
13213:             return %{$result};
13214:         }
13215:     } else {
13216:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13217:         my ($tmp) = keys(%slots);
13218:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13219:             &do_cache_new('allslots',$hashid,\%slots,600);
13220:             return %slots;
13221:         }
13222:     }
13223:     return;
13224: }
13225: 
13226: sub devalidate_slots_cache {
13227:     my ($cnum,$cdom)=@_;
13228:     my $hashid=$cnum.':'.$cdom;
13229:     &devalidate_cache_new('allslots',$hashid);
13230: }
13231: 
13232: sub get_coursechange {
13233:     my ($cdom,$cnum) = @_;
13234:     if ($cdom eq '' || $cnum eq '') {
13235:         return unless ($env{'request.course.id'});
13236:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13237:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13238:     }
13239:     my $hashid=$cdom.'_'.$cnum;
13240:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13241:     if ((defined($cached)) && ($change ne '')) {
13242:         return $change;
13243:     } else {
13244:         my %crshash;
13245:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13246:         if ($crshash{'internal.contentchange'} eq '') {
13247:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13248:             if ($change eq '') {
13249:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13250:                 $change = $crshash{'internal.created'};
13251:             }
13252:         } else {
13253:             $change = $crshash{'internal.contentchange'};
13254:         }
13255:         my $cachetime = 600;
13256:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13257:     }
13258:     return $change;
13259: }
13260: 
13261: sub devalidate_coursechange_cache {
13262:     my ($cnum,$cdom)=@_;
13263:     my $hashid=$cnum.':'.$cdom;
13264:     &devalidate_cache_new('crschange',$hashid);
13265: }
13266: 
13267: # ------------------------------------------------- Update symbolic store links
13268: 
13269: sub symblist {
13270:     my ($mapname,%newhash)=@_;
13271:     $mapname=&deversion(&declutter($mapname));
13272:     my %hash;
13273:     if (($env{'request.course.fn'}) && (%newhash)) {
13274:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13275:                       &GDBM_WRCREAT(),0640)) {
13276: 	    foreach my $url (keys(%newhash)) {
13277: 		next if ($url eq 'last_known'
13278: 			 && $env{'form.no_update_last_known'});
13279: 		$hash{declutter($url)}=&encode_symb($mapname,
13280: 						    $newhash{$url}->[1],
13281: 						    $newhash{$url}->[0]);
13282:             }
13283:             if (untie(%hash)) {
13284: 		return 'ok';
13285:             }
13286:         }
13287:     }
13288:     return 'error';
13289: }
13290: 
13291: # --------------------------------------------------------------- Verify a symb
13292: 
13293: sub symbverify {
13294:     my ($symb,$thisurl,$encstate)=@_;
13295:     my $thisfn=$thisurl;
13296:     $thisfn=&declutter($thisfn);
13297: # direct jump to resource in page or to a sequence - will construct own symbs
13298:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13299: # check URL part
13300:     my ($map,$resid,$url)=&decode_symb($symb);
13301: 
13302:     unless ($url eq $thisfn) { return 0; }
13303: 
13304:     $symb=&symbclean($symb);
13305:     $thisurl=&deversion($thisurl);
13306:     $thisfn=&deversion($thisfn);
13307: 
13308:     my %bighash;
13309:     my $okay=0;
13310: 
13311:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13312:                             &GDBM_READER(),0640)) {
13313:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13314:             $thisurl =~ s/\?.+$//;
13315:             if ($map =~ m{^uploaded/.+\.page$}) {
13316:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13317:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13318:             }
13319:         }
13320:         my $ids;
13321:         if ($map =~ m{^uploaded/.+\.page$}) {
13322:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13323:         } else {
13324:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13325:         }
13326:         unless ($ids) {
13327:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13328:             $ids=$bighash{$idkey};
13329:         }
13330:         if ($ids) {
13331: # ------------------------------------------------------------------- Has ID(s)
13332:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13333:                 $symb =~ s/\?.+$//;
13334:             }
13335: 	    foreach my $id (split(/\,/,$ids)) {
13336: 	       my ($mapid,$resid)=split(/\./,$id);
13337:                if (
13338:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13339:    eq $symb) {
13340:                    if (ref($encstate)) {
13341:                        $$encstate = $bighash{'encrypted_'.$id};
13342:                    }
13343: 		   if (($env{'request.role.adv'}) ||
13344: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13345:                        ($thisurl eq '/adm/navmaps')) {
13346: 		       $okay=1;
13347:                        last;
13348: 		   }
13349: 	       }
13350: 	   }
13351:         }
13352: 	untie(%bighash);
13353:     }
13354:     return $okay;
13355: }
13356: 
13357: # --------------------------------------------------------------- Clean-up symb
13358: 
13359: sub symbclean {
13360:     my $symb=shift;
13361:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13362: # remove version from map
13363:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13364: 
13365: # remove version from URL
13366:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13367: 
13368: # remove wrapper
13369: 
13370:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13371:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13372:     return $symb;
13373: }
13374: 
13375: # ---------------------------------------------- Split symb to find map and url
13376: 
13377: sub encode_symb {
13378:     my ($map,$resid,$url)=@_;
13379:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13380: }
13381: 
13382: sub decode_symb {
13383:     my $symb=shift;
13384:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13385:     my ($map,$resid,$url)=split(/___/,$symb);
13386:     return (&fixversion($map),$resid,&fixversion($url));
13387: }
13388: 
13389: sub fixversion {
13390:     my $fn=shift;
13391:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13392:     my %bighash;
13393:     my $uri=&clutter($fn);
13394:     my $key=$env{'request.course.id'}.'_'.$uri;
13395: # is this cached?
13396:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13397:     if (defined($cached)) { return $result; }
13398: # unfortunately not cached, or expired
13399:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13400: 	    &GDBM_READER(),0640)) {
13401:  	if ($bighash{'version_'.$uri}) {
13402:  	    my $version=$bighash{'version_'.$uri};
13403:  	    unless (($version eq 'mostrecent') || 
13404: 		    ($version==&getversion($uri))) {
13405:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13406:  	    }
13407:  	}
13408:  	untie %bighash;
13409:     }
13410:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13411: }
13412: 
13413: sub deversion {
13414:     my $url=shift;
13415:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13416:     return $url;
13417: }
13418: 
13419: # ------------------------------------------------------ Return symb list entry
13420: 
13421: sub symbread {
13422:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
13423:         $ignoresymbdb,$noenccheck)=@_;
13424:     my $cache_str='request.symbread.cached.'.$thisfn;
13425:     if (defined($env{$cache_str})) {
13426:         unless (ref($possibles) eq 'HASH') {
13427:             if ($ignorecachednull) {
13428:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
13429:             } else {
13430:                 return $env{$cache_str};
13431:             }
13432:         }
13433:     }
13434: # no filename provided? try from environment
13435:     unless ($thisfn) {
13436:         if ($env{'request.symb'}) {
13437:             return $env{$cache_str}=&symbclean($env{'request.symb'});
13438: 	}
13439: 	$thisfn=$env{'request.filename'};
13440:     }
13441:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13442: # is that filename actually a symb? Verify, clean, and return
13443:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13444: 	if (&symbverify($thisfn,$1)) {
13445: 	    return $env{$cache_str}=&symbclean($thisfn);
13446: 	}
13447:     }
13448:     $thisfn=declutter($thisfn);
13449:     my %hash;
13450:     my %bighash;
13451:     my $syval='';
13452:     if (($env{'request.course.fn'}) && ($thisfn)) {
13453:         my $targetfn = $thisfn;
13454:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13455:             $targetfn = 'adm/wrapper/'.$thisfn;
13456:         }
13457: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13458: 	    $targetfn=$1;
13459: 	}
13460:         unless ($ignoresymbdb) {
13461:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13462:                           &GDBM_READER(),0640)) {
13463: 	        $syval=$hash{$targetfn};
13464:                 untie(%hash);
13465:             }
13466:             if ($syval && $checkforblock) {
13467:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
13468:                 if (@blockers) {
13469:                     $syval='';
13470:                 }
13471:             }
13472:         }
13473: # ---------------------------------------------------------- There was an entry
13474:         if ($syval) {
13475: 	    #unless ($syval=~/\_\d+$/) {
13476: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13477: 		    #&appenv({'request.ambiguous' => $thisfn});
13478: 		    #return $env{$cache_str}='';
13479: 		#}    
13480: 		#$syval.=$1;
13481: 	    #}
13482:         } else {
13483: # ------------------------------------------------------- Was not in symb table
13484:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13485:                             &GDBM_READER(),0640)) {
13486: # ---------------------------------------------- Get ID(s) for current resource
13487:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13488:               unless ($ids) { 
13489:                  $ids=$bighash{'ids_/'.$thisfn};
13490:               }
13491:               unless ($ids) {
13492: # alias?
13493: 		  $ids=$bighash{'mapalias_'.$thisfn};
13494:               }
13495:               if ($ids) {
13496: # ------------------------------------------------------------------- Has ID(s)
13497:                  my @possibilities=split(/\,/,$ids);
13498:                  if ($#possibilities==0) {
13499: # ----------------------------------------------- There is only one possibility
13500: 		     my ($mapid,$resid)=split(/\./,$ids);
13501: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13502: 						    $resid,$thisfn);
13503:                      if (ref($possibles) eq 'HASH') {
13504:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13505:                              $possibles->{$syval} = 1;
13506:                          }
13507:                      }
13508:                      if ($checkforblock) {
13509:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13510:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
13511:                              if (@blockers) {
13512:                                  $syval = '';
13513:                                  untie(%bighash);
13514:                                  return $env{$cache_str}='';
13515:                              }
13516:                          }
13517:                      }
13518:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13519: # ------------------------------------------ There is more than one possibility
13520:                      my $realpossible=0;
13521:                      foreach my $id (@possibilities) {
13522: 			 my $file=$bighash{'src_'.$id};
13523:                          my $canaccess;
13524:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13525:                              $canaccess = 1;
13526:                          } else { 
13527:                              $canaccess = &allowed('bre',$file);
13528:                          }
13529:                          if ($canaccess) {
13530:          		     my ($mapid,$resid)=split(/\./,$id);
13531:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13532:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13533: 						             $resid,$thisfn);
13534:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
13535:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
13536:                                  if ($checkforblock) {
13537:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
13538:                                      if (@blockers > 0) {
13539:                                          $syval = '';
13540:                                      } else {
13541:                                          $syval = $poss_syval;
13542:                                          $realpossible++;
13543:                                      }
13544:                                  } else {
13545:                                      $syval = $poss_syval;
13546:                                      $realpossible++;
13547:                                  }
13548:                                  if ($syval) {
13549:                                      if (ref($possibles) eq 'HASH') {
13550:                                          $possibles->{$syval} = 1;
13551:                                      }
13552:                                  }
13553:                              }
13554: 			 }
13555:                      }
13556: 		     if ($realpossible!=1) { $syval=''; }
13557:                  } else {
13558:                      $syval='';
13559:                  }
13560: 	      }
13561:               untie(%bighash);
13562:            }
13563:         }
13564:         if ($syval) {
13565: 	    return $env{$cache_str}=$syval;
13566:         }
13567:     }
13568:     &appenv({'request.ambiguous' => $thisfn});
13569:     return $env{$cache_str}='';
13570: }
13571: 
13572: # ---------------------------------------------------------- Return random seed
13573: 
13574: sub numval {
13575:     my $txt=shift;
13576:     $txt=~tr/A-J/0-9/;
13577:     $txt=~tr/a-j/0-9/;
13578:     $txt=~tr/K-T/0-9/;
13579:     $txt=~tr/k-t/0-9/;
13580:     $txt=~tr/U-Z/0-5/;
13581:     $txt=~tr/u-z/0-5/;
13582:     $txt=~s/\D//g;
13583:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13584:     return int($txt);
13585: }
13586: 
13587: sub numval2 {
13588:     my $txt=shift;
13589:     $txt=~tr/A-J/0-9/;
13590:     $txt=~tr/a-j/0-9/;
13591:     $txt=~tr/K-T/0-9/;
13592:     $txt=~tr/k-t/0-9/;
13593:     $txt=~tr/U-Z/0-5/;
13594:     $txt=~tr/u-z/0-5/;
13595:     $txt=~s/\D//g;
13596:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13597:     my $total;
13598:     foreach my $val (@txts) { $total+=$val; }
13599:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13600:     return int($total);
13601: }
13602: 
13603: sub numval3 {
13604:     use integer;
13605:     my $txt=shift;
13606:     $txt=~tr/A-J/0-9/;
13607:     $txt=~tr/a-j/0-9/;
13608:     $txt=~tr/K-T/0-9/;
13609:     $txt=~tr/k-t/0-9/;
13610:     $txt=~tr/U-Z/0-5/;
13611:     $txt=~tr/u-z/0-5/;
13612:     $txt=~s/\D//g;
13613:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13614:     my $total;
13615:     foreach my $val (@txts) { $total+=$val; }
13616:     if ($_64bit) { $total=(($total<<32)>>32); }
13617:     return $total;
13618: }
13619: 
13620: sub digest {
13621:     my ($data)=@_;
13622:     my $digest=&Digest::MD5::md5($data);
13623:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13624:     my ($e,$f);
13625:     {
13626:         use integer;
13627:         $e=($a+$b);
13628:         $f=($c+$d);
13629:         if ($_64bit) {
13630:             $e=(($e<<32)>>32);
13631:             $f=(($f<<32)>>32);
13632:         }
13633:     }
13634:     if (wantarray) {
13635: 	return ($e,$f);
13636:     } else {
13637: 	my $g;
13638: 	{
13639: 	    use integer;
13640: 	    $g=($e+$f);
13641: 	    if ($_64bit) {
13642: 		$g=(($g<<32)>>32);
13643: 	    }
13644: 	}
13645: 	return $g;
13646:     }
13647: }
13648: 
13649: sub latest_rnd_algorithm_id {
13650:     return '64bit5';
13651: }
13652: 
13653: sub get_rand_alg {
13654:     my ($courseid)=@_;
13655:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13656:     if ($courseid) {
13657: 	return $env{"course.$courseid.rndseed"};
13658:     }
13659:     return &latest_rnd_algorithm_id();
13660: }
13661: 
13662: sub validCODE {
13663:     my ($CODE)=@_;
13664:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13665:     return 0;
13666: }
13667: 
13668: sub getCODE {
13669:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13670:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13671: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13672: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13673: 	return $Apache::lonhomework::history{'resource.CODE'};
13674:     }
13675:     return undef;
13676: }
13677: #
13678: #  Determines the random seed for a specific context:
13679: #
13680: # parameters:
13681: #   symb      - in course context the symb for the seed.
13682: #   course_id - The course id of the form domain_coursenum.
13683: #   domain    - Domain for the user.
13684: #   course    - Course for the user.
13685: #   cenv      - environment of the course.
13686: #
13687: # NOTE:
13688: #   All parameters are picked out of the environment if missing
13689: #   or not defined.
13690: #   If a symb cannot be determined the current time is used instead.
13691: #
13692: #  For a given well defined symb, courside, domain, username,
13693: #  and course environment, the seed is reproducible.
13694: #
13695: sub rndseed {
13696:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13697:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13698:     if (!defined($symb)) {
13699: 	unless ($symb=$wsymb) { return time; }
13700:     }
13701:     if (!defined $courseid) { 
13702: 	$courseid=$wcourseid; 
13703:     }
13704:     if (!defined $domain) { $domain=$wdomain; }
13705:     if (!defined $username) { $username=$wusername }
13706: 
13707:     my $which;
13708:     if (defined($cenv->{'rndseed'})) {
13709: 	$which = $cenv->{'rndseed'};
13710:     } else {
13711: 	$which =&get_rand_alg($courseid);
13712:     }
13713:     if (defined(&getCODE())) {
13714: 
13715: 	if ($which eq '64bit5') {
13716: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13717: 	} elsif ($which eq '64bit4') {
13718: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13719: 	} else {
13720: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13721: 	}
13722:     } elsif ($which eq '64bit5') {
13723: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13724:     } elsif ($which eq '64bit4') {
13725: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13726:     } elsif ($which eq '64bit3') {
13727: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13728:     } elsif ($which eq '64bit2') {
13729: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13730:     } elsif ($which eq '64bit') {
13731: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13732:     }
13733:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13734: }
13735: 
13736: sub rndseed_32bit {
13737:     my ($symb,$courseid,$domain,$username)=@_;
13738:     {
13739: 	use integer;
13740: 	my $symbchck=unpack("%32C*",$symb) << 27;
13741: 	my $symbseed=numval($symb) << 22;
13742: 	my $namechck=unpack("%32C*",$username) << 17;
13743: 	my $nameseed=numval($username) << 12;
13744: 	my $domainseed=unpack("%32C*",$domain) << 7;
13745: 	my $courseseed=unpack("%32C*",$courseid);
13746: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13747: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13748: 	#&logthis("rndseed :$num:$symb");
13749: 	if ($_64bit) { $num=(($num<<32)>>32); }
13750: 	return $num;
13751:     }
13752: }
13753: 
13754: sub rndseed_64bit {
13755:     my ($symb,$courseid,$domain,$username)=@_;
13756:     {
13757: 	use integer;
13758: 	my $symbchck=unpack("%32S*",$symb) << 21;
13759: 	my $symbseed=numval($symb) << 10;
13760: 	my $namechck=unpack("%32S*",$username);
13761: 	
13762: 	my $nameseed=numval($username) << 21;
13763: 	my $domainseed=unpack("%32S*",$domain) << 10;
13764: 	my $courseseed=unpack("%32S*",$courseid);
13765: 	
13766: 	my $num1=$symbchck+$symbseed+$namechck;
13767: 	my $num2=$nameseed+$domainseed+$courseseed;
13768: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13769: 	#&logthis("rndseed :$num:$symb");
13770: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13771: 	return "$num1,$num2";
13772:     }
13773: }
13774: 
13775: sub rndseed_64bit2 {
13776:     my ($symb,$courseid,$domain,$username)=@_;
13777:     {
13778: 	use integer;
13779: 	# strings need to be an even # of cahracters long, it it is odd the
13780:         # last characters gets thrown away
13781: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13782: 	my $symbseed=numval($symb) << 10;
13783: 	my $namechck=unpack("%32S*",$username.' ');
13784: 	
13785: 	my $nameseed=numval($username) << 21;
13786: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13787: 	my $courseseed=unpack("%32S*",$courseid.' ');
13788: 	
13789: 	my $num1=$symbchck+$symbseed+$namechck;
13790: 	my $num2=$nameseed+$domainseed+$courseseed;
13791: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13792: 	#&logthis("rndseed :$num:$symb");
13793: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13794: 	return "$num1,$num2";
13795:     }
13796: }
13797: 
13798: sub rndseed_64bit3 {
13799:     my ($symb,$courseid,$domain,$username)=@_;
13800:     {
13801: 	use integer;
13802: 	# strings need to be an even # of cahracters long, it it is odd the
13803:         # last characters gets thrown away
13804: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13805: 	my $symbseed=numval2($symb) << 10;
13806: 	my $namechck=unpack("%32S*",$username.' ');
13807: 	
13808: 	my $nameseed=numval2($username) << 21;
13809: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13810: 	my $courseseed=unpack("%32S*",$courseid.' ');
13811: 	
13812: 	my $num1=$symbchck+$symbseed+$namechck;
13813: 	my $num2=$nameseed+$domainseed+$courseseed;
13814: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13815: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13816: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13817: 	
13818: 	return "$num1:$num2";
13819:     }
13820: }
13821: 
13822: sub rndseed_64bit4 {
13823:     my ($symb,$courseid,$domain,$username)=@_;
13824:     {
13825: 	use integer;
13826: 	# strings need to be an even # of cahracters long, it it is odd the
13827:         # last characters gets thrown away
13828: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13829: 	my $symbseed=numval3($symb) << 10;
13830: 	my $namechck=unpack("%32S*",$username.' ');
13831: 	
13832: 	my $nameseed=numval3($username) << 21;
13833: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13834: 	my $courseseed=unpack("%32S*",$courseid.' ');
13835: 	
13836: 	my $num1=$symbchck+$symbseed+$namechck;
13837: 	my $num2=$nameseed+$domainseed+$courseseed;
13838: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13839: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13840: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13841: 	
13842: 	return "$num1:$num2";
13843:     }
13844: }
13845: 
13846: sub rndseed_64bit5 {
13847:     my ($symb,$courseid,$domain,$username)=@_;
13848:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
13849:     return "$num1:$num2";
13850: }
13851: 
13852: sub rndseed_CODE_64bit {
13853:     my ($symb,$courseid,$domain,$username)=@_;
13854:     {
13855: 	use integer;
13856: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13857: 	my $symbseed=numval2($symb);
13858: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13859: 	my $CODEseed=numval(&getCODE());
13860: 	my $courseseed=unpack("%32S*",$courseid.' ');
13861: 	my $num1=$symbseed+$CODEchck;
13862: 	my $num2=$CODEseed+$courseseed+$symbchck;
13863: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13864: 	#&logthis("rndseed :$num1:$num2:$symb");
13865: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13866: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13867: 	return "$num1:$num2";
13868:     }
13869: }
13870: 
13871: sub rndseed_CODE_64bit4 {
13872:     my ($symb,$courseid,$domain,$username)=@_;
13873:     {
13874: 	use integer;
13875: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13876: 	my $symbseed=numval3($symb);
13877: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13878: 	my $CODEseed=numval3(&getCODE());
13879: 	my $courseseed=unpack("%32S*",$courseid.' ');
13880: 	my $num1=$symbseed+$CODEchck;
13881: 	my $num2=$CODEseed+$courseseed+$symbchck;
13882: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13883: 	#&logthis("rndseed :$num1:$num2:$symb");
13884: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13885: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13886: 	return "$num1:$num2";
13887:     }
13888: }
13889: 
13890: sub rndseed_CODE_64bit5 {
13891:     my ($symb,$courseid,$domain,$username)=@_;
13892:     my $code = &getCODE();
13893:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
13894:     return "$num1:$num2";
13895: }
13896: 
13897: sub setup_random_from_rndseed {
13898:     my ($rndseed)=@_;
13899:     if ($rndseed =~/([,:])/) {
13900:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
13901:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
13902:             &Math::Random::random_set_seed_from_phrase($rndseed);
13903:         } else {
13904:             &Math::Random::random_set_seed($num1,$num2);
13905:         }
13906:     } else {
13907: 	&Math::Random::random_set_seed_from_phrase($rndseed);
13908:     }
13909: }
13910: 
13911: sub latest_receipt_algorithm_id {
13912:     return 'receipt3';
13913: }
13914: 
13915: sub recunique {
13916:     my $fucourseid=shift;
13917:     my $unique;
13918:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
13919: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13920: 	$unique=$env{"course.$fucourseid.internal.encseed"};
13921:     } else {
13922: 	$unique=$perlvar{'lonReceipt'};
13923:     }
13924:     return unpack("%32C*",$unique);
13925: }
13926: 
13927: sub recprefix {
13928:     my $fucourseid=shift;
13929:     my $prefix;
13930:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
13931: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13932: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
13933:     } else {
13934: 	$prefix=$perlvar{'lonHostID'};
13935:     }
13936:     return unpack("%32C*",$prefix);
13937: }
13938: 
13939: sub ireceipt {
13940:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
13941: 
13942:     my $return =&recprefix($fucourseid).'-';
13943: 
13944:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
13945: 	$env{'request.state'} eq 'construct') {
13946: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
13947: 	return $return;
13948:     }
13949: 
13950:     my $cuname=unpack("%32C*",$funame);
13951:     my $cudom=unpack("%32C*",$fudom);
13952:     my $cucourseid=unpack("%32C*",$fucourseid);
13953:     my $cusymb=unpack("%32C*",$fusymb);
13954:     my $cunique=&recunique($fucourseid);
13955:     my $cpart=unpack("%32S*",$part);
13956:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
13957: 
13958: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
13959: 			       
13960: 	$return.= ($cunique%$cuname+
13961: 		   $cunique%$cudom+
13962: 		   $cusymb%$cuname+
13963: 		   $cusymb%$cudom+
13964: 		   $cucourseid%$cuname+
13965: 		   $cucourseid%$cudom+
13966: 		   $cpart%$cuname+
13967: 		   $cpart%$cudom);
13968:     } else {
13969: 	$return.= ($cunique%$cuname+
13970: 		   $cunique%$cudom+
13971: 		   $cusymb%$cuname+
13972: 		   $cusymb%$cudom+
13973: 		   $cucourseid%$cuname+
13974: 		   $cucourseid%$cudom);
13975:     }
13976:     return $return;
13977: }
13978: 
13979: sub receipt {
13980:     my ($part)=@_;
13981:     my ($symb,$courseid,$domain,$name) = &whichuser();
13982:     return &ireceipt($name,$domain,$courseid,$symb,$part);
13983: }
13984: 
13985: sub whichuser {
13986:     my ($passedsymb)=@_;
13987:     my ($symb,$courseid,$domain,$name,$publicuser);
13988:     if (defined($env{'form.grade_symb'})) {
13989: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
13990: 	my $allowed=&allowed('vgr',$tmp_courseid);
13991: 	if (!$allowed &&
13992: 	    exists($env{'request.course.sec'}) &&
13993: 	    $env{'request.course.sec'} !~ /^\s*$/) {
13994: 	    $allowed=&allowed('vgr',$tmp_courseid.
13995: 			      '/'.$env{'request.course.sec'});
13996: 	}
13997: 	if ($allowed) {
13998: 	    ($symb)=&get_env_multiple('form.grade_symb');
13999: 	    $courseid=$tmp_courseid;
14000: 	    ($domain)=&get_env_multiple('form.grade_domain');
14001: 	    ($name)=&get_env_multiple('form.grade_username');
14002: 	    return ($symb,$courseid,$domain,$name,$publicuser);
14003: 	}
14004:     }
14005:     if (!$passedsymb) {
14006: 	$symb=&symbread();
14007:     } else {
14008: 	$symb=$passedsymb;
14009:     }
14010:     $courseid=$env{'request.course.id'};
14011:     $domain=$env{'user.domain'};
14012:     $name=$env{'user.name'};
14013:     if ($name eq 'public' && $domain eq 'public') {
14014: 	if (!defined($env{'form.username'})) {
14015: 	    $env{'form.username'}.=time.rand(10000000);
14016: 	}
14017: 	$name.=$env{'form.username'};
14018:     }
14019:     return ($symb,$courseid,$domain,$name,$publicuser);
14020: 
14021: }
14022: 
14023: # ------------------------------------------------------------ Serves up a file
14024: # returns either the contents of the file or 
14025: # -1 if the file doesn't exist
14026: #
14027: # if the target is a file that was uploaded via DOCS, 
14028: # a check will be made to see if a current copy exists on the local server,
14029: # if it does this will be served, otherwise a copy will be retrieved from
14030: # the home server for the course and stored in /home/httpd/html/userfiles on
14031: # the local server.   
14032: 
14033: sub getfile {
14034:     my ($file) = @_;
14035:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
14036:     &repcopy($file);
14037:     return &readfile($file);
14038: }
14039: 
14040: sub repcopy_userfile {
14041:     my ($file)=@_;
14042:     my $londocroot = $perlvar{'lonDocRoot'};
14043:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
14044:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
14045:     my ($cdom,$cnum,$filename) = 
14046: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
14047:     my $uri="/uploaded/$cdom/$cnum/$filename";
14048:     if (-e "$file") {
14049: # we already have a local copy, check it out
14050: 	my @fileinfo = stat($file);
14051: 	my $rtncode;
14052: 	my $info;
14053: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
14054: 	if ($lwpresp ne 'ok') {
14055: # there is no such file anymore, even though we had a local copy
14056: 	    if ($rtncode eq '404') {
14057: 		unlink($file);
14058: 	    }
14059: 	    return -1;
14060: 	}
14061: 	if ($info < $fileinfo[9]) {
14062: # nice, the file we have is up-to-date, just say okay
14063: 	    return 'ok';
14064: 	} else {
14065: # the file is outdated, get rid of it
14066: 	    unlink($file);
14067: 	}
14068:     }
14069: # one way or the other, at this point, we don't have the file
14070: # construct the correct path for the file
14071:     my @parts = ($cdom,$cnum); 
14072:     if ($filename =~ m|^(.+)/[^/]+$|) {
14073: 	push @parts, split(/\//,$1);
14074:     }
14075:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14076:     foreach my $part (@parts) {
14077: 	$path .= '/'.$part;
14078: 	if (!-e $path) {
14079: 	    mkdir($path,0770);
14080: 	}
14081:     }
14082: # now the path exists for sure
14083: # get a user agent
14084:     my $transferfile=$file.'.in.transfer';
14085: # FIXME: this should flock
14086:     if (-e $transferfile) { return 'ok'; }
14087:     my $request;
14088:     $uri=~s/^\///;
14089:     my $homeserver = &homeserver($cnum,$cdom);
14090:     my $hostname = &hostname($homeserver);
14091:     my $protocol = $protocol{$homeserver};
14092:     $protocol = 'http' if ($protocol ne 'https');
14093:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
14094:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
14095: # did it work?
14096:     if ($response->is_error()) {
14097: 	unlink($transferfile);
14098: 	&logthis("Userfile repcopy failed for $uri");
14099: 	return -1;
14100:     }
14101: # worked, rename the transfer file
14102:     rename($transferfile,$file);
14103:     return 'ok';
14104: }
14105: 
14106: sub tokenwrapper {
14107:     my $uri=shift;
14108:     $uri=~s|^https?\://([^/]+)||;
14109:     $uri=~s|^/||;
14110:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14111:     my $token=$1;
14112:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14113:     if ($udom && $uname && $file) {
14114: 	$file=~s|(\?\.*)*$||;
14115:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14116:         my $homeserver = &homeserver($uname,$udom);
14117:         my $hostname = &hostname($homeserver);
14118:         my $protocol = $protocol{$homeserver};
14119:         $protocol = 'http' if ($protocol ne 'https');
14120:         return $protocol.'://'.$hostname.'/'.$uri.
14121:                (($uri=~/\?/)?'&':'?').'token='.$token.
14122:                                '&tokenissued='.$perlvar{'lonHostID'};
14123:     } else {
14124:         return '/adm/notfound.html';
14125:     }
14126: }
14127: 
14128: # call with reqtype HEAD: get last modification time
14129: # call with reqtype GET: get the file contents
14130: # Do not call this with reqtype GET for large files! It loads everything into memory
14131: #
14132: sub getuploaded {
14133:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14134:     $uri=~s/^\///;
14135:     my $homeserver = &homeserver($cnum,$cdom);
14136:     my $hostname = &hostname($homeserver);
14137:     my $protocol = $protocol{$homeserver};
14138:     $protocol = 'http' if ($protocol ne 'https');
14139:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14140:     my $request=new HTTP::Request($reqtype,$uri);
14141:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14142:     $$rtncode = $response->code;
14143:     if (! $response->is_success()) {
14144: 	return 'failed';
14145:     }      
14146:     if ($reqtype eq 'HEAD') {
14147: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14148:     } elsif ($reqtype eq 'GET') {
14149: 	$$info = $response->content;
14150:     }
14151:     return 'ok';
14152: }
14153: 
14154: sub readfile {
14155:     my $file = shift;
14156:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14157:     my $fh;
14158:     open($fh,"<",$file);
14159:     my $a='';
14160:     while (my $line = <$fh>) { $a .= $line; }
14161:     return $a;
14162: }
14163: 
14164: sub filelocation {
14165:     my ($dir,$file) = @_;
14166:     my $location;
14167:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14168: 
14169:     if ($file =~ m-^/adm/-) {
14170: 	$file=~s-^/adm/wrapper/-/-;
14171: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14172:     }
14173: 
14174:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14175:         $location = $file;
14176:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14177:         my ($udom,$uname,$filename)=
14178:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14179:         my $home=&homeserver($uname,$udom);
14180:         my $is_me=0;
14181:         my @ids=&current_machine_ids();
14182:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14183:         if ($is_me) {
14184:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14185:         } else {
14186:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14187:   	      $udom.'/'.$uname.'/'.$filename;
14188:         }
14189:     } elsif ($file =~ m-^/adm/-) {
14190: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14191:     } else {
14192:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14193:         $file=~s:^/(res|priv)/:/:;
14194:         my $space=$1;
14195:         if ( !( $file =~ m:^/:) ) {
14196:             $location = $dir. '/'.$file;
14197:         } else {
14198:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14199:         }
14200:     }
14201:     $location=~s://+:/:g; # remove duplicate /
14202:     while ($location=~m{/\.\./}) {
14203: 	if ($location =~ m{/[^/]+/\.\./}) {
14204: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14205: 	} else {
14206: 	    $location=~ s{/\.\./}{/}g;
14207: 	}
14208:     } #remove dir/..
14209:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14210:     return $location;
14211: }
14212: 
14213: sub hreflocation {
14214:     my ($dir,$file)=@_;
14215:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14216: 	$file=filelocation($dir,$file);
14217:     } elsif ($file=~m-^/adm/-) {
14218: 	$file=~s-^/adm/wrapper/-/-;
14219: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14220:     }
14221:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14222: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14223:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14224: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14225: 	        {/uploaded/$1/$2/}x;
14226:     }
14227:     if ($file=~ m{^/userfiles/}) {
14228: 	$file =~ s{^/userfiles/}{/uploaded/};
14229:     }
14230:     return $file;
14231: }
14232: 
14233: 
14234: 
14235: 
14236: 
14237: sub current_machine_domains {
14238:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14239: }
14240: 
14241: sub machine_domains {
14242:     my ($hostname) = @_;
14243:     my @domains;
14244:     my %hostname = &all_hostnames();
14245:     while( my($id, $name) = each(%hostname)) {
14246: #	&logthis("-$id-$name-$hostname-");
14247: 	if ($hostname eq $name) {
14248: 	    push(@domains,&host_domain($id));
14249: 	}
14250:     }
14251:     return @domains;
14252: }
14253: 
14254: sub current_machine_ids {
14255:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14256: }
14257: 
14258: sub machine_ids {
14259:     my ($hostname) = @_;
14260:     $hostname ||= &hostname($perlvar{'lonHostID'});
14261:     my @ids;
14262:     my %name_to_host = &all_names();
14263:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14264: 	return @{ $name_to_host{$hostname} };
14265:     }
14266:     return;
14267: }
14268: 
14269: sub additional_machine_domains {
14270:     my @domains;
14271:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
14272:     while( my $line = <$fh>) {
14273:         $line =~ s/\s//g;
14274:         push(@domains,$line);
14275:     }
14276:     return @domains;
14277: }
14278: 
14279: sub default_login_domain {
14280:     my $domain = $perlvar{'lonDefDomain'};
14281:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14282:     foreach my $posdom (&current_machine_domains(),
14283:                         &additional_machine_domains()) {
14284:         if (lc($posdom) eq lc($testdomain)) {
14285:             $domain=$posdom;
14286:             last;
14287:         }
14288:     }
14289:     return $domain;
14290: }
14291: 
14292: sub shared_institution {
14293:     my ($dom,$lonhost) = @_;
14294:     if ($lonhost eq '') {
14295:         $lonhost = $perlvar{'lonHostID'};
14296:     }
14297:     my $same_intdom;
14298:     my $hostintdom = &internet_dom($lonhost);
14299:     if ($hostintdom ne '') {
14300:         my %iphost = &get_iphost();
14301:         my $primary_id = &domain($dom,'primary');
14302:         my $primary_ip = &get_host_ip($primary_id);
14303:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14304:             foreach my $id (@{$iphost{$primary_ip}}) {
14305:                 my $intdom = &internet_dom($id);
14306:                 if ($intdom eq $hostintdom) {
14307:                     $same_intdom = 1;
14308:                     last;
14309:                 }
14310:             }
14311:         }
14312:     }
14313:     return $same_intdom;
14314: }
14315: 
14316: sub uses_sts {
14317:     my ($ignore_cache) = @_;
14318:     my $lonhost = $perlvar{'lonHostID'};
14319:     my $hostname = &hostname($lonhost);
14320:     my $sts_on;
14321:     if ($protocol{$lonhost} eq 'https') {
14322:         my $cachetime = 12*3600;
14323:         if (!$ignore_cache) {
14324:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14325:             if (defined($cached)) {
14326:                 return $sts_on;
14327:             }
14328:         }
14329:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14330:         my $request=new HTTP::Request('HEAD',$url);
14331:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14332:         if ($response->is_success) {
14333:             my $has_sts = $response->header('Strict-Transport-Security');
14334:             if ($has_sts eq '') {
14335:                 $sts_on = 0;
14336:             } else {
14337:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14338:                     my $maxage = $1;
14339:                     if ($maxage) {
14340:                         $sts_on = 1;
14341:                     } else {
14342:                         $sts_on = 0;
14343:                     }
14344:                 } else {
14345:                     $sts_on = 0;
14346:                 }
14347:             }
14348:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14349:         }
14350:     }
14351:     return;
14352: }
14353: 
14354: sub get_requestor_ip {
14355:     my ($r,$nolookup,$noproxy) = @_;
14356:     my $from_ip;
14357:     if (ref($r)) {
14358:         if ($r->can('useragent_ip')) {
14359:             if ($noproxy && $r->can('client_ip')) {
14360:                 $from_ip = $r->client_ip();
14361:             } else {
14362:                 $from_ip = $r->useragent_ip();
14363:             }
14364:         } elsif ($r->connection->can('remote_ip')) {
14365:             $from_ip = $r->connection->remote_ip();
14366:         } else {
14367:             $from_ip = $r->get_remote_host($nolookup);
14368:         }
14369:     } else {
14370:         $from_ip = $ENV{'REMOTE_ADDR'};
14371:     }
14372:     return $from_ip if ($noproxy); 
14373:     # Who controls proxy settings for server
14374:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14375:     my $proxyinfo = &get_proxy_settings($dom_in_use);
14376:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14377:         if ($proxyinfo->{'vpnint'}) {
14378:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14379:                 return $from_ip;
14380:             }
14381:         }
14382:         if ($proxyinfo->{'trusted'}) {
14383:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14384:                 my $ipheader = $proxyinfo->{'ipheader'};
14385:                 my ($ip,$xfor);
14386:                 if (ref($r)) {
14387:                     if ($ipheader) {
14388:                         $ip = $r->headers_in->{$ipheader};
14389:                     }
14390:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
14391:                 } else {
14392:                     if ($ipheader) {
14393:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
14394:                     }
14395:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14396:                 }
14397:                 if (($ip eq '') && ($xfor ne '')) {
14398:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14399:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14400:                             $ip = $poss_ip;
14401:                             last;
14402:                         }
14403:                     }
14404:                 }
14405:                 if ($ip ne '') {
14406:                     return $ip;
14407:                 }
14408:             }
14409:         }
14410:     }
14411:     return $from_ip;
14412: }
14413: 
14414: sub get_proxy_settings {
14415:     my ($dom_in_use) = @_;
14416:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14417:     my $proxyinfo = {
14418:                        ipheader => $domdefaults{'waf_ipheader'},
14419:                        trusted  => $domdefaults{'waf_trusted'},
14420:                        vpnint   => $domdefaults{'waf_vpnint'},
14421:                        vpnext   => $domdefaults{'waf_vpnext'},
14422:                     };
14423:     return $proxyinfo;
14424: }
14425: 
14426: sub ip_match {
14427:     my ($ip,$pattern_str) = @_;
14428:     $ip=Net::CIDR::cidrvalidate($ip);
14429:     if ($ip) {
14430:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14431:     }
14432:     return;
14433: }
14434: 
14435: sub get_proxy_alias {
14436:     my $lonhost = $perlvar{'lonHostID'};
14437:     if ($lonhost ne '') {
14438:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonhost);
14439:         if ($cached) {
14440:             return $alias;
14441:         }
14442:         my $dom = &Apache::lonnet::host_domain($lonhost);
14443:         if ($dom ne '') {
14444:             my $cachetime = 60*60*24;
14445:             my %domconfig =
14446:                 &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14447:             my $alias;
14448:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14449:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14450:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonhost};
14451:                 }
14452:             }
14453:             return &do_cache_new('proxyalias',$lonhost,$alias,$cachetime);
14454:         }
14455:     }
14456:     return;
14457: }
14458: 
14459: # ------------------------------------------------------------- Declutters URLs
14460: 
14461: sub declutter {
14462:     my $thisfn=shift;
14463:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14464:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14465:         $thisfn=~s{^/home/httpd/html}{};
14466:     }
14467:     $thisfn=~s/^\///;
14468:     $thisfn=~s|^adm/wrapper/||;
14469:     $thisfn=~s|^adm/coursedocs/showdoc/||;
14470:     $thisfn=~s/^res\///;
14471:     $thisfn=~s/^priv\///;
14472:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14473:         $thisfn=~s/\?.+$//;
14474:     }
14475:     return $thisfn;
14476: }
14477: 
14478: # ------------------------------------------------------------- Clutter up URLs
14479: 
14480: sub clutter {
14481:     my $thisfn='/'.&declutter(shift);
14482:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14483: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14484:        $thisfn='/res'.$thisfn; 
14485:     }
14486:     if ($thisfn !~m|^/adm|) {
14487: 	if ($thisfn =~ m|^/ext/|) {
14488: 	    $thisfn='/adm/wrapper'.$thisfn;
14489: 	} else {
14490: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14491: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14492: 	    if ($embstyle eq 'ssi'
14493: 		|| ($embstyle eq 'hdn')
14494: 		|| ($embstyle eq 'rat')
14495: 		|| ($embstyle eq 'prv')
14496: 		|| ($embstyle eq 'ign')) {
14497: 		#do nothing with these
14498: 	    } elsif (($embstyle eq 'img') 
14499: 		|| ($embstyle eq 'emb')
14500: 		|| ($embstyle eq 'wrp')) {
14501: 		$thisfn='/adm/wrapper'.$thisfn;
14502: 	    } elsif ($embstyle eq 'unk'
14503: 		     && $thisfn!~/\.(sequence|page)$/) {
14504: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14505: 	    } else {
14506: #		&logthis("Got a blank emb style");
14507: 	    }
14508: 	}
14509:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14510:         $thisfn='/adm/wrapper'.$thisfn;
14511:     }
14512:     return $thisfn;
14513: }
14514: 
14515: sub clutter_with_no_wrapper {
14516:     my $uri = &clutter(shift);
14517:     if ($uri =~ m-^/adm/-) {
14518: 	$uri =~ s-^/adm/wrapper/-/-;
14519: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14520:     }
14521:     return $uri;
14522: }
14523: 
14524: sub freeze_escape {
14525:     my ($value)=@_;
14526:     if (ref($value)) {
14527: 	$value=&nfreeze($value);
14528: 	return '__FROZEN__'.&escape($value);
14529:     }
14530:     return &escape($value);
14531: }
14532: 
14533: 
14534: sub thaw_unescape {
14535:     my ($value)=@_;
14536:     if ($value =~ /^__FROZEN__/) {
14537: 	substr($value,0,10,undef);
14538: 	$value=&unescape($value);
14539: 	return &thaw($value);
14540:     }
14541:     return &unescape($value);
14542: }
14543: 
14544: sub correct_line_ends {
14545:     my ($result)=@_;
14546:     $$result =~s/\r\n/\n/mg;
14547:     $$result =~s/\r/\n/mg;
14548: }
14549: # ================================================================ Main Program
14550: 
14551: sub goodbye {
14552:    &logthis("Starting Shut down");
14553: #not converted to using infrastruture and probably shouldn't be
14554:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14555: #converted
14556: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14557:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14558: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14559: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14560: #1.1 only
14561: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14562: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14563: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14564: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14565:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14566:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14567:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14568:    &flushcourselogs();
14569:    &logthis("Shutting down");
14570: }
14571: 
14572: sub get_dns {
14573:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14574:     if (!$ignore_cache) {
14575: 	my ($content,$cached)=
14576: 	    &Apache::lonnet::is_cached_new('dns',$url);
14577: 	if ($cached) {
14578: 	    &$func($content,$hashref);
14579: 	    return;
14580: 	}
14581:     }
14582: 
14583:     my %alldns;
14584:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14585:         foreach my $dns (<$config>) {
14586: 	    next if ($dns !~ /^\^(\S*)/x);
14587:             my $line = $1;
14588:             my ($host,$protocol) = split(/:/,$line);
14589:             if ($protocol ne 'https') {
14590:                 $protocol = 'http';
14591:             }
14592: 	    $alldns{$host} = $protocol;
14593:         }
14594:         close($config);
14595:     }
14596:     while (%alldns) {
14597: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14598: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14599:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14600:         delete($alldns{$dns});
14601: 	next if ($response->is_error());
14602:         if ($url eq '/adm/dns/loncapaCRL') {
14603:             return &$func($response);
14604:         } else {
14605: 	    my @content = split("\n",$response->content);
14606: 	    unless ($nocache) {
14607: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14608: 	    }
14609: 	    &$func(\@content,$hashref);
14610:             return;
14611:         }
14612:     }
14613:     my $which = (split('/',$url,4))[3];
14614:     if ($which eq 'loncapaCRL') {
14615:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14616:         if (-e $diskfile) {
14617:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14618:         } else {
14619:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14620:         }
14621:     } else {
14622:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14623:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14624:             my @content = <$config>;
14625:             close($config);
14626:             &$func(\@content,$hashref);
14627:         }
14628:     }
14629:     return;
14630: }
14631: 
14632: # ------------------------------------------------------Get DNS checksums file
14633: sub parse_dns_checksums_tab {
14634:     my ($lines,$hashref) = @_;
14635:     my $lonhost = $perlvar{'lonHostID'};
14636:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14637:     my $loncaparev = &get_server_loncaparev($machine_dom);
14638:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14639:     my $webconfdir = '/etc/httpd/conf';
14640:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14641:         $webconfdir = '/etc/apache2';
14642:     } elsif ($distro =~ /^sles(\d+)$/) {
14643:         if ($1 >= 10) {
14644:             $webconfdir = '/etc/apache2';
14645:         }
14646:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14647:         if ($1 >= 10.0) {
14648:             $webconfdir = '/etc/apache2';
14649:         }
14650:     }
14651:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14652:     my (%chksum,%revnum);
14653:     if (ref($lines) eq 'ARRAY') {
14654:         chomp(@{$lines});
14655:         my $version = shift(@{$lines});
14656:         if ($version eq $release) {  
14657:             foreach my $line (@{$lines}) {
14658:                 my ($file,$version,$shasum) = split(/,/,$line);
14659:                 if ($file =~ m{^/etc/httpd/conf}) {
14660:                     if ($webconfdir eq '/etc/apache2') {
14661:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14662:                     }
14663:                 }
14664:                 $chksum{$file} = $shasum;
14665:                 $revnum{$file} = $version;
14666:             }
14667:             if (ref($hashref) eq 'HASH') {
14668:                 %{$hashref} = (
14669:                                 sums     => \%chksum,
14670:                                 versions => \%revnum,
14671:                               );
14672:             }
14673:         }
14674:     }
14675:     return;
14676: }
14677: 
14678: sub fetch_dns_checksums {
14679:     my %checksums;
14680:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14681:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14682:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14683:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14684:              \%checksums);
14685:     return \%checksums;
14686: }
14687: 
14688: sub fetch_crl_pemfile {
14689:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14690: }
14691: 
14692: sub save_crl_pem {
14693:     my ($response) = @_;
14694:     my ($msg,$hadchanges);
14695:     if (ref($response)) {
14696:         my $now = time;
14697:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14698:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14699:         if (open(my $fh,'>',"$tmpcrl")) {
14700:             print $fh $response->content;
14701:             close($fh);
14702:             if (-e $lonca) {
14703:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14704:                     my $check = <PIPE>;
14705:                     close(PIPE);
14706:                     chomp($check);
14707:                     if ($check eq 'verify OK') {
14708:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14709:                         my $backup;
14710:                         if (-e $dest) {
14711:                             if (&File::Copy::move($dest,"$dest.bak")) {
14712:                                 $backup = 'ok';
14713:                             }
14714:                         }
14715:                         if (&File::Copy::move($tmpcrl,$dest)) {
14716:                             $msg = 'ok';
14717:                             if ($backup) {
14718:                                 my (%oldnums,%newnums);
14719:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
14720:                                     while (<PIPE>) {
14721:                                         $oldnums{(split(/:/))[1]} = 1;
14722:                                     }
14723:                                     close(PIPE);
14724:                                 }
14725:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
14726:                                     while(<PIPE>) {
14727:                                         $newnums{(split(/:/))[1]} = 1;
14728:                                     }
14729:                                     close(PIPE);
14730:                                 }
14731:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
14732:                                     unless (exists($oldnums{$key})) {
14733:                                         $hadchanges = 1;
14734:                                         last;
14735:                                     }
14736:                                 }
14737:                                 unless ($hadchanges) {
14738:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
14739:                                         unless (exists($newnums{$key})) {
14740:                                             $hadchanges = 1;
14741:                                             last;
14742:                                         }
14743:                                     }
14744:                                 }
14745:                             }
14746:                         }
14747:                     } else {
14748:                         unlink($tmpcrl);
14749:                     }
14750:                 } else {
14751:                     unlink($tmpcrl);
14752:                 }
14753:             } else {
14754:                 unlink($tmpcrl);
14755:             }
14756:         }
14757:     }
14758:     return ($msg,$hadchanges);
14759: }
14760: 
14761: # ------------------------------------------------------------ Read domain file
14762: {
14763:     my $loaded;
14764:     my %domain;
14765: 
14766:     sub parse_domain_tab {
14767: 	my ($lines) = @_;
14768: 	foreach my $line (@$lines) {
14769: 	    next if ($line =~ /^(\#|\s*$ )/x);
14770: 
14771: 	    chomp($line);
14772: 	    my ($name,@elements) = split(/:/,$line,9);
14773: 	    my %this_domain;
14774: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
14775: 			       'lang_def', 'city', 'longi', 'lati',
14776: 			       'primary') {
14777: 		$this_domain{$field} = shift(@elements);
14778: 	    }
14779: 	    $domain{$name} = \%this_domain;
14780: 	}
14781:     }
14782: 
14783:     sub reset_domain_info {
14784: 	undef($loaded);
14785: 	undef(%domain);
14786:     }
14787: 
14788:     sub load_domain_tab {
14789: 	my ($ignore_cache,$nocache) = @_;
14790: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
14791: 	my $fh;
14792: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
14793: 	    my @lines = <$fh>;
14794: 	    &parse_domain_tab(\@lines);
14795: 	}
14796: 	close($fh);
14797: 	$loaded = 1;
14798:     }
14799: 
14800:     sub domain {
14801: 	&load_domain_tab() if (!$loaded);
14802: 
14803: 	my ($name,$what) = @_;
14804: 	return if ( !exists($domain{$name}) );
14805: 
14806: 	if (!$what) {
14807: 	    return $domain{$name}{'description'};
14808: 	}
14809: 	return $domain{$name}{$what};
14810:     }
14811: 
14812:     sub domain_info {
14813:         &load_domain_tab() if (!$loaded);
14814:         return %domain;
14815:     }
14816: 
14817: }
14818: 
14819: 
14820: # ------------------------------------------------------------- Read hosts file
14821: {
14822:     my %hostname;
14823:     my %hostdom;
14824:     my %libserv;
14825:     my $loaded;
14826:     my %name_to_host;
14827:     my %internetdom;
14828:     my %LC_dns_serv;
14829: 
14830:     sub parse_hosts_tab {
14831: 	my ($file) = @_;
14832: 	foreach my $configline (@$file) {
14833: 	    next if ($configline =~ /^(\#|\s*$ )/x);
14834:             chomp($configline);
14835: 	    if ($configline =~ /^\^/) {
14836:                 if ($configline =~ /^\^([\w.\-]+)/) {
14837:                     $LC_dns_serv{$1} = 1;
14838:                 }
14839:                 next;
14840:             }
14841: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
14842: 	    $name=~s/\s//g;
14843: 	    if ($id && $domain && $role && $name) {
14844:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
14845:                     my $curr = $hostname{$id};
14846:                     my $skip;
14847:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
14848:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
14849:                             $skip = 1;
14850:                         } else {
14851:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
14852:                         }
14853:                     }
14854:                     unless ($skip) {
14855:                         push(@{$name_to_host{$name}},$id);
14856:                     }
14857:                 } else {
14858:                     push(@{$name_to_host{$name}},$id);
14859:                 }
14860: 		$hostname{$id}=$name;
14861: 		$hostdom{$id}=$domain;
14862: 		if ($role eq 'library') { $libserv{$id}=$name; }
14863:                 if (defined($protocol)) {
14864:                     if ($protocol eq 'https') {
14865:                         $protocol{$id} = $protocol;
14866:                     } else {
14867:                         $protocol{$id} = 'http'; 
14868:                     }
14869:                 } else {
14870:                     $protocol{$id} = 'http';
14871:                 }
14872:                 if (defined($intdom)) {
14873:                     $internetdom{$id} = $intdom;
14874:                 }
14875: 	    }
14876: 	}
14877:     }
14878:     
14879:     sub reset_hosts_info {
14880: 	&purge_remembered();
14881: 	&reset_domain_info();
14882: 	&reset_hosts_ip_info();
14883:         undef(%internetdom);
14884: 	undef(%name_to_host);
14885: 	undef(%hostname);
14886: 	undef(%hostdom);
14887: 	undef(%libserv);
14888: 	undef($loaded);
14889:     }
14890: 
14891:     sub load_hosts_tab {
14892: 	my ($ignore_cache,$nocache) = @_;
14893: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
14894: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
14895: 	my @config = <$config>;
14896: 	&parse_hosts_tab(\@config);
14897: 	close($config);
14898: 	$loaded=1;
14899:     }
14900: 
14901:     sub hostname {
14902: 	&load_hosts_tab() if (!$loaded);
14903: 
14904: 	my ($lonid) = @_;
14905: 	return $hostname{$lonid};
14906:     }
14907: 
14908:     sub all_hostnames {
14909: 	&load_hosts_tab() if (!$loaded);
14910: 
14911: 	return %hostname;
14912:     }
14913: 
14914:     sub all_names {
14915:         my ($ignore_cache,$nocache) = @_;
14916: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
14917: 
14918: 	return %name_to_host;
14919:     }
14920: 
14921:     sub all_host_domain {
14922:         &load_hosts_tab() if (!$loaded);
14923:         return %hostdom;
14924:     }
14925: 
14926:     sub all_host_intdom {
14927:         &load_hosts_tab() if (!$loaded);
14928:         return %internetdom;
14929:     }
14930: 
14931:     sub is_library {
14932: 	&load_hosts_tab() if (!$loaded);
14933: 
14934: 	return exists($libserv{$_[0]});
14935:     }
14936: 
14937:     sub all_library {
14938: 	&load_hosts_tab() if (!$loaded);
14939: 
14940: 	return %libserv;
14941:     }
14942: 
14943:     sub unique_library {
14944: 	#2x reverse removes all hostnames that appear more than once
14945:         my %unique = reverse &all_library();
14946:         return reverse %unique;
14947:     }
14948: 
14949:     sub get_servers {
14950: 	&load_hosts_tab() if (!$loaded);
14951: 
14952: 	my ($domain,$type) = @_;
14953: 	my %possible_hosts = ($type eq 'library') ? %libserv
14954: 	                                          : %hostname;
14955: 	my %result;
14956: 	if (ref($domain) eq 'ARRAY') {
14957: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14958: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
14959: 		    $result{$host} = $hostname;
14960: 		}
14961: 	    }
14962: 	} else {
14963: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14964: 		if ($hostdom{$host} eq $domain) {
14965: 		    $result{$host} = $hostname;
14966: 		}
14967: 	    }
14968: 	}
14969: 	return %result;
14970:     }
14971: 
14972:     sub get_unique_servers {
14973:         my %unique = reverse &get_servers(@_);
14974: 	return reverse %unique;
14975:     }
14976: 
14977:     sub host_domain {
14978: 	&load_hosts_tab() if (!$loaded);
14979: 
14980: 	my ($lonid) = @_;
14981: 	return $hostdom{$lonid};
14982:     }
14983: 
14984:     sub all_domains {
14985: 	&load_hosts_tab() if (!$loaded);
14986: 
14987: 	my %seen;
14988: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
14989: 	return @uniq;
14990:     }
14991: 
14992:     sub internet_dom {
14993:         &load_hosts_tab() if (!$loaded);
14994: 
14995:         my ($lonid) = @_;
14996:         return $internetdom{$lonid};
14997:     }
14998: 
14999:     sub is_LC_dns {
15000:         &load_hosts_tab() if (!$loaded);
15001: 
15002:         my ($hostname) = @_;
15003:         return exists($LC_dns_serv{$hostname});
15004:     }
15005: 
15006: }
15007: 
15008: { 
15009:     my %iphost;
15010:     my %name_to_ip;
15011:     my %lonid_to_ip;
15012: 
15013:     sub get_hosts_from_ip {
15014: 	my ($ip) = @_;
15015: 	my %iphosts = &get_iphost();
15016: 	if (ref($iphosts{$ip})) {
15017: 	    return @{$iphosts{$ip}};
15018: 	}
15019: 	return;
15020:     }
15021:     
15022:     sub reset_hosts_ip_info {
15023: 	undef(%iphost);
15024: 	undef(%name_to_ip);
15025: 	undef(%lonid_to_ip);
15026:     }
15027: 
15028:     sub get_host_ip {
15029: 	my ($lonid) = @_;
15030: 	if (exists($lonid_to_ip{$lonid})) {
15031: 	    return $lonid_to_ip{$lonid};
15032: 	}
15033: 	my $name=&hostname($lonid);
15034:    	my $ip = gethostbyname($name);
15035: 	return if (!$ip || length($ip) ne 4);
15036: 	$ip=inet_ntoa($ip);
15037: 	$name_to_ip{$name}   = $ip;
15038: 	$lonid_to_ip{$lonid} = $ip;
15039: 	return $ip;
15040:     }
15041:     
15042:     sub get_iphost {
15043: 	my ($ignore_cache,$nocache) = @_;
15044: 
15045: 	if (!$ignore_cache) {
15046: 	    if (%iphost) {
15047: 		return %iphost;
15048: 	    }
15049: 	    my ($ip_info,$cached)=
15050: 		&Apache::lonnet::is_cached_new('iphost','iphost');
15051: 	    if ($cached) {
15052: 		%iphost      = %{$ip_info->[0]};
15053: 		%name_to_ip  = %{$ip_info->[1]};
15054: 		%lonid_to_ip = %{$ip_info->[2]};
15055: 		return %iphost;
15056: 	    }
15057: 	}
15058: 
15059: 	# get yesterday's info for fallback
15060: 	my %old_name_to_ip;
15061: 	my ($ip_info,$cached)=
15062: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
15063: 	if ($cached) {
15064: 	    %old_name_to_ip = %{$ip_info->[1]};
15065: 	}
15066: 
15067: 	my %name_to_host = &all_names($ignore_cache,$nocache);
15068: 	foreach my $name (keys(%name_to_host)) {
15069: 	    my $ip;
15070: 	    if (!exists($name_to_ip{$name})) {
15071: 		$ip = gethostbyname($name);
15072: 		if (!$ip || length($ip) ne 4) {
15073: 		    if (defined($old_name_to_ip{$name})) {
15074: 			$ip = $old_name_to_ip{$name};
15075: 			&logthis("Can't find $name defaulting to old $ip");
15076: 		    } else {
15077: 			&logthis("Name $name no IP found");
15078: 			next;
15079: 		    }
15080: 		} else {
15081: 		    $ip=inet_ntoa($ip);
15082: 		}
15083: 		$name_to_ip{$name} = $ip;
15084: 	    } else {
15085: 		$ip = $name_to_ip{$name};
15086: 	    }
15087: 	    foreach my $id (@{ $name_to_host{$name} }) {
15088: 		$lonid_to_ip{$id} = $ip;
15089: 	    }
15090: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
15091: 	}
15092:         unless ($nocache) {
15093: 	    &do_cache_new('iphost','iphost',
15094: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
15095: 		          48*60*60);
15096:         }
15097: 
15098: 	return %iphost;
15099:     }
15100: 
15101:     #
15102:     #  Given a DNS returns the loncapa host name for that DNS 
15103:     # 
15104:     sub host_from_dns {
15105:         my ($dns) = @_;
15106:         my @hosts;
15107:         my $ip;
15108: 
15109:         if (exists($name_to_ip{$dns})) {
15110:             $ip = $name_to_ip{$dns};
15111:         }
15112:         if (!$ip) {
15113:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15114:             if (length($ip) == 4) { 
15115: 	        $ip   = &IO::Socket::inet_ntoa($ip);
15116:             }
15117:         }
15118:         if ($ip) {
15119: 	    @hosts = get_hosts_from_ip($ip);
15120: 	    return $hosts[0];
15121:         }
15122:         return undef;
15123:     }
15124: 
15125:     sub get_internet_names {
15126:         my ($lonid) = @_;
15127:         return if ($lonid eq '');
15128:         my ($idnref,$cached)=
15129:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
15130:         if ($cached) {
15131:             return $idnref;
15132:         }
15133:         my $ip = &get_host_ip($lonid);
15134:         my @hosts = &get_hosts_from_ip($ip);
15135:         my %iphost = &get_iphost();
15136:         my (@idns,%seen);
15137:         foreach my $id (@hosts) {
15138:             my $dom = &host_domain($id);
15139:             my $prim_id = &domain($dom,'primary');
15140:             my $prim_ip = &get_host_ip($prim_id);
15141:             next if ($seen{$prim_ip});
15142:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15143:                 foreach my $id (@{$iphost{$prim_ip}}) {
15144:                     my $intdom = &internet_dom($id);
15145:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
15146:                         push(@idns,$intdom);
15147:                     }
15148:                 }
15149:             }
15150:             $seen{$prim_ip} = 1;
15151:         }
15152:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
15153:     }
15154: 
15155: }
15156: 
15157: sub all_loncaparevs {
15158:     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);
15159: }
15160: 
15161: # ---------------------------------------------------------- Read loncaparev table
15162: {
15163:     sub load_loncaparevs { 
15164:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
15165:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
15166:                 while (my $configline=<$config>) {
15167:                     chomp($configline);
15168:                     my ($hostid,$loncaparev)=split(/:/,$configline);
15169:                     $loncaparevs{$hostid}=$loncaparev;
15170:                 }
15171:                 close($config);
15172:             }
15173:         }
15174:     }
15175: }
15176: 
15177: # ---------------------------------------------------------- Read serverhostID table
15178: {
15179:     sub load_serverhomeIDs {
15180:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
15181:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
15182:                 while (my $configline=<$config>) {
15183:                     chomp($configline);
15184:                     my ($name,$id)=split(/:/,$configline);
15185:                     $serverhomeIDs{$name}=$id;
15186:                 }
15187:                 close($config);
15188:             }
15189:         }
15190:     }
15191: }
15192: 
15193: 
15194: BEGIN {
15195: 
15196: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15197:     unless ($readit) {
15198: {
15199:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15200:     %perlvar = (%perlvar,%{$configvars});
15201: }
15202: 
15203: 
15204: # ------------------------------------------------------ Read spare server file
15205: {
15206:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
15207: 
15208:     while (my $configline=<$config>) {
15209:        chomp($configline);
15210:        if ($configline) {
15211: 	   my ($host,$type) = split(':',$configline,2);
15212: 	   if (!defined($type) || $type eq '') { $type = 'default' };
15213: 	   push(@{ $spareid{$type} }, $host);
15214:        }
15215:     }
15216:     close($config);
15217: }
15218: # ------------------------------------------------------------ Read permissions
15219: {
15220:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
15221: 
15222:     while (my $configline=<$config>) {
15223: 	chomp($configline);
15224: 	if ($configline) {
15225: 	    my ($role,$perm)=split(/ /,$configline);
15226: 	    if ($perm ne '') { $pr{$role}=$perm; }
15227: 	}
15228:     }
15229:     close($config);
15230: }
15231: 
15232: # -------------------------------------------- Read plain texts for permissions
15233: {
15234:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
15235: 
15236:     while (my $configline=<$config>) {
15237: 	chomp($configline);
15238: 	if ($configline) {
15239: 	    my ($short,@plain)=split(/:/,$configline);
15240:             %{$prp{$short}} = ();
15241: 	    if (@plain > 0) {
15242:                 $prp{$short}{'std'} = $plain[0];
15243:                 for (my $i=1; $i<@plain; $i++) {
15244:                     $prp{$short}{'alt'.$i} = $plain[$i];  
15245:                 }
15246:             }
15247: 	}
15248:     }
15249:     close($config);
15250: }
15251: 
15252: # ---------------------------------------------------------- Read package table
15253: {
15254:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
15255: 
15256:     while (my $configline=<$config>) {
15257: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
15258: 	chomp($configline);
15259: 	my ($short,$plain)=split(/:/,$configline);
15260: 	my ($pack,$name)=split(/\&/,$short);
15261: 	if ($plain ne '') {
15262: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
15263: 	    $packagetab{$short}=$plain; 
15264: 	}
15265:     }
15266:     close($config);
15267: }
15268: 
15269: # ---------------------------------------------------------- Read loncaparev table
15270: 
15271: &load_loncaparevs();
15272: 
15273: # ---------------------------------------------------------- Read serverhostID table
15274: 
15275: &load_serverhomeIDs();
15276: 
15277: # ---------------------------------------------------------- Read releaseslist XML
15278: {
15279:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15280:     if (-e $file) {
15281:         my $parser = HTML::LCParser->new($file);
15282:         while (my $token = $parser->get_token()) {
15283:             if ($token->[0] eq 'S') {
15284:                 my $item = $token->[1];
15285:                 my $name = $token->[2]{'name'};
15286:                 my $value = $token->[2]{'value'};
15287:                 my $valuematch = $token->[2]{'valuematch'};
15288:                 my $namematch = $token->[2]{'namematch'};
15289:                 if ($item eq 'parameter') {
15290:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
15291:                         my $release = $parser->get_text();
15292:                         $release =~ s/(^\s*|\s*$ )//gx;
15293:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
15294:                     }
15295:                 } elsif ($item ne '' && $name ne '') {
15296:                     my $release = $parser->get_text();
15297:                     $release =~ s/(^\s*|\s*$ )//gx;
15298:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
15299:                 }
15300:             }
15301:         }
15302:     }
15303: }
15304: 
15305: # ---------------------------------------------------------- Read managers table
15306: {
15307:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
15308:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
15309:             while (my $configline=<$config>) {
15310:                 chomp($configline);
15311:                 next if ($configline =~ /^\#/);
15312:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15313:                     $managerstab{$configline} = 1;
15314:                 }
15315:             }
15316:             close($config);
15317:         }
15318:     }
15319: }
15320: 
15321: # ------------- set up temporary directory
15322: {
15323:     $tmpdir = LONCAPA::tempdir();
15324: 
15325: }
15326: 
15327: # ------------- set default texengine (domain default overrides this)
15328: {
15329:     $deftex = LONCAPA::texengine();
15330: }
15331: 
15332: # ------------- set default minimum length for passwords for internal auth users
15333: {
15334:     $passwdmin = LONCAPA::passwd_min();
15335: }
15336: 
15337: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
15338: 				'compress_threshold'=> 20_000,
15339:  			        });
15340: 
15341: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
15342: $dumpcount=0;
15343: $locknum=0;
15344: 
15345: &logtouch();
15346: &logthis('<font color="yellow">INFO: Read configuration</font>');
15347: $readit=1;
15348:     {
15349: 	use integer;
15350: 	my $test=(2**32)+1;
15351: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
15352: 	&logthis(" Detected 64bit platform ($_64bit)");
15353:     }
15354: }
15355: }
15356: 
15357: 1;
15358: __END__
15359: 
15360: =pod
15361: 
15362: =head1 NAME
15363: 
15364: Apache::lonnet - Subroutines to ask questions about things in the network.
15365: 
15366: =head1 SYNOPSIS
15367: 
15368: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
15369: 
15370:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15371: 
15372: Common parameters:
15373: 
15374: =over 4
15375: 
15376: =item *
15377: 
15378: $uname : an internal username (if $cname expecting a course Id specifically)
15379: 
15380: =item *
15381: 
15382: $udom : a domain (if $cdom expecting a course's domain specifically)
15383: 
15384: =item *
15385: 
15386: $symb : a resource instance identifier
15387: 
15388: =item *
15389: 
15390: $namespace : the name of a .db file that contains the data needed or
15391: being set.
15392: 
15393: =back
15394: 
15395: =head1 OVERVIEW
15396: 
15397: lonnet provides subroutines which interact with the
15398: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15399: about classes, users, and resources.
15400: 
15401: For many of these objects you can also use this to store data about
15402: them or modify them in various ways.
15403: 
15404: =head2 Symbs
15405: 
15406: To identify a specific instance of a resource, LON-CAPA uses symbols
15407: or "symbs"X<symb>. These identifiers are built from the URL of the
15408: map, the resource number of the resource in the map, and the URL of
15409: the resource itself. The latter is somewhat redundant, but might help
15410: if maps change.
15411: 
15412: An example is
15413: 
15414:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15415: 
15416: The respective map entry is
15417: 
15418:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
15419:   title="Problem 2">
15420:  </resource>
15421: 
15422: Symbs are used by the random number generator, as well as to store and
15423: restore data specific to a certain instance of for example a problem.
15424: 
15425: =head2 Storing And Retrieving Data
15426: 
15427: X<store()>X<cstore()>X<restore()>Three of the most important functions
15428: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15429: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15430: is is the non-critical message twin of cstore. These functions are for
15431: handlers to store a perl hash to a user's permanent data space in an
15432: easy manner, and to retrieve it again on another call. It is expected
15433: that a handler would use this once at the beginning to retrieve data,
15434: and then again once at the end to send only the new data back.
15435: 
15436: The data is stored in the user's data directory on the user's
15437: homeserver under the ID of the course.
15438: 
15439: The hash that is returned by restore will have all of the previous
15440: value for all of the elements of the hash.
15441: 
15442: Example:
15443: 
15444:  #creating a hash
15445:  my %hash;
15446:  $hash{'foo'}='bar';
15447: 
15448:  #storing it
15449:  &Apache::lonnet::cstore(\%hash);
15450: 
15451:  #changing a value
15452:  $hash{'foo'}='notbar';
15453: 
15454:  #adding a new value
15455:  $hash{'bar'}='foo';
15456:  &Apache::lonnet::cstore(\%hash);
15457: 
15458:  #retrieving the hash
15459:  my %history=&Apache::lonnet::restore();
15460: 
15461:  #print the hash
15462:  foreach my $key (sort(keys(%history))) {
15463:    print("\%history{$key} = $history{$key}");
15464:  }
15465: 
15466: Will print out:
15467: 
15468:  %history{1:foo} = bar
15469:  %history{1:keys} = foo:timestamp
15470:  %history{1:timestamp} = 990455579
15471:  %history{2:bar} = foo
15472:  %history{2:foo} = notbar
15473:  %history{2:keys} = foo:bar:timestamp
15474:  %history{2:timestamp} = 990455580
15475:  %history{bar} = foo
15476:  %history{foo} = notbar
15477:  %history{timestamp} = 990455580
15478:  %history{version} = 2
15479: 
15480: Note that the special hash entries C<keys>, C<version> and
15481: C<timestamp> were added to the hash. C<version> will be equal to the
15482: total number of versions of the data that have been stored. The
15483: C<timestamp> attribute will be the UNIX time the hash was
15484: stored. C<keys> is available in every historical section to list which
15485: keys were added or changed at a specific historical revision of a
15486: hash.
15487: 
15488: B<Warning>: do not store the hash that restore returns directly. This
15489: will cause a mess since it will restore the historical keys as if the
15490: were new keys. I.E. 1:foo will become 1:1:foo etc.
15491: 
15492: Calling convention:
15493: 
15494:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15495:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15496: 
15497: For more detailed information, see lonnet specific documentation.
15498: 
15499: =head1 RETURN MESSAGES
15500: 
15501: =over 4
15502: 
15503: =item * B<con_lost>: unable to contact remote host
15504: 
15505: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15506: when the connection is brought back up
15507: 
15508: =item * B<con_failed>: unable to contact remote host and unable to save message
15509: for later delivery
15510: 
15511: =item * B<error:>: an error a occurred, a description of the error follows the :
15512: 
15513: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15514: that was requested
15515: 
15516: =back
15517: 
15518: =head1 PUBLIC SUBROUTINES
15519: 
15520: =head2 Session Environment Functions
15521: 
15522: =over 4
15523: 
15524: =item * 
15525: X<appenv()>
15526: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15527: the user envirnoment file, and will be restored for each access this
15528: user makes during this session, also modifies the %env for the current
15529: process. Optional rolesarrayref - if defined contains a reference to an array
15530: of roles which are exempt from the restriction on modifying user.role entries 
15531: in the user's environment.db and in %env.    
15532: 
15533: =item *
15534: X<delenv()>
15535: B<delenv($delthis,$regexp)>: removes all items from the session
15536: environment file that begin with $delthis. If the 
15537: optional second arg - $regexp - is true, $delthis is treated as a 
15538: regular expression, otherwise \Q$delthis\E is used. 
15539: The values are also deleted from the current processes %env.
15540: 
15541: =item * get_env_multiple($name) 
15542: 
15543: gets $name from the %env hash, it seemlessly handles the cases where multiple
15544: values may be defined and end up as an array ref.
15545: 
15546: returns an array of values
15547: 
15548: =back
15549: 
15550: =head2 User Information
15551: 
15552: =over 4
15553: 
15554: =item *
15555: X<queryauthenticate()>
15556: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15557: authentication scheme
15558: 
15559: =item *
15560: X<authenticate()>
15561: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15562: authenticate user from domain's lib servers (first use the current
15563: one). C<$upass> should be the users password.
15564: $checkdefauth is optional (value is 1 if a check should be made to
15565:    authenticate user using default authentication method, and allow
15566:    account creation if username does not have account in the domain).
15567: $clientcancheckhost is optional (value is 1 if checking whether the
15568:    server can host will occur on the client side in lonauth.pm).   
15569: 
15570: =item *
15571: X<homeserver()>
15572: B<homeserver($uname,$udom)>: find the server which has
15573: the user's directory and files (there must be only one), this caches
15574: the answer, and also caches if there is a borken connection.
15575: 
15576: =item *
15577: X<idget()>
15578: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15579: a list of student/employee IDs or clicker IDs
15580: (student/employee IDs are a unique resource in a domain, there must be 
15581: only 1 ID per username, and only 1 username per ID in a specific domain).
15582: clickerIDs are not necessarily unique, as students might share clickers.
15583: (returns hash: id=>name,id=>name)
15584: 
15585: =item *
15586: X<idrget()>
15587: B<idrget($udom,@unames)>: find the IDs behind a list of
15588: usernames (returns hash: name=>id,name=>id)
15589: 
15590: =item *
15591: X<idput()>
15592: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15593: names and associated student/employee IDs or clicker IDs.
15594: 
15595: =item *
15596: X<iddel()>
15597: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15598: student/employee ID or clicker ID username look-ups from domain.
15599: The homeserver ($uhome) and namespace ($namespace) are optional.
15600: If no $uhome is provided, it will be determined usig &homeserver()
15601: for each user.  If no $namespace is provided, the default is ids.
15602: 
15603: =item *
15604: X<updateclickers()>
15605: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15606: clicker ID-to-username look-ups in clickers.db on library server.
15607: Permitted actions are add or del (i.e., add or delete). The 
15608: clickers.db contains clickerID as keys (escaped), and each corresponding
15609: value is an escaped comma-separated list of usernames (for whom the
15610: library server is the homeserver), who registered that particular ID.
15611: If $critical is true, the update will be sent via &critical, otherwise
15612: &reply() will be used.
15613: 
15614: =item *
15615: X<rolesinit()>
15616: B<rolesinit($udom,$username)>: get user privileges.
15617: returns user role, first access and timer interval hashes
15618: 
15619: =item *
15620: X<privileged()>
15621: B<privileged($username,$domain)>: returns a true if user has a
15622: privileged and active role (i.e. su or dc), false otherwise.
15623: 
15624: =item *
15625: X<getsection()>
15626: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15627: course $cname, return section name/number or '' for "not in course"
15628: and '-1' for "no section"
15629: 
15630: =item *
15631: X<userenvironment()>
15632: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15633: passed in @what from the requested user's environment, returns a hash
15634: 
15635: =item * 
15636: X<userlog_query()>
15637: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15638: activity.log file. %filters defines filters applied when parsing the
15639: log file. These can be start or end timestamps, or the type of action
15640: - log to look for Login or Logout events, check for Checkin or
15641: Checkout, role for role selection. The response is in the form
15642: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15643: escaped strings of the action recorded in the activity.log file.
15644: 
15645: =back
15646: 
15647: =head2 User Roles
15648: 
15649: =over 4
15650: 
15651: =item *
15652: 
15653: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15654: returns codes for allowed actions.
15655: 
15656: The first argument is required, all others are optional.
15657: 
15658: $priv is the privilege being checked.
15659: $uri contains additional information about what is being checked for access (e.g.,
15660: URL, course ID etc.). 
15661: $symb is the unique resource instance identifier in a course; if needed,
15662: but not provided, it will be retrieved via a call to &symbread(). 
15663: $role is the role for which a priv is being checked (only used if priv is evb). 
15664: $clientip is the user's IP address (only used when checking for access to portfolio 
15665: files).
15666: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15667: prevents recursive calls to &allowed.
15668: 
15669:  F: full access
15670:  U,I,K: authentication modes (cxx only)
15671:  '': forbidden
15672:  1: user needs to choose course
15673:  2: browse allowed
15674:  A: passphrase authentication needed
15675:  B: access temporarily blocked because of a blocking event in a course.
15676:  D: access blocked because access is required via session initiated via deep-link 
15677: 
15678: =item *
15679: 
15680: constructaccess($url,$setpriv) : check for access to construction space URL
15681: 
15682: See if the owner domain and name in the URL match those in the
15683: expected environment.  If so, return three element list
15684: ($ownername,$ownerdomain,$ownerhome).
15685: 
15686: Otherwise return the null string.
15687: 
15688: If second argument 'setpriv' is true, it assigns the privileges,
15689: and returns the same three element list, unless the owner has
15690: blocked "ad hoc" Domain Coordinator access to the Author Space,
15691: in which case the null string is returned.
15692: 
15693: =item *
15694: 
15695: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
15696: define a custom role rolename set privileges in format of lonTabs/roles.tab
15697: for system, domain, and course level. $uname and $udom are optional (current
15698: user's username and domain will be used when either of $uname or $udom are absent.
15699: 
15700: =item *
15701: 
15702: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
15703: (rolesplain.tab); plain text explanation of a user role term.
15704: $type is Course (default) or Community.
15705: If $forcedefault evaluates to true, text returned will be default 
15706: text for $type. Otherwise, if this is a course, the text returned 
15707: will be a custom name for the role (if defined in the course's 
15708: environment).  If no custom name is defined the default is returned.
15709:    
15710: =item *
15711: 
15712: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
15713: All arguments are optional. Returns a hash of a roles, either for
15714: co-author/assistant author roles for a user's Construction Space
15715: (default), or if $context is 'userroles', roles for the user himself,
15716: In the hash, keys are set to colon-separated $uname,$udom,$role, and
15717: (optionally) if $withsec is true, a fourth colon-separated item - $section.
15718: For each key, value is set to colon-separated start and end times for
15719: the role.  If no username and domain are specified, will default to
15720: current user/domain. Types, roles, and roledoms are references to arrays
15721: of role statuses (active, future or previous), roles 
15722: (e.g., cc,in, st etc.) and domains of the roles which can be used
15723: to restrict the list of roles reported. If no array ref is 
15724: provided for types, will default to return only active roles.
15725: 
15726: =item *
15727: 
15728: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
15729: user: $uname:$udom has a role in the course: $cdom_$cnum. 
15730: 
15731: Additional optional arguments are: $type (if role checking is to be restricted 
15732: to certain user status types -- previous (expired roles), active (currently
15733: available roles) or future (roles available in the future), and
15734: $hideprivileged -- if true will not report course roles for users who
15735: have active Domain Coordinator role in course's domain or in additional
15736: domains (specified in 'Domains to check for privileged users' in course
15737: environment -- set via:  Course Settings -> Classlists and staff listing).
15738: 
15739: =item *
15740: 
15741: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
15742: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
15743: $possdomains and $possroles are optional array refs -- to domains to check and
15744: roles to check.  If $possdomains is not specified, a dump will be done of the
15745: users' roles.db to check for a dc or su role in any domain. This can be
15746: time consuming if &privileged is called repeatedly (e.g., when displaying a
15747: classlist), so in such cases, supplying a $possdomains array is preferred, as
15748: this then allows &privileged_by_domain() to be used, which caches the identity
15749: of privileged users, eliminating the need for repeated calls to &dump().
15750: 
15751: =item *
15752: 
15753: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
15754: where the outer hash keys are domains specified in the $possdomains array ref,
15755: next inner hash keys are privileged roles specified in the $roles array ref,
15756: and the innermost hash contains key = value pairs for username:domain = end:start
15757: for active or future "privileged" users with that role in that domain. To avoid
15758: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
15759: innerhash are cached using priv_$role and $dom as the identifiers.
15760: 
15761: =back
15762: 
15763: =head2 User Modification
15764: 
15765: =over 4
15766: 
15767: =item *
15768: 
15769: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
15770: user for the level given by URL.  Optional start and end dates (leave empty
15771: string or zero for "no date")
15772: 
15773: =item *
15774: 
15775: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
15776: change a users, password, possible return values are: ok,
15777: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
15778: refused
15779: 
15780: =item *
15781: 
15782: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
15783: 
15784: =item *
15785: 
15786: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
15787:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
15788: 
15789: will update user information (firstname,middlename,lastname,generation,
15790: permanentemail), and if forceid is true, student/employee ID also.
15791: A user's institutional affiliation(s) can also be updated.
15792: User information fields will not be overwritten with empty entries 
15793: unless the field is included in the $candelete array reference.
15794: This array is included when a single user is modified via "Manage Users",
15795: or when Autoupdate.pl is run by cron in a domain.
15796: 
15797: =item *
15798: 
15799: modifystudent
15800: 
15801: modify a student's enrollment and identification information.
15802: The course id is resolved based on the current user's environment.  
15803: This means the invoking user must be a course coordinator or otherwise
15804: associated with a course.
15805: 
15806: This call is essentially a wrapper for lonnet::modifyuser and
15807: lonnet::modify_student_enrollment
15808: 
15809: Inputs: 
15810: 
15811: =over 4
15812: 
15813: =item B<$udom> Student's loncapa domain
15814: 
15815: =item B<$uname> Student's loncapa login name
15816: 
15817: =item B<$uid> Student/Employee ID
15818: 
15819: =item B<$umode> Student's authentication mode
15820: 
15821: =item B<$upass> Student's password
15822: 
15823: =item B<$first> Student's first name
15824: 
15825: =item B<$middle> Student's middle name
15826: 
15827: =item B<$last> Student's last name
15828: 
15829: =item B<$gene> Student's generation
15830: 
15831: =item B<$usec> Student's section in course
15832: 
15833: =item B<$end> Unix time of the roles expiration
15834: 
15835: =item B<$start> Unix time of the roles start date
15836: 
15837: =item B<$forceid> If defined, allow $uid to be changed
15838: 
15839: =item B<$desiredhome> server to use as home server for student
15840: 
15841: =item B<$email> Student's permanent e-mail address
15842: 
15843: =item B<$type> Type of enrollment (auto or manual)
15844: 
15845: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
15846: 
15847: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
15848: 
15849: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
15850: 
15851: =item B<$context> role change context (shown in User Management Logs display in a course)
15852: 
15853: =item B<$inststatus> institutional status of user - : separated string of escaped status types
15854: 
15855: =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.
15856: 
15857: =back
15858: 
15859: =item *
15860: 
15861: modify_student_enrollment
15862: 
15863: Change a student's enrollment status in a class.  The environment variable
15864: 'role.request.course' must be defined for this function to proceed.
15865: 
15866: Inputs:
15867: 
15868: =over 4
15869: 
15870: =item $udom, student's domain
15871: 
15872: =item $uname, student's name
15873: 
15874: =item $uid, student's user id
15875: 
15876: =item $first, student's first name
15877: 
15878: =item $middle
15879: 
15880: =item $last
15881: 
15882: =item $gene
15883: 
15884: =item $usec
15885: 
15886: =item $end
15887: 
15888: =item $start
15889: 
15890: =item $type
15891: 
15892: =item $locktype
15893: 
15894: =item $cid
15895: 
15896: =item $selfenroll
15897: 
15898: =item $context
15899: 
15900: =item $credits, number of credits student will earn from this class
15901: 
15902: =item $instsec, institutional course section code for student
15903: 
15904: =back
15905: 
15906: 
15907: =item *
15908: 
15909: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
15910: custom role; give a custom role to a user for the level given by URL.  Specify
15911: name and domain of role author, and role name
15912: 
15913: =item *
15914: 
15915: revokerole($udom,$uname,$url,$role) : revoke a role for url
15916: 
15917: =item *
15918: 
15919: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
15920: 
15921: =back
15922: 
15923: =head2 Course Infomation
15924: 
15925: =over 4
15926: 
15927: =item *
15928: 
15929: coursedescription($courseid,$options) : returns a hash of information about the
15930: specified course id, including all environment settings for the
15931: course, the description of the course will be in the hash under the
15932: key 'description'
15933: 
15934: $options is an optional parameter that if supplied is a hash reference that controls
15935: what how this function works.  It has the following key/values:
15936: 
15937: =over 4
15938: 
15939: =item freshen_cache
15940: 
15941: If defined, and the environment cache for the course is valid, it is 
15942: returned in the returned hash.
15943: 
15944: =item one_time
15945: 
15946: If defined, the last cache time is set to _now_
15947: 
15948: =item user
15949: 
15950: If defined, the supplied username is used instead of the current user.
15951: 
15952: 
15953: =back
15954: 
15955: =item *
15956: 
15957: resdata($name,$domain,$type,@which) : request for current parameter
15958: setting for a specific $type, where $type is either 'course' or 'user',
15959: @what should be a list of parameters to ask about. This routine caches
15960: answers for 10 minutes.
15961: 
15962: =item *
15963: 
15964: get_courseresdata($courseid, $domain) : dump the entire course resource
15965: data base, returning a hash that is keyed by the resource name and has
15966: values that are the resource value.  I believe that the timestamps and
15967: versions are also returned.
15968: 
15969: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
15970: supplemental content area. This routine caches the number of files for 
15971: 10 minutes.
15972: 
15973: =back
15974: 
15975: =head2 Course Modification
15976: 
15977: =over 4
15978: 
15979: =item *
15980: 
15981: writecoursepref($courseid,%prefs) : write preferences (environment
15982: database) for a course
15983: 
15984: =item *
15985: 
15986: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
15987: 
15988: =item *
15989: 
15990: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
15991: 
15992: =item *
15993: 
15994: is_course($courseid), is_course($cdom, $cnum)
15995: 
15996: Accepts either a combined $courseid (in the form of domain_courseid) or the
15997: two component version $cdom, $cnum. It checks if the specified course exists.
15998: 
15999: Returns:
16000:     undef if the course doesn't exist, otherwise
16001:     in scalar context the combined courseid.
16002:     in list context the two components of the course identifier, domain and 
16003:     courseid.    
16004: 
16005: =back
16006: 
16007: =head2 Bubblesheet Configuration
16008: 
16009: =over 4
16010: 
16011: =item *
16012: 
16013: get_scantron_config($which)
16014: 
16015: $which - the name of the configuration to parse from the file.
16016: 
16017: Parses and returns the bubblesheet configuration line selected as a
16018: hash of configuration file fields.
16019: 
16020: 
16021: Returns:
16022:     If the named configuration is not in the file, an empty
16023:     hash is returned.
16024: 
16025:     a hash with the fields
16026:       name         - internal name for the this configuration setup
16027:       description  - text to display to operator that describes this config
16028:       CODElocation - if 0 or the string 'none'
16029:                           - no CODE exists for this config
16030:                      if -1 || the string 'letter'
16031:                           - a CODE exists for this config and is
16032:                             a string of letters
16033:                      Unsupported value (but planned for future support)
16034:                           if a positive integer
16035:                                - The CODE exists as the first n items from
16036:                                  the question section of the form
16037:                           if the string 'number'
16038:                                - The CODE exists for this config and is
16039:                                  a string of numbers
16040:       CODEstart   - (only matter if a CODE exists) column in the line where
16041:                      the CODE starts
16042:       CODElength  - length of the CODE
16043:       IDstart     - column where the student/employee ID starts
16044:       IDlength    - length of the student/employee ID info
16045:       Qstart      - column where the information from the bubbled
16046:                     'questions' start
16047:       Qlength     - number of columns comprising a single bubble line from
16048:                     the sheet. (usually either 1 or 10)
16049:       Qon         - either a single character representing the character used
16050:                     to signal a bubble was chosen in the positional setup, or
16051:                     the string 'letter' if the letter of the chosen bubble is
16052:                     in the final, or 'number' if a number representing the
16053:                     chosen bubble is in the file (1->A 0->J)
16054:       Qoff        - the character used to represent that a bubble was
16055:                     left blank
16056:       PaperID     - if the scanning process generates a unique number for each
16057:                     sheet scanned the column that this ID number starts in
16058:       PaperIDlength - number of columns that comprise the unique ID number
16059:                       for the sheet of paper
16060:       FirstName   - column that the first name starts in
16061:       FirstNameLength - number of columns that the first name spans
16062:       LastName    - column that the last name starts in
16063:       LastNameLength - number of columns that the last name spans
16064:       BubblesPerRow - number of bubbles available in each row used to
16065:                       bubble an answer. (If not specified, 10 assumed).
16066: 
16067: 
16068: =item *
16069: 
16070: get_scantronformat_file($cdom)
16071: 
16072: $cdom - the course's domain (optional); if not supplied, uses
16073: domain for current $env{'request.course.id'}.
16074: 
16075: Returns an array containing lines from the scantron format file for
16076: the domain of the course.
16077: 
16078: If a url for a custom.tab file is listed in domain's configuration.db,
16079: lines are from this file.
16080: 
16081: Otherwise, if a default.tab has been published in RES space by the
16082: domainconfig user, lines are from this file.
16083: 
16084: Otherwise, fall back to getting lines from the legacy file on the
16085: local server:  /home/httpd/lonTabs/default_scantronformat.tab
16086: 
16087: =back
16088: 
16089: =head2 Resource Subroutines
16090: 
16091: =over 4
16092: 
16093: =item *
16094: 
16095: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
16096: 
16097: =item *
16098: 
16099: repcopy($filename) : subscribes to the requested file, and attempts to
16100: replicate from the owning library server, Might return
16101: 'unavailable', 'not_found', 'forbidden', 'ok', or
16102: 'bad_request', also attempts to grab the metadata for the
16103: resource. Expects the local filesystem pathname
16104: (/home/httpd/html/res/....)
16105: 
16106: =back
16107: 
16108: =head2 Resource Information
16109: 
16110: =over 4
16111: 
16112: =item *
16113: 
16114: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
16115: and returns the value of a variety of different possible values,
16116: $varname should be a request string, and the other parameters can be
16117: used to specify who and what one is asking about. Ordinarily, $cid 
16118: does not need to be specified, as it is retrived from 
16119: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16120: within lonuserstate::loadmap() when initializing a course, before
16121: $env{'request.course.id'} has been set, so it needs to be provided
16122: in that one case.
16123: 
16124: Possible values for $varname are environment.lastname (or other item
16125: from the envirnment hash), user.name (or someother aspect about the
16126: user), resource.0.maxtries (or some other part and parameter of a
16127: resource)
16128: 
16129: =item *
16130: 
16131: directcondval($number) : get current value of a condition; reads from a state
16132: string
16133: 
16134: =item *
16135: 
16136: condval($condidx) : value of condition index based on state
16137: 
16138: =item *
16139: 
16140: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
16141: resource's metadata, $what should be either a specific key, or either
16142: 'keys' (to get a list of possible keys) or 'packages' to get a list of
16143: packages that this resource currently uses, the last 3 arguments are 
16144: only used internally for recursive metadata.
16145: 
16146: the toolsymb is only used where the uri is for an external tool (for which
16147: the uri as well as the symb are guaranteed to be unique).
16148: 
16149: this function automatically caches all requests except any made recursively
16150: to retrieve a list of metadata keys for an imported library file ($liburi is 
16151: defined).
16152: 
16153: =item *
16154: 
16155: metadata_query($query,$custom,$customshow) : make a metadata query against the
16156: network of library servers; returns file handle of where SQL and regex results
16157: will be stored for query
16158: 
16159: =item *
16160: 
16161: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
16162: return symbolic list entry (all arguments optional). 
16163: 
16164: Args: filename is the filename (including path) for the file for which a symb 
16165: is required; donotrecurse, if true will prevent calls to allowed() being made 
16166: to check access status if more than one resource was found in the bighash 
16167: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
16168: a randompick); ignorecachednull, if true will prevent a symb of '' being 
16169: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16170: cause possible symbs to be checked to determine if they are subject to content
16171: blocking, if so they will not be included as possible symbs; possibles is a
16172: ref to a hash, which, as a side effect, will be populated with all possible 
16173: symbs (content blocking not tested).
16174:  
16175: returns the data handle
16176: 
16177: =item *
16178: 
16179: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
16180: and is a possible symb for the URL in $thisfn, and if is an encrypted
16181: resource that the user accessed using /enc/ returns a 1 on success, 0
16182: on failure, user must be in a course, as it assumes the existence of
16183: the course initial hash, and uses $env('request.course.id'}.  The third
16184: arg is an optional reference to a scalar.  If this arg is passed in the 
16185: call to symbverify, it will be set to 1 if the symb has been set to be 
16186: encrypted; otherwise it will be null.  
16187: 
16188: =item *
16189: 
16190: symbclean($symb) : removes versions numbers from a symb, returns the
16191: cleaned symb
16192: 
16193: =item *
16194: 
16195: is_on_map($uri) : checks if the $uri is somewhere on the current
16196: course map, user must be in a course for it to work.
16197: 
16198: =item *
16199: 
16200: numval($salt) : return random seed value (addend for rndseed)
16201: 
16202: =item *
16203: 
16204: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16205: a random seed, all arguments are optional, if they aren't sent it uses the
16206: environment to derive them. Note: if symb isn't sent and it can't get one
16207: from &symbread it will use the current time as its return value
16208: 
16209: =item *
16210: 
16211: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16212: unfakeable, receipt
16213: 
16214: =item *
16215: 
16216: receipt() : API to ireceipt working off of env values; given out to users
16217: 
16218: =item *
16219: 
16220: countacc($url) : count the number of accesses to a given URL
16221: 
16222: =item *
16223: 
16224: 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
16225: 
16226: =item *
16227: 
16228: 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)
16229: 
16230: =item *
16231: 
16232: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
16233: 
16234: =item *
16235: 
16236: devalidate($symb) : devalidate temporary spreadsheet calculations,
16237: forcing spreadsheet to reevaluate the resource scores next time.
16238: 
16239: =item * 
16240: 
16241: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16242: when viewing in course context.
16243: 
16244:  input: six args -- filename (decluttered), course number, course domain,
16245:                     url, symb (if registered) and group (if this is a 
16246:                     group item -- e.g., bulletin board, group page etc.).
16247: 
16248:  output: array of five scalars --
16249:          $cfile -- url for file editing if editable on current server
16250:          $home -- homeserver of resource (i.e., for author if published,
16251:                                           or course if uploaded.).
16252:          $switchserver --  1 if server switch will be needed.
16253:          $forceedit -- 1 if icon/link should be to go to edit mode 
16254:          $forceview -- 1 if icon/link should be to go to view mode
16255: 
16256: =item *
16257: 
16258: is_course_upload($file,$cnum,$cdom)
16259: 
16260: Used in course context to determine if current file was uploaded to 
16261: the course (i.e., would be found in /userfiles/docs on the course's 
16262: homeserver.
16263: 
16264:   input: 3 args -- filename (decluttered), course number and course domain.
16265:   output: boolean -- 1 if file was uploaded.
16266: 
16267: =back
16268: 
16269: =head2 Storing/Retreiving Data
16270: 
16271: =over 4
16272: 
16273: =item *
16274: 
16275: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16276: permanently for this url; hashref needs to be given and should be a \%hashname;
16277: the remaining args aren't required and if they aren't passed or are '' they will
16278: be derived from the env (with the exception of $laststore, which is an 
16279: optional arg used when a user's submission is stored in grading).
16280: $laststore is $version=$timestamp, where $version is the most recent version
16281: number retrieved for the corresponding $symb in the $namespace db file, and
16282: $timestamp is the timestamp for that transaction (UNIX time).
16283: $laststore is currently only passed when cstore() is called by 
16284: structuretags::finalize_storage().
16285: 
16286: =item *
16287: 
16288: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16289: but uses critical subroutine
16290: 
16291: =item *
16292: 
16293: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16294: all args are optional
16295: 
16296: =item *
16297: 
16298: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
16299: dumps the complete (or key matching regexp) namespace into a hash
16300: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16301: normally &store()ed into
16302: 
16303: $range should be either an integer '100' (give me the first 100
16304:                                            matching records)
16305:               or be  two integers sperated by a - with no spaces
16306:                  '30-50' (give me the 30th through the 50th matching
16307:                           records)
16308: 
16309: 
16310: =item *
16311: 
16312: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
16313: replaces a &store() version of data with a replacement set of data
16314: for a particular resource in a namespace passed in the $storehash hash 
16315: reference. If $tolog is true, the transaction is logged in the courselog
16316: with an action=PUTSTORE.
16317: 
16318: =item *
16319: 
16320: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16321: works very similar to store/cstore, but all data is stored in a
16322: temporary location and can be reset using tmpreset, $storehash should
16323: be a hash reference, returns nothing on success
16324: 
16325: =item *
16326: 
16327: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16328: similar to restore, but all data is stored in a temporary location and
16329: can be reset using tmpreset. Returns a hash of values on success,
16330: error string otherwise.
16331: 
16332: =item *
16333: 
16334: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16335: deltes all keys for $symb form the temporary storage hash.
16336: 
16337: =item *
16338: 
16339: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16340: reference filled in from namesp ($udom and $uname are optional)
16341: 
16342: =item *
16343: 
16344: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16345: namesp ($udom and $uname are optional)
16346: 
16347: =item *
16348: 
16349: dump($namespace,$udom,$uname,$regexp,$range) : 
16350: dumps the complete (or key matching regexp) namespace into a hash
16351: ($udom, $uname, $regexp, $range are optional)
16352: 
16353: $range should be either an integer '100' (give me the first 100
16354:                                            matching records)
16355:               or be  two integers sperated by a - with no spaces
16356:                  '30-50' (give me the 30th through the 50th matching
16357:                           records)
16358: =item *
16359: 
16360: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16361: $store can be a scalar, an array reference, or if the amount to be 
16362: incremented is > 1, a hash reference.
16363: 
16364: ($udom and $uname are optional)
16365: 
16366: =item *
16367: 
16368: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16369: ($udom and $uname are optional)
16370: 
16371: =item *
16372: 
16373: cput($namespace,$storehash,$udom,$uname) : critical put
16374: ($udom and $uname are optional)
16375: 
16376: =item *
16377: 
16378: newput($namespace,$storehash,$udom,$uname) :
16379: 
16380: Attempts to store the items in the $storehash, but only if they don't
16381: currently exist, if this succeeds you can be certain that you have 
16382: successfully created a new key value pair in the $namespace db.
16383: 
16384: 
16385: Args:
16386:  $namespace: name of database to store values to
16387:  $storehash: hashref to store to the db
16388:  $udom: (optional) domain of user containing the db
16389:  $uname: (optional) name of user caontaining the db
16390: 
16391: Returns:
16392:  'ok' -> succeeded in storing all keys of $storehash
16393:  'key_exists: <key>' -> failed to anything out of $storehash, as at
16394:                         least <key> already existed in the db (other
16395:                         requested keys may also already exist)
16396:  'error: <msg>' -> unable to tie the DB or other error occurred
16397:  'con_lost' -> unable to contact request server
16398:  'refused' -> action was not allowed by remote machine
16399: 
16400: 
16401: =item *
16402: 
16403: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16404: reference filled in from namesp (encrypts the return communication)
16405: ($udom and $uname are optional)
16406: 
16407: =item *
16408: 
16409: log($udom,$name,$home,$message) : write to permanent log for user; use
16410: critical subroutine
16411: 
16412: =item *
16413: 
16414: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16415: array reference filled in from namespace found in domain level on either
16416: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
16417: 
16418: =item *
16419: 
16420: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
16421: domain level either on specified domain server ($uhome) or primary domain 
16422: server ($udom and $uhome are optional)
16423: 
16424: =item * 
16425: 
16426: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
16427: for: authentication, language, quotas, timezone, date locale, and portal URL in
16428: the target domain.
16429: 
16430: May also include additional key => value pairs for the following groups:
16431: 
16432: =over
16433: 
16434: =item
16435: disk quotas (MB allocated by default to portfolios and authoring spaces).
16436: 
16437: =over
16438: 
16439: =item defaultquota, authorquota
16440: 
16441: =back
16442: 
16443: =item
16444: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16445: portfolio for users).
16446: 
16447: =over
16448: 
16449: =item
16450: aboutme, blog, webdav, portfolio
16451: 
16452: =back
16453: 
16454: =item
16455: requestcourses: ability to request courses, and how requests are processed.
16456: 
16457: =over
16458: 
16459: =item
16460: official, unofficial, community, textbook, placement
16461: 
16462: =back
16463: 
16464: =item
16465: inststatus: types of institutional affiliation, and order in which they are displayed.
16466: 
16467: =over
16468: 
16469: =item
16470: inststatustypes, inststatusorder, inststatusguest
16471: 
16472: =back
16473: 
16474: =item
16475: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16476: for course's uploaded content.
16477: 
16478: =over
16479: 
16480: =item
16481: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16482: communityquota, textbookquota, placementquota
16483: 
16484: =back
16485: 
16486: =item
16487: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16488: on your servers.
16489: 
16490: =over
16491: 
16492: =item 
16493: remotesessions, hostedsessions
16494: 
16495: =back
16496: 
16497: =back
16498: 
16499: In cases where a domain coordinator has never used the "Set Domain Configuration"
16500: utility to create a configuration.db file on a domain's primary library server 
16501: only the following domain defaults: auth_def, auth_arg_def, lang_def
16502: -- corresponding values are authentication type (internal, krb4, krb5,
16503: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16504: will be available. Values are retrieved from cache (if current), unless the
16505: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16506: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16507: 
16508: Typical usage:
16509: 
16510: %domdefaults = &get_domain_defaults($target_domain);
16511: 
16512: =back
16513: 
16514: =head2 Network Status Functions
16515: 
16516: =over 4
16517: 
16518: =item *
16519: 
16520: dirlist() : return directory list based on URI (first arg).
16521: 
16522: Inputs: 1 required, 5 optional.
16523: 
16524: =over
16525: 
16526: =item 
16527: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16528: 
16529: =item
16530: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16531: 
16532: =item
16533: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16534: 
16535: =item
16536: $getpropath - boolean: 1 if prepend path using &propath(). 
16537: 
16538: =item
16539: $getuserdir - boolean: 1 if prepend path for "userfiles".
16540: 
16541: =item 
16542: $alternateRoot - path to prepend in place of path from $uri.
16543: 
16544: =back
16545: 
16546: Returns: Array of up to two items.
16547: 
16548: =over
16549: 
16550: a reference to an array of files/subdirectories
16551: 
16552: =over
16553: 
16554: Each element in the array of files/subdirectories is a & separated list of
16555: item name and the result of running stat on the item.  If dirlist was requested
16556: for a file instead of a directory, the item name will be ''. For a directory 
16557: listing, if the item is a metadata file, the element will end &N&M 
16558: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16559: default copyright set (1).  
16560: 
16561: =back
16562: 
16563: a scalar containing error condition (if encountered).
16564: 
16565: =over
16566: 
16567: =item 
16568: no_host (no homeserver identified for $username:$domain).
16569: 
16570: =item 
16571: no_such_host (server contacted for listing not identified as valid host).
16572: 
16573: =item 
16574: con_lost (connection to remote server failed).
16575: 
16576: =item 
16577: refused (invalid $username:$domain received on lond side).
16578: 
16579: =item 
16580: no_such_dir (directory at specified path on lond side does not exist). 
16581: 
16582: =item 
16583: empty (directory at specified path on lond side is empty).
16584: 
16585: =over
16586: 
16587: This is currently not encountered because the &ls3, &ls2, 
16588: &ls (_handler) routines on the lond side do not filter out
16589: . and .. from a directory listing. 
16590: 
16591: =back
16592: 
16593: =back
16594: 
16595: =back
16596: 
16597: =item *
16598: 
16599: spareserver() : find server with least workload from spare.tab
16600: 
16601: 
16602: =item *
16603: 
16604: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16605: if there is no corresponding loncapa host.
16606: 
16607: =back
16608: 
16609: 
16610: =head2 Apache Request
16611: 
16612: =over 4
16613: 
16614: =item *
16615: 
16616: ssi($url,%hash) : server side include, does a complete request cycle on url to
16617: localhost, posts hash
16618: 
16619: =back
16620: 
16621: =head2 Data to String to Data
16622: 
16623: =over 4
16624: 
16625: =item *
16626: 
16627: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16628: and '&' separators, supports elements that are arrayrefs and hashrefs
16629: 
16630: =item *
16631: 
16632: hashref2str($hashref) : convert a hashref into a string complete with
16633: escaping and '=' and '&' separators, supports elements that are
16634: arrayrefs and hashrefs
16635: 
16636: =item *
16637: 
16638: arrayref2str($arrayref) : convert an arrayref into a string complete
16639: with escaping and '&' separators, supports elements that are arrayrefs
16640: and hashrefs
16641: 
16642: =item *
16643: 
16644: str2hash($string) : convert string to hash using unescaping and
16645: splitting on '=' and '&', supports elements that are arrayrefs and
16646: hashrefs
16647: 
16648: =item *
16649: 
16650: str2array($string) : convert string to hash using unescaping and
16651: splitting on '&', supports elements that are arrayrefs and hashrefs
16652: 
16653: =back
16654: 
16655: =head2 Logging Routines
16656: 
16657: 
16658: These routines allow one to make log messages in the lonnet.log and
16659: lonnet.perm logfiles.
16660: 
16661: =over 4
16662: 
16663: =item *
16664: 
16665: logtouch() : make sure the logfile, lonnet.log, exists
16666: 
16667: =item *
16668: 
16669: logthis() : append message to the normal lonnet.log file, it gets
16670: preiodically rolled over and deleted.
16671: 
16672: =item *
16673: 
16674: logperm() : append a permanent message to lonnet.perm.log, this log
16675: file never gets deleted by any automated portion of the system, only
16676: messages of critical importance should go in here.
16677: 
16678: 
16679: =back
16680: 
16681: =head2 General File Helper Routines
16682: 
16683: =over 4
16684: 
16685: =item *
16686: 
16687: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
16688: (a) files in /uploaded
16689:   (i) If a local copy of the file exists - 
16690:       compares modification date of local copy with last-modified date for 
16691:       definitive version stored on home server for course. If local copy is 
16692:       stale, requests a new version from the home server and stores it. 
16693:       If the original has been removed from the home server, then local copy 
16694:       is unlinked.
16695:   (ii) If local copy does not exist -
16696:       requests the file from the home server and stores it. 
16697:   
16698:   If $caller is 'uploadrep':  
16699:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
16700:     for request for files originally uploaded via DOCS. 
16701:      - returns 'ok' if fresh local copy now available, -1 otherwise.
16702:   
16703:   Otherwise:
16704:      This indicates a call from the content generation phase of the request.
16705:      -  returns the entire contents of the file or -1.
16706:      
16707: (b) files in /res
16708:    - returns the entire contents of a file or -1; 
16709:    it properly subscribes to and replicates the file if neccessary.
16710: 
16711: 
16712: =item *
16713: 
16714: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
16715:                   reference
16716: 
16717: returns either a stat() list of data about the file or an empty list
16718: if the file doesn't exist or couldn't find out about it (connection
16719: problems or user unknown)
16720: 
16721: =item *
16722: 
16723: filelocation($dir,$file) : returns file system location of a file
16724: based on URI; meant to be "fairly clean" absolute reference, $dir is a
16725: directory that relative $file lookups are to looked in ($dir of /a/dir
16726: and a file of ../bob will become /a/bob)
16727: 
16728: =item *
16729: 
16730: hreflocation($dir,$file) : returns file system location or a URL; same as
16731: filelocation except for hrefs
16732: 
16733: =item *
16734: 
16735: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
16736: also removes beginning /home/httpd/html unless /priv/ follows it.
16737: 
16738: =back
16739: 
16740: =head2 Usererfile file routines (/uploaded*)
16741: 
16742: =over 4
16743: 
16744: =item *
16745: 
16746: userfileupload(): main rotine for putting a file in a user or course's
16747:                   filespace, arguments are,
16748: 
16749:  formname - required - this is the name of the element in $env where the
16750:            filename, and the contents of the file to create/modifed exist
16751:            the filename is in $env{'form.'.$formname.'.filename'} and the
16752:            contents of the file is located in $env{'form.'.$formname}
16753:  context - if coursedoc, store the file in the course of the active role
16754:              of the current user; 
16755:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
16756:            if 'canceloverwrite': delete file in tmp/overwrites directory
16757:  subdir - required - subdirectory to put the file in under ../userfiles/
16758:          if undefined, it will be placed in "unknown"
16759: 
16760:  (This routine calls clean_filename() to remove any dangerous
16761:  characters from the filename, and then calls finuserfileupload() to
16762:  complete the transaction)
16763: 
16764:  returns either the url of the uploaded file (/uploaded/....) if successful
16765:  and /adm/notfound.html if unsuccessful
16766: 
16767: =item *
16768: 
16769: clean_filename(): routine for cleaing a filename up for storage in
16770:                  userfile space, argument is:
16771: 
16772:  filename - proposed filename
16773: 
16774: returns: the new clean filename
16775: 
16776: =item *
16777: 
16778: finishuserfileupload(): routine that creates and sends the file to
16779: userspace, probably shouldn't be called directly
16780: 
16781:   docuname: username or courseid of destination for the file
16782:   docudom: domain of user/course of destination for the file
16783:   formname: same as for userfileupload()
16784:   fname: filename (including subdirectories) for the file
16785:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
16786:           if hashref, and context is scantron, will convert csv format to standard format
16787:   allfiles: reference to hash used to store objects found by parser
16788:   codebase: reference to hash used for codebases of java objects found by parser
16789:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
16790:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
16791:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
16792:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
16793:   context: if 'overwrite', will move the uploaded file from its temporary location to
16794:             userfiles to facilitate overwriting a previously uploaded file with same name.
16795:   mimetype: reference to scalar to accommodate mime type determined
16796:             from File::MMagic if $parser = parse.
16797: 
16798:  returns either the url of the uploaded file (/uploaded/....) if successful
16799:  and /adm/notfound.html if unsuccessful (or an error message if context 
16800:  was 'overwrite').
16801:  
16802: 
16803: =item *
16804: 
16805: renameuserfile(): renames an existing userfile to a new name
16806: 
16807:   Args:
16808:    docuname: username or courseid of destination for the file
16809:    docudom: domain of user/course of destination for the file
16810:    old: current file name (including any subdirs under userfiles)
16811:    new: desired file name (including any subdirs under userfiles)
16812: 
16813: =item *
16814: 
16815: mkdiruserfile(): creates a directory is a userfiles dir
16816: 
16817:   Args:
16818:    docuname: username or courseid of destination for the file
16819:    docudom: domain of user/course of destination for the file
16820:    dir: dir to create (including any subdirs under userfiles)
16821: 
16822: =item *
16823: 
16824: removeuserfile(): removes a file that exists in userfiles
16825: 
16826:   Args:
16827:    docuname: username or courseid of destination for the file
16828:    docudom: domain of user/course of destination for the file
16829:    fname: filname to delete (including any subdirs under userfiles)
16830: 
16831: =item *
16832: 
16833: removeuploadedurl(): convience function for removeuserfile()
16834: 
16835:   Args:
16836:    url:  a full /uploaded/... url to delete
16837: 
16838: =item * 
16839: 
16840: get_portfile_permissions():
16841:   Args:
16842:     domain: domain of user or course contain the portfolio files
16843:     user: name of user or num of course contain the portfolio files
16844:   Returns:
16845:     hashref of a dump of the proper file_permissions.db
16846:    
16847: 
16848: =item * 
16849: 
16850: get_access_controls():
16851: 
16852: Args:
16853:   current_permissions: the hash ref returned from get_portfile_permissions()
16854:   group: (optional) the group you want the files associated with
16855:   file: (optional) the file you want access info on
16856: 
16857: Returns:
16858:     a hash (keys are file names) of hashes containing
16859:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
16860:         values are XML containing access control settings (see below) 
16861: 
16862: Internal notes:
16863: 
16864:  access controls are stored in file_permissions.db as key=value pairs.
16865:     key -> path to file/file_name\0uniqueID:scope_end_start
16866:         where scope -> public,guest,course,group,domains or users.
16867:               end -> UNIX time for end of access (0 -> no end date)
16868:               start -> UNIX time for start of access
16869: 
16870:     value -> XML description of access control
16871:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
16872:             <start></start>
16873:             <end></end>
16874: 
16875:             <password></password>  for scope type = guest
16876: 
16877:             <domain></domain>     for scope type = course or group
16878:             <number></number>
16879:             <roles id="">
16880:              <role></role>
16881:              <access></access>
16882:              <section></section>
16883:              <group></group>
16884:             </roles>
16885: 
16886:             <dom></dom>         for scope type = domains
16887: 
16888:             <users>             for scope type = users
16889:              <user>
16890:               <uname></uname>
16891:               <udom></udom>
16892:              </user>
16893:             </users>
16894:            </scope> 
16895:               
16896:  Access data is also aggregated for each file in an additional key=value pair:
16897:  key -> path to file/file_name\0accesscontrol 
16898:  value -> reference to hash
16899:           hash contains key = value pairs
16900:           where key = uniqueID:scope_end_start
16901:                 value = UNIX time record was last updated
16902: 
16903:           Used to improve speed of look-ups of access controls for each file.  
16904:  
16905:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
16906: 
16907: =item *
16908: 
16909: modify_access_controls():
16910: 
16911: Modifies access controls for a portfolio file
16912: Args
16913: 1. file name
16914: 2. reference to hash of required changes,
16915: 3. domain
16916: 4. username
16917:   where domain,username are the domain of the portfolio owner 
16918:   (either a user or a course) 
16919: 
16920: Returns:
16921: 1. result of additions or updates ('ok' or 'error', with error message). 
16922: 2. result of deletions ('ok' or 'error', with error message).
16923: 3. reference to hash of any new or updated access controls.
16924: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
16925:    key = integer (inbound ID)
16926:    value = uniqueID
16927: 
16928: =item *
16929: 
16930: get_timebased_id():
16931: 
16932: Attempts to get a unique timestamp-based suffix for use with items added to a 
16933: course via the Course Editor (e.g., folders, composite pages, 
16934: group bulletin boards).
16935: 
16936: Args: (first three required; six others optional)
16937: 
16938: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
16939:    docssequence, or name of group
16940: 
16941: 2. keyid (alphanumeric): name of temporary locking key in hash,
16942:    e.g., num, boardids
16943: 
16944: 3. namespace: name of gdbm file used to store suffixes already assigned;  
16945:    file will be named nohist_namespace.db
16946: 
16947: 4. cdom: domain of course; default is current course domain from %env
16948: 
16949: 5. cnum: course number; default is current course number from %env
16950: 
16951: 6. idtype: set to concat if an additional digit is to be appended to the 
16952:    unix timestamp to form the suffix, if the plain timestamp is already
16953:    in use.  Default is to not do this, but simply increment the unix 
16954:    timestamp by 1 until a unique key is obtained.
16955: 
16956: 7. who: holder of locking key; defaults to user:domain for user.
16957: 
16958: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
16959:    retrying); default is 3.
16960: 
16961: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
16962: 
16963: Returns:
16964: 
16965: 1. suffix obtained (numeric)
16966: 
16967: 2. result of deleting locking key (ok if deleted, or lock never obtained)
16968: 
16969: 3. error: contains (localized) error message if an error occurred.
16970: 
16971: 
16972: =back
16973: 
16974: =head2 HTTP Helper Routines
16975: 
16976: =over 4
16977: 
16978: =item *
16979: 
16980: escape() : unpack non-word characters into CGI-compatible hex codes
16981: 
16982: =item *
16983: 
16984: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
16985: 
16986: =back
16987: 
16988: =head1 PRIVATE SUBROUTINES
16989: 
16990: =head2 Underlying communication routines (Shouldn't call)
16991: 
16992: =over 4
16993: 
16994: =item *
16995: 
16996: subreply() : tries to pass a message to lonc, returns con_lost if incapable
16997: 
16998: =item *
16999: 
17000: reply() : uses subreply to send a message to remote machine, logs all failures
17001: 
17002: =item *
17003: 
17004: critical() : passes a critical message to another server; if cannot
17005: get through then place message in connection buffer directory and
17006: returns con_delayed, if incapable of saving message, returns
17007: con_failed
17008: 
17009: =item *
17010: 
17011: reconlonc() : tries to reconnect lonc client processes.
17012: 
17013: =back
17014: 
17015: =head2 Resource Access Logging
17016: 
17017: =over 4
17018: 
17019: =item *
17020: 
17021: flushcourselogs() : flush (save) buffer logs and access logs
17022: 
17023: =item *
17024: 
17025: courselog($what) : save message for course in hash
17026: 
17027: =item *
17028: 
17029: courseacclog($what) : save message for course using &courselog().  Perform
17030: special processing for specific resource types (problems, exams, quizzes, etc).
17031: 
17032: =item *
17033: 
17034: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17035: as a PerlChildExitHandler
17036: 
17037: =back
17038: 
17039: =head2 Other
17040: 
17041: =over 4
17042: 
17043: =item *
17044: 
17045: symblist($mapname,%newhash) : update symbolic storage links
17046: 
17047: =back
17048: 
17049: =cut
17050: 

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