File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1461: download - view: text, annotated - select for diffs
Mon Jul 19 15:48:27 2021 UTC (3 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907 Content in a course can be set to be deep-link only.
  If initial access is via a deep-link (/tiny/$domain/$uniqueid), and
  target folder/resource is deep-link only, access to resources, menus,
  and items listed in Course Contents controlled by deeplink parameter value.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1461 2021/07/19 15:48:27 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 Sys::Hostname::FQDN();
  101: use LONCAPA qw(:DEFAULT :match);
  102: use LONCAPA::Configuration;
  103: use LONCAPA::lonmetadata;
  104: use LONCAPA::Lond;
  105: use LONCAPA::LWPReq;
  106: use LONCAPA::transliterate;
  107: 
  108: use File::Copy;
  109: 
  110: my $readit;
  111: my $max_connection_retries = 20;     # Or some such value.
  112: 
  113: require Exporter;
  114: 
  115: our @ISA = qw (Exporter);
  116: our @EXPORT = qw(%env);
  117: 
  118: 
  119: # ------------------------------------ Logging (parameters, docs, slots, roles)
  120: {
  121:     my $logid;
  122:     sub write_log {
  123: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  124:         if ($context eq 'course') {
  125:             if (($cnum eq '') || ($cdom eq '')) {
  126:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  127:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  128:             }
  129:         }
  130: 	$logid ++;
  131:         my $now = time();
  132: 	my $id=$now.'00000'.$$.'00000'.$logid;
  133:         my $ip = &get_requestor_ip();
  134:         my $logentry = { 
  135:                           $id => {
  136:                                    'exe_uname' => $env{'user.name'},
  137:                                    'exe_udom'  => $env{'user.domain'},
  138:                                    'exe_time'  => $now,
  139:                                    'exe_ip'    => $ip,
  140:                                    'delflag'   => $delflag,
  141:                                    'logentry'  => $storehash,
  142:                                    'uname'     => $uname,
  143:                                    'udom'      => $udom,
  144:                                   }
  145:                        };
  146: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  147:     }
  148: }
  149: 
  150: sub logtouch {
  151:     my $execdir=$perlvar{'lonDaemons'};
  152:     unless (-e "$execdir/logs/lonnet.log") {	
  153: 	open(my $fh,">>","$execdir/logs/lonnet.log");
  154: 	close $fh;
  155:     }
  156:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  157:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  158: }
  159: 
  160: sub logthis {
  161:     my $message=shift;
  162:     my $execdir=$perlvar{'lonDaemons'};
  163:     my $now=time;
  164:     my $local=localtime($now);
  165:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
  166: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  167: 	print $fh $logstring;
  168: 	close($fh);
  169:     }
  170:     return 1;
  171: }
  172: 
  173: sub logperm {
  174:     my $message=shift;
  175:     my $execdir=$perlvar{'lonDaemons'};
  176:     my $now=time;
  177:     my $local=localtime($now);
  178:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
  179: 	print $fh "$now:$message:$local\n";
  180: 	close($fh);
  181:     }
  182:     return 1;
  183: }
  184: 
  185: sub create_connection {
  186:     my ($hostname,$lonid) = @_;
  187:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  188: 				     Type    => SOCK_STREAM,
  189: 				     Timeout => 10);
  190:     return 0 if (!$client);
  191:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
  192:     my $result = <$client>;
  193:     chomp($result);
  194:     return 1 if ($result eq 'done');
  195:     return 0;
  196: }
  197: 
  198: sub get_server_timezone {
  199:     my ($cnum,$cdom) = @_;
  200:     my $home=&homeserver($cnum,$cdom);
  201:     if ($home ne 'no_host') {
  202:         my $cachetime = 24*3600;
  203:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  204:         if (defined($cached)) {
  205:             return $timezone;
  206:         } else {
  207:             my $timezone = &reply('servertimezone',$home);
  208:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  209:         }
  210:     }
  211: }
  212: 
  213: sub get_server_distarch {
  214:     my ($lonhost,$ignore_cache) = @_;
  215:     if (defined($lonhost)) {
  216:         if (!defined(&hostname($lonhost))) {
  217:             return;
  218:         }
  219:         my $cachetime = 12*3600;
  220:         if (!$ignore_cache) {
  221:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  222:             if (defined($cached)) {
  223:                 return $distarch;
  224:             }
  225:         }
  226:         my $rep = &reply('serverdistarch',$lonhost);
  227:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  228:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  229:                 $rep eq '') {
  230:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  231:         }
  232:     }
  233:     return;
  234: }
  235: 
  236: sub get_servercerts_info {
  237:     my ($lonhost,$hostname,$context) = @_;
  238:     return if ($lonhost eq '');
  239:     if ($hostname eq '') {
  240:         $hostname = &hostname($lonhost);
  241:     }
  242:     return if ($hostname eq '');
  243:     my ($rep,$uselocal);
  244:     if ($context eq 'install') {
  245:         $uselocal = 1;
  246:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  247:         $uselocal = 1;
  248:     }
  249:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  250:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  251:         if ($distro eq '') {
  252:             $uselocal = 0;
  253:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  254:             if ($1 < 6) {
  255:                 $uselocal = 0;
  256:             }
  257:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  258:             if ($1 < 12) {
  259:                 $uselocal = 0;
  260:             }
  261:         }
  262:     }
  263:     if ($uselocal) {
  264:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  265:     } else {
  266:         $rep=&reply('servercerts',$lonhost);
  267:     }
  268:     my ($result,%returnhash);
  269:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  270:         ($rep eq 'unknown_cmd')) {
  271:         $result = $rep;
  272:     } else {
  273:         $result = 'ok';
  274:         my @pairs=split(/\&/,$rep);
  275:         foreach my $item (@pairs) {
  276:             my ($key,$value)=split(/=/,$item,2);
  277:             my $what = &unescape($key);
  278:             $returnhash{$what}=&thaw_unescape($value);
  279:         }
  280:     }
  281:     return ($result,\%returnhash);
  282: }
  283: 
  284: sub get_server_loncaparev {
  285:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  286:     if (defined($lonhost)) {
  287:         if (!defined(&hostname($lonhost))) {
  288:             undef($lonhost);
  289:         }
  290:     }
  291:     if (!defined($lonhost)) {
  292:         if (defined(&domain($dom,'primary'))) {
  293:             $lonhost=&domain($dom,'primary');
  294:             if ($lonhost eq 'no_host') {
  295:                 undef($lonhost);
  296:             }
  297:         }
  298:     }
  299:     if (defined($lonhost)) {
  300:         my $cachetime = 12*3600;
  301:         if (!$ignore_cache) {
  302:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  303:             if (defined($cached)) {
  304:                 return $loncaparev;
  305:             }
  306:         }
  307:         my ($answer,$loncaparev);
  308:         my @ids=&current_machine_ids();
  309:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  310:             $answer = $perlvar{'lonVersion'};
  311:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  312:                 $loncaparev = $1;
  313:             }
  314:         } else {
  315:             $answer = &reply('serverloncaparev',$lonhost);
  316:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  317:                 if ($caller eq 'loncron') {
  318:                     my $hostname = &hostname($lonhost);
  319:                     my $protocol = $protocol{$lonhost};
  320:                     $protocol = 'http' if ($protocol ne 'https');
  321:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  322:                     my $request=new HTTP::Request('GET',$url);
  323:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  324:                     unless ($response->is_error()) {
  325:                         my $content = $response->content;
  326:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  327:                             $loncaparev = $1;
  328:                         }
  329:                     }
  330:                 } else {
  331:                     $loncaparev = $loncaparevs{$lonhost};
  332:                 }
  333:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  334:                 $loncaparev = $1;
  335:             }
  336:         }
  337:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  338:     }
  339: }
  340: 
  341: sub get_server_homeID {
  342:     my ($hostname,$ignore_cache,$caller) = @_;
  343:     unless ($ignore_cache) {
  344:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  345:         if (defined($cached)) {
  346:             return $serverhomeID;
  347:         }
  348:     }
  349:     my $cachetime = 12*3600;
  350:     my $serverhomeID;
  351:     if ($caller eq 'loncron') { 
  352:         my @machine_ids = &machine_ids($hostname);
  353:         foreach my $id (@machine_ids) {
  354:             my $response = &reply('serverhomeID',$id);
  355:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  356:                 $serverhomeID = $response;
  357:                 last;
  358:             }
  359:         }
  360:         if ($serverhomeID eq '') {
  361:             $serverhomeID = $machine_ids[-1];
  362:         }
  363:     } else {
  364:         $serverhomeID = $serverhomeIDs{$hostname};
  365:     }
  366:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  367: }
  368: 
  369: sub get_remote_globals {
  370:     my ($lonhost,$whathash,$ignore_cache) = @_;
  371:     my ($result,%returnhash,%whatneeded);
  372:     if (ref($whathash) eq 'HASH') {
  373:         foreach my $what (sort(keys(%{$whathash}))) {
  374:             my $hashid = $lonhost.'-'.$what;
  375:             my ($response,$cached);
  376:             unless ($ignore_cache) {
  377:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  378:             }
  379:             if (defined($cached)) {
  380:                 $returnhash{$what} = $response;
  381:             } else {
  382:                 $whatneeded{$what} = 1;
  383:             }
  384:         }
  385:         if (keys(%whatneeded) == 0) {
  386:             $result = 'ok';
  387:         } else {
  388:             my $requested = &freeze_escape(\%whatneeded);
  389:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  390:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  391:                 ($rep eq 'unknown_cmd')) {
  392:                 $result = $rep;
  393:             } else {
  394:                 $result = 'ok';
  395:                 my @pairs=split(/\&/,$rep);
  396:                 foreach my $item (@pairs) {
  397:                     my ($key,$value)=split(/=/,$item,2);
  398:                     my $what = &unescape($key);
  399:                     my $hashid = $lonhost.'-'.$what;
  400:                     $returnhash{$what}=&thaw_unescape($value);
  401:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  402:                 }
  403:             }
  404:         }
  405:     }
  406:     return ($result,\%returnhash);
  407: }
  408: 
  409: sub remote_devalidate_cache {
  410:     my ($lonhost,$cachekeys) = @_;
  411:     my $items;
  412:     return unless (ref($cachekeys) eq 'ARRAY');
  413:     my $cachestr = join('&',@{$cachekeys});
  414:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  415:     return $response;
  416: }
  417: 
  418: # -------------------------------------------------- Non-critical communication
  419: sub subreply {
  420:     my ($cmd,$server)=@_;
  421:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  422:     #
  423:     #  With loncnew process trimming, there's a timing hole between lonc server
  424:     #  process exit and the master server picking up the listen on the AF_UNIX
  425:     #  socket.  In that time interval, a lock file will exist:
  426: 
  427:     my $lockfile=$peerfile.".lock";
  428:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  429: 	sleep(0.1);
  430:     }
  431:     # At this point, either a loncnew parent is listening or an old lonc
  432:     # or loncnew child is listening so we can connect or everything's dead.
  433:     #
  434:     #   We'll give the connection a few tries before abandoning it.  If
  435:     #   connection is not possible, we'll con_lost back to the client.
  436:     #   
  437:     my $client;
  438:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  439: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  440: 				      Type    => SOCK_STREAM,
  441: 				      Timeout => 10);
  442: 	if ($client) {
  443: 	    last;		# Connected!
  444: 	} else {
  445: 	    &create_connection(&hostname($server),$server);
  446: 	}
  447:         sleep(0.1);	# Try again later if failed connection.
  448:     }
  449:     my $answer;
  450:     if ($client) {
  451: 	print $client "sethost:$server:$cmd\n";
  452: 	$answer=<$client>;
  453: 	if (!$answer) { $answer="con_lost"; }
  454: 	chomp($answer);
  455:     } else {
  456: 	$answer = 'con_lost';	# Failed connection.
  457:     }
  458:     return $answer;
  459: }
  460: 
  461: sub reply {
  462:     my ($cmd,$server)=@_;
  463:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  464:     my $answer=subreply($cmd,$server);
  465:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  466:         my $logged = $cmd;
  467:         if ($cmd =~ /^encrypt:([^:]+):/) {
  468:             my $subcmd = $1;
  469:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  470:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  471:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
  472:                 (undef,undef,my @rest) = split(/:/,$cmd);
  473:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  474:                     splice(@rest,2,1,'Hidden');
  475:                 } elsif ($subcmd eq 'passwd') {
  476:                     splice(@rest,2,2,('Hidden','Hidden'));
  477:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  478:                          ($subcmd eq 'autoexportgrades')) {
  479:                     splice(@rest,3,1,'Hidden');
  480:                 }
  481:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  482:             }
  483:         }
  484:         &logthis("<font color=\"blue\">WARNING:".
  485:                  " $logged to $server returned $answer</font>");
  486:     }
  487:     return $answer;
  488: }
  489: 
  490: # ----------------------------------------------------------- Send USR1 to lonc
  491: 
  492: sub reconlonc {
  493:     my ($lonid) = @_;
  494:     if ($lonid) {
  495:         my $hostname = &hostname($lonid);
  496: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  497: 	if ($hostname && -e $peerfile) {
  498: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  499: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  500: 					     Type    => SOCK_STREAM,
  501: 					     Timeout => 10);
  502: 	    if ($client) {
  503: 		print $client ("reset_retries\n");
  504: 		my $answer=<$client>;
  505: 		#reset just this one.
  506: 	    }
  507: 	}
  508: 	return;
  509:     }
  510: 
  511:     &logthis("Trying to reconnect lonc");
  512:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  513:     if (open(my $fh,"<",$loncfile)) {
  514: 	my $loncpid=<$fh>;
  515:         chomp($loncpid);
  516:         if (kill 0 => $loncpid) {
  517: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  518:             kill USR1 => $loncpid;
  519:             sleep 1;
  520:         } else {
  521: 	    &logthis(
  522:                "<font color=\"blue\">WARNING:".
  523:                " lonc at pid $loncpid not responding, giving up</font>");
  524:         }
  525:     } else {
  526: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  527:     }
  528: }
  529: 
  530: # ------------------------------------------------------ Critical communication
  531: 
  532: sub critical {
  533:     my ($cmd,$server)=@_;
  534:     unless (&hostname($server)) {
  535:         &logthis("<font color=\"blue\">WARNING:".
  536:                " Critical message to unknown server ($server)</font>");
  537:         return 'no_such_host';
  538:     }
  539:     my $answer=reply($cmd,$server);
  540:     if ($answer eq 'con_lost') {
  541: 	&reconlonc($server);
  542: 	my $answer=reply($cmd,$server);
  543:         if ($answer eq 'con_lost') {
  544:             my $now=time;
  545:             my $middlename=$cmd;
  546:             $middlename=substr($middlename,0,16);
  547:             $middlename=~s/\W//g;
  548:             my $dfilename=
  549:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  550:             $dumpcount++;
  551:             {
  552: 		my $dfh;
  553: 		if (open($dfh,">",$dfilename)) {
  554: 		    print $dfh "$cmd\n"; 
  555: 		    close($dfh);
  556: 		}
  557:             }
  558:             sleep 1;
  559:             my $wcmd='';
  560:             {
  561: 		my $dfh;
  562: 		if (open($dfh,"<",$dfilename)) {
  563: 		    $wcmd=<$dfh>; 
  564: 		    close($dfh);
  565: 		}
  566:             }
  567:             chomp($wcmd);
  568:             if ($wcmd eq $cmd) {
  569: 		&logthis("<font color=\"blue\">WARNING: ".
  570:                          "Connection buffer $dfilename: $cmd</font>");
  571:                 &logperm("D:$server:$cmd");
  572: 	        return 'con_delayed';
  573:             } else {
  574:                 &logthis("<font color=\"red\">CRITICAL:"
  575:                         ." Critical connection failed: $server $cmd</font>");
  576:                 &logperm("F:$server:$cmd");
  577:                 return 'con_failed';
  578:             }
  579:         }
  580:     }
  581:     return $answer;
  582: }
  583: 
  584: # ------------------------------------------- check if return value is an error
  585: 
  586: sub error {
  587:     my ($result) = @_;
  588:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  589: 	if ($2 == 2) { return undef; }
  590: 	return $1;
  591:     }
  592:     return undef;
  593: }
  594: 
  595: sub convert_and_load_session_env {
  596:     my ($lonidsdir,$handle)=@_;
  597:     my @profile;
  598:     {
  599: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  600: 	if (!$opened) {
  601: 	    return 0;
  602: 	}
  603: 	flock($idf,LOCK_SH);
  604: 	@profile=<$idf>;
  605: 	close($idf);
  606:     }
  607:     my %temp_env;
  608:     foreach my $line (@profile) {
  609: 	if ($line !~ m/=/) {
  610: 	    return 0;
  611: 	}
  612: 	chomp($line);
  613: 	my ($envname,$envvalue)=split(/=/,$line,2);
  614: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  615:     }
  616:     unlink("$lonidsdir/$handle.id");
  617:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  618: 	    0640)) {
  619: 	%disk_env = %temp_env;
  620: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  621: 	untie(%disk_env);
  622:     }
  623:     return 1;
  624: }
  625: 
  626: # ------------------------------------------- Transfer profile into environment
  627: my $env_loaded;
  628: sub transfer_profile_to_env {
  629:     my ($lonidsdir,$handle,$force_transfer) = @_;
  630:     if (!$force_transfer && $env_loaded) { return; } 
  631: 
  632:     if (!defined($lonidsdir)) {
  633: 	$lonidsdir = $perlvar{'lonIDsDir'};
  634:     }
  635:     if (!defined($handle)) {
  636:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  637:     }
  638: 
  639:     my $convert;
  640:     {
  641:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  642: 	if (!$opened) {
  643: 	    return;
  644: 	}
  645: 	flock($idf,LOCK_SH);
  646: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  647: 		&GDBM_READER(),0640)) {
  648: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  649: 	    untie(%disk_env);
  650: 	} else {
  651: 	    $convert = 1;
  652: 	}
  653:     }
  654:     if ($convert) {
  655: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  656: 	    &logthis("Failed to load session, or convert session.");
  657: 	}
  658:     }
  659: 
  660:     my %remove;
  661:     while ( my $envname = each(%env) ) {
  662:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  663:             if ($time < time-300) {
  664:                 $remove{$key}++;
  665:             }
  666:         }
  667:     }
  668: 
  669:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  670:     $env_loaded=1;
  671:     foreach my $expired_key (keys(%remove)) {
  672:         &delenv($expired_key);
  673:     }
  674: }
  675: 
  676: # ---------------------------------------------------- Check for valid session 
  677: sub check_for_valid_session {
  678:     my ($r,$name,$userhashref,$domref) = @_;
  679:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  680:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  681:     if ($name eq 'lonDAV') {
  682:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  683:     } else {
  684:         $lonidsdir=$r->dir_config('lonIDsDir');
  685:         if ($name eq '') {
  686:             $name = 'lonID';
  687:         }
  688:     }
  689:     if ($name eq 'lonID') {
  690:         $secure = 'lonSID';
  691:         $linkname = 'lonLinkID';
  692:         $pubname = 'lonPubID';
  693:         if (exists($cookies{$secure})) {
  694:             $lonid=$cookies{$secure};
  695:         } elsif (exists($cookies{$name})) {
  696:             $lonid=$cookies{$name};
  697:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  698:             $lonid=$cookies{$linkname};
  699:         } elsif (exists($cookies{$pubname})) {
  700:             $lonid=$cookies{$pubname};
  701:         }
  702:     } else {
  703:         $lonid=$cookies{$name};
  704:     }
  705:     return undef if (!$lonid);
  706: 
  707:     my $handle=&LONCAPA::clean_handle($lonid->value);
  708:     if (-l "$lonidsdir/$handle.id") {
  709:         my $link = readlink("$lonidsdir/$handle.id");
  710:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  711:             $handle = $1;
  712:         }
  713:     }
  714:     if (!-e "$lonidsdir/$handle.id") {
  715:         if ((ref($domref)) && ($name eq 'lonID') && 
  716:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  717:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  718:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  719:                 $$domref = $possudom;
  720:             }
  721:         }
  722:         return undef;
  723:     }
  724: 
  725:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  726:     return undef if (!$opened);
  727: 
  728:     flock($idf,LOCK_SH);
  729:     my %disk_env;
  730:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  731: 	    &GDBM_READER(),0640)) {
  732: 	return undef;	
  733:     }
  734: 
  735:     if (!defined($disk_env{'user.name'})
  736: 	|| !defined($disk_env{'user.domain'})) {
  737:         untie(%disk_env);
  738: 	return undef;
  739:     }
  740: 
  741:     if (ref($userhashref) eq 'HASH') {
  742:         $userhashref->{'name'} = $disk_env{'user.name'};
  743:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  744:         if ($disk_env{'request.role'}) {
  745:             $userhashref->{'role'} = $disk_env{'request.role'};
  746:         }
  747:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  748:         if ($userhashref->{'lti'}) {
  749:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  750:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  751:         }
  752:     }
  753:     untie(%disk_env);
  754: 
  755:     return $handle;
  756: }
  757: 
  758: sub timed_flock {
  759:     my ($file,$lock_type) = @_;
  760:     my $failed=0;
  761:     eval {
  762: 	local $SIG{__DIE__}='DEFAULT';
  763: 	local $SIG{ALRM}=sub {
  764: 	    $failed=1;
  765: 	    die("failed lock");
  766: 	};
  767: 	alarm(13);
  768: 	flock($file,$lock_type);
  769: 	alarm(0);
  770:     };
  771:     if ($failed) {
  772: 	return undef;
  773:     } else {
  774: 	return 1;
  775:     }
  776: }
  777: 
  778: sub get_sessionfile_vars {
  779:     my ($handle,$lonidsdir,$storearr) = @_;
  780:     my %returnhash;
  781:     unless (ref($storearr) eq 'ARRAY') {
  782:         return %returnhash;
  783:     }
  784:     if (-l "$lonidsdir/$handle.id") {
  785:         my $link = readlink("$lonidsdir/$handle.id");
  786:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  787:             $handle = $1;
  788:         }
  789:     }
  790:     if ((-e "$lonidsdir/$handle.id") &&
  791:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  792:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  793:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  794:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  795:                 flock($idf,LOCK_SH);
  796:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  797:                         &GDBM_READER(),0640)) {
  798:                     foreach my $item (@{$storearr}) {
  799:                         $returnhash{$item} = $disk_env{$item};
  800:                     }
  801:                     untie(%disk_env);
  802:                 }
  803:             }
  804:         }
  805:     }
  806:     return %returnhash;
  807: }
  808: 
  809: # ---------------------------------------------------------- Append Environment
  810: 
  811: sub appenv {
  812:     my ($newenv,$roles) = @_;
  813:     if (ref($newenv) eq 'HASH') {
  814:         foreach my $key (keys(%{$newenv})) {
  815:             my $refused = 0;
  816: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  817:                 $refused = 1;
  818:                 if (ref($roles) eq 'ARRAY') {
  819:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  820:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  821:                         $refused = 0;
  822:                     }
  823:                 }
  824:             }
  825:             if ($refused) {
  826:                 &logthis("<font color=\"blue\">WARNING: ".
  827:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  828:                          .'</font>');
  829: 	        delete($newenv->{$key});
  830:             } else {
  831:                 $env{$key}=$newenv->{$key};
  832:             }
  833:         }
  834:         my $lonids = $perlvar{'lonIDsDir'};
  835:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  836:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  837:             if ($opened
  838: 	        && &timed_flock($env_file,LOCK_EX)
  839: 	        &&
  840: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  841: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  842: 	        while (my ($key,$value) = each(%{$newenv})) {
  843: 	            $disk_env{$key} = $value;
  844: 	        }
  845: 	        untie(%disk_env);
  846:             }
  847:         }
  848:     }
  849:     return 'ok';
  850: }
  851: # ----------------------------------------------------- Delete from Environment
  852: 
  853: sub delenv {
  854:     my ($delthis,$regexp,$roles) = @_;
  855:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  856:         my $refused = 1;
  857:         if (ref($roles) eq 'ARRAY') {
  858:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  859:             if (grep(/^\Q$role\E$/,@{$roles})) {
  860:                 $refused = 0;
  861:             }
  862:         }
  863:         if ($refused) {
  864:             &logthis("<font color=\"blue\">WARNING: ".
  865:                      "Attempt to delete from environment ".$delthis);
  866:             return 'error';
  867:         }
  868:     }
  869:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  870:     if ($opened
  871: 	&& &timed_flock($env_file,LOCK_EX)
  872: 	&&
  873: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  874: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  875: 	foreach my $key (keys(%disk_env)) {
  876: 	    if ($regexp) {
  877:                 if ($key=~/^$delthis/) {
  878:                     delete($env{$key});
  879:                     delete($disk_env{$key});
  880:                 } 
  881:             } else {
  882:                 if ($key=~/^\Q$delthis\E/) {
  883: 		    delete($env{$key});
  884: 		    delete($disk_env{$key});
  885: 	        }
  886:             }
  887: 	}
  888: 	untie(%disk_env);
  889:     }
  890:     return 'ok';
  891: }
  892: 
  893: sub get_env_multiple {
  894:     my ($name) = @_;
  895:     my @values;
  896:     if (defined($env{$name})) {
  897:         # exists is it an array
  898:         if (ref($env{$name})) {
  899:             @values=@{ $env{$name} };
  900:         } else {
  901:             $values[0]=$env{$name};
  902:         }
  903:     }
  904:     return(@values);
  905: }
  906: 
  907: # ------------------------------------------------------------------- Locking
  908: 
  909: sub set_lock {
  910:     my ($text)=@_;
  911:     $locknum++;
  912:     my $id=$$.'-'.$locknum;
  913:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  914:              'session.lock.'.$id => $text});
  915:     return $id;
  916: }
  917: 
  918: sub get_locks {
  919:     my $num=0;
  920:     my %texts=();
  921:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  922:        if ($lock=~/\w/) {
  923:           $num++;
  924:           $texts{$lock}=$env{'session.lock.'.$lock};
  925:        }
  926:    }
  927:    return ($num,%texts);
  928: }
  929: 
  930: sub remove_lock {
  931:     my ($id)=@_;
  932:     my $newlocks='';
  933:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  934:        if (($lock=~/\w/) && ($lock ne $id)) {
  935:           $newlocks.=','.$lock;
  936:        }
  937:     }
  938:     &appenv({'session.locks' => $newlocks});
  939:     &delenv('session.lock.'.$id);
  940: }
  941: 
  942: sub remove_all_locks {
  943:     my $activelocks=$env{'session.locks'};
  944:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  945:        if ($lock=~/\w/) {
  946:           &remove_lock($lock);
  947:        }
  948:     }
  949: }
  950: 
  951: 
  952: # ------------------------------------------ Find out current server userload
  953: sub userload {
  954:     my $numusers=0;
  955:     {
  956: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  957: 	my $filename;
  958: 	my $curtime=time;
  959: 	while ($filename=readdir(LONIDS)) {
  960: 	    next if ($filename eq '.' || $filename eq '..');
  961: 	    next if ($filename =~ /publicuser_\d+\.id/);
  962:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
  963: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  964: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  965: 	}
  966: 	closedir(LONIDS);
  967:     }
  968:     my $userloadpercent=0;
  969:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  970:     if ($maxuserload) {
  971: 	$userloadpercent=100*$numusers/$maxuserload;
  972:     }
  973:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  974:     return $userloadpercent;
  975: }
  976: 
  977: # ------------------------------ Find server with least workload from spare.tab
  978: 
  979: sub spareserver {
  980:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  981:     my $spare_server;
  982:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  983:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  984:                                                      :  $userloadpercent;
  985:     my ($uint_dom,$remotesessions);
  986:     if (($udom ne '') && (&domain($udom) ne '')) {
  987:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  988:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  989:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  990:         $remotesessions = $udomdefaults{'remotesessions'};
  991:     }
  992:     my $spareshash = &this_host_spares($udom);
  993:     if (ref($spareshash) eq 'HASH') {
  994:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  995:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  996:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  997:                                              $try_server));
  998: 	        ($spare_server, $lowest_load) =
  999: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
 1000:             }
 1001:         }
 1002: 
 1003:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1004: 
 1005:         if (!$found_server) {
 1006:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1007: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1008:                     next unless (&spare_can_host($udom,$uint_dom,
 1009:                                                  $remotesessions,$try_server));
 1010: 	            ($spare_server, $lowest_load) =
 1011: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1012:                 }
 1013: 	    }
 1014:         }
 1015:     }
 1016: 
 1017:     if (!$want_server_name) {
 1018:         if (defined($spare_server)) {
 1019:             my $hostname = &hostname($spare_server);
 1020:             if (defined($hostname)) {
 1021:                 my $protocol = 'http';
 1022:                 if ($protocol{$spare_server} eq 'https') {
 1023:                     $protocol = $protocol{$spare_server};
 1024:                 }
 1025:                 my $alias = &Apache::lonnet::use_proxy_alias($r,$spare_server);
 1026:                 $hostname = $alias if ($alias ne '');
 1027: 	        $spare_server = $protocol.'://'.$hostname;
 1028:             }
 1029:         }
 1030:     }
 1031:     return $spare_server;
 1032: }
 1033: 
 1034: sub compare_server_load {
 1035:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1036: 
 1037:     if ($required) {
 1038:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1039:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1040:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1041:         if (($major eq '' && $minor eq '') ||
 1042:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1043:             return ($spare_server,$lowest_load);
 1044:         }
 1045:     }
 1046: 
 1047:     my $loadans     = &reply('load',    $try_server);
 1048:     my $userloadans = &reply('userload',$try_server);
 1049: 
 1050:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1051: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1052:     }
 1053: 
 1054:     my $load;
 1055:     if ($loadans =~ /\d/) {
 1056: 	if ($userloadans =~ /\d/) {
 1057: 	    #both are numbers, pick the bigger one
 1058: 	    $load = ($loadans > $userloadans) ? $loadans 
 1059: 		                              : $userloadans;
 1060: 	} else {
 1061: 	    $load = $loadans;
 1062: 	}
 1063:     } else {
 1064: 	$load = $userloadans;
 1065:     }
 1066: 
 1067:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1068: 	$spare_server = $try_server;
 1069: 	$lowest_load  = $load;
 1070:     }
 1071:     return ($spare_server,$lowest_load);
 1072: }
 1073: 
 1074: # --------------------------- ask offload servers if user already has a session
 1075: sub find_existing_session {
 1076:     my ($udom,$uname) = @_;
 1077:     my $spareshash = &this_host_spares($udom);
 1078:     if (ref($spareshash) eq 'HASH') {
 1079:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1080:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1081:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1082:             }
 1083:         }
 1084:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1085:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1086:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1087:             }
 1088:         }
 1089:     }
 1090:     return;
 1091: }
 1092: 
 1093: sub delusersession {
 1094:     my ($lonid,$udom,$uname) = @_;
 1095:     my $uprimary_id = &domain($udom,'primary');
 1096:     my $uintdom = &internet_dom($uprimary_id);
 1097:     my $intdom = &internet_dom($lonid);
 1098:     my $serverhomedom = &host_domain($lonid);
 1099:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1100:         return &reply(join(':','delusersession',
 1101:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1102:     }
 1103:     return;
 1104: }
 1105: 
 1106: # check if user's browser sent load balancer cookie and server still has session
 1107: # and is not overloaded.
 1108: sub check_for_balancer_cookie {
 1109:     my ($r,$update_mtime) = @_;
 1110:     my ($otherserver,$cookie);
 1111:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1112:     if (exists($cookies{'balanceID'})) {
 1113:         my $balid = $cookies{'balanceID'};
 1114:         $cookie=&LONCAPA::clean_handle($balid->value);
 1115:         my $balancedir=$r->dir_config('lonBalanceDir');
 1116:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1117:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1118:                 my ($possudom,$possuname) = ($1,$2);
 1119:                 my $has_session = 0;
 1120:                 if ((&domain($possudom) ne '') &&
 1121:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1122:                     my $try_server;
 1123:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1124:                     if ($opened) {
 1125:                         flock($idf,LOCK_SH);
 1126:                         while (my $line = <$idf>) {
 1127:                             chomp($line);
 1128:                             if (&hostname($line) ne '') {
 1129:                                 $try_server = $line;
 1130:                                 last;
 1131:                             }
 1132:                         }
 1133:                         close($idf);
 1134:                         if (($try_server) &&
 1135:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1136:                             my $lowest_load = 30000;
 1137:                             ($otherserver,$lowest_load) =
 1138:                                 &compare_server_load($try_server,undef,$lowest_load);
 1139:                             if ($otherserver ne '' && $lowest_load < 100) {
 1140:                                 $has_session = 1;
 1141:                             } else {
 1142:                                 undef($otherserver);
 1143:                             }
 1144:                         }
 1145:                     }
 1146:                 }
 1147:                 if ($has_session) {
 1148:                     if ($update_mtime) {
 1149:                         my $atime = my $mtime = time;
 1150:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1151:                     }
 1152:                 } else {
 1153:                     unlink("$balancedir/$cookie.id");
 1154:                 }
 1155:             }
 1156:         }
 1157:     }
 1158:     return ($otherserver,$cookie);
 1159: }
 1160: 
 1161: sub updatebalcookie {
 1162:     my ($cookie,$balancer,$lastentry)=@_;
 1163:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1164:         my ($udom,$uname) = ($1,$2);
 1165:         my $uprimary_id = &domain($udom,'primary');
 1166:         my $uintdom = &internet_dom($uprimary_id);
 1167:         my $intdom = &internet_dom($balancer);
 1168:         my $serverhomedom = &host_domain($balancer);
 1169:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1170:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1171:         }
 1172:     }
 1173:     return;
 1174: }
 1175: 
 1176: sub delbalcookie {
 1177:     my ($cookie,$balancer) =@_;
 1178:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1179:         my ($udom,$uname) = ($1,$2);
 1180:         my $uprimary_id = &domain($udom,'primary');
 1181:         my $uintdom = &internet_dom($uprimary_id);
 1182:         my $intdom = &internet_dom($balancer);
 1183:         my $serverhomedom = &host_domain($balancer);
 1184:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1185:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1186:         }
 1187:     }
 1188: }
 1189: 
 1190: # -------------------------------- ask if server already has a session for user
 1191: sub has_user_session {
 1192:     my ($lonid,$udom,$uname) = @_;
 1193:     my $result = &reply(join(':','userhassession',
 1194: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1195:     return 1 if ($result eq 'ok');
 1196: 
 1197:     return 0;
 1198: }
 1199: 
 1200: # --------- determine least loaded server in a user's domain which allows login
 1201: 
 1202: sub choose_server {
 1203:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1204:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1205:     my %servers = &get_servers($udom);
 1206:     my $lowest_load = 30000;
 1207:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1208:     if ($skiploadbal) {
 1209:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1210:         unless (defined($cached)) {
 1211:             my $cachetime = 60*60*24;
 1212:             my %domconfig =
 1213:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1214:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1215:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1216:                                            $cachetime);
 1217:             }
 1218:         }
 1219:     }
 1220:     foreach my $lonhost (keys(%servers)) {
 1221:         if ($skiploadbal) {
 1222:             if (ref($balancers) eq 'HASH') {
 1223:                 next if (exists($balancers->{$lonhost}));
 1224:             }
 1225:         }
 1226:         my $loginvia;
 1227:         if ($checkloginvia) {
 1228:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1229:             if ($loginvia) {
 1230:                 my ($server,$path) = split(/:/,$loginvia);
 1231:                 ($login_host, $lowest_load) =
 1232:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1233:                 if ($login_host eq $server) {
 1234:                     $portal_path = $path;
 1235:                     $isredirect = 1;
 1236:                 }
 1237:             } else {
 1238:                 ($login_host, $lowest_load) =
 1239:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1240:                 if ($login_host eq $lonhost) {
 1241:                     $portal_path = '';
 1242:                     $isredirect = ''; 
 1243:                 }
 1244:             }
 1245:         } else {
 1246:             ($login_host, $lowest_load) =
 1247:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1248:         }
 1249:     }
 1250:     if ($login_host ne '') {
 1251:         $hostname = &hostname($login_host);
 1252:     }
 1253:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1254: }
 1255: 
 1256: sub get_course_sessions {
 1257:     my ($cnum,$cdom,$lastactivity) = @_;
 1258:     my %servers = &internet_dom_servers($cdom);
 1259:     my %returnhash;
 1260:     foreach my $server (sort(keys(%servers))) {
 1261:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1262:         my @pairs=split(/\&/,$rep);
 1263:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1264:             foreach my $item (@pairs) {
 1265:                 my ($key,$value)=split(/=/,$item,2);
 1266:                 $key = &unescape($key);
 1267:                 next if ($key =~ /^error: 2 /);
 1268:                 if (exists($returnhash{$key})) {
 1269:                     next if ($value < $returnhash{$key});
 1270:                 }
 1271:                 $returnhash{$key}=$value;
 1272:             }
 1273:         }
 1274:     }
 1275:     return %returnhash;
 1276: }
 1277: 
 1278: # --------------------------------------------- Try to change a user's password
 1279: 
 1280: sub changepass {
 1281:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1282:     $currentpass = &escape($currentpass);
 1283:     $newpass     = &escape($newpass);
 1284:     my $lonhost = $perlvar{'lonHostID'};
 1285:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1286: 		       $server);
 1287:     if (! $answer) {
 1288: 	&logthis("No reply on password change request to $server ".
 1289: 		 "by $uname in domain $udom.");
 1290:     } elsif ($answer =~ "^ok") {
 1291:         &logthis("$uname in $udom successfully changed their password ".
 1292: 		 "on $server.");
 1293:     } elsif ($answer =~ "^pwchange_failure") {
 1294: 	&logthis("$uname in $udom was unable to change their password ".
 1295: 		 "on $server.  The action was blocked by either lcpasswd ".
 1296: 		 "or pwchange");
 1297:     } elsif ($answer =~ "^non_authorized") {
 1298:         &logthis("$uname in $udom did not get their password correct when ".
 1299: 		 "attempting to change it on $server.");
 1300:     } elsif ($answer =~ "^auth_mode_error") {
 1301:         &logthis("$uname in $udom attempted to change their password despite ".
 1302: 		 "not being locally or internally authenticated on $server.");
 1303:     } elsif ($answer =~ "^unknown_user") {
 1304:         &logthis("$uname in $udom attempted to change their password ".
 1305: 		 "on $server but were unable to because $server is not ".
 1306: 		 "their home server.");
 1307:     } elsif ($answer =~ "^refused") {
 1308: 	&logthis("$server refused to change $uname in $udom password because ".
 1309: 		 "it was sent an unencrypted request to change the password.");
 1310:     } elsif ($answer =~ "invalid_client") {
 1311:         &logthis("$server refused to change $uname in $udom password because ".
 1312:                  "it was a reset by e-mail originating from an invalid server.");
 1313:     } elsif ($answer =~ "^prioruse") {
 1314:        &logthis("$server refused to change $uname in $udom password because ".
 1315:                 "the password had been used before");
 1316:     }
 1317:     return $answer;
 1318: }
 1319: 
 1320: # ----------------------- Try to determine user's current authentication scheme
 1321: 
 1322: sub queryauthenticate {
 1323:     my ($uname,$udom)=@_;
 1324:     my $uhome=&homeserver($uname,$udom);
 1325:     if (!$uhome) {
 1326: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1327: 	return 'no_host';
 1328:     }
 1329:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1330:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1331: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1332:     }
 1333:     return $answer;
 1334: }
 1335: 
 1336: # --------- Try to authenticate user from domain's lib servers (first this one)
 1337: 
 1338: sub authenticate {
 1339:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1340:     $upass=&escape($upass);
 1341:     $uname= &LONCAPA::clean_username($uname);
 1342:     my $uhome=&homeserver($uname,$udom,1);
 1343:     my $newhome;
 1344:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1345: # Maybe the machine was offline and only re-appeared again recently?
 1346:         &reconlonc();
 1347: # One more
 1348: 	$uhome=&homeserver($uname,$udom,1);
 1349:         if (($uhome eq 'no_host') && $checkdefauth) {
 1350:             if (defined(&domain($udom,'primary'))) {
 1351:                 $newhome=&domain($udom,'primary');
 1352:             }
 1353:             if ($newhome ne '') {
 1354:                 $uhome = $newhome;
 1355:             }
 1356:         }
 1357: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1358: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1359: 	    return 'no_host';
 1360:         }
 1361:     }
 1362:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1363:     if ($answer eq 'authorized') {
 1364:         if ($newhome) {
 1365:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1366:             return 'no_account_on_host'; 
 1367:         } else {
 1368:             &logthis("User $uname at $udom authorized by $uhome");
 1369:             return $uhome;
 1370:         }
 1371:     }
 1372:     if ($answer eq 'non_authorized') {
 1373: 	&logthis("User $uname at $udom rejected by $uhome");
 1374: 	return 'no_host'; 
 1375:     }
 1376:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1377:     return 'no_host';
 1378: }
 1379: 
 1380: sub can_host_session {
 1381:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1382:     my $canhost = 1;
 1383:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1384:     if (ref($remotesessions) eq 'HASH') {
 1385:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1386:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1387:                 $canhost = 0;
 1388:             } else {
 1389:                 $canhost = 1;
 1390:             }
 1391:         }
 1392:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1393:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1394:                 $canhost = 1;
 1395:             } else {
 1396:                 $canhost = 0;
 1397:             }
 1398:         }
 1399:         if ($canhost) {
 1400:             if ($remotesessions->{'version'} ne '') {
 1401:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1402:                 if ($reqmajor ne '' && $reqminor ne '') {
 1403:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1404:                         my $major = $1;
 1405:                         my $minor = $2;
 1406:                         if (($major < $reqmajor ) ||
 1407:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1408:                             $canhost = 0;
 1409:                         }
 1410:                     } else {
 1411:                         $canhost = 0;
 1412:                     }
 1413:                 }
 1414:             }
 1415:         }
 1416:     }
 1417:     if ($canhost) {
 1418:         if (ref($hostedsessions) eq 'HASH') {
 1419:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1420:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1421:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1422:                 if (($uint_dom ne '') && 
 1423:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1424:                     $canhost = 0;
 1425:                 } else {
 1426:                     $canhost = 1;
 1427:                 }
 1428:             }
 1429:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1430:                 if (($uint_dom ne '') && 
 1431:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1432:                     $canhost = 1;
 1433:                 } else {
 1434:                     $canhost = 0;
 1435:                 }
 1436:             }
 1437:         }
 1438:     }
 1439:     return $canhost;
 1440: }
 1441: 
 1442: sub spare_can_host {
 1443:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1444:     my $canhost=1;
 1445:     my $try_server_hostname = &hostname($try_server);
 1446:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1447:     my $serverhomedom = &host_domain($serverhomeID);
 1448:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1449:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1450:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1451:             $canhost = 0;
 1452:         }
 1453:     }
 1454:     if ($canhost) {
 1455:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1456:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1457:                 unless (&shared_institution($udom,$try_server)) {
 1458:                     $canhost = 0;
 1459:                 }
 1460:             }
 1461:         }
 1462:     }
 1463:     if (($canhost) && ($uint_dom)) {
 1464:         my @intdoms;
 1465:         my $internet_names = &get_internet_names($try_server);
 1466:         if (ref($internet_names) eq 'ARRAY') {
 1467:             @intdoms = @{$internet_names};
 1468:         }
 1469:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1470:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1471:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1472:                                          $remotesessions,
 1473:                                          $defdomdefaults{'hostedsessions'});
 1474:         }
 1475:     }
 1476:     return $canhost;
 1477: }
 1478: 
 1479: sub this_host_spares {
 1480:     my ($dom) = @_;
 1481:     my ($dom_in_use,$lonhost_in_use,$result);
 1482:     my @hosts = &current_machine_ids();
 1483:     foreach my $lonhost (@hosts) {
 1484:         if (&host_domain($lonhost) eq $dom) {
 1485:             $dom_in_use = $dom;
 1486:             $lonhost_in_use = $lonhost;
 1487:             last;
 1488:         }
 1489:     }
 1490:     if ($dom_in_use ne '') {
 1491:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1492:     }
 1493:     if (ref($result) ne 'HASH') {
 1494:         $lonhost_in_use = $perlvar{'lonHostID'};
 1495:         $dom_in_use = &host_domain($lonhost_in_use);
 1496:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1497:         if (ref($result) ne 'HASH') {
 1498:             $result = \%spareid;
 1499:         }
 1500:     }
 1501:     return $result;
 1502: }
 1503: 
 1504: sub spares_for_offload  {
 1505:     my ($dom_in_use,$lonhost_in_use) = @_;
 1506:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1507:     if (defined($cached)) {
 1508:         return $result;
 1509:     } else {
 1510:         my $cachetime = 60*60*24;
 1511:         my %domconfig =
 1512:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1513:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1514:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1515:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1516:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1517:                 }
 1518:             }
 1519:         }
 1520:     }
 1521:     return;
 1522: }
 1523: 
 1524: sub get_lonbalancer_config {
 1525:     my ($servers) = @_;
 1526:     my ($currbalancer,$currtargets);
 1527:     if (ref($servers) eq 'HASH') {
 1528:         foreach my $server (keys(%{$servers})) {
 1529:             my %what = (
 1530:                          spareid => 1,
 1531:                          perlvar => 1,
 1532:                        );
 1533:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1534:             if ($result eq 'ok') {
 1535:                 if (ref($returnhash) eq 'HASH') {
 1536:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1537:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1538:                             $currbalancer = $server;
 1539:                             $currtargets = {};
 1540:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1541:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1542:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1543:                                 }
 1544:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1545:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1546:                                 }
 1547:                             }
 1548:                             last;
 1549:                         }
 1550:                     }
 1551:                 }
 1552:             }
 1553:         }
 1554:     }
 1555:     return ($currbalancer,$currtargets);
 1556: }
 1557: 
 1558: sub check_loadbalancing {
 1559:     my ($uname,$udom,$caller) = @_;
 1560:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1561:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1562:     my $lonhost = $perlvar{'lonHostID'};
 1563:     my @hosts = &current_machine_ids();
 1564:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1565:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1566:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1567:     my $serverhomedom = &host_domain($lonhost);
 1568:     my $domneedscache;
 1569:     my $cachetime = 60*60*24;
 1570: 
 1571:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1572:         $dom_in_use = $udom;
 1573:         $homeintdom = 1;
 1574:     } else {
 1575:         $dom_in_use = $serverhomedom;
 1576:     }
 1577:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1578:     unless (defined($cached)) {
 1579:         my %domconfig =
 1580:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1581:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1582:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1583:         } else {
 1584:             $domneedscache = $dom_in_use;
 1585:         }
 1586:     }
 1587:     if (ref($result) eq 'HASH') {
 1588:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1589:             &check_balancer_result($result,@hosts);
 1590:         if ($is_balancer) {
 1591:             if (ref($currrules) eq 'HASH') {
 1592:                 if ($homeintdom) {
 1593:                     if ($uname ne '') {
 1594:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1595:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1596:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1597:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1598:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1599:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1600:                             }
 1601:                         }
 1602:                         if ($rule_in_effect eq '') {
 1603:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1604:                             if ($userenv{'inststatus'} ne '') {
 1605:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1606:                                 my ($othertitle,$usertypes,$types) =
 1607:                                     &Apache::loncommon::sorted_inst_types($udom);
 1608:                                 if (ref($types) eq 'ARRAY') {
 1609:                                     foreach my $type (@{$types}) {
 1610:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1611:                                             if (exists($currrules->{$type})) {
 1612:                                                 $rule_in_effect = $currrules->{$type};
 1613:                                             }
 1614:                                         }
 1615:                                     }
 1616:                                 }
 1617:                             } else {
 1618:                                 if (exists($currrules->{'default'})) {
 1619:                                     $rule_in_effect = $currrules->{'default'};
 1620:                                 }
 1621:                             }
 1622:                         }
 1623:                     } else {
 1624:                         if (exists($currrules->{'default'})) {
 1625:                             $rule_in_effect = $currrules->{'default'};
 1626:                         }
 1627:                     }
 1628:                 } else {
 1629:                     if ($currrules->{'_LC_external'} ne '') {
 1630:                         $rule_in_effect = $currrules->{'_LC_external'};
 1631:                     }
 1632:                 }
 1633:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1634:                                                        $uname,$udom);
 1635:             }
 1636:         }
 1637:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1638:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1639:         unless (defined($cached)) {
 1640:             my %domconfig =
 1641:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1642:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1643:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1644:             } else {
 1645:                 $domneedscache = $serverhomedom;
 1646:             }
 1647:         }
 1648:         if (ref($result) eq 'HASH') {
 1649:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1650:                 &check_balancer_result($result,@hosts);
 1651:             if ($is_balancer) {
 1652:                 if (ref($currrules) eq 'HASH') {
 1653:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1654:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1655:                     }
 1656:                 }
 1657:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1658:                                                        $uname,$udom);
 1659:             }
 1660:         } else {
 1661:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1662:                 $is_balancer = 1;
 1663:                 $offloadto = &this_host_spares($dom_in_use);
 1664:             }
 1665:             unless (defined($cached)) {
 1666:                 $domneedscache = $serverhomedom;
 1667:             }
 1668:         }
 1669:     } else {
 1670:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1671:             $is_balancer = 1;
 1672:             $offloadto = &this_host_spares($dom_in_use);
 1673:         }
 1674:         unless (defined($cached)) {
 1675:             $domneedscache = $serverhomedom;
 1676:         }
 1677:     }
 1678:     if ($domneedscache) {
 1679:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1680:     }
 1681:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1682:         my $lowest_load = 30000;
 1683:         if (ref($offloadto) eq 'HASH') {
 1684:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1685:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1686:                     ($otherserver,$lowest_load) =
 1687:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1688:                 }
 1689:             }
 1690:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1691: 
 1692:             if (!$found_server) {
 1693:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1694:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1695:                         ($otherserver,$lowest_load) =
 1696:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1697:                     }
 1698:                 }
 1699:             }
 1700:         } elsif (ref($offloadto) eq 'ARRAY') {
 1701:             if (@{$offloadto} == 1) {
 1702:                 $otherserver = $offloadto->[0];
 1703:             } elsif (@{$offloadto} > 1) {
 1704:                 foreach my $try_server (@{$offloadto}) {
 1705:                     ($otherserver,$lowest_load) =
 1706:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1707:                 }
 1708:             }
 1709:         }
 1710:         unless ($caller eq 'login') {
 1711:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1712:                 $is_balancer = 0;
 1713:                 if ($uname ne '' && $udom ne '') {
 1714:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1715:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1716:                                  'user.loadbalcheck.time' => time});
 1717:                     }
 1718:                 }
 1719:             }
 1720:         }
 1721:     }
 1722:     if (($is_balancer) && (!$homeintdom)) {
 1723:         undef($setcookie);
 1724:     }
 1725:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1726: }
 1727: 
 1728: sub check_balancer_result {
 1729:     my ($result,@hosts) = @_;
 1730:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1731:     if (ref($result) eq 'HASH') {
 1732:         if ($result->{'lonhost'} ne '') {
 1733:             my $currbalancer = $result->{'lonhost'};
 1734:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1735:                 $is_balancer = 1;
 1736:                 $currtargets = $result->{'targets'};
 1737:                 $currrules = $result->{'rules'};
 1738:             }
 1739:             $dom_balancers = $currbalancer;
 1740:         } else {
 1741:             if (keys(%{$result})) {
 1742:                 foreach my $key (keys(%{$result})) {
 1743:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1744:                         (ref($result->{$key}) eq 'HASH')) {
 1745:                         $is_balancer = 1;
 1746:                         $currrules = $result->{$key}{'rules'};
 1747:                         $currtargets = $result->{$key}{'targets'};
 1748:                         $setcookie = $result->{$key}{'cookie'};
 1749:                         last;
 1750:                     }
 1751:                 }
 1752:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1753:             }
 1754:         }
 1755:     }
 1756:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1757: }
 1758: 
 1759: sub get_loadbalancer_targets {
 1760:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1761:     my $offloadto;
 1762:     if ($rule_in_effect eq 'none') {
 1763:         return [$perlvar{'lonHostID'}];
 1764:     } elsif ($rule_in_effect eq '') {
 1765:         $offloadto = $currtargets;
 1766:     } else {
 1767:         if ($rule_in_effect eq 'homeserver') {
 1768:             my $homeserver = &homeserver($uname,$udom);
 1769:             if ($homeserver ne 'no_host') {
 1770:                 $offloadto = [$homeserver];
 1771:             }
 1772:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1773:             my %domconfig =
 1774:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1775:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1776:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1777:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1778:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1779:                     }
 1780:                 }
 1781:             } else {
 1782:                 my %servers = &internet_dom_servers($udom);
 1783:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1784:                 if (&hostname($remotebalancer) ne '') {
 1785:                     $offloadto = [$remotebalancer];
 1786:                 }
 1787:             }
 1788:         } elsif (&hostname($rule_in_effect) ne '') {
 1789:             $offloadto = [$rule_in_effect];
 1790:         }
 1791:     }
 1792:     return $offloadto;
 1793: }
 1794: 
 1795: sub internet_dom_servers {
 1796:     my ($dom) = @_;
 1797:     my (%uniqservers,%servers);
 1798:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1799:     my @machinedoms = &machine_domains($primaryserver);
 1800:     foreach my $mdom (@machinedoms) {
 1801:         my %currservers = %servers;
 1802:         my %server = &get_servers($mdom);
 1803:         %servers = (%currservers,%server);
 1804:     }
 1805:     my %by_hostname;
 1806:     foreach my $id (keys(%servers)) {
 1807:         push(@{$by_hostname{$servers{$id}}},$id);
 1808:     }
 1809:     foreach my $hostname (sort(keys(%by_hostname))) {
 1810:         if (@{$by_hostname{$hostname}} > 1) {
 1811:             my $match = 0;
 1812:             foreach my $id (@{$by_hostname{$hostname}}) {
 1813:                 if (&host_domain($id) eq $dom) {
 1814:                     $uniqservers{$id} = $hostname;
 1815:                     $match = 1;
 1816:                 }
 1817:             }
 1818:             unless ($match) {
 1819:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1820:             }
 1821:         } else {
 1822:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1823:         }
 1824:     }
 1825:     return %uniqservers;
 1826: }
 1827: 
 1828: sub trusted_domains {
 1829:     my ($cmdtype,$calldom) = @_;
 1830:     my ($trusted,$untrusted);
 1831:     if (&domain($calldom) eq '') {
 1832:         return ($trusted,$untrusted);
 1833:     }
 1834:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1835:         return ($trusted,$untrusted);
 1836:     }
 1837:     my $callprimary = &domain($calldom,'primary');
 1838:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1839:     if ($intcalldom eq '') {
 1840:         return ($trusted,$untrusted);
 1841:     }
 1842: 
 1843:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1844:     unless (defined($cached)) {
 1845:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1846:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1847:         $trustconfig = $domconfig{'trust'};
 1848:     }
 1849:     if (ref($trustconfig)) {
 1850:         my (%possexc,%possinc,@allexc,@allinc); 
 1851:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1852:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1853:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1854:             }
 1855:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1856:                 $possinc{$intcalldom} = 1;
 1857:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1858:             }
 1859:         }
 1860:         if (keys(%possexc)) {
 1861:             if (keys(%possinc)) {
 1862:                 foreach my $key (sort(keys(%possexc))) {
 1863:                     next if ($key eq $intcalldom);
 1864:                     unless ($possinc{$key}) {
 1865:                         push(@allexc,$key);
 1866:                     }
 1867:                 }
 1868:             } else {
 1869:                 @allexc = sort(keys(%possexc));
 1870:             }
 1871:         }
 1872:         if (keys(%possinc)) {
 1873:             $possinc{$intcalldom} = 1;
 1874:             @allinc = sort(keys(%possinc));
 1875:         }
 1876:         if ((@allexc > 0) || (@allinc > 0)) {
 1877:             my %doms_by_intdom;
 1878:             my %allintdoms = &all_host_intdom();
 1879:             my %alldoms = &all_host_domain();
 1880:             foreach my $key (%allintdoms) {
 1881:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1882:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1883:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1884:                     }
 1885:                 } else {
 1886:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1887:                 }
 1888:             }
 1889:             foreach my $exc (@allexc) {
 1890:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1891:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1892:                 }
 1893:             }
 1894:             foreach my $inc (@allinc) {
 1895:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1896:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1897:                 }
 1898:             }
 1899:         }
 1900:     }
 1901:     return ($trusted,$untrusted);
 1902: }
 1903: 
 1904: sub will_trust {
 1905:     my ($cmdtype,$domain,$possdom) = @_;
 1906:     return 1 if ($domain eq $possdom);
 1907:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1908:     my $willtrust; 
 1909:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1910:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1911:             $willtrust = 1;
 1912:         }
 1913:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1914:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1915:             $willtrust = 1;
 1916:         }
 1917:     } else {
 1918:         $willtrust = 1;
 1919:     }
 1920:     return $willtrust;
 1921: }
 1922: 
 1923: # ---------------------- Find the homebase for a user from domain's lib servers
 1924: 
 1925: my %homecache;
 1926: sub homeserver {
 1927:     my ($uname,$udom,$ignoreBadCache)=@_;
 1928:     my $index="$uname:$udom";
 1929: 
 1930:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1931: 
 1932:     my %servers = &get_servers($udom,'library');
 1933:     foreach my $tryserver (keys(%servers)) {
 1934:         next if ($ignoreBadCache ne 'true' && 
 1935: 		 exists($badServerCache{$tryserver}));
 1936: 
 1937: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1938: 	if ($answer eq 'found') {
 1939: 	    delete($badServerCache{$tryserver}); 
 1940: 	    return $homecache{$index}=$tryserver;
 1941: 	} elsif ($answer eq 'no_host') {
 1942: 	    $badServerCache{$tryserver}=1;
 1943: 	}
 1944:     }    
 1945:     return 'no_host';
 1946: }
 1947: 
 1948: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1949: 
 1950: sub idget {
 1951:     my ($udom,$idsref,$namespace)=@_;
 1952:     my %returnhash=();
 1953:     my @ids=(); 
 1954:     if (ref($idsref) eq 'ARRAY') {
 1955:         @ids = @{$idsref};
 1956:     } else {
 1957:         return %returnhash; 
 1958:     }
 1959:     if ($namespace eq '') {
 1960:         $namespace = 'ids';
 1961:     }
 1962:     
 1963:     my %servers = &get_servers($udom,'library');
 1964:     foreach my $tryserver (keys(%servers)) {
 1965: 	my $idlist=join('&', map { &escape($_); } @ids);
 1966: 	if ($namespace eq 'ids') {
 1967: 	    $idlist=~tr/A-Z/a-z/;
 1968: 	}
 1969: 	my $reply;
 1970: 	if ($namespace eq 'ids') {
 1971: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1972: 	} else {
 1973: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1974: 	}
 1975: 	my @answer=();
 1976: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1977: 	    @answer=split(/\&/,$reply);
 1978: 	}                    ;
 1979: 	my $i;
 1980: 	for ($i=0;$i<=$#ids;$i++) {
 1981: 	    if ($answer[$i]) {
 1982: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1983: 	    }
 1984: 	}
 1985:     }
 1986:     return %returnhash;
 1987: }
 1988: 
 1989: # ------------------------------------- Find the IDs behind a list of usernames
 1990: 
 1991: sub idrget {
 1992:     my ($udom,@unames)=@_;
 1993:     my %returnhash=();
 1994:     foreach my $uname (@unames) {
 1995:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1996:     }
 1997:     return %returnhash;
 1998: }
 1999: 
 2000: # Store away a list of names and associated student/employee IDs or clicker IDs
 2001: 
 2002: sub idput {
 2003:     my ($udom,$idsref,$uhom,$namespace)=@_;
 2004:     my %servers=();
 2005:     my %ids=();
 2006:     my %byid = ();
 2007:     if (ref($idsref) eq 'HASH') {
 2008:         %ids=%{$idsref};
 2009:     }
 2010:     if ($namespace eq '') {
 2011:         $namespace = 'ids'; 
 2012:     }
 2013:     foreach my $uname (keys(%ids)) {
 2014: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2015:         if ($uhom eq '') {
 2016:             $uhom=&homeserver($uname,$udom);
 2017:         }
 2018:         if ($uhom ne 'no_host') {
 2019:             my $esc_unam=&escape($uname);
 2020:             if ($namespace eq 'ids') {
 2021:                 my $id=&escape($ids{$uname});
 2022:                 $id=~tr/A-Z/a-z/;
 2023:                 my $esc_unam=&escape($uname);
 2024:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2025:             } else {
 2026:                 my @currids = split(/,/,$ids{$uname});
 2027:                 foreach my $id (@currids) {
 2028:                     $byid{$uhom}{$id} .= $uname.',';
 2029:                 }
 2030:             }
 2031:         }
 2032:     }
 2033:     if ($namespace eq 'clickers') {
 2034:         foreach my $server (keys(%byid)) {
 2035:             if (ref($byid{$server}) eq 'HASH') {
 2036:                 foreach my $id (keys(%{$byid{$server}})) {
 2037:                     $byid{$server} =~ s/,$//;
 2038:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2039:                 }
 2040:             }
 2041:         }
 2042:     }
 2043:     foreach my $server (keys(%servers)) {
 2044:         $servers{$server} =~ s/\&$//;
 2045:         if ($namespace eq 'ids') {     
 2046:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2047:         } else {
 2048:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2049:         }
 2050:     }
 2051: }
 2052: 
 2053: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2054: 
 2055: sub iddel {
 2056:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2057:     my %result=();
 2058:     my %ids=();
 2059:     my %byid = ();
 2060:     if (ref($idshashref) eq 'HASH') {
 2061:         %ids=%{$idshashref};
 2062:     } else {
 2063:         return %result;
 2064:     }
 2065:     if ($namespace eq '') {
 2066:         $namespace = 'ids';
 2067:     }
 2068:     my %servers=();
 2069:     while (my ($id,$unamestr) = each(%ids)) {
 2070:         if ($namespace eq 'ids') {
 2071:             my $uhom = $uhome;
 2072:             if ($uhom eq '') { 
 2073:                 $uhom=&homeserver($unamestr,$udom);
 2074:             }
 2075:             if ($uhom ne 'no_host') {
 2076:                 $servers{$uhom}.='&'.&escape($id);
 2077:             }
 2078:          } else {
 2079:             my @curritems = split(/,/,$ids{$id});
 2080:             foreach my $uname (@curritems) {
 2081:                 my $uhom = $uhome;
 2082:                 if ($uhom eq '') {
 2083:                     $uhom=&homeserver($uname,$udom);
 2084:                 }
 2085:                 if ($uhom ne 'no_host') { 
 2086:                     $byid{$uhom}{$id} .= $uname.',';
 2087:                 }
 2088:             }
 2089:         }
 2090:     }
 2091:     if ($namespace eq 'clickers') {
 2092:         foreach my $server (keys(%byid)) {
 2093:             if (ref($byid{$server}) eq 'HASH') {
 2094:                 foreach my $id (keys(%{$byid{$server}})) {
 2095:                     $byid{$server}{$id} =~ s/,$//;
 2096:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2097:                 }
 2098:             }
 2099:         }
 2100:     }
 2101:     foreach my $server (keys(%servers)) {
 2102:         $servers{$server} =~ s/\&$//;
 2103:         if ($namespace eq 'ids') {
 2104:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2105:         } elsif ($namespace eq 'clickers') {
 2106:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2107:         }
 2108:     }
 2109:     return %result;
 2110: }
 2111: 
 2112: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2113: 
 2114: sub updateclickers {
 2115:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2116:     my %clickers;
 2117:     if (ref($idshashref) eq 'HASH') {
 2118:         %clickers=%{$idshashref};
 2119:     } else {
 2120:         return;
 2121:     }
 2122:     my $items='';
 2123:     foreach my $item (keys(%clickers)) {
 2124:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2125:     }
 2126:     $items=~s/\&$//;
 2127:     my $request = "updateclickers:$udom:$action:$items";
 2128:     if ($critical) {
 2129:         return &critical($request,$uhome);
 2130:     } else {
 2131:         return &reply($request,$uhome);
 2132:     }
 2133: }
 2134: 
 2135: # ------------------------------dump from db file owned by domainconfig user
 2136: sub dump_dom {
 2137:     my ($namespace, $udom, $regexp) = @_;
 2138: 
 2139:     $udom ||= $env{'user.domain'};
 2140: 
 2141:     return () unless $udom;
 2142: 
 2143:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2144: }
 2145: 
 2146: # ------------------------------------------ get items from domain db files   
 2147: 
 2148: sub get_dom {
 2149:     my ($namespace,$storearr,$udom,$uhome)=@_;
 2150:     return if ($udom eq 'public');
 2151:     my $items='';
 2152:     foreach my $item (@$storearr) {
 2153:         $items.=&escape($item).'&';
 2154:     }
 2155:     $items=~s/\&$//;
 2156:     if (!$udom) {
 2157:         $udom=$env{'user.domain'};
 2158:         return if ($udom eq 'public');
 2159:         if (defined(&domain($udom,'primary'))) {
 2160:             $uhome=&domain($udom,'primary');
 2161:         } else {
 2162:             undef($uhome);
 2163:         }
 2164:     } else {
 2165:         if (!$uhome) {
 2166:             if (defined(&domain($udom,'primary'))) {
 2167:                 $uhome=&domain($udom,'primary');
 2168:             }
 2169:         }
 2170:     }
 2171:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2172:         my $rep;
 2173:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2174:             # domain information is hosted on this machine
 2175:             my $cmd = 'getdom';
 2176:             if ($namespace =~ /^enc/) {
 2177:                 $cmd = 'egetdom';
 2178:             }
 2179:             $rep = &LONCAPA::Lond::get_dom("$cmd:$udom:$namespace:$items");
 2180:         } else {
 2181:             if ($namespace =~ /^enc/) {
 2182:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2183:             } else {
 2184:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2185:             }
 2186:         }
 2187:         my %returnhash;
 2188:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2189:             return %returnhash;
 2190:         }
 2191:         my @pairs=split(/\&/,$rep);
 2192:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2193:             return @pairs;
 2194:         }
 2195:         my $i=0;
 2196:         foreach my $item (@$storearr) {
 2197:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2198:             $i++;
 2199:         }
 2200:         return %returnhash;
 2201:     } else {
 2202:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2203:     }
 2204: }
 2205: 
 2206: # -------------------------------------------- put items in domain db files 
 2207: 
 2208: sub put_dom {
 2209:     my ($namespace,$storehash,$udom,$uhome)=@_;
 2210:     if (!$udom) {
 2211:         $udom=$env{'user.domain'};
 2212:         if (defined(&domain($udom,'primary'))) {
 2213:             $uhome=&domain($udom,'primary');
 2214:         } else {
 2215:             undef($uhome);
 2216:         }
 2217:     } else {
 2218:         if (!$uhome) {
 2219:             if (defined(&domain($udom,'primary'))) {
 2220:                 $uhome=&domain($udom,'primary');
 2221:             }
 2222:         }
 2223:     } 
 2224:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2225:         my $items='';
 2226:         foreach my $item (keys(%$storehash)) {
 2227:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2228:         }
 2229:         $items=~s/\&$//;
 2230:         if ($namespace =~ /^enc/) {
 2231:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2232:         } else {
 2233:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2234:         }
 2235:     } else {
 2236:         &logthis("put_dom failed - no homeserver and/or domain");
 2237:     }
 2238: }
 2239: 
 2240: # --------------------- newput for items in db file owned by domainconfig user
 2241: sub newput_dom {
 2242:     my ($namespace,$storehash,$udom) = @_;
 2243:     my $result;
 2244:     if (!$udom) {
 2245:         $udom=$env{'user.domain'};
 2246:     }
 2247:     if ($udom) {
 2248:         my $uname = &get_domainconfiguser($udom);
 2249:         $result = &newput($namespace,$storehash,$udom,$uname);
 2250:     }
 2251:     return $result;
 2252: }
 2253: 
 2254: # --------------------- delete for items in db file owned by domainconfig user
 2255: sub del_dom {
 2256:     my ($namespace,$storearr,$udom)=@_;
 2257:     if (ref($storearr) eq 'ARRAY') {
 2258:         if (!$udom) {
 2259:             $udom=$env{'user.domain'};
 2260:         }
 2261:         if ($udom) {
 2262:             my $uname = &get_domainconfiguser($udom); 
 2263:             return &del($namespace,$storearr,$udom,$uname);
 2264:         }
 2265:     }
 2266: }
 2267: 
 2268: # ----------------------------------construct domainconfig user for a domain 
 2269: sub get_domainconfiguser {
 2270:     my ($udom) = @_;
 2271:     return $udom.'-domainconfig';
 2272: }
 2273: 
 2274: sub retrieve_inst_usertypes {
 2275:     my ($udom) = @_;
 2276:     my (%returnhash,@order);
 2277:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2278:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2279:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2280:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2281:     } else {
 2282:         if (defined(&domain($udom,'primary'))) {
 2283:             my $uhome=&domain($udom,'primary');
 2284:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2285:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2286:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2287:                 return (\%returnhash,\@order);
 2288:             }
 2289:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2290:             my @pairs=split(/\&/,$hashitems);
 2291:             foreach my $item (@pairs) {
 2292:                 my ($key,$value)=split(/=/,$item,2);
 2293:                 $key = &unescape($key);
 2294:                 next if ($key =~ /^error: 2 /);
 2295:                 $returnhash{$key}=&thaw_unescape($value);
 2296:             }
 2297:             my @esc_order = split(/\&/,$orderitems);
 2298:             foreach my $item (@esc_order) {
 2299:                 push(@order,&unescape($item));
 2300:             }
 2301:         } else {
 2302:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2303:         }
 2304:         return (\%returnhash,\@order);
 2305:     }
 2306: }
 2307: 
 2308: sub is_domainimage {
 2309:     my ($url) = @_;
 2310:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2311:         if (&domain($1) ne '') {
 2312:             return '1';
 2313:         }
 2314:     }
 2315:     return;
 2316: }
 2317: 
 2318: sub inst_directory_query {
 2319:     my ($srch) = @_;
 2320:     my $udom = $srch->{'srchdomain'};
 2321:     my %results;
 2322:     my $homeserver = &domain($udom,'primary');
 2323:     my $outcome;
 2324:     if ($homeserver ne '') {
 2325:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2326:             if ($srch->{'srchby'} eq 'email') {
 2327:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2328:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2329:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2330:                     (($major == 2) && ($minor < 12))) {
 2331:                     return;
 2332:                 }
 2333:             }
 2334:         }
 2335: 	my $queryid=&reply("querysend:instdirsearch:".
 2336: 			   &escape($srch->{'srchby'}).':'.
 2337: 			   &escape($srch->{'srchterm'}).':'.
 2338: 			   &escape($srch->{'srchtype'}),$homeserver);
 2339: 	my $host=&hostname($homeserver);
 2340: 	if ($queryid !~/^\Q$host\E\_/) {
 2341: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2342: 	    return;
 2343: 	}
 2344: 	my $response = &get_query_reply($queryid);
 2345: 	my $maxtries = 5;
 2346: 	my $tries = 1;
 2347: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2348: 	    $response = &get_query_reply($queryid);
 2349: 	    $tries ++;
 2350: 	}
 2351: 
 2352:         if (!&error($response) && $response ne 'refused') {
 2353:             if ($response eq 'unavailable') {
 2354:                 $outcome = $response;
 2355:             } else {
 2356:                 $outcome = 'ok';
 2357:                 my @matches = split(/\n/,$response);
 2358:                 foreach my $match (@matches) {
 2359:                     my ($key,$value) = split(/=/,$match);
 2360:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2361:                 }
 2362:             }
 2363:         }
 2364:     }
 2365:     return ($outcome,%results);
 2366: }
 2367: 
 2368: sub usersearch {
 2369:     my ($srch) = @_;
 2370:     my $dom = $srch->{'srchdomain'};
 2371:     my %results;
 2372:     my %libserv = &all_library();
 2373:     my $query = 'usersearch';
 2374:     foreach my $tryserver (keys(%libserv)) {
 2375:         if (&host_domain($tryserver) eq $dom) {
 2376:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2377:                 if ($srch->{'srchby'} eq 'email') {
 2378:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2379:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2380:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2381:                              (($major == 2) && ($minor < 12)));
 2382:                 }
 2383:             }
 2384:             my $host=&hostname($tryserver);
 2385:             my $queryid=
 2386:                 &reply("querysend:".&escape($query).':'.
 2387:                        &escape($srch->{'srchby'}).':'.
 2388:                        &escape($srch->{'srchtype'}).':'.
 2389:                        &escape($srch->{'srchterm'}),$tryserver);
 2390:             if ($queryid !~/^\Q$host\E\_/) {
 2391:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2392:                 next;
 2393:             }
 2394:             my $reply = &get_query_reply($queryid);
 2395:             my $maxtries = 1;
 2396:             my $tries = 1;
 2397:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2398:                 $reply = &get_query_reply($queryid);
 2399:                 $tries ++;
 2400:             }
 2401:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2402:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2403:             } else {
 2404:                 my @matches;
 2405:                 if ($reply =~ /\n/) {
 2406:                     @matches = split(/\n/,$reply);
 2407:                 } else {
 2408:                     @matches = split(/\&/,$reply);
 2409:                 }
 2410:                 foreach my $match (@matches) {
 2411:                     my ($uname,$udom,%userhash);
 2412:                     foreach my $entry (split(/:/,$match)) {
 2413:                         my ($key,$value) =
 2414:                             map {&unescape($_);} split(/=/,$entry);
 2415:                         $userhash{$key} = $value;
 2416:                         if ($key eq 'username') {
 2417:                             $uname = $value;
 2418:                         } elsif ($key eq 'domain') {
 2419:                             $udom = $value;
 2420:                         }
 2421:                     }
 2422:                     $results{$uname.':'.$udom} = \%userhash;
 2423:                 }
 2424:             }
 2425:         }
 2426:     }
 2427:     return %results;
 2428: }
 2429: 
 2430: sub get_instuser {
 2431:     my ($udom,$uname,$id) = @_;
 2432:     my $homeserver = &domain($udom,'primary');
 2433:     my ($outcome,%results);
 2434:     if ($homeserver ne '') {
 2435:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2436:                            &escape($id).':'.&escape($udom),$homeserver);
 2437:         my $host=&hostname($homeserver);
 2438:         if ($queryid !~/^\Q$host\E\_/) {
 2439:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2440:             return;
 2441:         }
 2442:         my $response = &get_query_reply($queryid);
 2443:         my $maxtries = 5;
 2444:         my $tries = 1;
 2445:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2446:             $response = &get_query_reply($queryid);
 2447:             $tries ++;
 2448:         }
 2449:         if (!&error($response) && $response ne 'refused') {
 2450:             if ($response eq 'unavailable') {
 2451:                 $outcome = $response;
 2452:             } else {
 2453:                 $outcome = 'ok';
 2454:                 my @matches = split(/\n/,$response);
 2455:                 foreach my $match (@matches) {
 2456:                     my ($key,$value) = split(/=/,$match);
 2457:                     $results{&unescape($key)} = &thaw_unescape($value);
 2458:                 }
 2459:             }
 2460:         }
 2461:     }
 2462:     my %userinfo;
 2463:     if (ref($results{$uname}) eq 'HASH') {
 2464:         %userinfo = %{$results{$uname}};
 2465:     } 
 2466:     return ($outcome,%userinfo);
 2467: }
 2468: 
 2469: sub get_multiple_instusers {
 2470:     my ($udom,$users,$caller) = @_;
 2471:     my ($outcome,$results);
 2472:     if (ref($users) eq 'HASH') {
 2473:         my $count = keys(%{$users}); 
 2474:         my $requested = &freeze_escape($users);
 2475:         my $homeserver = &domain($udom,'primary');
 2476:         if ($homeserver ne '') {
 2477:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2478:             my $host=&hostname($homeserver);
 2479:             if ($queryid !~/^\Q$host\E\_/) {
 2480:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2481:                          ' for host: '.$homeserver.'in domain '.$udom);
 2482:                 return ($outcome,$results);
 2483:             }
 2484:             my $response = &get_query_reply($queryid);
 2485:             my $maxtries = 5;
 2486:             if ($count > 100) {
 2487:                 $maxtries = 1+int($count/20);
 2488:             }
 2489:             my $tries = 1;
 2490:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2491:                 $response = &get_query_reply($queryid);
 2492:                 $tries ++;
 2493:             }
 2494:             if ($response eq '') {
 2495:                 $results = {};
 2496:                 foreach my $key (keys(%{$users})) {
 2497:                     my ($uname,$id);
 2498:                     if ($caller eq 'id') {
 2499:                         $id = $key;
 2500:                     } else {
 2501:                         $uname = $key;
 2502:                     }
 2503:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2504:                     $outcome = $resp;
 2505:                     if ($resp eq 'ok') {
 2506:                         %{$results} = (%{$results}, %info);
 2507:                     } else {
 2508:                         last;
 2509:                     }
 2510:                 }
 2511:             } elsif(!&error($response) && ($response ne 'refused')) {
 2512:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2513:                     $outcome = $response;
 2514:                 } else {
 2515:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2516:                     if ($outcome eq 'ok') {
 2517:                         $results = &thaw_unescape($userdata); 
 2518:                     }
 2519:                 }
 2520:             }
 2521:         }
 2522:     }
 2523:     return ($outcome,$results);
 2524: }
 2525: 
 2526: sub inst_rulecheck {
 2527:     my ($udom,$uname,$id,$item,$rules) = @_;
 2528:     my %returnhash;
 2529:     if ($udom ne '') {
 2530:         if (ref($rules) eq 'ARRAY') {
 2531:             @{$rules} = map {&escape($_);} (@{$rules});
 2532:             my $rulestr = join(':',@{$rules});
 2533:             my $homeserver=&domain($udom,'primary');
 2534:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2535:                 my $response;
 2536:                 if ($item eq 'username') {                
 2537:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2538:                                               ':'.&escape($uname).':'.$rulestr,
 2539:                                               $homeserver));
 2540:                 } elsif ($item eq 'id') {
 2541:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2542:                                               ':'.&escape($id).':'.$rulestr,
 2543:                                               $homeserver));
 2544:                 } elsif ($item eq 'selfcreate') {
 2545:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2546:                                                &escape($udom).':'.&escape($uname).
 2547:                                               ':'.$rulestr,$homeserver));
 2548:                 }
 2549:                 if ($response ne 'refused') {
 2550:                     my @pairs=split(/\&/,$response);
 2551:                     foreach my $item (@pairs) {
 2552:                         my ($key,$value)=split(/=/,$item,2);
 2553:                         $key = &unescape($key);
 2554:                         next if ($key =~ /^error: 2 /);
 2555:                         $returnhash{$key}=&thaw_unescape($value);
 2556:                     }
 2557:                 }
 2558:             }
 2559:         }
 2560:     }
 2561:     return %returnhash;
 2562: }
 2563: 
 2564: sub inst_userrules {
 2565:     my ($udom,$check) = @_;
 2566:     my (%ruleshash,@ruleorder);
 2567:     if ($udom ne '') {
 2568:         my $homeserver=&domain($udom,'primary');
 2569:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2570:             my $response;
 2571:             if ($check eq 'id') {
 2572:                 $response=&reply('instidrules:'.&escape($udom),
 2573:                                  $homeserver);
 2574:             } elsif ($check eq 'email') {
 2575:                 $response=&reply('instemailrules:'.&escape($udom),
 2576:                                  $homeserver);
 2577:             } else {
 2578:                 $response=&reply('instuserrules:'.&escape($udom),
 2579:                                  $homeserver);
 2580:             }
 2581:             if (($response ne 'refused') && ($response ne 'error') && 
 2582:                 ($response ne 'unknown_cmd') && 
 2583:                 ($response ne 'no_such_host')) {
 2584:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2585:                 my @pairs=split(/\&/,$hashitems);
 2586:                 foreach my $item (@pairs) {
 2587:                     my ($key,$value)=split(/=/,$item,2);
 2588:                     $key = &unescape($key);
 2589:                     next if ($key =~ /^error: 2 /);
 2590:                     $ruleshash{$key}=&thaw_unescape($value);
 2591:                 }
 2592:                 my @esc_order = split(/\&/,$orderitems);
 2593:                 foreach my $item (@esc_order) {
 2594:                     push(@ruleorder,&unescape($item));
 2595:                 }
 2596:             }
 2597:         }
 2598:     }
 2599:     return (\%ruleshash,\@ruleorder);
 2600: }
 2601: 
 2602: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2603: 
 2604: sub get_domain_defaults {
 2605:     my ($domain,$ignore_cache) = @_;
 2606:     return if (($domain eq '') || ($domain eq 'public'));
 2607:     my $cachetime = 60*60*24;
 2608:     unless ($ignore_cache) {
 2609:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2610:         if (defined($cached)) {
 2611:             if (ref($result) eq 'HASH') {
 2612:                 return %{$result};
 2613:             }
 2614:         }
 2615:     }
 2616:     my %domdefaults;
 2617:     my %domconfig =
 2618:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2619:                                   'requestcourses','inststatus',
 2620:                                   'coursedefaults','usersessions',
 2621:                                   'requestauthor','selfenrollment',
 2622:                                   'coursecategories','ssl','autoenroll',
 2623:                                   'trust','helpsettings','wafproxy'],$domain);
 2624:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2625:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2626:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2627:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2628:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2629:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2630:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2631:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2632:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2633:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2634:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2635:     } else {
 2636:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2637:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2638:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2639:     }
 2640:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2641:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2642:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2643:         } else {
 2644:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2645:         }
 2646:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2647:         foreach my $item (@usertools) {
 2648:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2649:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2650:             }
 2651:         }
 2652:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2653:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2654:         }
 2655:     }
 2656:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2657:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2658:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2659:         }
 2660:     }
 2661:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2662:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2663:     }
 2664:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2665:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2666:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2667:         }
 2668:     }
 2669:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2670:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2671:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2672:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2673:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2674:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2675:         }
 2676:         foreach my $type (@coursetypes) {
 2677:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2678:                 unless ($type eq 'community') {
 2679:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2680:                 }
 2681:             }
 2682:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2683:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2684:             }
 2685:             if ($domdefaults{'postsubmit'} eq 'on') {
 2686:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2687:                     $domdefaults{$type.'postsubtimeout'} = 
 2688:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2689:                 }
 2690:             }
 2691:         }
 2692:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2693:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2694:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2695:                 if (@clonecodes) {
 2696:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2697:                 }
 2698:             }
 2699:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2700:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2701:         }
 2702:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2703:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2704:         } 
 2705:     }
 2706:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2707:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2708:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2709:         }
 2710:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2711:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2712:         }
 2713:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2714:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2715:         }
 2716:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2717:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2718:         }
 2719:     }
 2720:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2721:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2722:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2723:                             'approval','limit');
 2724:             foreach my $type (@coursetypes) {
 2725:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2726:                     my @mgrdc = ();
 2727:                     foreach my $item (@settings) {
 2728:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2729:                             push(@mgrdc,$item);
 2730:                         }
 2731:                     }
 2732:                     if (@mgrdc) {
 2733:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2734:                     }
 2735:                 }
 2736:             }
 2737:         }
 2738:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2739:             foreach my $type (@coursetypes) {
 2740:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2741:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2742:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2743:                     }
 2744:                 }
 2745:             }
 2746:         }
 2747:     }
 2748:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2749:         $domdefaults{'catauth'} = 'std';
 2750:         $domdefaults{'catunauth'} = 'std';
 2751:         if ($domconfig{'coursecategories'}{'auth'}) {
 2752:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2753:         }
 2754:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2755:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2756:         }
 2757:     }
 2758:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2759:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2760:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2761:         }
 2762:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2763:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2764:         }
 2765:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2766:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2767:         }
 2768:     }
 2769:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2770:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2771:         foreach my $prefix (@prefixes) {
 2772:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2773:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2774:             }
 2775:         }
 2776:     }
 2777:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2778:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2779:     }
 2780:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2781:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2782:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2783:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2784:         }
 2785:     }
 2786:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2787:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
 2788:             if ($domconfig{'wafproxy'}{$item}) {
 2789:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2790:             }
 2791:         }
 2792:     } 
 2793:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2794:     return %domdefaults;
 2795: }
 2796: 
 2797: sub get_dom_cats {
 2798:     my ($dom) = @_;
 2799:     return unless (&domain($dom));
 2800:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2801:     unless (defined($cached)) {
 2802:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2803:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2804:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2805:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2806:             } else {
 2807:                 $cats = {};
 2808:             }
 2809:         } else {
 2810:             $cats = {};
 2811:         }
 2812:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
 2813:     }
 2814:     return $cats;
 2815: }
 2816: 
 2817: sub get_dom_instcats {
 2818:     my ($dom) = @_;
 2819:     return unless (&domain($dom));
 2820:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2821:     unless (defined($cached)) {
 2822:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 2823:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 2824:         if ($totcodes > 0) {
 2825:             my $caller = 'global';
 2826:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 2827:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 2828:                 $instcats = {
 2829:                                 codes => \%codes,
 2830:                                 codetitles => \@codetitles,
 2831:                                 cat_titles => \%cat_titles,
 2832:                                 cat_order => \%cat_order,
 2833:                             };
 2834:                 &do_cache_new('instcats',$dom,$instcats,3600);
 2835:             }
 2836:         }
 2837:     }
 2838:     return $instcats;
 2839: }
 2840: 
 2841: sub retrieve_instcodes {
 2842:     my ($coursecodes,$dom) = @_;
 2843:     my $totcodes;
 2844:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 2845:     foreach my $course (keys(%courses)) {
 2846:         if (ref($courses{$course}) eq 'HASH') {
 2847:             if ($courses{$course}{'inst_code'} ne '') {
 2848:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 2849:                 $totcodes ++;
 2850:             }
 2851:         }
 2852:     }
 2853:     return $totcodes;
 2854: }
 2855: 
 2856: sub course_portal_url {
 2857:     my ($cnum,$cdom,$r) = @_;
 2858:     my $chome = &homeserver($cnum,$cdom);
 2859:     my $hostname = &hostname($chome);
 2860:     my $protocol = $protocol{$chome};
 2861:     $protocol = 'http' if ($protocol ne 'https');
 2862:     my %domdefaults = &get_domain_defaults($cdom);
 2863:     my $firsturl;
 2864:     if ($domdefaults{'portal_def'}) {
 2865:         $firsturl = $domdefaults{'portal_def'};
 2866:     } else {
 2867:         my $alias = &Apache::lonnet::use_proxy_alias($r,$chome);
 2868:         $hostname = $alias if ($alias ne '');
 2869:         $firsturl = $protocol.'://'.$hostname;
 2870:     }
 2871:     return $firsturl;
 2872: }
 2873: 
 2874: # --------------------------------------------- Get domain config for passwords
 2875: 
 2876: sub get_passwdconf {
 2877:     my ($dom) = @_;
 2878:     my (%passwdconf,$gotconf,$lookup);
 2879:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2880:     if (defined($cached)) {
 2881:         if (ref($result) eq 'HASH') {
 2882:             %passwdconf = %{$result};
 2883:             $gotconf = 1;
 2884:         }
 2885:     }
 2886:     unless ($gotconf) {
 2887:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2888:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2889:             %passwdconf = %{$domconfig{'passwords'}};
 2890:         }
 2891:         my $cachetime = 24*60*60;
 2892:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2893:     }
 2894:     return %passwdconf;
 2895: }
 2896: 
 2897: # --------------------------------------------------- Assign a key to a student
 2898: 
 2899: sub assign_access_key {
 2900: #
 2901: # a valid key looks like uname:udom#comments
 2902: # comments are being appended
 2903: #
 2904:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2905:     $kdom=
 2906:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2907:     $knum=
 2908:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2909:     $cdom=
 2910:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2911:     $cnum=
 2912:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2913:     $udom=$env{'user.name'} unless (defined($udom));
 2914:     $uname=$env{'user.domain'} unless (defined($uname));
 2915:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2916:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2917:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2918:                                                   # assigned to this person
 2919:                                                   # - this should not happen,
 2920:                                                   # unless something went wrong
 2921:                                                   # the first time around
 2922: # ready to assign
 2923:         $logentry=$1.'; '.$logentry;
 2924:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2925:                                                  $kdom,$knum) eq 'ok') {
 2926: # key now belongs to user
 2927: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2928:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2929:                 &appenv({'environment.'.$envkey => $ckey});
 2930:                 return 'ok';
 2931:             } else {
 2932:                 return 
 2933:   'error: Count not permanently assign key, will need to be re-entered later.';
 2934: 	    }
 2935:         } else {
 2936:             return 'error: Could not assign key, try again later.';
 2937:         }
 2938:     } elsif (!$existing{$ckey}) {
 2939: # the key does not exist
 2940: 	return 'error: The key does not exist';
 2941:     } else {
 2942: # the key is somebody else's
 2943: 	return 'error: The key is already in use';
 2944:     }
 2945: }
 2946: 
 2947: # ------------------------------------------ put an additional comment on a key
 2948: 
 2949: sub comment_access_key {
 2950: #
 2951: # a valid key looks like uname:udom#comments
 2952: # comments are being appended
 2953: #
 2954:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2955:     $cdom=
 2956:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2957:     $cnum=
 2958:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2959:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2960:     if ($existing{$ckey}) {
 2961:         $existing{$ckey}.='; '.$logentry;
 2962: # ready to assign
 2963:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2964:                                                  $cdom,$cnum) eq 'ok') {
 2965: 	    return 'ok';
 2966:         } else {
 2967: 	    return 'error: Count not store comment.';
 2968:         }
 2969:     } else {
 2970: # the key does not exist
 2971: 	return 'error: The key does not exist';
 2972:     }
 2973: }
 2974: 
 2975: # ------------------------------------------------------ Generate a set of keys
 2976: 
 2977: sub generate_access_keys {
 2978:     my ($number,$cdom,$cnum,$logentry)=@_;
 2979:     $cdom=
 2980:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2981:     $cnum=
 2982:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2983:     unless (&allowed('mky',$cdom)) { return 0; }
 2984:     unless (($cdom) && ($cnum)) { return 0; }
 2985:     if ($number>10000) { return 0; }
 2986:     sleep(2); # make sure don't get same seed twice
 2987:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2988:     my $total=0;
 2989:     for (my $i=1;$i<=$number;$i++) {
 2990:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2991:                   sprintf("%lx",int(100000*rand)).'-'.
 2992:                   sprintf("%lx",int(100000*rand));
 2993:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2994:        $newkey=~s/0/h/g; # and also 0 and O
 2995:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2996:        if ($existing{$newkey}) {
 2997:            $i--;
 2998:        } else {
 2999: 	  if (&put('accesskeys',
 3000:               { $newkey => '# generated '.localtime().
 3001:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 3002:                            '; '.$logentry },
 3003: 		   $cdom,$cnum) eq 'ok') {
 3004:               $total++;
 3005: 	  }
 3006:        }
 3007:     }
 3008:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3009:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3010:     return $total;
 3011: }
 3012: 
 3013: # ------------------------------------------------------- Validate an accesskey
 3014: 
 3015: sub validate_access_key {
 3016:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3017:     $cdom=
 3018:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3019:     $cnum=
 3020:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3021:     $udom=$env{'user.domain'} unless (defined($udom));
 3022:     $uname=$env{'user.name'} unless (defined($uname));
 3023:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3024:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3025: }
 3026: 
 3027: # ------------------------------------- Find the section of student in a course
 3028: sub devalidate_getsection_cache {
 3029:     my ($udom,$unam,$courseid)=@_;
 3030:     my $hashid="$udom:$unam:$courseid";
 3031:     &devalidate_cache_new('getsection',$hashid);
 3032: }
 3033: 
 3034: sub courseid_to_courseurl {
 3035:     my ($courseid) = @_;
 3036:     #already url style courseid
 3037:     return $courseid if ($courseid =~ m{^/});
 3038: 
 3039:     if (exists($env{'course.'.$courseid.'.num'})) {
 3040: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3041: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3042: 	return "/$cdom/$cnum";
 3043:     }
 3044: 
 3045:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 3046:     if (exists($courseinfo{'num'})) {
 3047: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3048:     }
 3049: 
 3050:     return undef;
 3051: }
 3052: 
 3053: sub getsection {
 3054:     my ($udom,$unam,$courseid)=@_;
 3055:     my $cachetime=1800;
 3056: 
 3057:     my $hashid="$udom:$unam:$courseid";
 3058:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3059:     if (defined($cached)) { return $result; }
 3060: 
 3061:     my %Pending; 
 3062:     my %Expired;
 3063:     #
 3064:     # Each role can either have not started yet (pending), be active, 
 3065:     #    or have expired.
 3066:     #
 3067:     # If there is an active role, we are done.
 3068:     #
 3069:     # If there is more than one role which has not started yet, 
 3070:     #     choose the one which will start sooner
 3071:     # If there is one role which has not started yet, return it.
 3072:     #
 3073:     # If there is more than one expired role, choose the one which ended last.
 3074:     # If there is a role which has expired, return it.
 3075:     #
 3076:     $courseid = &courseid_to_courseurl($courseid);
 3077:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3078:     foreach my $key (keys(%roleshash)) {
 3079:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3080:         my $section=$1;
 3081:         if ($key eq $courseid.'_st') { $section=''; }
 3082:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3083:         my $now=time;
 3084:         if (defined($end) && $end && ($now > $end)) {
 3085:             $Expired{$end}=$section;
 3086:             next;
 3087:         }
 3088:         if (defined($start) && $start && ($now < $start)) {
 3089:             $Pending{$start}=$section;
 3090:             next;
 3091:         }
 3092:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3093:     }
 3094:     #
 3095:     # Presumedly there will be few matching roles from the above
 3096:     # loop and the sorting time will be negligible.
 3097:     if (scalar(keys(%Pending))) {
 3098:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3099:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3100:     } 
 3101:     if (scalar(keys(%Expired))) {
 3102:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3103:         my $time = pop(@sorted);
 3104:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3105:     }
 3106:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3107: }
 3108: 
 3109: sub save_cache {
 3110:     &purge_remembered();
 3111:     #&Apache::loncommon::validate_page();
 3112:     undef(%env);
 3113:     undef($env_loaded);
 3114: }
 3115: 
 3116: my $to_remember=-1;
 3117: my %remembered;
 3118: my %accessed;
 3119: my $kicks=0;
 3120: my $hits=0;
 3121: sub make_key {
 3122:     my ($name,$id) = @_;
 3123:     if (length($id) > 65 
 3124: 	&& length(&escape($id)) > 200) {
 3125: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3126:     }
 3127:     return &escape($name.':'.$id);
 3128: }
 3129: 
 3130: sub devalidate_cache_new {
 3131:     my ($name,$id,$debug) = @_;
 3132:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3133:     my $remembered_id=$name.':'.$id;
 3134:     $id=&make_key($name,$id);
 3135:     $memcache->delete($id);
 3136:     delete($remembered{$remembered_id});
 3137:     delete($accessed{$remembered_id});
 3138: }
 3139: 
 3140: sub is_cached_new {
 3141:     my ($name,$id,$debug) = @_;
 3142:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3143:     if (exists($remembered{$remembered_id})) {
 3144: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3145: 	$accessed{$remembered_id}=[&gettimeofday()];
 3146: 	$hits++;
 3147: 	return ($remembered{$remembered_id},1);
 3148:     }
 3149:     $id=&make_key($name,$id);
 3150:     my $value = $memcache->get($id);
 3151:     if (!(defined($value))) {
 3152: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3153: 	return (undef,undef);
 3154:     }
 3155:     if ($value eq '__undef__') {
 3156: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3157: 	$value=undef;
 3158:     }
 3159:     &make_room($remembered_id,$value,$debug);
 3160:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3161:     return ($value,1);
 3162: }
 3163: 
 3164: sub do_cache_new {
 3165:     my ($name,$id,$value,$time,$debug) = @_;
 3166:     my $remembered_id=$name.':'.$id;
 3167:     $id=&make_key($name,$id);
 3168:     my $setvalue=$value;
 3169:     if (!defined($setvalue)) {
 3170: 	$setvalue='__undef__';
 3171:     }
 3172:     if (!defined($time) ) {
 3173: 	$time=600;
 3174:     }
 3175:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3176:     my $result = $memcache->set($id,$setvalue,$time);
 3177:     if (! $result) {
 3178: 	&logthis("caching of id -> $id  failed");
 3179: 	$memcache->disconnect_all();
 3180:     }
 3181:     # need to make a copy of $value
 3182:     &make_room($remembered_id,$value,$debug);
 3183:     return $value;
 3184: }
 3185: 
 3186: sub make_room {
 3187:     my ($remembered_id,$value,$debug)=@_;
 3188: 
 3189:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3190:                                     : $value;
 3191:     if ($to_remember<0) { return; }
 3192:     $accessed{$remembered_id}=[&gettimeofday()];
 3193:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3194:     my $to_kick;
 3195:     my $max_time=0;
 3196:     foreach my $other (keys(%accessed)) {
 3197: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3198: 	    $to_kick=$other;
 3199: 	    $max_time=&tv_interval($accessed{$other});
 3200: 	}
 3201:     }
 3202:     delete($remembered{$to_kick});
 3203:     delete($accessed{$to_kick});
 3204:     $kicks++;
 3205:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3206:     return;
 3207: }
 3208: 
 3209: sub purge_remembered {
 3210:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3211:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3212:     undef(%remembered);
 3213:     undef(%accessed);
 3214: }
 3215: # ------------------------------------- Read an entry from a user's environment
 3216: 
 3217: sub userenvironment {
 3218:     my ($udom,$unam,@what)=@_;
 3219:     my $items;
 3220:     foreach my $item (@what) {
 3221:         $items.=&escape($item).'&';
 3222:     }
 3223:     $items=~s/\&$//;
 3224:     my %returnhash=();
 3225:     my $uhome = &homeserver($unam,$udom);
 3226:     unless ($uhome eq 'no_host') {
 3227:         my @answer=split(/\&/, 
 3228:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3229:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3230:             return %returnhash;
 3231:         }
 3232:         my $i;
 3233:         for ($i=0;$i<=$#what;$i++) {
 3234: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3235:         }
 3236:     }
 3237:     return %returnhash;
 3238: }
 3239: 
 3240: # ---------------------------------------------------------- Get a studentphoto
 3241: sub studentphoto {
 3242:     my ($udom,$unam,$ext) = @_;
 3243:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3244:     if (defined($env{'request.course.id'})) {
 3245:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3246:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3247:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3248:             } else {
 3249:                 my ($result,$perm_reqd)=
 3250: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3251:                 if ($result eq 'ok') {
 3252:                     if (!($perm_reqd eq 'yes')) {
 3253:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3254:                     }
 3255:                 }
 3256:             }
 3257:         }
 3258:     } else {
 3259:         my ($result,$perm_reqd) = 
 3260: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3261:         if ($result eq 'ok') {
 3262:             if (!($perm_reqd eq 'yes')) {
 3263:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3264:             }
 3265:         }
 3266:     }
 3267:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3268: }
 3269: 
 3270: sub retrievestudentphoto {
 3271:     my ($udom,$unam,$ext,$type) = @_;
 3272:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3273:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3274:     if ($ret eq 'ok') {
 3275:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3276:         if ($type eq 'thumbnail') {
 3277:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3278:         }
 3279:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3280:         return $tokenurl;
 3281:     } else {
 3282:         if ($type eq 'thumbnail') {
 3283:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3284:         } else { 
 3285:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3286:         }
 3287:     }
 3288: }
 3289: 
 3290: # -------------------------------------------------------------------- New chat
 3291: 
 3292: sub chatsend {
 3293:     my ($newentry,$anon,$group)=@_;
 3294:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3295:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3296:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3297:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3298: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3299: 		   &escape($newentry)).':'.$group,$chome);
 3300: }
 3301: 
 3302: # ------------------------------------------ Find current version of a resource
 3303: 
 3304: sub getversion {
 3305:     my $fname=&clutter(shift);
 3306:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3307:     return &currentversion(&filelocation('',$fname));
 3308: }
 3309: 
 3310: sub currentversion {
 3311:     my $fname=shift;
 3312:     my $author=$fname;
 3313:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3314:     my ($udom,$uname)=split(/\//,$author);
 3315:     my $home=&homeserver($uname,$udom);
 3316:     if ($home eq 'no_host') { 
 3317:         return -1; 
 3318:     }
 3319:     my $answer=&reply("currentversion:$fname",$home);
 3320:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3321: 	return -1;
 3322:     }
 3323:     return $answer;
 3324: }
 3325: 
 3326: #
 3327: # Return special version number of resource if set by override, empty otherwise
 3328: #
 3329: sub usedversion {
 3330:     my $fname=shift;
 3331:     unless ($fname) { $fname=$env{'request.uri'}; }
 3332:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3333:     if ($urlversion) { return $urlversion; }
 3334:     return '';
 3335: }
 3336: 
 3337: # ----------------------------- Subscribe to a resource, return URL if possible
 3338: 
 3339: sub subscribe {
 3340:     my $fname=shift;
 3341:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3342:     $fname=~s/[\n\r]//g;
 3343:     my $author=$fname;
 3344:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3345:     my ($udom,$uname)=split(/\//,$author);
 3346:     my $home=homeserver($uname,$udom);
 3347:     if ($home eq 'no_host') {
 3348:         return 'not_found';
 3349:     }
 3350:     my $answer=reply("sub:$fname",$home);
 3351:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3352: 	$answer.=' by '.$home;
 3353:     }
 3354:     return $answer;
 3355: }
 3356:     
 3357: # -------------------------------------------------------------- Replicate file
 3358: 
 3359: sub repcopy {
 3360:     my $filename=shift;
 3361:     $filename=~s/\/+/\//g;
 3362:     my $londocroot = $perlvar{'lonDocRoot'};
 3363:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3364:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3365:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3366: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3367: 	return &repcopy_userfile($filename);
 3368:     }
 3369:     $filename=~s/[\n\r]//g;
 3370:     my $transname="$filename.in.transfer";
 3371: # FIXME: this should flock
 3372:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3373:     my $remoteurl=subscribe($filename);
 3374:     if ($remoteurl =~ /^con_lost by/) {
 3375: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3376:            return 'unavailable';
 3377:     } elsif ($remoteurl eq 'not_found') {
 3378: 	   #&logthis("Subscribe returned not_found: $filename");
 3379: 	   return 'not_found';
 3380:     } elsif ($remoteurl =~ /^rejected by/) {
 3381: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3382:            return 'forbidden';
 3383:     } elsif ($remoteurl eq 'directory') {
 3384:            return 'ok';
 3385:     } else {
 3386:         my $author=$filename;
 3387:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3388:         my ($udom,$uname)=split(/\//,$author);
 3389:         my $home=homeserver($uname,$udom);
 3390:         unless ($home eq $perlvar{'lonHostID'}) {
 3391:            my @parts=split(/\//,$filename);
 3392:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3393:            if ($path ne "$londocroot/res") {
 3394:                &logthis("Malconfiguration for replication: $filename");
 3395: 	       return 'bad_request';
 3396:            }
 3397:            my $count;
 3398:            for ($count=5;$count<$#parts;$count++) {
 3399:                $path.="/$parts[$count]";
 3400:                if ((-e $path)!=1) {
 3401: 		   mkdir($path,0777);
 3402:                }
 3403:            }
 3404:            my $request=new HTTP::Request('GET',"$remoteurl");
 3405:            my $response;
 3406:            if ($remoteurl =~ m{/raw/}) {
 3407:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3408:            } else {
 3409:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3410:            }
 3411:            if ($response->is_error()) {
 3412: 	       unlink($transname);
 3413:                my $message=$response->status_line;
 3414:                &logthis("<font color=\"blue\">WARNING:"
 3415:                        ." LWP get: $message: $filename</font>");
 3416:                return 'unavailable';
 3417:            } else {
 3418: 	       if ($remoteurl!~/\.meta$/) {
 3419:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3420:                   my $mresponse;
 3421:                   if ($remoteurl =~ m{/raw/}) {
 3422:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3423:                   } else {
 3424:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3425:                   }
 3426:                   if ($mresponse->is_error()) {
 3427: 		      unlink($filename.'.meta');
 3428:                       &logthis(
 3429:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3430:                   }
 3431: 	       }
 3432:                rename($transname,$filename);
 3433:                return 'ok';
 3434:            }
 3435:        }
 3436:     }
 3437: }
 3438: 
 3439: # ------------------------------------------------- Unsubscribe from a resource
 3440: 
 3441: sub unsubscribe {
 3442:     my ($fname) = @_;
 3443:     my $answer;
 3444:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3445:     $fname=~s/[\n\r]//g;
 3446:     my $author=$fname;
 3447:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3448:     my ($udom,$uname)=split(/\//,$author);
 3449:     my $home=homeserver($uname,$udom);
 3450:     if ($home eq 'no_host') {
 3451:         $answer = 'no_host';
 3452:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3453:         $answer = 'home';
 3454:     } else {
 3455:         my $defdom = $perlvar{'lonDefDomain'};
 3456:         if (&will_trust('content',$defdom,$udom)) {
 3457:             $answer = reply("unsub:$fname",$home);
 3458:         } else {
 3459:             $answer = 'untrusted';
 3460:         }
 3461:     }
 3462:     return $answer;
 3463: }
 3464: 
 3465: # ------------------------------------------------ Get server side include body
 3466: sub ssi_body {
 3467:     my ($filelink,%form)=@_;
 3468:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3469:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3470:     }
 3471:     my $output='';
 3472:     my $response;
 3473:     if ($filelink=~/^https?\:/) {
 3474:        ($output,$response)=&externalssi($filelink);
 3475:     } else {
 3476:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3477:        $filelink .= 'inhibitmenu=yes';
 3478:        ($output,$response)=&ssi($filelink,%form);
 3479:     }
 3480:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3481:     $output=~s/^.*?\<body[^\>]*\>//si;
 3482:     $output=~s/\<\/body\s*\>.*?$//si;
 3483:     if (wantarray) {
 3484:         return ($output, $response);
 3485:     } else {
 3486:         return $output;
 3487:     }
 3488: }
 3489: 
 3490: # --------------------------------------------------------- Server Side Include
 3491: 
 3492: sub absolute_url {
 3493:     my ($host_name,$unalias,$keep_proto) = @_;
 3494:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3495:     if ($host_name eq '') {
 3496: 	$host_name = $ENV{'SERVER_NAME'};
 3497:     }
 3498:     if ($unalias) {
 3499:         my $alias = &get_proxy_alias();
 3500:         if ($alias eq $host_name) {
 3501:             my $lonhost = $perlvar{'lonHostID'};
 3502:             my $hostname = &hostname($lonhost);
 3503:             my $lcproto; 
 3504:             if (($keep_proto) || ($hostname eq '')) {
 3505:                 $lcproto = $protocol;
 3506:             } else {
 3507:                 $lcproto = $protocol{$lonhost};
 3508:                 $lcproto = 'http' if ($lcproto ne 'https');
 3509:                 $lcproto .= '://';
 3510:             }
 3511:             unless ($hostname eq '') {
 3512:                 return $lcproto.$hostname;
 3513:             }
 3514:         }
 3515:     }
 3516:     return $protocol.$host_name;
 3517: }
 3518: 
 3519: #
 3520: #   Server side include.
 3521: # Parameters:
 3522: #  fn     Possibly encrypted resource name/id.
 3523: #  form   Hash that describes how the rendering should be done
 3524: #         and other things.
 3525: # Returns:
 3526: #   Scalar context: The content of the response.
 3527: #   Array context:  2 element list of the content and the full response object.
 3528: #     
 3529: sub ssi {
 3530: 
 3531:     my ($fn,%form)=@_;
 3532:     my ($host,$request,$response);
 3533:     $host = &absolute_url('',1);
 3534: 
 3535:     $form{'no_update_last_known'}=1;
 3536:     &Apache::lonenc::check_encrypt(\$fn);
 3537:     if (%form) {
 3538:       $request=new HTTP::Request('POST',$host.$fn);
 3539:       $request->content(join('&',map { 
 3540:             my $name = escape($_);
 3541:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3542:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3543:             : &escape($form{$_}) );    
 3544:         } keys(%form)));
 3545:     } else {
 3546:       $request=new HTTP::Request('GET',$host.$fn);
 3547:     }
 3548: 
 3549:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3550:     my $lonhost = $perlvar{'lonHostID'};
 3551:     my $islocal;
 3552:     if (($env{'request.course.id'}) &&
 3553:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3554:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3555:         ($form{'grade_symb'} ne '') &&
 3556:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3557:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3558:         $islocal = 1;
 3559:     }
 3560:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3561:                                              '','','',$islocal);
 3562: 
 3563:     if (wantarray) {
 3564: 	return ($response->content, $response);
 3565:     } else {
 3566: 	return $response->content;
 3567:     }
 3568: }
 3569: 
 3570: sub externalssi {
 3571:     my ($url)=@_;
 3572:     my $request=new HTTP::Request('GET',$url);
 3573:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3574:     if (wantarray) {
 3575:         return ($response->content, $response);
 3576:     } else {
 3577:         return $response->content;
 3578:     }
 3579: }
 3580: 
 3581: 
 3582: # If the local copy of a replicated resource is outdated, trigger a  
 3583: # connection from the homeserver to flush the delayed queue. If no update 
 3584: # happens, remove local copies of outdated resource (and corresponding
 3585: # metadata file).
 3586: 
 3587: sub remove_stale_resfile {
 3588:     my ($url) = @_;
 3589:     my $removed;
 3590:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3591:         my $audom = $1;
 3592:         my $auname = $2;
 3593:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3594:             my $homeserver = &homeserver($auname,$audom);
 3595:             unless (($homeserver eq 'no_host') ||
 3596:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3597:                 my $fname = &filelocation('',$url);
 3598:                 if (-e $fname) {
 3599:                     my $hostname = &hostname($homeserver);
 3600:                     if ($hostname) {
 3601:                         my $protocol = $protocol{$homeserver};
 3602:                         $protocol = 'http' if ($protocol ne 'https');
 3603:                         my $uri = &declutter($url);
 3604:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3605:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3606:                         if ($response->is_success()) {
 3607:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3608:                             my $locmodtime = (stat($fname))[9];
 3609:                             if ($locmodtime < $remmodtime) {
 3610:                                 my $stale;
 3611:                                 my $answer = &reply('pong',$homeserver);
 3612:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3613:                                     sleep(0.2);
 3614:                                     $locmodtime = (stat($fname))[9];
 3615:                                     if ($locmodtime < $remmodtime) {
 3616:                                         my $posstransfer = $fname.'.in.transfer';
 3617:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3618:                                             $removed = 1;
 3619:                                         } else {
 3620:                                             $stale = 1;
 3621:                                         }
 3622:                                     } else {
 3623:                                         $removed = 1;
 3624:                                     }
 3625:                                 } else {
 3626:                                     $stale = 1;
 3627:                                 }
 3628:                                 if ($stale) {
 3629:                                     if (unlink($fname)) {
 3630:                                         if ($uri!~/\.meta$/) {
 3631:                                             if (-e $fname.'.meta') {
 3632:                                                 unlink($fname.'.meta');
 3633:                                             }
 3634:                                         }
 3635:                                         my $unsubresult = &unsubscribe($fname);
 3636:                                         unless ($unsubresult eq 'ok') {
 3637:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3638:                                         }
 3639:                                         $removed = 1;
 3640:                                     }
 3641:                                 }
 3642:                             }
 3643:                         }
 3644:                     }
 3645:                 }
 3646:             }
 3647:         }
 3648:     }
 3649:     return $removed;
 3650: }
 3651: 
 3652: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3653: 
 3654: sub allowuploaded {
 3655:     my ($srcurl,$url)=@_;
 3656:     $url=&clutter(&declutter($url));
 3657:     my $dir=$url;
 3658:     $dir=~s/\/[^\/]+$//;
 3659:     my %httpref=();
 3660:     my $httpurl=&hreflocation('',$url);
 3661:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3662:     &Apache::lonnet::appenv(\%httpref);
 3663: }
 3664: 
 3665: #
 3666: # Determine if the current user should be able to edit a particular resource,
 3667: # when viewing in course context.
 3668: # (a) When viewing resource used to determine if "Edit" item is included in 
 3669: #     Functions.
 3670: # (b) When displaying folder contents in course editor, used to determine if
 3671: #     "Edit" link will be displayed alongside resource.
 3672: #
 3673: #  input: six args -- filename (decluttered), course number, course domain,
 3674: #                   url, symb (if registered) and group (if this is a group
 3675: #                   item -- e.g., bulletin board, group page etc.).
 3676: #  output: array of five scalars -- 
 3677: #          $cfile -- url for file editing if editable on current server
 3678: #          $home -- homeserver of resource (i.e., for author if published,
 3679: #                                           or course if uploaded.).
 3680: #          $switchserver --  1 if server switch will be needed.
 3681: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3682: #          $forceview -- 1 if icon/link should be to go to view mode
 3683: #
 3684: 
 3685: sub can_edit_resource {
 3686:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3687:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3688: #
 3689: # For aboutme pages user can only edit his/her own.
 3690: #
 3691:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3692:         my ($sdom,$sname) = ($1,$2);
 3693:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3694:             $home = $env{'user.home'};
 3695:             $cfile = $resurl;
 3696:             if ($env{'form.forceedit'}) {
 3697:                 $forceview = 1;
 3698:             } else {
 3699:                 $forceedit = 1;
 3700:             }
 3701:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3702:         } else {
 3703:             return;
 3704:         }
 3705:     }
 3706: 
 3707:     if ($env{'request.course.id'}) {
 3708:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3709:         if ($group ne '') {
 3710: # if this is a group homepage or group bulletin board, check group privs
 3711:             my $allowed = 0;
 3712:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3713:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3714:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3715:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3716:                     $allowed = 1;
 3717:                 }
 3718:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3719:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3720:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3721:                     $allowed = 1;
 3722:                 }
 3723:             }
 3724:             if ($allowed) {
 3725:                 $home=&homeserver($cnum,$cdom);
 3726:                 if ($env{'form.forceedit'}) {
 3727:                     $forceview = 1;
 3728:                 } else {
 3729:                     $forceedit = 1;
 3730:                 }
 3731:                 $cfile = $resurl;
 3732:             } else {
 3733:                 return;
 3734:             }
 3735:         } else {
 3736:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3737:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3738:                     return;
 3739:                 }
 3740:             } elsif (!$crsedit) {
 3741: #
 3742: # No edit allowed where CC has switched to student role.
 3743: #
 3744:                 return;
 3745:             }
 3746:         }
 3747:     }
 3748: 
 3749:     if ($file ne '') {
 3750:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3751:             if (&is_course_upload($file,$cnum,$cdom)) {
 3752:                 $uploaded = 1;
 3753:                 $incourse = 1;
 3754:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3755:                     $cfile = &hreflocation('',$file);
 3756:                     if ($env{'form.forceedit'}) {
 3757:                         $forceview = 1;
 3758:                     } else {
 3759:                         $forceedit = 1;
 3760:                     }
 3761:                 }
 3762:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3763:                 $incourse = 1;
 3764:                 if ($env{'form.forceedit'}) {
 3765:                     $forceview = 1;
 3766:                 } else {
 3767:                     $forceedit = 1;
 3768:                 }
 3769:                 $cfile = $resurl;
 3770:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3771:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3772:                     $incourse = 1;
 3773:                     if ($env{'form.forceedit'}) {
 3774:                         $forceview = 1;
 3775:                     } else {
 3776:                         $forceedit = 1;
 3777:                     }
 3778:                     $cfile = $resurl;
 3779:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3780:                     $incourse = 1;
 3781:                     $cfile = $resurl.'/smpedit';
 3782:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3783:                     $incourse = 1;
 3784:                     if ($env{'form.forceedit'}) {
 3785:                         $forceview = 1;
 3786:                     } else {
 3787:                         $forceedit = 1;
 3788:                     }
 3789:                     $cfile = $resurl;
 3790:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3791:                     my ($map,$id,$res) = &decode_symb($symb);
 3792:                     if ($map =~ /\.page$/) {
 3793:                         $incourse = 1;
 3794:                         if ($env{'form.forceedit'}) {
 3795:                             $forceview = 1;
 3796:                             $cfile = $map;
 3797:                         } else {
 3798:                             $forceedit = 1;
 3799:                             $cfile =  '/adm/wrapper'.$resurl;
 3800:                         }
 3801:                     }
 3802:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3803:                     $incourse = 1;
 3804:                     if ($env{'form.forceedit'}) {
 3805:                         $forceview = 1;
 3806:                     } else {
 3807:                         $forceedit = 1;
 3808:                     }
 3809:                     $cfile = $resurl;
 3810:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3811:                     $incourse = 1;
 3812:                     if ($env{'form.forceedit'}) {
 3813:                         $forceview = 1;
 3814:                     } else {
 3815:                         $forceedit = 1;
 3816:                     }
 3817:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3818:                 }
 3819:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3820:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3821:                 if (&is_on_map($template)) { 
 3822:                     $incourse = 1;
 3823:                     $forceview = 1;
 3824:                     $cfile = $template;
 3825:                 }
 3826:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($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 =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3835:                 $incourse = 1;
 3836:                 if ($env{'form.forceedit'}) {
 3837:                     $forceview = 1;
 3838:                 } else {
 3839:                     $forceedit = 1;
 3840:                 }
 3841:                 $cfile = $resurl;
 3842:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3843:                 $incourse = 1;
 3844:                 $forceview = 1;
 3845:                 if ($symb) {
 3846:                     my ($map,$id,$res)=&decode_symb($symb);
 3847:                     $env{'request.symb'} = $symb;
 3848:                     $cfile = &clutter($res);
 3849:                 } else {
 3850:                     $cfile = $env{'form.suppurl'};
 3851:                     my $escfile = &unescape($cfile);
 3852:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3853:                         $cfile = '/adm/wrapper'.$escfile;
 3854:                     } else {
 3855:                         $escfile =~ s{^http://}{};
 3856:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3857:                     }
 3858:                 }
 3859:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3860:                 if ($env{'form.forceedit'}) {
 3861:                     $forceview = 1;
 3862:                 } else {
 3863:                     $forceedit = 1;
 3864:                 }
 3865:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3866:             }
 3867:         }
 3868:         if ($uploaded || $incourse) {
 3869:             $home=&homeserver($cnum,$cdom);
 3870:         } elsif ($file !~ m{/$}) {
 3871:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3872:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3873:             # Check that the user has permission to edit this resource
 3874:             my $setpriv = 1;
 3875:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3876:             if (defined($cfudom)) {
 3877:                 $home=&homeserver($cfuname,$cfudom);
 3878:                 $cfile=$file;
 3879:             }
 3880:         }
 3881:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3882:             (($home ne '') && ($home ne 'no_host'))) {
 3883:             my @ids=&current_machine_ids();
 3884:             unless (grep(/^\Q$home\E$/,@ids)) {
 3885:                 $switchserver=1;
 3886:             }
 3887:         }
 3888:     }
 3889:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3890: }
 3891: 
 3892: sub is_course_upload {
 3893:     my ($file,$cnum,$cdom) = @_;
 3894:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3895:     $uploadpath =~ s{^\/}{};
 3896:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3897:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3898:         return 1;
 3899:     }
 3900:     return;
 3901: }
 3902: 
 3903: sub in_course {
 3904:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3905:     if ($hideprivileged) {
 3906:         my $skipuser;
 3907:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3908:         my @possdoms = ($cdom);  
 3909:         if ($coursehash{'checkforpriv'}) { 
 3910:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3911:         }
 3912:         if (&privileged($uname,$udom,\@possdoms)) {
 3913:             $skipuser = 1;
 3914:             if ($coursehash{'nothideprivileged'}) {
 3915:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3916:                     my $user;
 3917:                     if ($item =~ /:/) {
 3918:                         $user = $item;
 3919:                     } else {
 3920:                         $user = join(':',split(/[\@]/,$item));
 3921:                     }
 3922:                     if ($user eq $uname.':'.$udom) {
 3923:                         undef($skipuser);
 3924:                         last;
 3925:                     }
 3926:                 }
 3927:             }
 3928:             if ($skipuser) {
 3929:                 return 0;
 3930:             }
 3931:         }
 3932:     }
 3933:     $type ||= 'any';
 3934:     if (!defined($cdom) || !defined($cnum)) {
 3935:         my $cid  = $env{'request.course.id'};
 3936:         $cdom = $env{'course.'.$cid.'.domain'};
 3937:         $cnum = $env{'course.'.$cid.'.num'};
 3938:     }
 3939:     my $typesref;
 3940:     if (($type eq 'any') || ($type eq 'all')) {
 3941:         $typesref = ['active','previous','future'];
 3942:     } elsif ($type eq 'previous' || $type eq 'future') {
 3943:         $typesref = [$type];
 3944:     }
 3945:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3946:                               $typesref,undef,[$cdom]);
 3947:     my ($tmp) = keys(%roles);
 3948:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3949:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3950:     if (@course_roles > 0) {
 3951:         return 1;
 3952:     }
 3953:     return 0;
 3954: }
 3955: 
 3956: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3957: # input: action, courseID, current domain, intended
 3958: #        path to file, source of file, instruction to parse file for objects,
 3959: #        ref to hash for embedded objects,
 3960: #        ref to hash for codebase of java objects.
 3961: #        reference to scalar to accommodate mime type determined
 3962: #          from File::MMagic if $parser = parse.
 3963: #
 3964: # output: url to file (if action was uploaddoc), 
 3965: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3966: #
 3967: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3968: # course.
 3969: #
 3970: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3971: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3972: #          course's home server.
 3973: #
 3974: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3975: #          be copied from $source (current location) to 
 3976: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3977: #         and will then be copied to
 3978: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3979: #         course's home server.
 3980: #
 3981: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3982: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3983: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3984: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3985: #         in course's home server.
 3986: #
 3987: 
 3988: sub process_coursefile {
 3989:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3990:         $mimetype)=@_;
 3991:     my $fetchresult;
 3992:     my $home=&homeserver($docuname,$docudom);
 3993:     if ($action eq 'propagate') {
 3994:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3995: 			     $home);
 3996:     } else {
 3997:         my $fpath = '';
 3998:         my $fname = $file;
 3999:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4000:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4001:         my $filepath = &build_filepath($fpath);
 4002:         if ($action eq 'copy') {
 4003:             if ($source eq '') {
 4004:                 $fetchresult = 'no source file';
 4005:                 return $fetchresult;
 4006:             } else {
 4007:                 my $destination = $filepath.'/'.$fname;
 4008:                 rename($source,$destination);
 4009:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4010:                                  $home);
 4011:             }
 4012:         } elsif ($action eq 'uploaddoc') {
 4013:             open(my $fh,'>',$filepath.'/'.$fname);
 4014:             print $fh $env{'form.'.$source};
 4015:             close($fh);
 4016:             if ($parser eq 'parse') {
 4017:                 my $mm = new File::MMagic;
 4018:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4019:                 if ($type eq 'text/html') {
 4020:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4021:                     unless ($parse_result eq 'ok') {
 4022:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4023:                     }
 4024:                 }
 4025:                 if (ref($mimetype)) {
 4026:                     $$mimetype = $type;
 4027:                 } 
 4028:             }
 4029:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4030:                                  $home);
 4031:             if ($fetchresult eq 'ok') {
 4032:                 return '/uploaded/'.$fpath.'/'.$fname;
 4033:             } else {
 4034:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4035:                         ' to host '.$home.': '.$fetchresult);
 4036:                 return '/adm/notfound.html';
 4037:             }
 4038:         }
 4039:     }
 4040:     unless ( $fetchresult eq 'ok') {
 4041:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4042:              ' to host '.$home.': '.$fetchresult);
 4043:     }
 4044:     return $fetchresult;
 4045: }
 4046: 
 4047: sub build_filepath {
 4048:     my ($fpath) = @_;
 4049:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4050:     unless ($fpath eq '') {
 4051:         my @parts=split('/',$fpath);
 4052:         foreach my $part (@parts) {
 4053:             $filepath.= '/'.$part;
 4054:             if ((-e $filepath)!=1) {
 4055:                 mkdir($filepath,0777);
 4056:             }
 4057:         }
 4058:     }
 4059:     return $filepath;
 4060: }
 4061: 
 4062: sub store_edited_file {
 4063:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4064:     my $file = $primary_url;
 4065:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4066:     my $fpath = '';
 4067:     my $fname = $file;
 4068:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4069:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4070:     my $filepath = &build_filepath($fpath);
 4071:     open(my $fh,'>',$filepath.'/'.$fname);
 4072:     print $fh $content;
 4073:     close($fh);
 4074:     my $home=&homeserver($docuname,$docudom);
 4075:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4076: 			  $home);
 4077:     if ($$fetchresult eq 'ok') {
 4078:         return '/uploaded/'.$fpath.'/'.$fname;
 4079:     } else {
 4080:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4081: 		 ' to host '.$home.': '.$$fetchresult);
 4082:         return '/adm/notfound.html';
 4083:     }
 4084: }
 4085: 
 4086: sub clean_filename {
 4087:     my ($fname,$args)=@_;
 4088: # Replace Windows backslashes by forward slashes
 4089:     $fname=~s/\\/\//g;
 4090:     if (!$args->{'keep_path'}) {
 4091:         # Get rid of everything but the actual filename
 4092: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4093:     }
 4094: # Replace spaces by underscores
 4095:     $fname=~s/\s+/\_/g;
 4096: # Transliterate non-ascii text to ascii
 4097:     my $lang = &Apache::lonlocal::current_language();
 4098:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4099: # Replace all other weird characters by nothing
 4100:     $fname=~s{[^/\w\.\-]}{}g;
 4101: # Replace all .\d. sequences with _\d. so they no longer look like version
 4102: # numbers
 4103:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4104: # Replace three or more adjacent underscores with one for consistency 
 4105: # with loncfile::filename_check() so complete url can be extracted by
 4106: # lonnet::decode_symb()
 4107:     $fname=~s/_{3,}/_/g;
 4108:     return $fname;
 4109: }
 4110: 
 4111: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4112: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4113: # image with the same aspect ratio as the original, but with dimensions which do 
 4114: # not exceed $resizewidth and $resizeheight.
 4115:  
 4116: sub resizeImage {
 4117:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4118:     my $ima = Image::Magick->new;
 4119:     my $resized;
 4120:     if (-e $img_path) {
 4121:         $ima->Read($img_path);
 4122:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4123:             my $width = $ima->Get('width');
 4124:             my $height = $ima->Get('height');
 4125:             if ($width > $resizewidth) {
 4126: 	        my $factor = $width/$resizewidth;
 4127:                 my $newheight = $height/$factor;
 4128:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4129:                 $resized = 1;
 4130:             }
 4131:         }
 4132:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4133:             my $width = $ima->Get('width');
 4134:             my $height = $ima->Get('height');
 4135:             if ($height > $resizeheight) {
 4136:                 my $factor = $height/$resizeheight;
 4137:                 my $newwidth = $width/$factor;
 4138:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4139:                 $resized = 1;
 4140:             }
 4141:         }
 4142:         if ($resized) {
 4143:             $ima->Write($img_path);
 4144:         }
 4145:     }
 4146:     return;
 4147: }
 4148: 
 4149: # --------------- Take an uploaded file and put it into the userfiles directory
 4150: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4151: #                    the desired filename is in $env{"form.$formname.filename"}
 4152: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4153: #                                    canceloverwrite, scantron or ''.
 4154: #                   if 'coursedoc': upload to the current course
 4155: #                   if 'existingfile': write file to tmp/overwrites directory 
 4156: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4157: #                   $context is passed as argument to &finishuserfileupload
 4158: #        $subdir - directory in userfile to store the file into
 4159: #        $parser - instruction to parse file for objects ($parser = parse) or
 4160: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4161: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4162: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4163: #        $allfiles - reference to hash for embedded objects
 4164: #        $codebase - reference to hash for codebase of java objects
 4165: #        $desuname - username for permanent storage of uploaded file
 4166: #        $dsetudom - domain for permanaent storage of uploaded file
 4167: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4168: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4169: #        $resizewidth - width (pixels) to which to resize uploaded image
 4170: #        $resizeheight - height (pixels) to which to resize uploaded image
 4171: #        $mimetype - reference to scalar to accommodate mime type determined
 4172: #                    from File::MMagic.
 4173: # 
 4174: # output: url of file in userspace, or error: <message> 
 4175: #             or /adm/notfound.html if failure to upload occurse
 4176: 
 4177: sub userfileupload {
 4178:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4179:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4180:     if (!defined($subdir)) { $subdir='unknown'; }
 4181:     my $fname=$env{'form.'.$formname.'.filename'};
 4182:     $fname=&clean_filename($fname);
 4183:     # See if there is anything left
 4184:     unless ($fname) { return 'error: no uploaded file'; }
 4185:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4186:     if ($fname =~ /^\./) {
 4187:         my ($s,$usec) = &gettimeofday();
 4188:         while (length($usec) < 6) {
 4189:             $usec = '0'.$usec;
 4190:         }
 4191:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4192:     }
 4193:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4194:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4195:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4196:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4197:         my $now = time;
 4198:         my $filepath;
 4199:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4200:              $filepath = 'tmp/helprequests/'.$now;
 4201:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4202:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4203:                          '_'.$env{'user.domain'}.'/pending';
 4204:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4205:             my ($docuname,$docudom);
 4206:             if ($destudom =~ /^$match_domain$/) {
 4207:                 $docudom = $destudom;
 4208:             } else {
 4209:                 $docudom = $env{'user.domain'};
 4210:             }
 4211:             if ($destuname =~ /^$match_username$/) {
 4212:                 $docuname = $destuname;
 4213:             } else {
 4214:                 $docuname = $env{'user.name'};
 4215:             }
 4216:             if (exists($env{'form.group'})) {
 4217:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4218:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4219:             }
 4220:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4221:             if ($context eq 'canceloverwrite') {
 4222:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4223:                 if (-e  $tempfile) {
 4224:                     my @info = stat($tempfile);
 4225:                     if ($info[9] eq $env{'form.timestamp'}) {
 4226:                         unlink($tempfile);
 4227:                     }
 4228:                 }
 4229:                 return;
 4230:             }
 4231:         }
 4232:         # Create the directory if not present
 4233:         my @parts=split(/\//,$filepath);
 4234:         my $fullpath = $perlvar{'lonDaemons'};
 4235:         for (my $i=0;$i<@parts;$i++) {
 4236:             $fullpath .= '/'.$parts[$i];
 4237:             if ((-e $fullpath)!=1) {
 4238:                 mkdir($fullpath,0777);
 4239:             }
 4240:         }
 4241:         open(my $fh,'>',$fullpath.'/'.$fname);
 4242:         print $fh $env{'form.'.$formname};
 4243:         close($fh);
 4244:         if ($context eq 'existingfile') {
 4245:             my @info = stat($fullpath.'/'.$fname);
 4246:             return ($fullpath.'/'.$fname,$info[9]);
 4247:         } else {
 4248:             return $fullpath.'/'.$fname;
 4249:         }
 4250:     }
 4251:     if ($subdir eq 'scantron') {
 4252:         $fname = 'scantron_orig_'.$fname;
 4253:     } else {
 4254:         $fname="$subdir/$fname";
 4255:     }
 4256:     if ($context eq 'coursedoc') {
 4257: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4258: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4259:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4260:             return &finishuserfileupload($docuname,$docudom,
 4261: 					 $formname,$fname,$parser,$allfiles,
 4262: 					 $codebase,$thumbwidth,$thumbheight,
 4263:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4264:         } else {
 4265:             if ($env{'form.folder'}) {
 4266:                 $fname=$env{'form.folder'}.'/'.$fname;
 4267:             }
 4268:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4269: 				       $fname,$formname,$parser,
 4270: 				       $allfiles,$codebase,$mimetype);
 4271:         }
 4272:     } elsif (defined($destuname)) {
 4273:         my $docuname=$destuname;
 4274:         my $docudom=$destudom;
 4275: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4276: 				     $parser,$allfiles,$codebase,
 4277:                                      $thumbwidth,$thumbheight,
 4278:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4279:     } else {
 4280:         my $docuname=$env{'user.name'};
 4281:         my $docudom=$env{'user.domain'};
 4282:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4283:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4284:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4285:         }
 4286: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4287: 				     $parser,$allfiles,$codebase,
 4288:                                      $thumbwidth,$thumbheight,
 4289:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4290:     }
 4291: }
 4292: 
 4293: sub finishuserfileupload {
 4294:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4295:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4296:     my $path=$docudom.'/'.$docuname.'/';
 4297:     my $filepath=$perlvar{'lonDocRoot'};
 4298:   
 4299:     my ($fnamepath,$file,$fetchthumb);
 4300:     $file=$fname;
 4301:     if ($fname=~m|/|) {
 4302:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4303: 	$path.=$fnamepath.'/';
 4304:     }
 4305:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4306:     my $count;
 4307:     for ($count=4;$count<=$#parts;$count++) {
 4308:         $filepath.="/$parts[$count]";
 4309:         if ((-e $filepath)!=1) {
 4310: 	    mkdir($filepath,0777);
 4311:         }
 4312:     }
 4313: 
 4314: # Save the file
 4315:     {
 4316: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4317: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4318: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4319: 	    return '/adm/notfound.html';
 4320: 	}
 4321:         if ($context eq 'overwrite') {
 4322:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4323:             my $target = $filepath.'/'.$file;
 4324:             if (-e $source) {
 4325:                 my @info = stat($source);
 4326:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4327:                     unless (&File::Copy::move($source,$target)) {
 4328:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4329:                         return "Moving from $source failed";
 4330:                     }
 4331:                 } else {
 4332:                     return "Temporary file: $source had unexpected date/time for last modification";
 4333:                 }
 4334:             } else {
 4335:                 return "Temporary file: $source missing";
 4336:             }
 4337:         } elsif (!print FH ($env{'form.'.$formname})) {
 4338: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4339: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4340: 	    return '/adm/notfound.html';
 4341: 	}
 4342: 	close(FH);
 4343:         if ($resizewidth && $resizeheight) {
 4344:             my $mm = new File::MMagic;
 4345:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4346:             if ($mime_type =~ m{^image/}) {
 4347: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4348:             }  
 4349: 	}
 4350:     }
 4351:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4352:         if (ref($mimetype)) {
 4353:             if ($$mimetype eq '') {
 4354:                 my $mm = new File::MMagic;
 4355:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4356:                 $$mimetype = $type;
 4357:             }
 4358:         }
 4359:     }
 4360:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4361:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4362:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4363:                                                        $allfiles,$codebase);
 4364:             unless ($parse_result eq 'ok') {
 4365:                 &logthis('Failed to parse '.$filepath.$file.
 4366: 	   	         ' for embedded media: '.$parse_result); 
 4367:             }
 4368:         }
 4369:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4370:         my $format = $env{'form.scantron_format'};
 4371:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4372:     }
 4373:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4374:         my $input = $filepath.'/'.$file;
 4375:         my $output = $filepath.'/'.'tn-'.$file;
 4376:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4377:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4378:         system({$args[0]} @args);
 4379:         if (-e $filepath.'/'.'tn-'.$file) {
 4380:             $fetchthumb  = 1; 
 4381:         }
 4382:     }
 4383:  
 4384: # Notify homeserver to grep it
 4385: #
 4386:     my $docuhome=&homeserver($docuname,$docudom);	
 4387:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4388:     if ($fetchresult eq 'ok') {
 4389:         if ($fetchthumb) {
 4390:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4391:             if ($thumbresult ne 'ok') {
 4392:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4393:                          $docuhome.': '.$thumbresult);
 4394:             }
 4395:         }
 4396: #
 4397: # Return the URL to it
 4398:         return '/uploaded/'.$path.$file;
 4399:     } else {
 4400:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4401: 		 ': '.$fetchresult);
 4402:         return '/adm/notfound.html';
 4403:     }
 4404: }
 4405: 
 4406: sub extract_embedded_items {
 4407:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4408:     my @state = ();
 4409:     my (%lastids,%related,%shockwave,%flashvars);
 4410:     my %javafiles = (
 4411:                       codebase => '',
 4412:                       code => '',
 4413:                       archive => ''
 4414:                     );
 4415:     my %mediafiles = (
 4416:                       src => '',
 4417:                       movie => '',
 4418:                      );
 4419:     my $p;
 4420:     if ($content) {
 4421:         $p = HTML::LCParser->new($content);
 4422:     } else {
 4423:         $p = HTML::LCParser->new($fullpath);
 4424:     }
 4425:     while (my $t=$p->get_token()) {
 4426: 	if ($t->[0] eq 'S') {
 4427: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4428: 	    push(@state, $tagname);
 4429:             if (lc($tagname) eq 'allow') {
 4430:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4431:             }
 4432: 	    if (lc($tagname) eq 'img') {
 4433: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4434: 	    }
 4435: 	    if (lc($tagname) eq 'a') {
 4436:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4437:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4438:                 }
 4439: 	    }
 4440:             if (lc($tagname) eq 'script') {
 4441:                 my $src;
 4442:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4443:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4444:                 } else {
 4445:                     if ($attr->{'src'} ne '') {
 4446:                         $src = $attr->{'src'};
 4447:                         &add_filetype($allfiles,$src,'src');
 4448:                     }
 4449:                 }
 4450:                 my $text = $p->get_trimmed_text();
 4451:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4452:                     my @swfargs = split(/,/,$1);
 4453:                     foreach my $item (@swfargs) {
 4454:                         $item =~ s/["']//g;
 4455:                         $item =~ s/^\s+//;
 4456:                         $item =~ s/\s+$//;
 4457:                     }
 4458:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4459:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4460:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4461:                         } else {
 4462:                             $related{$swfargs[0]} = [$swfargs[2]];
 4463:                         }
 4464:                     }
 4465:                 }
 4466:             }
 4467:             if (lc($tagname) eq 'link') {
 4468:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4469:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4470:                 }
 4471:             }
 4472: 	    if (lc($tagname) eq 'object' ||
 4473: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4474: 		foreach my $item (keys(%javafiles)) {
 4475: 		    $javafiles{$item} = '';
 4476: 		}
 4477:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4478:                     $lastids{lc($tagname)} = $attr->{'id'};
 4479:                 }
 4480: 	    }
 4481: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4482: 		my $name = lc($attr->{'name'});
 4483: 		foreach my $item (keys(%javafiles)) {
 4484: 		    if ($name eq $item) {
 4485: 			$javafiles{$item} = $attr->{'value'};
 4486: 			last;
 4487: 		    }
 4488: 		}
 4489:                 my $pathfrom;
 4490: 		foreach my $item (keys(%mediafiles)) {
 4491: 		    if ($name eq $item) {
 4492:                         $pathfrom = $attr->{'value'};
 4493:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4494: 			&add_filetype($allfiles,$pathfrom,$name);
 4495: 			last;
 4496: 		    }
 4497: 		}
 4498:                 if ($name eq 'flashvars') {
 4499:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4500:                 }
 4501:                 if ($pathfrom ne '') {
 4502:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4503:                                          $pathfrom);
 4504:                 }
 4505: 	    }
 4506: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4507: 		foreach my $item (keys(%javafiles)) {
 4508: 		    if ($attr->{$item}) {
 4509: 			$javafiles{$item} = $attr->{$item};
 4510: 			last;
 4511: 		    }
 4512: 		}
 4513: 		foreach my $item (keys(%mediafiles)) {
 4514: 		    if ($attr->{$item}) {
 4515: 			&add_filetype($allfiles,$attr->{$item},$item);
 4516: 			last;
 4517: 		    }
 4518: 		}
 4519:                 if (lc($tagname) eq 'embed') {
 4520:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4521:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4522:                                              $attr->{'src'});
 4523:                     }
 4524:                 }
 4525: 	    }
 4526:             if (lc($tagname) eq 'iframe') {
 4527:                 my $src = $attr->{'src'} ;
 4528:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4529:                     &add_filetype($allfiles,$src,'src');
 4530:                 } elsif ($src =~ m{^/}) {
 4531:                     if ($env{'request.course.id'}) {
 4532:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4533:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4534:                         my $url = &hreflocation('',$fullpath);
 4535:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4536:                             my $relpath = $1;
 4537:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4538:                                 &add_filetype($allfiles,$1,'src');
 4539:                             }
 4540:                         }
 4541:                     }
 4542:                 }
 4543:             }
 4544:             if ($t->[4] =~ m{/>$}) {
 4545:                 pop(@state);
 4546:             }
 4547: 	} elsif ($t->[0] eq 'E') {
 4548: 	    my ($tagname) = ($t->[1]);
 4549: 	    if ($javafiles{'codebase'} ne '') {
 4550: 		$javafiles{'codebase'} .= '/';
 4551: 	    }  
 4552: 	    if (lc($tagname) eq 'applet' ||
 4553: 		lc($tagname) eq 'object' ||
 4554: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4555: 		) {
 4556: 		foreach my $item (keys(%javafiles)) {
 4557: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4558: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4559: 			&add_filetype($allfiles,$file,$item);
 4560: 		    }
 4561: 		}
 4562: 	    } 
 4563: 	    pop @state;
 4564: 	}
 4565:     }
 4566:     foreach my $id (sort(keys(%flashvars))) {
 4567:         if ($shockwave{$id} ne '') {
 4568:             my @pairs = split(/\&/,$flashvars{$id});
 4569:             foreach my $pair (@pairs) {
 4570:                 my ($key,$value) = split(/\=/,$pair);
 4571:                 if ($key eq 'thumb') {
 4572:                     &add_filetype($allfiles,$value,$key);
 4573:                 } elsif ($key eq 'content') {
 4574:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4575:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4576:                     if ($ext ne '') {
 4577:                         &add_filetype($allfiles,$path.$value,$ext);
 4578:                     }
 4579:                 }
 4580:             }
 4581:         }
 4582:     }
 4583:     return 'ok';
 4584: }
 4585: 
 4586: sub add_filetype {
 4587:     my ($allfiles,$file,$type)=@_;
 4588:     if (exists($allfiles->{$file})) {
 4589: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4590: 	    push(@{$allfiles->{$file}}, &escape($type));
 4591: 	}
 4592:     } else {
 4593: 	@{$allfiles->{$file}} = (&escape($type));
 4594:     }
 4595: }
 4596: 
 4597: sub embedded_dependency {
 4598:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4599:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4600:         if (($identifier ne '') &&
 4601:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4602:             ($pathfrom ne '')) {
 4603:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4604:             foreach my $dep (@{$related->{$identifier}}) {
 4605:                 &add_filetype($allfiles,$path.$dep,'object');
 4606:             }
 4607:         }
 4608:     }
 4609:     return;
 4610: }
 4611: 
 4612: sub bubblesheet_converter {
 4613:     my ($cdom,$fullpath,$config,$format) = @_;
 4614:     if ((&domain($cdom) ne '') &&
 4615:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4616:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4617:         my (%csvcols,%csvoptions);
 4618:         if (ref($config->{'fields'}) eq 'HASH') {  
 4619:             %csvcols = %{$config->{'fields'}};
 4620:         }
 4621:         if (ref($config->{'options'}) eq 'HASH') {
 4622:             %csvoptions = %{$config->{'options'}};
 4623:         }
 4624:         my %csvbynum = reverse(%csvcols);
 4625:         my %scantronconf = &get_scantron_config($format,$cdom);
 4626:         if (keys(%scantronconf)) {
 4627:             my %bynum = (
 4628:                           $scantronconf{CODEstart} => 'CODEstart',
 4629:                           $scantronconf{IDstart}   => 'IDstart',
 4630:                           $scantronconf{PaperID}   => 'PaperID',
 4631:                           $scantronconf{FirstName} => 'FirstName',
 4632:                           $scantronconf{LastName}  => 'LastName',
 4633:                           $scantronconf{Qstart}    => 'Qstart',
 4634:                         );
 4635:             my @ordered;
 4636:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4637:                 push(@ordered,$bynum{$item});
 4638:             }
 4639:             my %mapstart = (
 4640:                               CODEstart => 'CODE',
 4641:                               IDstart   => 'ID',
 4642:                               PaperID   => 'PaperID',
 4643:                               FirstName => 'FirstName',
 4644:                               LastName  => 'LastName',
 4645:                               Qstart    => 'FirstQuestion',
 4646:                            );
 4647:             my %maplength = (
 4648:                               CODEstart => 'CODElength',
 4649:                               IDstart   => 'IDlength',
 4650:                               PaperID   => 'PaperIDlength',
 4651:                               FirstName => 'FirstNamelength',
 4652:                               LastName  => 'LastNamelength',
 4653:             );
 4654:             if (open(my $fh,'<',$fullpath)) {
 4655:                 my $output;
 4656:                 my %lettdig = &letter_to_digits();
 4657:                 my %diglett = reverse(%lettdig);
 4658:                 my $numletts = scalar(keys(%lettdig));
 4659:                 my $num = 0;
 4660:                 while (my $line=<$fh>) {
 4661:                     $num ++;
 4662:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4663:                     $line =~ s{[\r\n]+$}{};
 4664:                     my %found;
 4665:                     my @values = split(/,/,$line);
 4666:                     my ($qstart,$record);
 4667:                     for (my $i=0; $i<@values; $i++) {
 4668:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4669:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4670:                             if ($values[$i] eq '') {
 4671:                                 $values[$i] = $scantronconf{'Qoff'};
 4672:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4673:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4674:                                     $values[$i] = $lettdig{uc($values[$i])};
 4675:                                 }
 4676:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4677:                                 if ($values[$i] =~ /^[0-9]$/) {
 4678:                                     $values[$i] = $diglett{$values[$i]};
 4679:                                 }
 4680:                             } else {
 4681:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4682:                                     my $digit;
 4683:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4684:                                         $digit = $lettdig{uc($values[$i])}-1;
 4685:                                         if ($values[$i] eq 'J') {
 4686:                                             $digit += $numletts;
 4687:                                         }
 4688:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4689:                                         $digit = $values[$i]-1;
 4690:                                         if ($values[$i] eq '0') {
 4691:                                             $digit += $numletts;
 4692:                                         }
 4693:                                     }
 4694:                                     my $qval='';
 4695:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4696:                                         if ($j == $digit) {
 4697:                                             $qval .= $scantronconf{'Qon'};
 4698:                                         } else {
 4699:                                             $qval .= $scantronconf{'Qoff'};
 4700:                                         }
 4701:                                     }
 4702:                                     $values[$i] = $qval;
 4703:                                 }
 4704:                             }
 4705:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4706:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4707:                             }
 4708:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4709:                             if ($numblank > 0) {
 4710:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4711:                             }
 4712:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4713:                                 $qstart = $i;
 4714:                                 $found{$csvbynum{$i}} = $values[$i];
 4715:                             } else {
 4716:                                 $found{'FirstQuestion'} .= $values[$i];
 4717:                             }
 4718:                         } elsif (exists($csvbynum{$i})) {
 4719:                             if ($csvoptions{'rem'}) {
 4720:                                 $values[$i] =~ s/^\s+//;
 4721:                             }
 4722:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4723:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4724:                                     $values[$i] = '0'.$values[$i];
 4725:                                 }
 4726:                             }
 4727:                             $found{$csvbynum{$i}} = $values[$i];
 4728:                         }
 4729:                     }
 4730:                     foreach my $item (@ordered) {
 4731:                         my $currlength = 1+length($record);
 4732:                         my $numspaces = $scantronconf{$item} - $currlength;
 4733:                         if ($numspaces > 0) {
 4734:                             $record .= (' ' x $numspaces);
 4735:                         }
 4736:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4737:                             unless ($item eq 'Qstart') {
 4738:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4739:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4740:                                 }
 4741:                             }
 4742:                             $record .= $found{$mapstart{$item}};
 4743:                         }
 4744:                     }
 4745:                     $output .= "$record\n";
 4746:                 }
 4747:                 close($fh);
 4748:                 if ($output) {
 4749:                     if (open(my $fh,'>',$fullpath)) {
 4750:                         print $fh $output;
 4751:                         close($fh);
 4752:                     }
 4753:                 }
 4754:             }
 4755:         }
 4756:         return;
 4757:     }
 4758: }
 4759: 
 4760: sub letter_to_digits {
 4761:     my %lettdig = (
 4762:                     A => 1,
 4763:                     B => 2,
 4764:                     C => 3,
 4765:                     D => 4,
 4766:                     E => 5,
 4767:                     F => 6,
 4768:                     G => 7,
 4769:                     H => 8,
 4770:                     I => 9,
 4771:                     J => 0,
 4772:                   );
 4773:     return %lettdig;
 4774: }
 4775: 
 4776: sub get_scantron_config {
 4777:     my ($which,$cdom) = @_;
 4778:     my @lines = &get_scantronformat_file($cdom);
 4779:     my %config;
 4780:     #FIXME probably should move to XML it has already gotten a bit much now
 4781:     foreach my $line (@lines) {
 4782:         my ($name,$descrip)=split(/:/,$line);
 4783:         if ($name ne $which ) { next; }
 4784:         chomp($line);
 4785:         my @config=split(/:/,$line);
 4786:         $config{'name'}=$config[0];
 4787:         $config{'description'}=$config[1];
 4788:         $config{'CODElocation'}=$config[2];
 4789:         $config{'CODEstart'}=$config[3];
 4790:         $config{'CODElength'}=$config[4];
 4791:         $config{'IDstart'}=$config[5];
 4792:         $config{'IDlength'}=$config[6];
 4793:         $config{'Qstart'}=$config[7];
 4794:         $config{'Qlength'}=$config[8];
 4795:         $config{'Qoff'}=$config[9];
 4796:         $config{'Qon'}=$config[10];
 4797:         $config{'PaperID'}=$config[11];
 4798:         $config{'PaperIDlength'}=$config[12];
 4799:         $config{'FirstName'}=$config[13];
 4800:         $config{'FirstNamelength'}=$config[14];
 4801:         $config{'LastName'}=$config[15];
 4802:         $config{'LastNamelength'}=$config[16];
 4803:         $config{'BubblesPerRow'}=$config[17];
 4804:         last;
 4805:     }
 4806:     return %config;
 4807: }
 4808: 
 4809: sub get_scantronformat_file {
 4810:     my ($cdom) = @_;
 4811:     if ($cdom eq '') {
 4812:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4813:     }
 4814:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4815:     my $gottab = 0;
 4816:     my @lines;
 4817:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4818:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4819:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4820:             if ($formatfile ne '-1') {
 4821:                 @lines = split("\n",$formatfile,-1);
 4822:                 $gottab = 1;
 4823:             }
 4824:         }
 4825:     }
 4826:     if (!$gottab) {
 4827:         my $confname = $cdom.'-domainconfig';
 4828:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4829:         my $formatfile = &getfile($default);
 4830:         if ($formatfile ne '-1') {
 4831:             @lines = split("\n",$formatfile,-1);
 4832:             $gottab = 1;
 4833:         }
 4834:     }
 4835:     if (!$gottab) {
 4836:         my @domains = &current_machine_domains();
 4837:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4838:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4839:                 @lines = <$fh>;
 4840:                 close($fh);
 4841:             }
 4842:         } else {
 4843:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4844:                 @lines = <$fh>;
 4845:                 close($fh);
 4846:             }
 4847:         }
 4848:     }
 4849:     return @lines;
 4850: }
 4851: 
 4852: sub removeuploadedurl {
 4853:     my ($url)=@_;	
 4854:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4855:     return &removeuserfile($uname,$udom,$fname);
 4856: }
 4857: 
 4858: sub removeuserfile {
 4859:     my ($docuname,$docudom,$fname)=@_;
 4860:     my $home=&homeserver($docuname,$docudom);    
 4861:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4862:     if ($result eq 'ok') {	
 4863:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4864:             my $metafile = $fname.'.meta';
 4865:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4866: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4867:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4868:             my $sqlresult = 
 4869:                 &update_portfolio_table($docuname,$docudom,$file,
 4870:                                         'portfolio_metadata',$group,
 4871:                                         'delete');
 4872:         }
 4873:     }
 4874:     return $result;
 4875: }
 4876: 
 4877: sub mkdiruserfile {
 4878:     my ($docuname,$docudom,$dir)=@_;
 4879:     my $home=&homeserver($docuname,$docudom);
 4880:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4881: }
 4882: 
 4883: sub renameuserfile {
 4884:     my ($docuname,$docudom,$old,$new)=@_;
 4885:     my $home=&homeserver($docuname,$docudom);
 4886:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4887:                         &escape("$old").':'.&escape("$new"),$home);
 4888:     if ($result eq 'ok') {
 4889:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4890:             my $oldmeta = $old.'.meta';
 4891:             my $newmeta = $new.'.meta';
 4892:             my $metaresult = 
 4893:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4894: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4895:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4896:             my $sqlresult = 
 4897:                 &update_portfolio_table($docuname,$docudom,$file,
 4898:                                         'portfolio_metadata',$group,
 4899:                                         'delete');
 4900:         }
 4901:     }
 4902:     return $result;
 4903: }
 4904: 
 4905: # ------------------------------------------------------------------------- Log
 4906: 
 4907: sub log {
 4908:     my ($dom,$nam,$hom,$what)=@_;
 4909:     return critical("log:$dom:$nam:$what",$hom);
 4910: }
 4911: 
 4912: # ------------------------------------------------------------------ Course Log
 4913: #
 4914: # This routine flushes several buffers of non-mission-critical nature
 4915: #
 4916: 
 4917: sub flushcourselogs {
 4918:     &logthis('Flushing log buffers');
 4919: #
 4920: # course logs
 4921: # This is a log of all transactions in a course, which can be used
 4922: # for data mining purposes
 4923: #
 4924: # It also collects the courseid database, which lists last transaction
 4925: # times and course titles for all courseids
 4926: #
 4927:     my %courseidbuffer=();
 4928:     foreach my $crsid (keys(%courselogs)) {
 4929:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4930: 		          &escape($courselogs{$crsid}),
 4931: 		          $coursehombuf{$crsid}) eq 'ok') {
 4932: 	    delete $courselogs{$crsid};
 4933:         } else {
 4934:             &logthis('Failed to flush log buffer for '.$crsid);
 4935:             if (length($courselogs{$crsid})>40000) {
 4936:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4937:                         " exceeded maximum size, deleting.</font>");
 4938:                delete $courselogs{$crsid};
 4939:             }
 4940:         }
 4941:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4942:             'description' => $coursedescrbuf{$crsid},
 4943:             'inst_code'    => $courseinstcodebuf{$crsid},
 4944:             'type'        => $coursetypebuf{$crsid},
 4945:             'owner'       => $courseownerbuf{$crsid},
 4946:         };
 4947:     }
 4948: #
 4949: # Write course id database (reverse lookup) to homeserver of courses 
 4950: # Is used in pickcourse
 4951: #
 4952:     foreach my $crs_home (keys(%courseidbuffer)) {
 4953:         my $response = &courseidput(&host_domain($crs_home),
 4954:                                     $courseidbuffer{$crs_home},
 4955:                                     $crs_home,'timeonly');
 4956:     }
 4957: #
 4958: # File accesses
 4959: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4960: #
 4961:     foreach my $entry (keys(%accesshash)) {
 4962:         if ($entry =~ /___count$/) {
 4963:             my ($dom,$name);
 4964:             ($dom,$name,undef)=
 4965: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4966:             if (! defined($dom) || $dom eq '' || 
 4967:                 ! defined($name) || $name eq '') {
 4968:                 my $cid = $env{'request.course.id'};
 4969:                 $dom  = $env{'request.'.$cid.'.domain'};
 4970:                 $name = $env{'request.'.$cid.'.num'};
 4971:             }
 4972:             my $value = $accesshash{$entry};
 4973:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4974:             my %temphash=($url => $value);
 4975:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4976:             if ($result eq 'ok') {
 4977:                 delete $accesshash{$entry};
 4978:             }
 4979:         } else {
 4980:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4981:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4982:             my %temphash=($entry => $accesshash{$entry});
 4983:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4984:                 delete $accesshash{$entry};
 4985:             }
 4986:         }
 4987:     }
 4988: #
 4989: # Roles
 4990: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4991: #
 4992:     foreach my $entry (keys(%userrolehash)) {
 4993:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4994: 	    split(/\:/,$entry);
 4995:         if (&Apache::lonnet::put('nohist_userroles',
 4996:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4997:                 $rudom,$runame) eq 'ok') {
 4998: 	    delete $userrolehash{$entry};
 4999:         }
 5000:     }
 5001: #
 5002: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 5003: #
 5004:     my %domrolebuffer = ();
 5005:     foreach my $entry (keys(%domainrolehash)) {
 5006:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 5007:         if ($domrolebuffer{$rudom}) {
 5008:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5009:                       '='.&escape($domainrolehash{$entry});
 5010:         } else {
 5011:             $domrolebuffer{$rudom}.=&escape($entry).
 5012:                       '='.&escape($domainrolehash{$entry});
 5013:         }
 5014:         delete $domainrolehash{$entry};
 5015:     }
 5016:     foreach my $dom (keys(%domrolebuffer)) {
 5017: 	my %servers;
 5018: 	if (defined(&domain($dom,'primary'))) {
 5019: 	    my $primary=&domain($dom,'primary');
 5020: 	    my $hostname=&hostname($primary);
 5021: 	    $servers{$primary} = $hostname;
 5022: 	} else { 
 5023: 	    %servers = &get_servers($dom,'library');
 5024: 	}
 5025: 	foreach my $tryserver (keys(%servers)) {
 5026: 	    if (&reply('domroleput:'.$dom.':'.
 5027: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5028: 		last;
 5029: 	    } else {  
 5030: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5031: 	    }
 5032:         }
 5033:     }
 5034:     $dumpcount++;
 5035: }
 5036: 
 5037: sub courselog {
 5038:     my $what=shift;
 5039:     $what=time.':'.$what;
 5040:     unless ($env{'request.course.id'}) { return ''; }
 5041:     $coursedombuf{$env{'request.course.id'}}=
 5042:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5043:     $coursenumbuf{$env{'request.course.id'}}=
 5044:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5045:     $coursehombuf{$env{'request.course.id'}}=
 5046:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5047:     $coursedescrbuf{$env{'request.course.id'}}=
 5048:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5049:     $courseinstcodebuf{$env{'request.course.id'}}=
 5050:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5051:     $courseownerbuf{$env{'request.course.id'}}=
 5052:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5053:     $coursetypebuf{$env{'request.course.id'}}=
 5054:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5055:     if (defined $courselogs{$env{'request.course.id'}}) {
 5056: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5057:     } else {
 5058: 	$courselogs{$env{'request.course.id'}}.=$what;
 5059:     }
 5060:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5061: 	&flushcourselogs();
 5062:     }
 5063: }
 5064: 
 5065: sub courseacclog {
 5066:     my $fnsymb=shift;
 5067:     unless ($env{'request.course.id'}) { return ''; }
 5068:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5069:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5070:         $what.=':POST';
 5071:         # FIXME: Probably ought to escape things....
 5072: 	foreach my $key (keys(%env)) {
 5073:             if ($key=~/^form\.(.*)/) {
 5074:                 my $formitem = $1;
 5075:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5076:                     $what.=':'.$formitem.'='.$env{$key};
 5077:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5078:                     if ($formitem eq 'proctorpassword') {
 5079:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5080:                     } else {
 5081:                         $what.=':'.$formitem.'='.$env{$key};
 5082:                     }
 5083:                 }
 5084:             }
 5085:         }
 5086:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5087:         # FIXME: We should not be depending on a form parameter that someone
 5088:         # editing lonsearchcat.pm might change in the future.
 5089:         if ($env{'form.phase'} eq 'course_search') {
 5090:             $what.= ':POST';
 5091:             # FIXME: Probably ought to escape things....
 5092:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5093:                                  'crsdiscuss') {
 5094:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5095:             }
 5096:         }
 5097:     }
 5098:     &courselog($what);
 5099: }
 5100: 
 5101: sub countacc {
 5102:     my $url=&declutter(shift);
 5103:     return if (! defined($url) || $url eq '');
 5104:     unless ($env{'request.course.id'}) { return ''; }
 5105: #
 5106: # Mark that this url was used in this course
 5107: #
 5108:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5109: #
 5110: # Increase the access count for this resource in this child process
 5111: #
 5112:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5113:     $accesshash{$key}++;
 5114: }
 5115: 
 5116: sub linklog {
 5117:     my ($from,$to)=@_;
 5118:     $from=&declutter($from);
 5119:     $to=&declutter($to);
 5120:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5121:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5122: }
 5123: 
 5124: sub statslog {
 5125:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5126:     if ($users<2) { return; }
 5127:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5128:             'course'       => $env{'request.course.id'},
 5129:             'sections'     => '"all"',
 5130:             'num_students' => $users,
 5131:             'part'         => $part,
 5132:             'symb'         => $symb,
 5133:             'mean_tries'   => $av_attempts,
 5134:             'deg_of_diff'  => $degdiff});
 5135:     foreach my $key (keys(%dynstore)) {
 5136:         $accesshash{$key}=$dynstore{$key};
 5137:     }
 5138: }
 5139:   
 5140: sub userrolelog {
 5141:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5142:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5143:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5144:        $userrolehash
 5145:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5146:                     =$tend.':'.$tstart;
 5147:     }
 5148:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5149:        $userrolehash
 5150:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5151:                     =$tend.':'.$tstart;
 5152:     }
 5153:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5154:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5155:        $domainrolehash
 5156:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5157:                     = $tend.':'.$tstart;
 5158:     }
 5159: }
 5160: 
 5161: sub courserolelog {
 5162:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5163:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5164:         my $cdom = $1;
 5165:         my $cnum = $2;
 5166:         my $sec = $3;
 5167:         my $namespace = 'rolelog';
 5168:         my %storehash = (
 5169:                            role    => $trole,
 5170:                            start   => $tstart,
 5171:                            end     => $tend,
 5172:                            selfenroll => $selfenroll,
 5173:                            context    => $context,
 5174:                         );
 5175:         if ($trole eq 'gr') {
 5176:             $namespace = 'groupslog';
 5177:             $storehash{'group'} = $sec;
 5178:         } else {
 5179:             $storehash{'section'} = $sec;
 5180:         }
 5181:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5182:                    $domain,$cnum,$cdom);
 5183:         if (($trole ne 'st') || ($sec ne '')) {
 5184:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5185:         }
 5186:     }
 5187:     return;
 5188: }
 5189: 
 5190: sub domainrolelog {
 5191:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5192:     if ($area =~ m{^/($match_domain)/$}) {
 5193:         my $cdom = $1;
 5194:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 5195:         my $namespace = 'rolelog';
 5196:         my %storehash = (
 5197:                            role    => $trole,
 5198:                            start   => $tstart,
 5199:                            end     => $tend,
 5200:                            context => $context,
 5201:                         );
 5202:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5203:                    $domain,$domconfiguser,$cdom);
 5204:     }
 5205:     return;
 5206: 
 5207: }
 5208: 
 5209: sub coauthorrolelog {
 5210:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5211:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5212:         my $audom = $1;
 5213:         my $auname = $2;
 5214:         my $namespace = 'rolelog';
 5215:         my %storehash = (
 5216:                            role    => $trole,
 5217:                            start   => $tstart,
 5218:                            end     => $tend,
 5219:                            context => $context,
 5220:                         );
 5221:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5222:                    $domain,$auname,$audom);
 5223:     }
 5224:     return;
 5225: }
 5226: 
 5227: sub get_course_adv_roles {
 5228:     my ($cid,$codes) = @_;
 5229:     $cid=$env{'request.course.id'} unless (defined($cid));
 5230:     my %coursehash=&coursedescription($cid);
 5231:     my $crstype = &Apache::loncommon::course_type($cid);
 5232:     my %nothide=();
 5233:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5234:         if ($user !~ /:/) {
 5235: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5236:         } else {
 5237:             $nothide{$user}=1;
 5238:         }
 5239:     }
 5240:     my @possdoms = ($coursehash{'domain'});
 5241:     if ($coursehash{'checkforpriv'}) {
 5242:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5243:     }
 5244:     my %returnhash=();
 5245:     my %dumphash=
 5246:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5247:     my $now=time;
 5248:     my %privileged;
 5249:     foreach my $entry (keys(%dumphash)) {
 5250: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5251:         if (($tstart) && ($tstart<0)) { next; }
 5252:         if (($tend) && ($tend<$now)) { next; }
 5253:         if (($tstart) && ($now<$tstart)) { next; }
 5254:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5255: 	if ($username eq '' || $domain eq '') { next; }
 5256:         if ((&privileged($username,$domain,\@possdoms)) &&
 5257:             (!$nothide{$username.':'.$domain})) { next; }
 5258: 	if ($role eq 'cr') { next; }
 5259:         if ($codes) {
 5260:             if ($section) { $role .= ':'.$section; }
 5261:             if ($returnhash{$role}) {
 5262:                 $returnhash{$role}.=','.$username.':'.$domain;
 5263:             } else {
 5264:                 $returnhash{$role}=$username.':'.$domain;
 5265:             }
 5266:         } else {
 5267:             my $key=&plaintext($role,$crstype);
 5268:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5269:             if ($returnhash{$key}) {
 5270: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5271:             } else {
 5272:                 $returnhash{$key}=$username.':'.$domain;
 5273:             }
 5274:         }
 5275:     }
 5276:     return %returnhash;
 5277: }
 5278: 
 5279: sub get_my_roles {
 5280:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5281:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5282:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5283:     my (%dumphash,%nothide);
 5284:     if ($context eq 'userroles') {
 5285:         %dumphash = &dump('roles',$udom,$uname);
 5286:     } else {
 5287:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5288:         if ($hidepriv) {
 5289:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5290:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5291:                 if ($user !~ /:/) {
 5292:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5293:                 } else {
 5294:                     $nothide{$user} = 1;
 5295:                 }
 5296:             }
 5297:         }
 5298:     }
 5299:     my %returnhash=();
 5300:     my $now=time;
 5301:     my %privileged;
 5302:     foreach my $entry (keys(%dumphash)) {
 5303:         my ($role,$tend,$tstart);
 5304:         if ($context eq 'userroles') {
 5305:             next if ($entry =~ /^rolesdef/);
 5306: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5307:         } else {
 5308:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5309:         }
 5310:         if (($tstart) && ($tstart<0)) { next; }
 5311:         my $status = 'active';
 5312:         if (($tend) && ($tend<=$now)) {
 5313:             $status = 'previous';
 5314:         } 
 5315:         if (($tstart) && ($now<$tstart)) {
 5316:             $status = 'future';
 5317:         }
 5318:         if (ref($types) eq 'ARRAY') {
 5319:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5320:                 next;
 5321:             } 
 5322:         } else {
 5323:             if ($status ne 'active') {
 5324:                 next;
 5325:             }
 5326:         }
 5327:         my ($rolecode,$username,$domain,$section,$area);
 5328:         if ($context eq 'userroles') {
 5329:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5330:             (undef,$domain,$username,$section) = split(/\//,$area);
 5331:         } else {
 5332:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5333:         }
 5334:         if (ref($roledoms) eq 'ARRAY') {
 5335:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5336:                 next;
 5337:             }
 5338:         }
 5339:         if (ref($roles) eq 'ARRAY') {
 5340:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5341:                 if ($role =~ /^cr\//) {
 5342:                     if (!grep(/^cr$/,@{$roles})) {
 5343:                         next;
 5344:                     }
 5345:                 } elsif ($role =~ /^gr\//) {
 5346:                     if (!grep(/^gr$/,@{$roles})) {
 5347:                         next;
 5348:                     }
 5349:                 } else {
 5350:                     next;
 5351:                 }
 5352:             }
 5353:         }
 5354:         if ($hidepriv) {
 5355:             my @privroles = ('dc','su');
 5356:             if ($context eq 'userroles') {
 5357:                 next if (grep(/^\Q$role\E$/,@privroles));
 5358:             } else {
 5359:                 my $possdoms = [$domain];
 5360:                 if (ref($roledoms) eq 'ARRAY') {
 5361:                    push(@{$possdoms},@{$roledoms}); 
 5362:                 }
 5363:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5364:                     if (!$nothide{$username.':'.$domain}) {
 5365:                         next;
 5366:                     }
 5367:                 }
 5368:             }
 5369:         }
 5370:         if ($withsec) {
 5371:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5372:                 $tstart.':'.$tend;
 5373:         } else {
 5374:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5375:         }
 5376:     }
 5377:     return %returnhash;
 5378: }
 5379: 
 5380: sub get_all_adhocroles {
 5381:     my ($dom) = @_;
 5382:     my @roles_by_num = ();
 5383:     my %domdefaults = &get_domain_defaults($dom);
 5384:     my (%description,%access_in_dom,%access_info);
 5385:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5386:         my $count = 0;
 5387:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5388:         my %ordered;
 5389:         foreach my $role (sort(keys(%domcurrent))) {
 5390:             my ($order,$desc,$access_in_dom);
 5391:             if (ref($domcurrent{$role}) eq 'HASH') {
 5392:                 $order = $domcurrent{$role}{'order'};
 5393:                 $desc = $domcurrent{$role}{'desc'};
 5394:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5395:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5396:             }
 5397:             if ($order eq '') {
 5398:                 $order = $count;
 5399:             }
 5400:             $ordered{$order} = $role;
 5401:             if ($desc ne '') {
 5402:                 $description{$role} = $desc;
 5403:             } else {
 5404:                 $description{$role}= $role;
 5405:             }
 5406:             $count++;
 5407:         }
 5408:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5409:             push(@roles_by_num,$ordered{$item});
 5410:         }
 5411:     }
 5412:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5413: }
 5414: 
 5415: sub get_my_adhocroles {
 5416:     my ($cid,$checkreg) = @_;
 5417:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5418:     if ($env{'request.course.id'} eq $cid) {
 5419:         $cdom = $env{'course.'.$cid.'.domain'};
 5420:         $cnum = $env{'course.'.$cid.'.num'};
 5421:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5422:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5423:         $cdom = $1;
 5424:         $cnum = $2;
 5425:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5426:                                      $cdom,$cnum);
 5427:     }
 5428:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5429:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5430:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5431:         if ($rosterhash{$user} ne '') {
 5432:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5433:             return ([],{}) if ($type eq 'auto');
 5434:         }
 5435:     }
 5436:     if (($cdom ne '') && ($cnum ne ''))  {
 5437:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5438:             my $then=$env{'user.login.time'};
 5439:             my $update=$env{'user.update.time'};
 5440:             if (!$update) {
 5441:                 $update = $then;
 5442:             }
 5443:             my @liveroles;
 5444:             foreach my $role ('dh','da') {
 5445:                 if ($env{"user.role.$role./$cdom/"}) {
 5446:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5447:                     my $limit = $update;
 5448:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5449:                         $limit = $then;
 5450:                     }
 5451:                     my $activerole = 1;
 5452:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5453:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5454:                     if ($activerole) {
 5455:                         push(@liveroles,$role);
 5456:                     }
 5457:                 }
 5458:             }
 5459:             if (@liveroles) {
 5460:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5461:                     my ($accessref,$accessinfo,%access_in_dom);
 5462:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5463:                     if (ref($roles_by_num) eq 'ARRAY') {
 5464:                         if (@{$roles_by_num}) {
 5465:                             my %settings;
 5466:                             if ($env{'request.course.id'} eq $cid) {
 5467:                                 foreach my $envkey (keys(%env)) {
 5468:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5469:                                         $settings{$1} = $env{$envkey};
 5470:                                     }
 5471:                                 }
 5472:                             } else {
 5473:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5474:                             }
 5475:                             my %setincrs;
 5476:                             if ($settings{'internal.adhocaccess'}) {
 5477:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5478:                             }
 5479:                             my @statuses;
 5480:                             if ($env{'environment.inststatus'}) {
 5481:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5482:                             }
 5483:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5484:                             if (ref($accessref) eq 'HASH') {
 5485:                                 %access_in_dom = %{$accessref};
 5486:                             }
 5487:                             foreach my $role (@{$roles_by_num}) {
 5488:                                 my ($curraccess,@okstatus,@personnel);
 5489:                                 if ($setincrs{$role}) {
 5490:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5491:                                     if ($curraccess eq 'status') {
 5492:                                         @okstatus = split(/\&/,$rest);
 5493:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5494:                                         @personnel = split(/\&/,$rest);
 5495:                                     }
 5496:                                 } else {
 5497:                                     $curraccess = $access_in_dom{$role};
 5498:                                     if (ref($accessinfo) eq 'HASH') {
 5499:                                         if ($curraccess eq 'status') {
 5500:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5501:                                                 @okstatus = @{$accessinfo->{$role}};
 5502:                                             }
 5503:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5504:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5505:                                                 @personnel = @{$accessinfo->{$role}};
 5506:                                             }
 5507:                                         }
 5508:                                     }
 5509:                                 }
 5510:                                 if ($curraccess eq 'none') {
 5511:                                     next;
 5512:                                 } elsif ($curraccess eq 'all') {
 5513:                                     push(@possroles,$role);
 5514:                                 } elsif ($curraccess eq 'dh') {
 5515:                                     if (grep(/^dh$/,@liveroles)) {
 5516:                                         push(@possroles,$role);
 5517:                                     } else {
 5518:                                         next;
 5519:                                     }
 5520:                                 } elsif ($curraccess eq 'da') {
 5521:                                     if (grep(/^da$/,@liveroles)) {
 5522:                                         push(@possroles,$role);
 5523:                                     } else {
 5524:                                         next;
 5525:                                     }
 5526:                                 } elsif ($curraccess eq 'status') {
 5527:                                     if (@okstatus) {
 5528:                                         if (!@statuses) {
 5529:                                             if (grep(/^default$/,@okstatus)) {
 5530:                                                 push(@possroles,$role);
 5531:                                             }
 5532:                                         } else {
 5533:                                             foreach my $status (@okstatus) {
 5534:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5535:                                                     push(@possroles,$role);
 5536:                                                     last;
 5537:                                                 }
 5538:                                             }
 5539:                                         }
 5540:                                     }
 5541:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5542:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5543:                                         if ($curraccess eq 'exc') {
 5544:                                             push(@possroles,$role);
 5545:                                         }
 5546:                                     } elsif ($curraccess eq 'inc') {
 5547:                                         push(@possroles,$role);
 5548:                                     }
 5549:                                 }
 5550:                             }
 5551:                         }
 5552:                     }
 5553:                 }
 5554:             }
 5555:         }
 5556:     }
 5557:     unless (ref($description) eq 'HASH') {
 5558:         if (ref($roles_by_num) eq 'ARRAY') {
 5559:             my %desc;
 5560:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5561:             $description = \%desc;
 5562:         } else {
 5563:             $description = {};
 5564:         }
 5565:     }
 5566:     return (\@possroles,$description);
 5567: }
 5568: 
 5569: # ----------------------------------------------------- Frontpage Announcements
 5570: #
 5571: #
 5572: 
 5573: sub postannounce {
 5574:     my ($server,$text)=@_;
 5575:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5576:     unless ($text=~/\w/) { $text=''; }
 5577:     return &reply('setannounce:'.&escape($text),$server);
 5578: }
 5579: 
 5580: sub getannounce {
 5581: 
 5582:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5583: 	my $announcement='';
 5584: 	while (my $line = <$fh>) { $announcement .= $line; }
 5585: 	close($fh);
 5586: 	if ($announcement=~/\w/) { 
 5587: 	    return 
 5588:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5589:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5590: 	} else {
 5591: 	    return '';
 5592: 	}
 5593:     } else {
 5594: 	return '';
 5595:     }
 5596: }
 5597: 
 5598: # ---------------------------------------------------------- Course ID routines
 5599: # Deal with domain's nohist_courseid.db files
 5600: #
 5601: 
 5602: sub courseidput {
 5603:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5604:     return unless (ref($storehash) eq 'HASH');
 5605:     my $outcome;
 5606:     if ($caller eq 'timeonly') {
 5607:         my $cids = '';
 5608:         foreach my $item (keys(%$storehash)) {
 5609:             $cids.=&escape($item).'&';
 5610:         }
 5611:         $cids=~s/\&$//;
 5612:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5613:                           $coursehome);       
 5614:     } else {
 5615:         my $items = '';
 5616:         foreach my $item (keys(%$storehash)) {
 5617:             $items.= &escape($item).'='.
 5618:                      &freeze_escape($$storehash{$item}).'&';
 5619:         }
 5620:         $items=~s/\&$//;
 5621:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5622:                           $coursehome);
 5623:     }
 5624:     if ($outcome eq 'unknown_cmd') {
 5625:         my $what;
 5626:         foreach my $cid (keys(%$storehash)) {
 5627:             $what .= &escape($cid).'=';
 5628:             foreach my $item ('description','inst_code','owner','type') {
 5629:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5630:             }
 5631:             $what =~ s/\:$/&/;
 5632:         }
 5633:         $what =~ s/\&$//;  
 5634:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5635:     } else {
 5636:         return $outcome;
 5637:     }
 5638: }
 5639: 
 5640: sub courseiddump {
 5641:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5642:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5643:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5644:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5645:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5646:     my $as_hash = 1;
 5647:     my %returnhash;
 5648:     if (!$domfilter) { $domfilter=''; }
 5649:     my %libserv = &all_library();
 5650:     foreach my $tryserver (keys(%libserv)) {
 5651:         if ( (  $hostidflag == 1 
 5652: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5653: 	     || (!defined($hostidflag)) ) {
 5654: 
 5655: 	    if (($domfilter eq '') ||
 5656: 		(&host_domain($tryserver) eq $domfilter)) {
 5657:                 my $rep;
 5658:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5659:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5660:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5661:                                 &escape($descfilter), &escape($instcodefilter), 
 5662:                                 &escape($ownerfilter), &escape($coursefilter),
 5663:                                 &escape($typefilter), &escape($regexp_ok), 
 5664:                                 $as_hash, &escape($selfenrollonly), 
 5665:                                 &escape($catfilter), $showhidden, $caller, 
 5666:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5667:                                 &escape($createdbefore), &escape($createdafter), 
 5668:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5669:                                 $reqcrsdom,&escape($reqinstcode))));
 5670:                 } else {
 5671:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5672:                              $sincefilter.':'.&escape($descfilter).':'.
 5673:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5674:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5675:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5676:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5677:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5678:                              &escape($cc_clone).':'.$cloneonly.':'.
 5679:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5680:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5681:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5682:                 }
 5683:                      
 5684:                 my @pairs=split(/\&/,$rep);
 5685:                 foreach my $item (@pairs) {
 5686:                     my ($key,$value)=split(/\=/,$item,2);
 5687:                     $key = &unescape($key);
 5688:                     next if ($key =~ /^error: 2 /);
 5689:                     my $result = &thaw_unescape($value);
 5690:                     if (ref($result) eq 'HASH') {
 5691:                         $returnhash{$key}=$result;
 5692:                     } else {
 5693:                         my @responses = split(/:/,$value);
 5694:                         my @items = ('description','inst_code','owner','type');
 5695:                         for (my $i=0; $i<@responses; $i++) {
 5696:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5697:                         }
 5698:                     }
 5699:                 }
 5700:             }
 5701:         }
 5702:     }
 5703:     return %returnhash;
 5704: }
 5705: 
 5706: sub courselastaccess {
 5707:     my ($cdom,$cnum,$hostidref) = @_;
 5708:     my %returnhash;
 5709:     if ($cdom && $cnum) {
 5710:         my $chome = &homeserver($cnum,$cdom);
 5711:         if ($chome ne 'no_host') {
 5712:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5713:             &extract_lastaccess(\%returnhash,$rep);
 5714:         }
 5715:     } else {
 5716:         if (!$cdom) { $cdom=''; }
 5717:         my %libserv = &all_library();
 5718:         foreach my $tryserver (keys(%libserv)) {
 5719:             if (ref($hostidref) eq 'ARRAY') {
 5720:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5721:             } 
 5722:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5723:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5724:                 &extract_lastaccess(\%returnhash,$rep);
 5725:             }
 5726:         }
 5727:     }
 5728:     return %returnhash;
 5729: }
 5730: 
 5731: sub extract_lastaccess {
 5732:     my ($returnhash,$rep) = @_;
 5733:     if (ref($returnhash) eq 'HASH') {
 5734:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5735:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5736:                  $rep eq '') {
 5737:             my @pairs=split(/\&/,$rep);
 5738:             foreach my $item (@pairs) {
 5739:                 my ($key,$value)=split(/\=/,$item,2);
 5740:                 $key = &unescape($key);
 5741:                 next if ($key =~ /^error: 2 /);
 5742:                 $returnhash->{$key} = &thaw_unescape($value);
 5743:             }
 5744:         }
 5745:     }
 5746:     return;
 5747: }
 5748: 
 5749: # ---------------------------------------------------------- DC e-mail
 5750: 
 5751: sub dcmailput {
 5752:     my ($domain,$msgid,$message,$server)=@_;
 5753:     my $status = &Apache::lonnet::critical(
 5754:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5755:        &escape($message),$server);
 5756:     return $status;
 5757: }
 5758: 
 5759: sub dcmaildump {
 5760:     my ($dom,$startdate,$enddate,$senders) = @_;
 5761:     my %returnhash=();
 5762: 
 5763:     if (defined(&domain($dom,'primary'))) {
 5764:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5765:                                                          &escape($enddate).':';
 5766: 	my @esc_senders=map { &escape($_)} @$senders;
 5767: 	$cmd.=&escape(join('&',@esc_senders));
 5768: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5769:             my ($key,$value) = split(/\=/,$line,2);
 5770:             if (($key) && ($value)) {
 5771:                 $returnhash{&unescape($key)} = &unescape($value);
 5772:             }
 5773:         }
 5774:     }
 5775:     return %returnhash;
 5776: }
 5777: # ---------------------------------------------------------- Domain roles
 5778: 
 5779: sub get_domain_roles {
 5780:     my ($dom,$roles,$startdate,$enddate)=@_;
 5781:     if ((!defined($startdate)) || ($startdate eq '')) {
 5782:         $startdate = '.';
 5783:     }
 5784:     if ((!defined($enddate)) || ($enddate eq '')) {
 5785:         $enddate = '.';
 5786:     }
 5787:     my $rolelist;
 5788:     if (ref($roles) eq 'ARRAY') {
 5789:         $rolelist = join('&',@{$roles});
 5790:     }
 5791:     my %personnel = ();
 5792: 
 5793:     my %servers = &get_servers($dom,'library');
 5794:     foreach my $tryserver (keys(%servers)) {
 5795: 	%{$personnel{$tryserver}}=();
 5796: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5797: 					    &escape($startdate).':'.
 5798: 					    &escape($enddate).':'.
 5799: 					    &escape($rolelist), $tryserver))) {
 5800: 	    my ($key,$value) = split(/\=/,$line,2);
 5801: 	    if (($key) && ($value)) {
 5802: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5803: 	    }
 5804: 	}
 5805:     }
 5806:     return %personnel;
 5807: }
 5808: 
 5809: sub get_active_domroles {
 5810:     my ($dom,$roles) = @_;
 5811:     return () unless (ref($roles) eq 'ARRAY');
 5812:     my $now = time;
 5813:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5814:     my %domroles;
 5815:     foreach my $server (keys(%dompersonnel)) {
 5816:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5817:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5818:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5819:         }
 5820:     }
 5821:     return %domroles;
 5822: }
 5823: 
 5824: # ----------------------------------------------------------- Interval timing 
 5825: 
 5826: {
 5827: # Caches needed for speedup of navmaps
 5828: # We don't want to cache this for very long at all (5 seconds at most)
 5829: # 
 5830: # The user for whom we cache
 5831: my $cachedkey='';
 5832: # The cached times for this user
 5833: my %cachedtimes=();
 5834: # When this was last done
 5835: my $cachedtime='';
 5836: 
 5837: sub load_all_first_access {
 5838:     my ($uname,$udom,$ignorecache)=@_;
 5839:     if (($cachedkey eq $uname.':'.$udom) &&
 5840:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5841:         (!$ignorecache)) {
 5842:         return;
 5843:     }
 5844:     $cachedtime=time;
 5845:     $cachedkey=$uname.':'.$udom;
 5846:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5847: }
 5848: 
 5849: sub get_first_access {
 5850:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5851:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5852:     if ($argsymb) { $symb=$argsymb; }
 5853:     my ($map,$id,$res)=&decode_symb($symb);
 5854:     if ($argmap) { $map = $argmap; }
 5855:     if ($type eq 'course') {
 5856: 	$res='course';
 5857:     } elsif ($type eq 'map') {
 5858: 	$res=&symbread($map);
 5859:     } else {
 5860: 	$res=$symb;
 5861:     }
 5862:     &load_all_first_access($uname,$udom,$ignorecache);
 5863:     return $cachedtimes{"$courseid\0$res"};
 5864: }
 5865: 
 5866: sub set_first_access {
 5867:     my ($type,$interval)=@_;
 5868:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5869:     my ($map,$id,$res)=&decode_symb($symb);
 5870:     if ($type eq 'course') {
 5871: 	$res='course';
 5872:     } elsif ($type eq 'map') {
 5873: 	$res=&symbread($map);
 5874:     } else {
 5875: 	$res=$symb;
 5876:     }
 5877:     $cachedkey='';
 5878:     my $firstaccess=&get_first_access($type,$symb,$map);
 5879:     if ($firstaccess) {
 5880:         &logthis("First access time already set ($firstaccess) when attempting ".
 5881:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5882:                  "in $courseid");
 5883:         return 'already_set';
 5884:     } else {
 5885:         my $start = time;
 5886: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5887:                           $udom,$uname);
 5888:         if ($putres eq 'ok') {
 5889:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5890:                  $udom,$uname); 
 5891:             &appenv(
 5892:                      {
 5893:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5894:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5895:                      }
 5896:                   );
 5897:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5898:                 $cachedtimes{"$courseid\0$res"} = $start;
 5899:             }
 5900:         } elsif ($putres ne 'refused') {
 5901:             &logthis("Result: $putres when attempting to set first access time ".
 5902:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5903:         }
 5904:         return $putres;
 5905:     }
 5906:     return 'already_set';
 5907: }
 5908: }
 5909: 
 5910: # --------------------------------------------- Set Expire Date for Spreadsheet
 5911: 
 5912: sub expirespread {
 5913:     my ($uname,$udom,$stype,$usymb)=@_;
 5914:     my $cid=$env{'request.course.id'}; 
 5915:     if ($cid) {
 5916:        my $now=time;
 5917:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5918:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5919:                             $env{'course.'.$cid.'.num'}.
 5920: 	        	    ':nohist_expirationdates:'.
 5921:                             &escape($key).'='.$now,
 5922:                             $env{'course.'.$cid.'.home'})
 5923:     }
 5924:     return 'ok';
 5925: }
 5926: 
 5927: # ----------------------------------------------------- Devalidate Spreadsheets
 5928: 
 5929: sub devalidate {
 5930:     my ($symb,$uname,$udom)=@_;
 5931:     my $cid=$env{'request.course.id'}; 
 5932:     if ($cid) {
 5933:         # delete the stored spreadsheets for
 5934:         # - the student level sheet of this user in course's homespace
 5935:         # - the assessment level sheet for this resource 
 5936:         #   for this user in user's homespace
 5937: 	# - current conditional state info
 5938: 	my $key=$uname.':'.$udom.':';
 5939:         my $status=
 5940: 	    &del('nohist_calculatedsheets',
 5941: 		 [$key.'studentcalc:'],
 5942: 		 $env{'course.'.$cid.'.domain'},
 5943: 		 $env{'course.'.$cid.'.num'})
 5944: 		.' '.
 5945: 	    &del('nohist_calculatedsheets_'.$cid,
 5946: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5947:         unless ($status eq 'ok ok') {
 5948:            &logthis('Could not devalidate spreadsheet '.
 5949:                     $uname.' at '.$udom.' for '.
 5950: 		    $symb.': '.$status);
 5951:         }
 5952: 	&delenv('user.state.'.$cid);
 5953:     }
 5954: }
 5955: 
 5956: sub get_scalar {
 5957:     my ($string,$end) = @_;
 5958:     my $value;
 5959:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5960: 	$value = $1;
 5961:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5962: 	$value = $1;
 5963:     }
 5964:     return &unescape($value);
 5965: }
 5966: 
 5967: sub array2str {
 5968:   my (@array) = @_;
 5969:   my $result=&arrayref2str(\@array);
 5970:   $result=~s/^__ARRAY_REF__//;
 5971:   $result=~s/__END_ARRAY_REF__$//;
 5972:   return $result;
 5973: }
 5974: 
 5975: sub arrayref2str {
 5976:   my ($arrayref) = @_;
 5977:   my $result='__ARRAY_REF__';
 5978:   foreach my $elem (@$arrayref) {
 5979:     if(ref($elem) eq 'ARRAY') {
 5980:       $result.=&arrayref2str($elem).'&';
 5981:     } elsif(ref($elem) eq 'HASH') {
 5982:       $result.=&hashref2str($elem).'&';
 5983:     } elsif(ref($elem)) {
 5984:       #print("Got a ref of ".(ref($elem))." skipping.");
 5985:     } else {
 5986:       $result.=&escape($elem).'&';
 5987:     }
 5988:   }
 5989:   $result=~s/\&$//;
 5990:   $result .= '__END_ARRAY_REF__';
 5991:   return $result;
 5992: }
 5993: 
 5994: sub hash2str {
 5995:   my (%hash) = @_;
 5996:   my $result=&hashref2str(\%hash);
 5997:   $result=~s/^__HASH_REF__//;
 5998:   $result=~s/__END_HASH_REF__$//;
 5999:   return $result;
 6000: }
 6001: 
 6002: sub hashref2str {
 6003:   my ($hashref)=@_;
 6004:   my $result='__HASH_REF__';
 6005:   foreach my $key (sort(keys(%$hashref))) {
 6006:     if (ref($key) eq 'ARRAY') {
 6007:       $result.=&arrayref2str($key).'=';
 6008:     } elsif (ref($key) eq 'HASH') {
 6009:       $result.=&hashref2str($key).'=';
 6010:     } elsif (ref($key)) {
 6011:       $result.='=';
 6012:       #print("Got a ref of ".(ref($key))." skipping.");
 6013:     } else {
 6014: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6015:     }
 6016: 
 6017:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6018:       $result.=&arrayref2str($hashref->{$key}).'&';
 6019:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6020:       $result.=&hashref2str($hashref->{$key}).'&';
 6021:     } elsif(ref($hashref->{$key})) {
 6022:        $result.='&';
 6023:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6024:     } else {
 6025:       $result.=&escape($hashref->{$key}).'&';
 6026:     }
 6027:   }
 6028:   $result=~s/\&$//;
 6029:   $result .= '__END_HASH_REF__';
 6030:   return $result;
 6031: }
 6032: 
 6033: sub str2hash {
 6034:     my ($string)=@_;
 6035:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6036:     return %$hash;
 6037: }
 6038: 
 6039: sub str2hashref {
 6040:   my ($string) = @_;
 6041: 
 6042:   my %hash;
 6043: 
 6044:   if($string !~ /^__HASH_REF__/) {
 6045:       if (! ($string eq '' || !defined($string))) {
 6046: 	  $hash{'error'}='Not hash reference';
 6047:       }
 6048:       return (\%hash, $string);
 6049:   }
 6050: 
 6051:   $string =~ s/^__HASH_REF__//;
 6052: 
 6053:   while($string !~ /^__END_HASH_REF__/) {
 6054:       #key
 6055:       my $key='';
 6056:       if($string =~ /^__HASH_REF__/) {
 6057:           ($key, $string)=&str2hashref($string);
 6058:           if(defined($key->{'error'})) {
 6059:               $hash{'error'}='Bad data';
 6060:               return (\%hash, $string);
 6061:           }
 6062:       } elsif($string =~ /^__ARRAY_REF__/) {
 6063:           ($key, $string)=&str2arrayref($string);
 6064:           if($key->[0] eq 'Array reference error') {
 6065:               $hash{'error'}='Bad data';
 6066:               return (\%hash, $string);
 6067:           }
 6068:       } else {
 6069:           $string =~ s/^(.*?)=//;
 6070: 	  $key=&unescape($1);
 6071:       }
 6072:       $string =~ s/^=//;
 6073: 
 6074:       #value
 6075:       my $value='';
 6076:       if($string =~ /^__HASH_REF__/) {
 6077:           ($value, $string)=&str2hashref($string);
 6078:           if(defined($value->{'error'})) {
 6079:               $hash{'error'}='Bad data';
 6080:               return (\%hash, $string);
 6081:           }
 6082:       } elsif($string =~ /^__ARRAY_REF__/) {
 6083:           ($value, $string)=&str2arrayref($string);
 6084:           if($value->[0] eq 'Array reference error') {
 6085:               $hash{'error'}='Bad data';
 6086:               return (\%hash, $string);
 6087:           }
 6088:       } else {
 6089: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6090:       }
 6091:       $string =~ s/^&//;
 6092: 
 6093:       $hash{$key}=$value;
 6094:   }
 6095: 
 6096:   $string =~ s/^__END_HASH_REF__//;
 6097: 
 6098:   return (\%hash, $string);
 6099: }
 6100: 
 6101: sub str2array {
 6102:     my ($string)=@_;
 6103:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6104:     return @$array;
 6105: }
 6106: 
 6107: sub str2arrayref {
 6108:   my ($string) = @_;
 6109:   my @array;
 6110: 
 6111:   if($string !~ /^__ARRAY_REF__/) {
 6112:       if (! ($string eq '' || !defined($string))) {
 6113: 	  $array[0]='Array reference error';
 6114:       }
 6115:       return (\@array, $string);
 6116:   }
 6117: 
 6118:   $string =~ s/^__ARRAY_REF__//;
 6119: 
 6120:   while($string !~ /^__END_ARRAY_REF__/) {
 6121:       my $value='';
 6122:       if($string =~ /^__HASH_REF__/) {
 6123:           ($value, $string)=&str2hashref($string);
 6124:           if(defined($value->{'error'})) {
 6125:               $array[0] ='Array reference error';
 6126:               return (\@array, $string);
 6127:           }
 6128:       } elsif($string =~ /^__ARRAY_REF__/) {
 6129:           ($value, $string)=&str2arrayref($string);
 6130:           if($value->[0] eq 'Array reference error') {
 6131:               $array[0] ='Array reference error';
 6132:               return (\@array, $string);
 6133:           }
 6134:       } else {
 6135: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6136:       }
 6137:       $string =~ s/^&//;
 6138: 
 6139:       push(@array, $value);
 6140:   }
 6141: 
 6142:   $string =~ s/^__END_ARRAY_REF__//;
 6143: 
 6144:   return (\@array, $string);
 6145: }
 6146: 
 6147: # -------------------------------------------------------------------Temp Store
 6148: 
 6149: sub tmpreset {
 6150:   my ($symb,$namespace,$domain,$stuname) = @_;
 6151:   if (!$symb) {
 6152:     $symb=&symbread();
 6153:     if (!$symb) { $symb= $env{'request.url'}; }
 6154:   }
 6155:   $symb=escape($symb);
 6156: 
 6157:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6158:   $namespace=~s/\//\_/g;
 6159:   $namespace=~s/\W//g;
 6160: 
 6161:   if (!$domain) { $domain=$env{'user.domain'}; }
 6162:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6163:   if ($domain eq 'public' && $stuname eq 'public') {
 6164:       $stuname=&get_requestor_ip();
 6165:   }
 6166:   my $path=LONCAPA::tempdir();
 6167:   my %hash;
 6168:   if (tie(%hash,'GDBM_File',
 6169: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6170: 	  &GDBM_WRCREAT(),0640)) {
 6171:     foreach my $key (keys(%hash)) {
 6172:       if ($key=~ /:$symb/) {
 6173: 	delete($hash{$key});
 6174:       }
 6175:     }
 6176:   }
 6177: }
 6178: 
 6179: sub tmpstore {
 6180:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6181: 
 6182:   if (!$symb) {
 6183:     $symb=&symbread();
 6184:     if (!$symb) { $symb= $env{'request.url'}; }
 6185:   }
 6186:   $symb=escape($symb);
 6187: 
 6188:   if (!$namespace) {
 6189:     # I don't think we would ever want to store this for a course.
 6190:     # it seems this will only be used if we don't have a course.
 6191:     #$namespace=$env{'request.course.id'};
 6192:     #if (!$namespace) {
 6193:       $namespace=$env{'request.state'};
 6194:     #}
 6195:   }
 6196:   $namespace=~s/\//\_/g;
 6197:   $namespace=~s/\W//g;
 6198:   if (!$domain) { $domain=$env{'user.domain'}; }
 6199:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6200:   if ($domain eq 'public' && $stuname eq 'public') {
 6201:       $stuname=&get_requestor_ip();
 6202:   }
 6203:   my $now=time;
 6204:   my %hash;
 6205:   my $path=LONCAPA::tempdir();
 6206:   if (tie(%hash,'GDBM_File',
 6207: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6208: 	  &GDBM_WRCREAT(),0640)) {
 6209:     $hash{"version:$symb"}++;
 6210:     my $version=$hash{"version:$symb"};
 6211:     my $allkeys=''; 
 6212:     foreach my $key (keys(%$storehash)) {
 6213:       $allkeys.=$key.':';
 6214:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6215:     }
 6216:     $hash{"$version:$symb:timestamp"}=$now;
 6217:     $allkeys.='timestamp';
 6218:     $hash{"$version:keys:$symb"}=$allkeys;
 6219:     if (untie(%hash)) {
 6220:       return 'ok';
 6221:     } else {
 6222:       return "error:$!";
 6223:     }
 6224:   } else {
 6225:     return "error:$!";
 6226:   }
 6227: }
 6228: 
 6229: # -----------------------------------------------------------------Temp Restore
 6230: 
 6231: sub tmprestore {
 6232:   my ($symb,$namespace,$domain,$stuname) = @_;
 6233: 
 6234:   if (!$symb) {
 6235:     $symb=&symbread();
 6236:     if (!$symb) { $symb= $env{'request.url'}; }
 6237:   }
 6238:   $symb=escape($symb);
 6239: 
 6240:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6241: 
 6242:   if (!$domain) { $domain=$env{'user.domain'}; }
 6243:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6244:   if ($domain eq 'public' && $stuname eq 'public') {
 6245:       $stuname=&get_requestor_ip();
 6246:   }
 6247:   my %returnhash;
 6248:   $namespace=~s/\//\_/g;
 6249:   $namespace=~s/\W//g;
 6250:   my %hash;
 6251:   my $path=LONCAPA::tempdir();
 6252:   if (tie(%hash,'GDBM_File',
 6253: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6254: 	  &GDBM_READER(),0640)) {
 6255:     my $version=$hash{"version:$symb"};
 6256:     $returnhash{'version'}=$version;
 6257:     my $scope;
 6258:     for ($scope=1;$scope<=$version;$scope++) {
 6259:       my $vkeys=$hash{"$scope:keys:$symb"};
 6260:       my @keys=split(/:/,$vkeys);
 6261:       my $key;
 6262:       $returnhash{"$scope:keys"}=$vkeys;
 6263:       foreach $key (@keys) {
 6264: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6265: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6266:       }
 6267:     }
 6268:     if (!(untie(%hash))) {
 6269:       return "error:$!";
 6270:     }
 6271:   } else {
 6272:     return "error:$!";
 6273:   }
 6274:   return %returnhash;
 6275: }
 6276: 
 6277: # ----------------------------------------------------------------------- Store
 6278: 
 6279: sub store {
 6280:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6281:     my $home='';
 6282: 
 6283:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6284: 
 6285:     $symb=&symbclean($symb);
 6286:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6287: 
 6288:     if (!$domain) { $domain=$env{'user.domain'}; }
 6289:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6290: 
 6291:     &devalidate($symb,$stuname,$domain);
 6292: 
 6293:     $symb=escape($symb);
 6294:     if (!$namespace) { 
 6295:        unless ($namespace=$env{'request.course.id'}) { 
 6296:           return ''; 
 6297:        } 
 6298:     }
 6299:     if (!$home) { $home=$env{'user.home'}; }
 6300: 
 6301:     $$storehash{'ip'}=&get_requestor_ip();
 6302:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6303: 
 6304:     my $namevalue='';
 6305:     foreach my $key (keys(%$storehash)) {
 6306:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6307:     }
 6308:     $namevalue=~s/\&$//;
 6309:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6310:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6311: }
 6312: 
 6313: # -------------------------------------------------------------- Critical Store
 6314: 
 6315: sub cstore {
 6316:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6317:     my $home='';
 6318: 
 6319:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6320: 
 6321:     $symb=&symbclean($symb);
 6322:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6323: 
 6324:     if (!$domain) { $domain=$env{'user.domain'}; }
 6325:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6326: 
 6327:     &devalidate($symb,$stuname,$domain);
 6328: 
 6329:     $symb=escape($symb);
 6330:     if (!$namespace) { 
 6331:        unless ($namespace=$env{'request.course.id'}) { 
 6332:           return ''; 
 6333:        } 
 6334:     }
 6335:     if (!$home) { $home=$env{'user.home'}; }
 6336: 
 6337:     $$storehash{'ip'}=&get_requestor_ip();
 6338:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6339: 
 6340:     my $namevalue='';
 6341:     foreach my $key (keys(%$storehash)) {
 6342:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6343:     }
 6344:     $namevalue=~s/\&$//;
 6345:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6346:     return critical
 6347:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6348: }
 6349: 
 6350: # --------------------------------------------------------------------- Restore
 6351: 
 6352: sub restore {
 6353:     my ($symb,$namespace,$domain,$stuname) = @_;
 6354:     my $home='';
 6355: 
 6356:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6357: 
 6358:     if (!$symb) {
 6359:         return if ($namespace eq 'courserequests');
 6360:         unless ($symb=escape(&symbread())) { return ''; }
 6361:     } else {
 6362:         unless ($namespace eq 'courserequests') {
 6363:             $symb=&escape(&symbclean($symb));
 6364:         }
 6365:     }
 6366:     if (!$namespace) { 
 6367:        unless ($namespace=$env{'request.course.id'}) { 
 6368:           return ''; 
 6369:        } 
 6370:     }
 6371:     if (!$domain) { $domain=$env{'user.domain'}; }
 6372:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6373:     if (!$home) { $home=$env{'user.home'}; }
 6374:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6375: 
 6376:     my %returnhash=();
 6377:     foreach my $line (split(/\&/,$answer)) {
 6378: 	my ($name,$value)=split(/\=/,$line);
 6379:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6380:     }
 6381:     my $version;
 6382:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6383:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6384:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6385:        }
 6386:     }
 6387:     return %returnhash;
 6388: }
 6389: 
 6390: # ---------------------------------------------------------- Course Description
 6391: #
 6392: #  
 6393: 
 6394: sub coursedescription {
 6395:     my ($courseid,$args)=@_;
 6396:     $courseid=~s/^\///;
 6397:     $courseid=~s/\_/\//g;
 6398:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6399:     my $chome=&homeserver($cnum,$cdomain);
 6400:     my $normalid=$cdomain.'_'.$cnum;
 6401:     # need to always cache even if we get errors otherwise we keep 
 6402:     # trying and trying and trying to get the course description.
 6403:     my %envhash=();
 6404:     my %returnhash=();
 6405:     
 6406:     my $expiretime=600;
 6407:     if ($env{'request.course.id'} eq $normalid) {
 6408: 	$expiretime=120;
 6409:     }
 6410: 
 6411:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6412:     if (!$args->{'freshen_cache'}
 6413: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6414: 	foreach my $key (keys(%env)) {
 6415: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6416: 	    my ($setting) = $1;
 6417: 	    $returnhash{$setting} = $env{$key};
 6418: 	}
 6419: 	return %returnhash;
 6420:     }
 6421: 
 6422:     # get the data again
 6423: 
 6424:     if (!$args->{'one_time'}) {
 6425: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6426:     }
 6427: 
 6428:     if ($chome ne 'no_host') {
 6429:        %returnhash=&dump('environment',$cdomain,$cnum);
 6430:        if (!exists($returnhash{'con_lost'})) {
 6431: 	   my $username = $env{'user.name'}; # Defult username
 6432: 	   if(defined $args->{'user'}) {
 6433: 	       $username = $args->{'user'};
 6434: 	   }
 6435:            $returnhash{'home'}= $chome;
 6436: 	   $returnhash{'domain'} = $cdomain;
 6437: 	   $returnhash{'num'} = $cnum;
 6438:            if (!defined($returnhash{'type'})) {
 6439:                $returnhash{'type'} = 'Course';
 6440:            }
 6441:            while (my ($name,$value) = each %returnhash) {
 6442:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6443:            }
 6444:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6445:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6446: 	       $username.'_'.$cdomain.'_'.$cnum;
 6447:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6448:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6449:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6450:        }
 6451:     }
 6452:     if (!$args->{'one_time'}) {
 6453: 	&appenv(\%envhash);
 6454:     }
 6455:     return %returnhash;
 6456: }
 6457: 
 6458: sub update_released_required {
 6459:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6460:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6461:         $cid = $env{'request.course.id'};
 6462:         $cdom = $env{'course.'.$cid.'.domain'};
 6463:         $cnum = $env{'course.'.$cid.'.num'};
 6464:         $chome = $env{'course.'.$cid.'.home'};
 6465:     }
 6466:     if ($needsrelease) {
 6467:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6468:         my $needsupdate;
 6469:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6470:             $needsupdate = 1;
 6471:         } else {
 6472:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6473:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6474:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6475:                 $needsupdate = 1;
 6476:             }
 6477:         }
 6478:         if ($needsupdate) {
 6479:             my %needshash = (
 6480:                              'internal.releaserequired' => $needsrelease,
 6481:                             );
 6482:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6483:             if ($putresult eq 'ok') {
 6484:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6485:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6486:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6487:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6488:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6489:                 }
 6490:             }
 6491:         }
 6492:     }
 6493:     return;
 6494: }
 6495: 
 6496: # -------------------------------------------------See if a user is privileged
 6497: 
 6498: sub privileged {
 6499:     my ($username,$domain,$possdomains,$possroles)=@_;
 6500:     my $now = time;
 6501:     my $roles;
 6502:     if (ref($possroles) eq 'ARRAY') {
 6503:         $roles = $possroles; 
 6504:     } else {
 6505:         $roles = ['dc','su'];
 6506:     }
 6507:     if (ref($possdomains) eq 'ARRAY') {
 6508:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6509:         foreach my $dom (@{$possdomains}) {
 6510:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6511:                 (ref($privileged{$dom}) eq 'HASH')) {
 6512:                 foreach my $role (@{$roles}) {
 6513:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6514:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6515:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6516:                             return 1 unless (($end && $end < $now) ||
 6517:                                              ($start && $start > $now));
 6518:                         }
 6519:                     }
 6520:                 }
 6521:             }
 6522:         }
 6523:     } else {
 6524:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6525:         my $now = time;
 6526: 
 6527:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6528:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6529:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6530:                 return 1 unless ($tend && $tend < $now) 
 6531:                         or ($tstart && $tstart > $now);
 6532:             }
 6533:         }
 6534:     }
 6535:     return 0;
 6536: }
 6537: 
 6538: sub privileged_by_domain {
 6539:     my ($domains,$roles) = @_;
 6540:     my %privileged = ();
 6541:     my $cachetime = 60*60*24;
 6542:     my $now = time;
 6543:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6544:         return %privileged;
 6545:     }
 6546:     foreach my $dom (@{$domains}) {
 6547:         next if (ref($privileged{$dom}) eq 'HASH');
 6548:         my $needroles;
 6549:         foreach my $role (@{$roles}) {
 6550:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6551:             if (defined($cached)) {
 6552:                 if (ref($result) eq 'HASH') {
 6553:                     $privileged{$dom}{$role} = $result;
 6554:                 }
 6555:             } else {
 6556:                 $needroles = 1;
 6557:             }
 6558:         }
 6559:         if ($needroles) {
 6560:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6561:             $privileged{$dom} = {};
 6562:             foreach my $server (keys(%dompersonnel)) {
 6563:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6564:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6565:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6566:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6567:                         next if ($end && $end < $now);
 6568:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6569:                             $dompersonnel{$server}{$item};
 6570:                     }
 6571:                 }
 6572:             }
 6573:             if (ref($privileged{$dom}) eq 'HASH') {
 6574:                 foreach my $role (@{$roles}) {
 6575:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6576:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6577:                     } else {
 6578:                         my %hash = ();
 6579:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6580:                     }
 6581:                 }
 6582:             }
 6583:         }
 6584:     }
 6585:     return %privileged;
 6586: }
 6587: 
 6588: # -------------------------------------------------------- Get user privileges
 6589: 
 6590: sub rolesinit {
 6591:     my ($domain, $username) = @_;
 6592:     my %userroles = ('user.login.time' => time);
 6593:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6594: 
 6595:     # firstaccess and timerinterval are related to timed maps/resources. 
 6596:     # also, blocking can be triggered by an activating timer
 6597:     # it's saved in the user's %env.
 6598:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6599:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6600:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6601:         %timerintchk, %timerintenv);
 6602: 
 6603:     foreach my $key (keys(%firstaccess)) {
 6604:         my ($cid, $rest) = split(/\0/, $key);
 6605:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6606:     }
 6607: 
 6608:     foreach my $key (keys(%timerinterval)) {
 6609:         my ($cid,$rest) = split(/\0/,$key);
 6610:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6611:     }
 6612: 
 6613:     my %allroles=();
 6614:     my %allgroups=();
 6615: 
 6616:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6617:         my $role = $rolesdump{$area};
 6618:         $area =~ s/\_\w\w$//;
 6619: 
 6620:         my ($trole, $tend, $tstart, $group_privs);
 6621: 
 6622:         if ($role =~ /^cr/) {
 6623:         # Custom role, defined by a user 
 6624:         # e.g., user.role.cr/msu/smith/mynewrole
 6625:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6626:                 $trole = $1;
 6627:                 ($tend, $tstart) = split('_', $2);
 6628:             } else {
 6629:                 $trole = $role;
 6630:             }
 6631:         } elsif ($role =~ m|^gr/|) {
 6632:         # Role of member in a group, defined within a course/community
 6633:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6634:             ($trole, $tend, $tstart) = split(/_/, $role);
 6635:             next if $tstart eq '-1';
 6636:             ($trole, $group_privs) = split(/\//, $trole);
 6637:             $group_privs = &unescape($group_privs);
 6638:         } else {
 6639:         # Just a normal role, defined in roles.tab
 6640:             ($trole, $tend, $tstart) = split(/_/,$role);
 6641:         }
 6642: 
 6643:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6644:                  $username);
 6645:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6646: 
 6647:         # role expired or not available yet?
 6648:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6649:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6650: 
 6651:         next if $area eq '' or $trole eq '';
 6652: 
 6653:         my $spec = "$trole.$area";
 6654:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6655: 
 6656:         if ($trole =~ /^cr\//) {
 6657:         # Custom role, defined by a user
 6658:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6659:         } elsif ($trole eq 'gr') {
 6660:         # Role of a member in a group, defined within a course/community
 6661:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6662:             next;
 6663:         } else {
 6664:         # Normal role, defined in roles.tab
 6665:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6666:         }
 6667: 
 6668:         my $cid = $tdomain.'_'.$trest;
 6669:         unless ($firstaccchk{$cid}) {
 6670:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6671:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6672:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6673:                         $coursetimerstarts{$cid}{$item}; 
 6674:                 }
 6675:             }
 6676:             $firstaccchk{$cid} = 1;
 6677:         }
 6678:         unless ($timerintchk{$cid}) {
 6679:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6680:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6681:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6682:                        $coursetimerintervals{$cid}{$item};
 6683:                 }
 6684:             }
 6685:             $timerintchk{$cid} = 1;
 6686:         }
 6687:     }
 6688: 
 6689:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6690:                                                           \%allroles, \%allgroups);
 6691:     $env{'user.adv'} = $userroles{'user.adv'};
 6692:     $env{'user.rar'} = $userroles{'user.rar'};
 6693: 
 6694:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6695: }
 6696: 
 6697: sub set_arearole {
 6698:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6699:     unless ($nolog) {
 6700: # log the associated role with the area
 6701:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6702:     }
 6703:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6704: }
 6705: 
 6706: sub custom_roleprivs {
 6707:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6708:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6709:     my $homsvr = &homeserver($rauthor,$rdomain);
 6710:     if (&hostname($homsvr) ne '') {
 6711:         my ($rdummy,$roledef)=
 6712:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6713:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6714:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6715:             if (defined($syspriv)) {
 6716:                 if ($trest =~ /^$match_community$/) {
 6717:                     $syspriv =~ s/bre\&S//; 
 6718:                 }
 6719:                 $$allroles{'cm./'}.=':'.$syspriv;
 6720:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6721:             }
 6722:             if ($tdomain ne '') {
 6723:                 if (defined($dompriv)) {
 6724:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6725:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6726:                 }
 6727:                 if (($trest ne '') && (defined($coursepriv))) {
 6728:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6729:                         my $rolename = $1;
 6730:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6731:                     }
 6732:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6733:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6734:                 }
 6735:             }
 6736:         }
 6737:     }
 6738: }
 6739: 
 6740: sub course_adhocrole_privs {
 6741:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6742:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6743:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6744:         my (%currprivs,%storeprivs);
 6745:         foreach my $item (split(/:/,$coursepriv)) {
 6746:             my ($priv,$restrict) = split(/\&/,$item);
 6747:             $currprivs{$priv} = $restrict;
 6748:         }
 6749:         my (%possadd,%possremove,%full);
 6750:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6751:             my ($priv,$restrict)=split(/\&/,$item);
 6752:             $full{$priv} = $restrict;
 6753:         }
 6754:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6755:              next if ($item eq '');
 6756:              my ($rule,$rest) = split(/=/,$item);
 6757:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6758:              foreach my $priv (split(/:/,$rest)) {
 6759:                  if ($priv ne '') {
 6760:                      if ($rule eq 'off') {
 6761:                          $possremove{$priv} = 1;
 6762:                      } else {
 6763:                          $possadd{$priv} = 1;
 6764:                      }
 6765:                  }
 6766:              }
 6767:          }
 6768:          foreach my $priv (sort(keys(%full))) {
 6769:              if (exists($currprivs{$priv})) {
 6770:                  unless (exists($possremove{$priv})) {
 6771:                      $storeprivs{$priv} = $currprivs{$priv};
 6772:                  }
 6773:              } elsif (exists($possadd{$priv})) {
 6774:                  $storeprivs{$priv} = $full{$priv};
 6775:              }
 6776:          }
 6777:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6778:      }
 6779:      return $coursepriv;
 6780: }
 6781: 
 6782: sub group_roleprivs {
 6783:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6784:     my $access = 1;
 6785:     my $now = time;
 6786:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6787:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6788:     if ($access) {
 6789:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6790:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6791:     }
 6792: }
 6793: 
 6794: sub standard_roleprivs {
 6795:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6796:     if (defined($pr{$trole.':s'})) {
 6797:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6798:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6799:     }
 6800:     if ($tdomain ne '') {
 6801:         if (defined($pr{$trole.':d'})) {
 6802:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6803:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6804:         }
 6805:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6806:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6807:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6808:         }
 6809:     }
 6810: }
 6811: 
 6812: sub set_userprivs {
 6813:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6814:     my $author=0;
 6815:     my $adv=0;
 6816:     my $rar=0;
 6817:     my %grouproles = ();
 6818:     if (keys(%{$allgroups}) > 0) {
 6819:         my @groupkeys; 
 6820:         foreach my $role (keys(%{$allroles})) {
 6821:             push(@groupkeys,$role);
 6822:         }
 6823:         if (ref($groups_roles) eq 'HASH') {
 6824:             foreach my $key (keys(%{$groups_roles})) {
 6825:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6826:                     push(@groupkeys,$key);
 6827:                 }
 6828:             }
 6829:         }
 6830:         if (@groupkeys > 0) {
 6831:             foreach my $role (@groupkeys) {
 6832:                 my ($trole,$area,$sec,$extendedarea);
 6833:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6834:                     $trole = $1;
 6835:                     $area = $2;
 6836:                     $sec = $3;
 6837:                     $extendedarea = $area.$sec;
 6838:                     if (exists($$allgroups{$area})) {
 6839:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6840:                             my $spec = $trole.'.'.$extendedarea;
 6841:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6842:                                                 $$allgroups{$area}{$group};
 6843:                         }
 6844:                     }
 6845:                 }
 6846:             }
 6847:         }
 6848:     }
 6849:     foreach my $group (keys(%grouproles)) {
 6850:         $$allroles{$group} = $grouproles{$group};
 6851:     }
 6852:     foreach my $role (keys(%{$allroles})) {
 6853:         my %thesepriv;
 6854:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6855:         foreach my $item (split(/:/,$$allroles{$role})) {
 6856:             if ($item ne '') {
 6857:                 my ($privilege,$restrictions)=split(/&/,$item);
 6858:                 if ($restrictions eq '') {
 6859:                     $thesepriv{$privilege}='F';
 6860:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6861:                     $thesepriv{$privilege}.=$restrictions;
 6862:                 }
 6863:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6864:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6865:             }
 6866:         }
 6867:         my $thesestr='';
 6868:         foreach my $priv (sort(keys(%thesepriv))) {
 6869: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6870: 	}
 6871:         $userroles->{'user.priv.'.$role} = $thesestr;
 6872:     }
 6873:     return ($author,$adv,$rar);
 6874: }
 6875: 
 6876: sub role_status {
 6877:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6878:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6879:         my ($one,$two) = split(m{\./},$rolekey,2);
 6880:         (undef,undef,$$role) = split(/\./,$one,3);
 6881:         unless (!defined($$role) || $$role eq '') {
 6882:             $$where = '/'.$two;
 6883:             $$trolecode=$$role.'.'.$$where;
 6884:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6885:             $$tstatus='is';
 6886:             if ($$tstart && $$tstart>$update) {
 6887:                 $$tstatus='future';
 6888:                 if ($$tstart<$now) {
 6889:                     if ($$tstart && $$tstart>$refresh) {
 6890:                         if (($$where ne '') && ($$role ne '')) {
 6891:                             my (%allroles,%allgroups,$group_privs,
 6892:                                 %groups_roles,@rolecodes);
 6893:                             my %userroles = (
 6894:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6895:                             );
 6896:                             @rolecodes = ('cm'); 
 6897:                             my $spec=$$role.'.'.$$where;
 6898:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6899:                             if ($$role =~ /^cr\//) {
 6900:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6901:                                 push(@rolecodes,'cr');
 6902:                             } elsif ($$role eq 'gr') {
 6903:                                 push(@rolecodes,$$role);
 6904:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6905:                                                     $env{'user.name'});
 6906:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6907:                                 (undef,my $group_privs) = split(/\//,$trole);
 6908:                                 $group_privs = &unescape($group_privs);
 6909:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6910:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6911:                                 &get_groups_roles($tdomain,$trest,
 6912:                                                   \%course_roles,\@rolecodes,
 6913:                                                   \%groups_roles);
 6914:                             } else {
 6915:                                 push(@rolecodes,$$role);
 6916:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6917:                             }
 6918:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6919:                                                                    \%groups_roles);
 6920:                             &appenv(\%userroles,\@rolecodes);
 6921:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6922:                         }
 6923:                     }
 6924:                     $$tstatus = 'is';
 6925:                 }
 6926:             }
 6927:             if ($$tend) {
 6928:                 if ($$tend<$update) {
 6929:                     $$tstatus='expired';
 6930:                 } elsif ($$tend<$now) {
 6931:                     $$tstatus='will_not';
 6932:                 }
 6933:             }
 6934:         }
 6935:     }
 6936: }
 6937: 
 6938: sub get_groups_roles {
 6939:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6940:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6941:                   (ref($rolecodes) eq 'ARRAY') && 
 6942:                   (ref($groups_roles) eq 'HASH')); 
 6943:     if (keys(%{$cdom_courseroles}) > 0) {
 6944:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6945:         if ($cdom ne '' && $cnum ne '') {
 6946:             foreach my $key (keys(%{$cdom_courseroles})) {
 6947:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6948:                     my $crsrole = $1;
 6949:                     my $crssec = $2;
 6950:                     if ($crsrole =~ /^cr/) {
 6951:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6952:                             push(@{$rolecodes},'cr');
 6953:                         }
 6954:                     } else {
 6955:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6956:                             push(@{$rolecodes},$crsrole);
 6957:                         }
 6958:                     }
 6959:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6960:                     if ($crssec ne '') {
 6961:                         $rolekey .= "/$crssec";
 6962:                     }
 6963:                     $rolekey .= './';
 6964:                     $groups_roles->{$rolekey} = $rolecodes;
 6965:                 }
 6966:             }
 6967:         }
 6968:     }
 6969:     return;
 6970: }
 6971: 
 6972: sub delete_env_groupprivs {
 6973:     my ($where,$courseroles,$possroles) = @_;
 6974:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6975:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6976:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6977:         %{$courseroles->{$udom}} =
 6978:             &get_my_roles('','','userroles',['active'],
 6979:                           $possroles,[$udom],1);
 6980:     }
 6981:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6982:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6983:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6984:             my $area = '/'.$cdom.'/'.$cnum;
 6985:             my $privkey = "user.priv.$crsrole.$area";
 6986:             if ($crssec ne '') {
 6987:                 $privkey .= '/'.$crssec;
 6988:             }
 6989:             $privkey .= ".$area/$group";
 6990:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6991:         }
 6992:     }
 6993:     return;
 6994: }
 6995: 
 6996: sub check_adhoc_privs {
 6997:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6998:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6999:     if ($sec) {
 7000:         $cckey .= '/'.$sec;
 7001:     } 
 7002:     my $setprivs;
 7003:     if ($env{$cckey}) {
 7004:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 7005:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 7006:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 7007:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7008:             $setprivs = 1;
 7009:         }
 7010:     } else {
 7011:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7012:         $setprivs = 1;
 7013:     }
 7014:     return $setprivs;
 7015: }
 7016: 
 7017: sub set_adhoc_privileges {
 7018: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7019:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7020:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7021:     if ($sec ne '') {
 7022:         $area .= '/'.$sec;
 7023:     }
 7024:     my $spec = $role.'.'.$area;
 7025:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7026:                                   $env{'user.name'},1);
 7027:     my %rolehash = ();
 7028:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7029:         my $rolename = $1;
 7030:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7031:         my %domdef = &get_domain_defaults($dcdom);
 7032:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7033:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7034:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7035:             }
 7036:         }
 7037:     } else {
 7038:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7039:     }
 7040:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7041:     &appenv(\%userroles,[$role,'cm']);
 7042:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7043:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7044:             ($caller eq 'tiny')) {
 7045:         &appenv( {'request.role'        => $spec,
 7046:                   'request.role.domain' => $dcdom,
 7047:                   'request.course.sec'  => $sec,
 7048:                  }
 7049:                );
 7050:         my $tadv=0;
 7051:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7052:         &appenv({'request.role.adv'    => $tadv});
 7053:     }
 7054: }
 7055: 
 7056: # --------------------------------------------------------------- get interface
 7057: 
 7058: sub get {
 7059:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7060:    my $items='';
 7061:    foreach my $item (@$storearr) {
 7062:        $items.=&escape($item).'&';
 7063:    }
 7064:    $items=~s/\&$//;
 7065:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7066:    if (!$uname) { $uname=$env{'user.name'}; }
 7067:    my $uhome=&homeserver($uname,$udomain);
 7068: 
 7069:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7070:    my @pairs=split(/\&/,$rep);
 7071:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7072:      return @pairs;
 7073:    }
 7074:    my %returnhash=();
 7075:    my $i=0;
 7076:    foreach my $item (@$storearr) {
 7077:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7078:       $i++;
 7079:    }
 7080:    return %returnhash;
 7081: }
 7082: 
 7083: # --------------------------------------------------------------- del interface
 7084: 
 7085: sub del {
 7086:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7087:    my $items='';
 7088:    foreach my $item (@$storearr) {
 7089:        $items.=&escape($item).'&';
 7090:    }
 7091: 
 7092:    $items=~s/\&$//;
 7093:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7094:    if (!$uname) { $uname=$env{'user.name'}; }
 7095:    my $uhome=&homeserver($uname,$udomain);
 7096:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7097: }
 7098: 
 7099: # -------------------------------------------------------------- dump interface
 7100: 
 7101: sub unserialize {
 7102:     my ($rep, $escapedkeys) = @_;
 7103: 
 7104:     return {} if $rep =~ /^error/;
 7105: 
 7106:     my %returnhash=();
 7107: 	foreach my $item (split(/\&/,$rep)) {
 7108: 	    my ($key, $value) = split(/=/, $item, 2);
 7109: 	    $key = unescape($key) unless $escapedkeys;
 7110: 	    next if $key =~ /^error: 2 /;
 7111: 	    $returnhash{$key} = &thaw_unescape($value);
 7112: 	}
 7113:     #return %returnhash;
 7114:     return \%returnhash;
 7115: }        
 7116: 
 7117: # see Lond::dump_with_regexp
 7118: # if $escapedkeys hash keys won't get unescaped.
 7119: sub dump {
 7120:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 7121:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7122:     if (!$uname) { $uname=$env{'user.name'}; }
 7123:     my $uhome=&homeserver($uname,$udomain);
 7124: 
 7125:     if ($regexp) {
 7126:         $regexp=&escape($regexp);
 7127:     } else {
 7128:         $regexp='.';
 7129:     }
 7130:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7131:         # user is hosted on this machine
 7132:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7133:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7134:         return %{unserialize($reply, $escapedkeys)};
 7135:     }
 7136:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7137:     my @pairs=split(/\&/,$rep);
 7138:     my %returnhash=();
 7139:     if (!($rep =~ /^error/ )) {
 7140: 	foreach my $item (@pairs) {
 7141: 	    my ($key,$value)=split(/=/,$item,2);
 7142:         $key = unescape($key) unless $escapedkeys;
 7143:         #$key = &unescape($key);
 7144: 	    next if ($key =~ /^error: 2 /);
 7145: 	    $returnhash{$key}=&thaw_unescape($value);
 7146: 	}
 7147:     }
 7148:     return %returnhash;
 7149: }
 7150: 
 7151: 
 7152: # --------------------------------------------------------- dumpstore interface
 7153: 
 7154: sub dumpstore {
 7155:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7156:    # same as dump but keys must be escaped. They may contain colon separated
 7157:    # lists of values that may themself contain colons (e.g. symbs).
 7158:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7159: }
 7160: 
 7161: # -------------------------------------------------------------- keys interface
 7162: 
 7163: sub getkeys {
 7164:    my ($namespace,$udomain,$uname)=@_;
 7165:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7166:    if (!$uname) { $uname=$env{'user.name'}; }
 7167:    my $uhome=&homeserver($uname,$udomain);
 7168:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7169:    my @keyarray=();
 7170:    foreach my $key (split(/\&/,$rep)) {
 7171:       next if ($key =~ /^error: 2 /);
 7172:       push(@keyarray,&unescape($key));
 7173:    }
 7174:    return @keyarray;
 7175: }
 7176: 
 7177: # --------------------------------------------------------------- currentdump
 7178: sub currentdump {
 7179:    my ($courseid,$sdom,$sname)=@_;
 7180:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7181:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7182:    $sname    = $env{'user.name'}         if (! defined($sname));
 7183:    my $uhome = &homeserver($sname,$sdom);
 7184:    my $rep;
 7185: 
 7186:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7187:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7188:                    $courseid)));
 7189:    } else {
 7190:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7191:    }
 7192: 
 7193:    return if ($rep =~ /^(error:|no_such_host)/);
 7194:    #
 7195:    my %returnhash=();
 7196:    #
 7197:    if ($rep eq 'unknown_cmd') {
 7198:        # an old lond will not know currentdump
 7199:        # Do a dump and make it look like a currentdump
 7200:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7201:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7202:        my %hash = @tmp;
 7203:        @tmp=();
 7204:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7205:    } else {
 7206:        my @pairs=split(/\&/,$rep);
 7207:        foreach my $pair (@pairs) {
 7208:            my ($key,$value)=split(/=/,$pair,2);
 7209:            my ($symb,$param) = split(/:/,$key);
 7210:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7211:                                                         &thaw_unescape($value);
 7212:        }
 7213:    }
 7214:    return %returnhash;
 7215: }
 7216: 
 7217: sub convert_dump_to_currentdump{
 7218:     my %hash = %{shift()};
 7219:     my %returnhash;
 7220:     # Code ripped from lond, essentially.  The only difference
 7221:     # here is the unescaping done by lonnet::dump().  Conceivably
 7222:     # we might run in to problems with parameter names =~ /^v\./
 7223:     while (my ($key,$value) = each(%hash)) {
 7224:         my ($v,$symb,$param) = split(/:/,$key);
 7225: 	$symb  = &unescape($symb);
 7226: 	$param = &unescape($param);
 7227:         next if ($v eq 'version' || $symb eq 'keys');
 7228:         next if (exists($returnhash{$symb}) &&
 7229:                  exists($returnhash{$symb}->{$param}) &&
 7230:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7231:         $returnhash{$symb}->{$param}=$value;
 7232:         $returnhash{$symb}->{'v.'.$param}=$v;
 7233:     }
 7234:     #
 7235:     # Remove all of the keys in the hashes which keep track of
 7236:     # the version of the parameter.
 7237:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7238:         # use a foreach because we are going to delete from the hash.
 7239:         foreach my $key (keys(%$param_hash)) {
 7240:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7241:         }
 7242:     }
 7243:     return \%returnhash;
 7244: }
 7245: 
 7246: # ------------------------------------------------------ critical inc interface
 7247: 
 7248: sub cinc {
 7249:     return &inc(@_,'critical');
 7250: }
 7251: 
 7252: # --------------------------------------------------------------- inc interface
 7253: 
 7254: sub inc {
 7255:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7256:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7257:     if (!$uname) { $uname=$env{'user.name'}; }
 7258:     my $uhome=&homeserver($uname,$udomain);
 7259:     my $items='';
 7260:     if (! ref($store)) {
 7261:         # got a single value, so use that instead
 7262:         $items = &escape($store).'=&';
 7263:     } elsif (ref($store) eq 'SCALAR') {
 7264:         $items = &escape($$store).'=&';        
 7265:     } elsif (ref($store) eq 'ARRAY') {
 7266:         $items = join('=&',map {&escape($_);} @{$store});
 7267:     } elsif (ref($store) eq 'HASH') {
 7268:         while (my($key,$value) = each(%{$store})) {
 7269:             $items.= &escape($key).'='.&escape($value).'&';
 7270:         }
 7271:     }
 7272:     $items=~s/\&$//;
 7273:     if ($critical) {
 7274: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7275:     } else {
 7276: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7277:     }
 7278: }
 7279: 
 7280: # --------------------------------------------------------------- put interface
 7281: 
 7282: sub put {
 7283:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7284:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7285:    if (!$uname) { $uname=$env{'user.name'}; }
 7286:    my $uhome=&homeserver($uname,$udomain);
 7287:    my $items='';
 7288:    foreach my $item (keys(%$storehash)) {
 7289:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7290:    }
 7291:    $items=~s/\&$//;
 7292:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7293: }
 7294: 
 7295: # ------------------------------------------------------------ newput interface
 7296: 
 7297: sub newput {
 7298:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7299:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7300:    if (!$uname) { $uname=$env{'user.name'}; }
 7301:    my $uhome=&homeserver($uname,$udomain);
 7302:    my $items='';
 7303:    foreach my $key (keys(%$storehash)) {
 7304:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7305:    }
 7306:    $items=~s/\&$//;
 7307:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7308: }
 7309: 
 7310: # ---------------------------------------------------------  putstore interface
 7311: 
 7312: sub putstore {
 7313:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7314:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7315:    if (!$uname) { $uname=$env{'user.name'}; }
 7316:    my $uhome=&homeserver($uname,$udomain);
 7317:    my $items='';
 7318:    foreach my $key (keys(%$storehash)) {
 7319:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7320:    }
 7321:    $items=~s/\&$//;
 7322:    my $esc_symb=&escape($symb);
 7323:    my $esc_v=&escape($version);
 7324:    my $reply =
 7325:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7326: 	      $uhome);
 7327:    if (($tolog) && ($reply eq 'ok')) {
 7328:        my $namevalue='';
 7329:        foreach my $key (keys(%{$storehash})) {
 7330:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7331:        }
 7332:        my $ip = &get_requestor_ip();
 7333:        $namevalue .= 'ip='.&escape($ip).
 7334:                      '&host='.&escape($perlvar{'lonHostID'}).
 7335:                      '&version='.$esc_v.
 7336:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7337:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7338:    }
 7339:    if ($reply eq 'unknown_cmd') {
 7340:        # gfall back to way things use to be done
 7341:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7342: 			    $uname);
 7343:    }
 7344:    return $reply;
 7345: }
 7346: 
 7347: sub old_putstore {
 7348:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7349:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7350:     if (!$uname) { $uname=$env{'user.name'}; }
 7351:     my $uhome=&homeserver($uname,$udomain);
 7352:     my %newstorehash;
 7353:     foreach my $item (keys(%$storehash)) {
 7354: 	my $key = $version.':'.&escape($symb).':'.$item;
 7355: 	$newstorehash{$key} = $storehash->{$item};
 7356:     }
 7357:     my $items='';
 7358:     my %allitems = ();
 7359:     foreach my $item (keys(%newstorehash)) {
 7360: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7361: 	    my $key = $1.':keys:'.$2;
 7362: 	    $allitems{$key} .= $3.':';
 7363: 	}
 7364: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7365:     }
 7366:     foreach my $item (keys(%allitems)) {
 7367: 	$allitems{$item} =~ s/\:$//;
 7368: 	$items.= $item.'='.$allitems{$item}.'&';
 7369:     }
 7370:     $items=~s/\&$//;
 7371:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7372: }
 7373: 
 7374: # ------------------------------------------------------ critical put interface
 7375: 
 7376: sub cput {
 7377:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7378:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7379:    if (!$uname) { $uname=$env{'user.name'}; }
 7380:    my $uhome=&homeserver($uname,$udomain);
 7381:    my $items='';
 7382:    foreach my $item (keys(%$storehash)) {
 7383:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7384:    }
 7385:    $items=~s/\&$//;
 7386:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7387: }
 7388: 
 7389: # -------------------------------------------------------------- eget interface
 7390: 
 7391: sub eget {
 7392:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7393:    my $items='';
 7394:    foreach my $item (@$storearr) {
 7395:        $items.=&escape($item).'&';
 7396:    }
 7397:    $items=~s/\&$//;
 7398:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7399:    if (!$uname) { $uname=$env{'user.name'}; }
 7400:    my $uhome=&homeserver($uname,$udomain);
 7401:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7402:    my @pairs=split(/\&/,$rep);
 7403:    my %returnhash=();
 7404:    my $i=0;
 7405:    foreach my $item (@$storearr) {
 7406:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7407:       $i++;
 7408:    }
 7409:    return %returnhash;
 7410: }
 7411: 
 7412: # ------------------------------------------------------------ tmpput interface
 7413: sub tmpput {
 7414:     my ($storehash,$server,$context)=@_;
 7415:     my $items='';
 7416:     foreach my $item (keys(%$storehash)) {
 7417: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7418:     }
 7419:     $items=~s/\&$//;
 7420:     if (defined($context)) {
 7421:         $items .= ':'.&escape($context);
 7422:     }
 7423:     return &reply("tmpput:$items",$server);
 7424: }
 7425: 
 7426: # ------------------------------------------------------------ tmpget interface
 7427: sub tmpget {
 7428:     my ($token,$server)=@_;
 7429:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7430:     my $rep=&reply("tmpget:$token",$server);
 7431:     my %returnhash;
 7432:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7433:         return %returnhash;
 7434:     }
 7435:     foreach my $item (split(/\&/,$rep)) {
 7436: 	my ($key,$value)=split(/=/,$item);
 7437: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7438:     }
 7439:     return %returnhash;
 7440: }
 7441: 
 7442: # ------------------------------------------------------------ tmpdel interface
 7443: sub tmpdel {
 7444:     my ($token,$server)=@_;
 7445:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7446:     return &reply("tmpdel:$token",$server);
 7447: }
 7448: 
 7449: # ------------------------------------------------------------ get_timebased_id 
 7450: 
 7451: sub get_timebased_id {
 7452:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7453:         $maxtries) = @_;
 7454:     my ($newid,$error,$dellock);
 7455:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7456:         return ('','ok','invalid call to get suffix');
 7457:     }
 7458: 
 7459: # set defaults for any optional args for which values were not supplied
 7460:     if ($who eq '') {
 7461:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7462:     }
 7463:     if (!$locktries) {
 7464:         $locktries = 3;
 7465:     }
 7466:     if (!$maxtries) {
 7467:         $maxtries = 10;
 7468:     }
 7469:     
 7470:     if (($cdom eq '') || ($cnum eq '')) {
 7471:         if ($env{'request.course.id'}) {
 7472:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7473:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7474:         }
 7475:         if (($cdom eq '') || ($cnum eq '')) {
 7476:             return ('','ok','call to get suffix not in course context');
 7477:         }
 7478:     }
 7479: 
 7480: # construct locking item
 7481:     my $lockhash = {
 7482:                       $prefix."\0".'locked_'.$keyid => $who,
 7483:                    };
 7484:     my $tries = 0;
 7485: 
 7486: # attempt to get lock on nohist_$namespace file
 7487:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7488:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7489:         $tries ++;
 7490:         sleep 1;
 7491:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7492:     }
 7493: 
 7494: # attempt to get unique identifier, based on current timestamp
 7495:     if ($gotlock eq 'ok') {
 7496:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7497:         my $id = time;
 7498:         $newid = $id;
 7499:         if ($idtype eq 'addcode') {
 7500:             $newid .= &sixnum_code();
 7501:         }
 7502:         my $idtries = 0;
 7503:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7504:             if ($idtype eq 'concat') {
 7505:                 $newid = $id.$idtries;
 7506:             } elsif ($idtype eq 'addcode') {
 7507:                 $newid = $newid.&sixnum_code();
 7508:             } else {
 7509:                 $newid ++;
 7510:             }
 7511:             $idtries ++;
 7512:         }
 7513:         if (!exists($inuse{$prefix."\0".$newid})) {
 7514:             my %new_item =  (
 7515:                               $prefix."\0".$newid => $who,
 7516:                             );
 7517:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7518:                                                  $cdom,$cnum);
 7519:             if ($putresult ne 'ok') {
 7520:                 undef($newid);
 7521:                 $error = 'error saving new item: '.$putresult;
 7522:             }
 7523:         } else {
 7524:              undef($newid);
 7525:              $error = ('error: no unique suffix available for the new item ');
 7526:         }
 7527: #  remove lock
 7528:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7529:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7530:     } else {
 7531:         $error = "error: could not obtain lockfile\n";
 7532:         $dellock = 'ok';
 7533:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7534:             $dellock = 'nolock';
 7535:         }
 7536:     }
 7537:     return ($newid,$dellock,$error);
 7538: }
 7539: 
 7540: sub sixnum_code {
 7541:     my $code;
 7542:     for (0..6) {
 7543:         $code .= int( rand(9) );
 7544:     }
 7545:     return $code;
 7546: }
 7547: 
 7548: # -------------------------------------------------- portfolio access checking
 7549: 
 7550: sub portfolio_access {
 7551:     my ($requrl,$clientip) = @_;
 7552:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7553:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7554:     if ($result) {
 7555:         my %setters;
 7556:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7557:             my ($startblock,$endblock) =
 7558:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 7559:             if ($startblock && $endblock) {
 7560:                 return 'B';
 7561:             }
 7562:         } else {
 7563:             my ($startblock,$endblock) =
 7564:                 &Apache::loncommon::blockcheck(\%setters,'port');
 7565:             if ($startblock && $endblock) {
 7566:                 return 'B';
 7567:             }
 7568:         }
 7569:     }
 7570:     if ($result eq 'ok') {
 7571:        return 'F';
 7572:     } elsif ($result =~ /^[^:]+:guest_/) {
 7573:        return 'A';
 7574:     }
 7575:     return '';
 7576: }
 7577: 
 7578: sub get_portfolio_access {
 7579:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7580: 
 7581:     if (!ref($access_hash)) {
 7582: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7583: 	my %access_controls = &get_access_controls($current_perms,$group,
 7584: 						   $file_name);
 7585: 	$access_hash = $access_controls{$file_name};
 7586:     }
 7587: 
 7588:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7589:     my $now = time;
 7590:     if (ref($access_hash) eq 'HASH') {
 7591:         foreach my $key (keys(%{$access_hash})) {
 7592:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7593:             if ($start > $now) {
 7594:                 next;
 7595:             }
 7596:             if ($end && $end<$now) {
 7597:                 next;
 7598:             }
 7599:             if ($scope eq 'public') {
 7600:                 $public = $key;
 7601:                 last;
 7602:             } elsif ($scope eq 'guest') {
 7603:                 $guest = $key;
 7604:             } elsif ($scope eq 'domains') {
 7605:                 push(@domains,$key);
 7606:             } elsif ($scope eq 'users') {
 7607:                 push(@users,$key);
 7608:             } elsif ($scope eq 'course') {
 7609:                 push(@courses,$key);
 7610:             } elsif ($scope eq 'group') {
 7611:                 push(@groups,$key);
 7612:             } elsif ($scope eq 'ip') {
 7613:                 push(@ips,$key);
 7614:             }
 7615:         }
 7616:         if ($public) {
 7617:             return 'ok';
 7618:         } elsif (@ips > 0) {
 7619:             my $allowed;
 7620:             foreach my $ipkey (@ips) {
 7621:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7622:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7623:                         $allowed = 1;
 7624:                         last; 
 7625:                     }
 7626:                 }
 7627:             }
 7628:             if ($allowed) {
 7629:                 return 'ok';
 7630:             }
 7631:         }
 7632:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7633:             if ($guest) {
 7634:                 return $guest;
 7635:             }
 7636:         } else {
 7637:             if (@domains > 0) {
 7638:                 foreach my $domkey (@domains) {
 7639:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7640:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7641:                             return 'ok';
 7642:                         }
 7643:                     }
 7644:                 }
 7645:             }
 7646:             if (@users > 0) {
 7647:                 foreach my $userkey (@users) {
 7648:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7649:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7650:                             if (ref($item) eq 'HASH') {
 7651:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7652:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7653:                                     return 'ok';
 7654:                                 }
 7655:                             }
 7656:                         }
 7657:                     } 
 7658:                 }
 7659:             }
 7660:             my %roleshash;
 7661:             my @courses_and_groups = @courses;
 7662:             push(@courses_and_groups,@groups); 
 7663:             if (@courses_and_groups > 0) {
 7664:                 my (%allgroups,%allroles); 
 7665:                 my ($start,$end,$role,$sec,$group);
 7666:                 foreach my $envkey (%env) {
 7667:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7668:                         my $cid = $2.'_'.$3; 
 7669:                         if ($1 eq 'gr') {
 7670:                             $group = $4;
 7671:                             $allgroups{$cid}{$group} = $env{$envkey};
 7672:                         } else {
 7673:                             if ($4 eq '') {
 7674:                                 $sec = 'none';
 7675:                             } else {
 7676:                                 $sec = $4;
 7677:                             }
 7678:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7679:                         }
 7680:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7681:                         my $cid = $2.'_'.$3;
 7682:                         if ($4 eq '') {
 7683:                             $sec = 'none';
 7684:                         } else {
 7685:                             $sec = $4;
 7686:                         }
 7687:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7688:                     }
 7689:                 }
 7690:                 if (keys(%allroles) == 0) {
 7691:                     return;
 7692:                 }
 7693:                 foreach my $key (@courses_and_groups) {
 7694:                     my %content = %{$$access_hash{$key}};
 7695:                     my $cnum = $content{'number'};
 7696:                     my $cdom = $content{'domain'};
 7697:                     my $cid = $cdom.'_'.$cnum;
 7698:                     if (!exists($allroles{$cid})) {
 7699:                         next;
 7700:                     }    
 7701:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7702:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7703:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7704:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7705:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7706:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7707:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7708:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7709:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7710:                                         if (grep/^all$/,@sections) {
 7711:                                             return 'ok';
 7712:                                         } else {
 7713:                                             if (grep/^$sec$/,@sections) {
 7714:                                                 return 'ok';
 7715:                                             }
 7716:                                         }
 7717:                                     }
 7718:                                 }
 7719:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7720:                                     if (grep/^none$/,@groups) {
 7721:                                         return 'ok';
 7722:                                     }
 7723:                                 } else {
 7724:                                     if (grep/^all$/,@groups) {
 7725:                                         return 'ok';
 7726:                                     } 
 7727:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7728:                                         if (grep/^$group$/,@groups) {
 7729:                                             return 'ok';
 7730:                                         }
 7731:                                     }
 7732:                                 } 
 7733:                             }
 7734:                         }
 7735:                     }
 7736:                 }
 7737:             }
 7738:             if ($guest) {
 7739:                 return $guest;
 7740:             }
 7741:         }
 7742:     }
 7743:     return;
 7744: }
 7745: 
 7746: sub course_group_datechecker {
 7747:     my ($dates,$now,$status) = @_;
 7748:     my ($start,$end) = split(/\./,$dates);
 7749:     if (!$start && !$end) {
 7750:         return 'ok';
 7751:     }
 7752:     if (grep/^active$/,@{$status}) {
 7753:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7754:             return 'ok';
 7755:         }
 7756:     }
 7757:     if (grep/^previous$/,@{$status}) {
 7758:         if ($end > $now ) {
 7759:             return 'ok';
 7760:         }
 7761:     }
 7762:     if (grep/^future$/,@{$status}) {
 7763:         if ($start > $now) {
 7764:             return 'ok';
 7765:         }
 7766:     }
 7767:     return; 
 7768: }
 7769: 
 7770: sub parse_portfolio_url {
 7771:     my ($url) = @_;
 7772: 
 7773:     my ($type,$udom,$unum,$group,$file_name);
 7774:     
 7775:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7776: 	$type = 1;
 7777:         $udom = $1;
 7778:         $unum = $2;
 7779:         $file_name = $3;
 7780:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7781: 	$type = 2;
 7782:         $udom = $1;
 7783:         $unum = $2;
 7784:         $group = $3;
 7785:         $file_name = $3.'/'.$4;
 7786:     }
 7787:     if (wantarray) {
 7788: 	return ($type,$udom,$unum,$file_name,$group);
 7789:     }
 7790:     return $type;
 7791: }
 7792: 
 7793: sub is_portfolio_url {
 7794:     my ($url) = @_;
 7795:     return scalar(&parse_portfolio_url($url));
 7796: }
 7797: 
 7798: sub is_portfolio_file {
 7799:     my ($file) = @_;
 7800:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7801:         return 1;
 7802:     }
 7803:     return;
 7804: }
 7805: 
 7806: sub usertools_access {
 7807:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7808:     my ($access,%tools);
 7809:     if ($context eq '') {
 7810:         $context = 'tools';
 7811:     }
 7812:     if ($context eq 'requestcourses') {
 7813:         %tools = (
 7814:                       official   => 1,
 7815:                       unofficial => 1,
 7816:                       community  => 1,
 7817:                       textbook   => 1,
 7818:                       placement  => 1,
 7819:                       lti        => 1,
 7820:                  );
 7821:     } elsif ($context eq 'requestauthor') {
 7822:         %tools = (
 7823:                       requestauthor => 1,
 7824:                  );
 7825:     } else {
 7826:         %tools = (
 7827:                       aboutme   => 1,
 7828:                       blog      => 1,
 7829:                       webdav    => 1,
 7830:                       portfolio => 1,
 7831:                  );
 7832:     }
 7833:     return if (!defined($tools{$tool}));
 7834: 
 7835:     if (($udom eq '') || ($uname eq '')) {
 7836:         $udom = $env{'user.domain'};
 7837:         $uname = $env{'user.name'};
 7838:     }
 7839: 
 7840:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7841:         if ($action ne 'reload') {
 7842:             if ($context eq 'requestcourses') {
 7843:                 return $env{'environment.canrequest.'.$tool};
 7844:             } elsif ($context eq 'requestauthor') {
 7845:                 return $env{'environment.canrequest.author'};
 7846:             } else {
 7847:                 return $env{'environment.availabletools.'.$tool};
 7848:             }
 7849:         }
 7850:     }
 7851: 
 7852:     my ($toolstatus,$inststatus,$envkey);
 7853:     if ($context eq 'requestauthor') {
 7854:         $envkey = $context; 
 7855:     } else {
 7856:         $envkey = $context.'.'.$tool;
 7857:     }
 7858: 
 7859:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7860:          ($action ne 'reload')) {
 7861:         $toolstatus = $env{'environment.'.$envkey};
 7862:         $inststatus = $env{'environment.inststatus'};
 7863:     } else {
 7864:         if (ref($userenvref) eq 'HASH') {
 7865:             $toolstatus = $userenvref->{$envkey};
 7866:             $inststatus = $userenvref->{'inststatus'};
 7867:         } else {
 7868:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7869:             $toolstatus = $userenv{$envkey};
 7870:             $inststatus = $userenv{'inststatus'};
 7871:         }
 7872:     }
 7873: 
 7874:     if ($toolstatus ne '') {
 7875:         if ($toolstatus) {
 7876:             $access = 1;
 7877:         } else {
 7878:             $access = 0;
 7879:         }
 7880:         return $access;
 7881:     }
 7882: 
 7883:     my ($is_adv,%domdef);
 7884:     if (ref($is_advref) eq 'HASH') {
 7885:         $is_adv = $is_advref->{'is_adv'};
 7886:     } else {
 7887:         $is_adv = &is_advanced_user($udom,$uname);
 7888:     }
 7889:     if (ref($domdefref) eq 'HASH') {
 7890:         %domdef = %{$domdefref};
 7891:     } else {
 7892:         %domdef = &get_domain_defaults($udom);
 7893:     }
 7894:     if (ref($domdef{$tool}) eq 'HASH') {
 7895:         if ($is_adv) {
 7896:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7897:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7898:                     $access = 1;
 7899:                 } else {
 7900:                     $access = 0;
 7901:                 }
 7902:                 return $access;
 7903:             }
 7904:         }
 7905:         if ($inststatus ne '') {
 7906:             my ($hasaccess,$hasnoaccess);
 7907:             foreach my $affiliation (split(/:/,$inststatus)) {
 7908:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7909:                     if ($domdef{$tool}{$affiliation}) {
 7910:                         $hasaccess = 1;
 7911:                     } else {
 7912:                         $hasnoaccess = 1;
 7913:                     }
 7914:                 }
 7915:             }
 7916:             if ($hasaccess || $hasnoaccess) {
 7917:                 if ($hasaccess) {
 7918:                     $access = 1;
 7919:                 } elsif ($hasnoaccess) {
 7920:                     $access = 0; 
 7921:                 }
 7922:                 return $access;
 7923:             }
 7924:         } else {
 7925:             if ($domdef{$tool}{'default'} ne '') {
 7926:                 if ($domdef{$tool}{'default'}) {
 7927:                     $access = 1;
 7928:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7929:                     $access = 0;
 7930:                 }
 7931:                 return $access;
 7932:             }
 7933:         }
 7934:     } else {
 7935:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7936:             $access = 1;
 7937:         } else {
 7938:             $access = 0;
 7939:         }
 7940:         return $access;
 7941:     }
 7942: }
 7943: 
 7944: sub is_course_owner {
 7945:     my ($cdom,$cnum,$udom,$uname) = @_;
 7946:     if (($udom eq '') || ($uname eq '')) {
 7947:         $udom = $env{'user.domain'};
 7948:         $uname = $env{'user.name'};
 7949:     }
 7950:     unless (($udom eq '') || ($uname eq '')) {
 7951:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7952:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7953:                 return 1;
 7954:             } else {
 7955:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7956:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7957:                     return 1;
 7958:                 }
 7959:             }
 7960:         }
 7961:     }
 7962:     return;
 7963: }
 7964: 
 7965: sub is_advanced_user {
 7966:     my ($udom,$uname) = @_;
 7967:     if ($udom ne '' && $uname ne '') {
 7968:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7969:             if (wantarray) {
 7970:                 return ($env{'user.adv'},$env{'user.author'});
 7971:             } else {
 7972:                 return $env{'user.adv'};
 7973:             }
 7974:         }
 7975:     }
 7976:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7977:     my %allroles;
 7978:     my ($is_adv,$is_author);
 7979:     foreach my $role (keys(%roleshash)) {
 7980:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7981:         my $area = '/'.$tdomain.'/'.$trest;
 7982:         if ($sec ne '') {
 7983:             $area .= '/'.$sec;
 7984:         }
 7985:         if (($area ne '') && ($trole ne '')) {
 7986:             my $spec=$trole.'.'.$area;
 7987:             if ($trole =~ /^cr\//) {
 7988:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7989:             } elsif ($trole ne 'gr') {
 7990:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7991:             }
 7992:             if ($trole eq 'au') {
 7993:                 $is_author = 1;
 7994:             }
 7995:         }
 7996:     }
 7997:     foreach my $role (keys(%allroles)) {
 7998:         last if ($is_adv);
 7999:         foreach my $item (split(/:/,$allroles{$role})) {
 8000:             if ($item ne '') {
 8001:                 my ($privilege,$restrictions)=split(/&/,$item);
 8002:                 if ($privilege eq 'adv') {
 8003:                     $is_adv = 1;
 8004:                     last;
 8005:                 }
 8006:             }
 8007:         }
 8008:     }
 8009:     if (wantarray) {
 8010:         return ($is_adv,$is_author);
 8011:     }
 8012:     return $is_adv;
 8013: }
 8014: 
 8015: sub check_can_request {
 8016:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8017:     my $canreq = 0;
 8018:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8019:         $uname = $env{'user.name'};
 8020:         $udom = $env{'user.domain'};
 8021:     }
 8022:     my ($types,$typename) = &Apache::loncommon::course_types();
 8023:     my @options = ('approval','validate','autolimit');
 8024:     my $optregex = join('|',@options);
 8025:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8026:         foreach my $type (@{$types}) {
 8027:             if (&usertools_access($uname,$udom,$type,undef,
 8028:                                   'requestcourses')) {
 8029:                 $canreq ++;
 8030:                 if (ref($request_domains) eq 'HASH') {
 8031:                     push(@{$request_domains->{$type}},$udom);
 8032:                 }
 8033:                 if ($dom eq $udom) {
 8034:                     $can_request->{$type} = 1;
 8035:                 }
 8036:             }
 8037:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8038:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8039:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8040:                 if (@curr > 0) {
 8041:                     foreach my $item (@curr) {
 8042:                         if (ref($request_domains) eq 'HASH') {
 8043:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8044:                             if ($otherdom ne '') {
 8045:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 8046:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8047:                                         push(@{$request_domains->{$type}},$otherdom);
 8048:                                     }
 8049:                                 } else {
 8050:                                     push(@{$request_domains->{$type}},$otherdom);
 8051:                                 }
 8052:                             }
 8053:                         }
 8054:                     }
 8055:                     unless ($dom eq $env{'user.domain'}) {
 8056:                         $canreq ++;
 8057:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8058:                             $can_request->{$type} = 1;
 8059:                         }
 8060:                     }
 8061:                 }
 8062:             }
 8063:         }
 8064:     }
 8065:     return $canreq;
 8066: }
 8067: 
 8068: # ---------------------------------------------- Custom access rule evaluation
 8069: 
 8070: sub customaccess {
 8071:     my ($priv,$uri)=@_;
 8072:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8073:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8074:     $udom = &LONCAPA::clean_domain($udom);
 8075:     $ucrs = &LONCAPA::clean_username($ucrs);
 8076:     my $access=0;
 8077:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8078: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8079: 	if ($type eq 'user') {
 8080: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8081: 		my ($tdom,$tuname)=split(m{/},$scope);
 8082: 		if ($tdom) {
 8083: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8084: 		}
 8085: 		if ($tuname) {
 8086: 		    if ($tuname ne $env{'user.name'}) { next; }
 8087: 		}
 8088: 		$access=($effect eq 'allow');
 8089: 		last;
 8090: 	    }
 8091: 	} else {
 8092: 	    if ($role) {
 8093: 		if ($role ne $urole) { next; }
 8094: 	    }
 8095: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8096: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8097: 		if ($tdom) {
 8098: 		    if ($tdom ne $udom) { next; }
 8099: 		}
 8100: 		if ($tcrs) {
 8101: 		    if ($tcrs ne $ucrs) { next; }
 8102: 		}
 8103: 		if ($tsec) {
 8104: 		    if ($tsec ne $usec) { next; }
 8105: 		}
 8106: 		$access=($effect eq 'allow');
 8107: 		last;
 8108: 	    }
 8109: 	    if ($realm eq '' && $role eq '') {
 8110: 		$access=($effect eq 'allow');
 8111: 	    }
 8112: 	}
 8113:     }
 8114:     return $access;
 8115: }
 8116: 
 8117: # ------------------------------------------------- Check for a user privilege
 8118: 
 8119: sub allowed {
 8120:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
 8121:     my $ver_orguri=$uri;
 8122:     $uri=&deversion($uri);
 8123:     my $orguri=$uri;
 8124:     $uri=&declutter($uri);
 8125: 
 8126:     if ($priv eq 'evb') {
 8127: # Evade communication block restrictions for specified role in a course
 8128:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8129:             return $1;
 8130:         } else {
 8131:             return;
 8132:         }
 8133:     }
 8134: 
 8135:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8136: # Free bre access to adm and meta resources
 8137:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8138: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8139: 	&& ($priv eq 'bre')) {
 8140: 	return 'F';
 8141:     }
 8142: 
 8143: # Free bre access to user's own portfolio contents
 8144:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8145:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8146: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8147:         my %setters;
 8148:         my ($startblock,$endblock) = 
 8149:             &Apache::loncommon::blockcheck(\%setters,'port');
 8150:         if ($startblock && $endblock) {
 8151:             return 'B';
 8152:         } else {
 8153:             return 'F';
 8154:         }
 8155:     }
 8156: 
 8157: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8158:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8159:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8160:         if (exists($env{'request.course.id'})) {
 8161:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8162:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8163:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8164:                 my $courseprivid=$env{'request.course.id'};
 8165:                 $courseprivid=~s/\_/\//;
 8166:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8167:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8168:                     return $1; 
 8169:                 } else {
 8170:                     if ($env{'request.course.sec'}) {
 8171:                         $courseprivid.='/'.$env{'request.course.sec'};
 8172:                     }
 8173:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8174:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8175:                         return $2;
 8176:                     }
 8177:                 }
 8178:             }
 8179:         }
 8180:     }
 8181: 
 8182: # Free bre to public access
 8183: 
 8184:     if ($priv eq 'bre') {
 8185:         my $copyright;
 8186:         unless ($uri =~ /ext\.tool/) {
 8187:             $copyright=&metadata($uri,'copyright');
 8188:         }
 8189: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8190:            return 'F'; 
 8191:         }
 8192:         if ($copyright eq 'priv') {
 8193:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8194: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8195: 		return '';
 8196:             }
 8197:         }
 8198:         if ($copyright eq 'domain') {
 8199:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8200: 	    unless (($env{'user.domain'} eq $1) ||
 8201:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8202: 		return '';
 8203:             }
 8204:         }
 8205:         if ($env{'request.role'}=~ /li\.\//) {
 8206:             # Library role, so allow browsing of resources in this domain.
 8207:             return 'F';
 8208:         }
 8209:         if ($copyright eq 'custom') {
 8210: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8211:         }
 8212:     }
 8213:     # Domain coordinator is trying to create a course
 8214:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8215:         # uri is the requested domain in this case.
 8216:         # comparison to 'request.role.domain' shows if the user has selected
 8217:         # a role of dc for the domain in question.
 8218:         return 'F' if ($uri eq $env{'request.role.domain'});
 8219:     }
 8220: 
 8221:     my $thisallowed='';
 8222:     my $statecond=0;
 8223:     my $courseprivid='';
 8224: 
 8225:     my $ownaccess;
 8226:     # Community Coordinator or Assistant Co-author browsing resource space.
 8227:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8228:         if ($uri eq '') {
 8229:             $ownaccess = 1;
 8230:         } else {
 8231:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8232:                 my $udom = $env{'user.domain'};
 8233:                 my $uname = $env{'user.name'};
 8234:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8235:                     $ownaccess = 1;
 8236:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8237:                     unless ($uri =~ m{\.\./}) {
 8238:                         $ownaccess = 1;
 8239:                     }
 8240:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8241:                     my $now = time;
 8242:                     if ($uri =~ m{^([^/]+)/?$}) {
 8243:                         my $adom = $1;
 8244:                         foreach my $key (keys(%env)) {
 8245:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8246:                                 my ($start,$end) = split(/\./,$env{$key});
 8247:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8248:                                     $ownaccess = 1;
 8249:                                     last;
 8250:                                 }
 8251:                             }
 8252:                         }
 8253:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8254:                         my $adom = $1;
 8255:                         my $aname = $2;
 8256:                         foreach my $role ('ca','aa') { 
 8257:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8258:                                 my ($start,$end) =
 8259:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
 8260:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8261:                                     $ownaccess = 1;
 8262:                                     last;
 8263:                                 }
 8264:                             }
 8265:                         }
 8266:                     }
 8267:                 }
 8268:             }
 8269:         }
 8270:     }
 8271: 
 8272: # Course
 8273: 
 8274:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8275:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8276:             $thisallowed.=$1;
 8277:         }
 8278:     }
 8279: 
 8280: # Domain
 8281: 
 8282:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8283:        =~/\Q$priv\E\&([^\:]*)/) {
 8284:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8285:             $thisallowed.=$1;
 8286:         }
 8287:     }
 8288: 
 8289: # User who is not author or co-author might still be able to edit
 8290: # resource of an author in the domain (e.g., if Domain Coordinator).
 8291:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8292:         (&allowed('mdc',$env{'request.course.id'}))) {
 8293:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8294:             $thisallowed.=$1;
 8295:         }
 8296:     }
 8297: 
 8298: # Course: uri itself is a course
 8299:     my $courseuri=$uri;
 8300:     $courseuri=~s/\_(\d)/\/$1/;
 8301:     $courseuri=~s/^([^\/])/\/$1/;
 8302: 
 8303:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8304:        =~/\Q$priv\E\&([^\:]*)/) {
 8305:         if ($priv eq 'mip') {
 8306:             my $rem = $1;
 8307:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8308:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8309:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8310:                 if ($cdom ne '') {
 8311:                     my %passwdconf = &get_passwdconf($cdom);
 8312:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8313:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8314:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8315:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8316:                                 unless (@inststatuses) {
 8317:                                     @inststatuses = ('default');
 8318:                                 }
 8319:                                 foreach my $status (@inststatuses) {
 8320:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8321:                                         $thisallowed.=$rem;
 8322:                                     }
 8323:                                 }
 8324:                             }
 8325:                         }
 8326:                     }
 8327:                 }
 8328:             }
 8329:         } else {
 8330:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8331:                 $thisallowed.=$1;
 8332:             }
 8333:         }
 8334:     }
 8335: 
 8336: # URI is an uploaded document for this course, default permissions don't matter
 8337: # not allowing 'edit' access (editupload) to uploaded course docs
 8338:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8339: 	$thisallowed='';
 8340:         my ($match)=&is_on_map($uri);
 8341:         if ($match) {
 8342:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8343:                   =~/\Q$priv\E\&([^\:]*)/) {
 8344:                 my $value = $1;
 8345:                 my $deeplinkblock;
 8346:                 unless ($nodeeplinkcheck) {
 8347:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8348:                 }
 8349:                 if ($deeplinkblock) {
 8350:                     $thisallowed='D';
 8351:                 } elsif ($noblockcheck) {
 8352:                     $thisallowed.=$value;
 8353:                 } else {
 8354:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8355:                     if (@blockers > 0) {
 8356:                         $thisallowed = 'B';
 8357:                     } else {
 8358:                         $thisallowed.=$value;
 8359:                     }
 8360:                 }
 8361:             }
 8362:         } else {
 8363:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8364:             if ($refuri) {
 8365:                 if ($refuri =~ m|^/adm/|) {
 8366:                     $thisallowed='F';
 8367:                 } else {
 8368:                     $refuri=&declutter($refuri);
 8369:                     my ($match) = &is_on_map($refuri);
 8370:                     if ($match) {
 8371:                         my $deeplinkblock;
 8372:                         unless ($nodeeplinkcheck) {
 8373:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8374:                         }
 8375:                         if ($deeplinkblock) {
 8376:                             $thisallowed='D';
 8377:                         } elsif ($noblockcheck) {
 8378:                             $thisallowed='F';
 8379:                         } else {
 8380:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8381:                             if (@blockers > 0) {
 8382:                                 $thisallowed = 'B';
 8383:                             } else {
 8384:                                 $thisallowed='F';
 8385:                             }
 8386:                         }
 8387:                     }
 8388:                 }
 8389:             }
 8390:         }
 8391:     }
 8392: 
 8393:     if ($priv eq 'bre'
 8394: 	&& $thisallowed ne 'F' 
 8395: 	&& $thisallowed ne '2'
 8396: 	&& &is_portfolio_url($uri)) {
 8397: 	$thisallowed = &portfolio_access($uri,$clientip);
 8398:     }
 8399: 
 8400: # Full access at system, domain or course-wide level? Exit.
 8401:     if ($thisallowed=~/F/) {
 8402: 	return 'F';
 8403:     }
 8404: 
 8405: # If this is generating or modifying users, exit with special codes
 8406: 
 8407:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8408: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8409: 	    my ($audom,$auname)=split('/',$uri);
 8410: # no author name given, so this just checks on the general right to make a co-author in this domain
 8411: 	    unless ($auname) { return $thisallowed; }
 8412: # an author name is given, so we are about to actually make a co-author for a certain account
 8413: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8414: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8415: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8416: 	}
 8417: 	return $thisallowed;
 8418:     }
 8419: #
 8420: # Gathered so far: system, domain and course wide privileges
 8421: #
 8422: # Course: See if uri or referer is an individual resource that is part of 
 8423: # the course
 8424: 
 8425:     if ($env{'request.course.id'}) {
 8426: 
 8427: # If this is modifying password (internal auth) domains must match for user and user's role.
 8428: 
 8429:         if ($priv eq 'mip') {
 8430:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8431:                 return $thisallowed;
 8432:             } else {
 8433:                 return '';
 8434:             }
 8435:         }
 8436: 
 8437:        $courseprivid=$env{'request.course.id'};
 8438:        if ($env{'request.course.sec'}) {
 8439:           $courseprivid.='/'.$env{'request.course.sec'};
 8440:        }
 8441:        $courseprivid=~s/\_/\//;
 8442:        my $checkreferer=1;
 8443:        my ($match,$cond)=&is_on_map($uri);
 8444:        if ($match) {
 8445:            $statecond=$cond;
 8446:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8447:                =~/\Q$priv\E\&([^\:]*)/) {
 8448:                my $value = $1;
 8449:                if ($priv eq 'bre') {
 8450:                    my $deeplinkblock;
 8451:                    unless ($nodeeplinkcheck) {
 8452:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8453:                    }
 8454:                    if ($deeplinkblock) {
 8455:                        $thisallowed = 'D';
 8456:                    } elsif ($noblockcheck) {
 8457:                        $thisallowed.=$value;
 8458:                    } else {
 8459:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8460:                        if (@blockers > 0) {
 8461:                            $thisallowed = 'B';
 8462:                        } else {
 8463:                            $thisallowed.=$value;
 8464:                        }
 8465:                    }
 8466:                } else {
 8467:                    $thisallowed.=$value;
 8468:                }
 8469:                $checkreferer=0;
 8470:            }
 8471:        }
 8472: 
 8473:        if ($checkreferer) {
 8474: 	  my $refuri=$env{'httpref.'.$orguri};
 8475:             unless ($refuri) {
 8476:                 foreach my $key (keys(%env)) {
 8477: 		    if ($key=~/^httpref\..*\*/) {
 8478: 			my $pattern=$key;
 8479:                         $pattern=~s/^httpref\.\/res\///;
 8480:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8481:                         $pattern=~s/\//\\\//g;
 8482:                         if ($orguri=~/$pattern/) {
 8483: 			    $refuri=$env{$key};
 8484:                         }
 8485:                     }
 8486:                 }
 8487:             }
 8488: 
 8489:          if ($refuri) { 
 8490: 	  $refuri=&declutter($refuri);
 8491:           my ($match,$cond)=&is_on_map($refuri);
 8492:             if ($match) {
 8493:               my $refstatecond=$cond;
 8494:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8495:                   =~/\Q$priv\E\&([^\:]*)/) {
 8496:                   my $value = $1;
 8497:                   if ($priv eq 'bre') {
 8498:                       my $deeplinkblock;
 8499:                       unless ($nodeeplinkcheck) {
 8500:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8501:                       }
 8502:                       if ($deeplinkblock) {
 8503:                           $thisallowed = 'D';
 8504:                       } elsif ($noblockcheck) {
 8505:                           $thisallowed.=$value;
 8506:                       } else {
 8507:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8508:                           if (@blockers > 0) {
 8509:                               $thisallowed = 'B';
 8510:                           } else {
 8511:                               $thisallowed.=$value;
 8512:                           }
 8513:                       }
 8514:                   } else {
 8515:                       $thisallowed.=$value;
 8516:                   }
 8517:                   $uri=$refuri;
 8518:                   $statecond=$refstatecond;
 8519:               }
 8520:           }
 8521:         }
 8522:        }
 8523:    }
 8524: 
 8525: #
 8526: # Gathered now: all privileges that could apply, and condition number
 8527: # 
 8528: #
 8529: # Full or no access?
 8530: #
 8531: 
 8532:     if ($thisallowed=~/F/) {
 8533: 	return 'F';
 8534:     }
 8535: 
 8536:     unless ($thisallowed) {
 8537:         return '';
 8538:     }
 8539: 
 8540: # Restrictions exist, deal with them
 8541: #
 8542: #   C:according to course preferences
 8543: #   R:according to resource settings
 8544: #   L:unless locked
 8545: #   X:according to user session state
 8546: #
 8547: 
 8548: # Possibly locked functionality, check all courses
 8549: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
 8550: # Locks might take effect only after 10 minutes cache expiration for other
 8551: # courses, and 2 minutes for current course, in which user has st or ta role
 8552: # which is neither expired nor a future role (unless current course).
 8553: 
 8554:     my ($needlockcheck,$now,$crsonly);
 8555:     if ($thisallowed=~/L/) {
 8556:         $now = time;
 8557:         if ($priv eq 'bre') {
 8558:             if ($uri ne '') {
 8559:                 if ($orguri =~ m{^/+res/}) {
 8560:                     if ($uri =~ m{^lib/templates/}) {
 8561:                         if ($env{'request.course.id'}) {
 8562:                             $crsonly = 1;
 8563:                             $needlockcheck = 1;
 8564:                         }
 8565:                     } else {
 8566:                         $needlockcheck = 1;
 8567:                     }
 8568:                 } elsif ($env{'request.course.id'}) {
 8569:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
 8570:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
 8571:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
 8572:                         $crsonly = 1;
 8573:                     }
 8574:                     $needlockcheck = 1;
 8575:                 }
 8576:             }
 8577:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
 8578:             $needlockcheck = 1;
 8579:         }
 8580:     }
 8581:     if ($needlockcheck) {
 8582:         foreach my $envkey (keys(%env)) {
 8583:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8584:                my $courseid=$2;
 8585:                my $roleid=$1.'.'.$2;
 8586:                $courseid=~s/^\///;
 8587:                unless ($env{'request.role'} eq $roleid) {
 8588:                    my ($start,$end) = split(/\./,$env{$envkey});
 8589:                    next unless (($now >= $start) && (!$end || $end > $now));
 8590:                }
 8591:                my $expiretime=600;
 8592:                if ($env{'request.role'} eq $roleid) {
 8593: 		  $expiretime=120;
 8594:                }
 8595: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8596:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8597:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8598: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8599:                }
 8600:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8601:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8602: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8603:                        &log($env{'user.domain'},$env{'user.name'},
 8604:                             $env{'user.home'},
 8605:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8606:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8607:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8608: 		       return '';
 8609:                    }
 8610:                }
 8611:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8612:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8613: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
 8614:                        &log($env{'user.domain'},$env{'user.name'},
 8615:                             $env{'user.home'},
 8616:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8617:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8618:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8619: 		       return '';
 8620:                    }
 8621:                }
 8622: 	   }
 8623:        }
 8624:     }
 8625: 
 8626: #
 8627: # Rest of the restrictions depend on selected course
 8628: #
 8629: 
 8630:     unless ($env{'request.course.id'}) {
 8631: 	if ($thisallowed eq 'A') {
 8632: 	    return 'A';
 8633:         } elsif ($thisallowed eq 'B') {
 8634:             return 'B';
 8635: 	} else {
 8636: 	    return '1';
 8637: 	}
 8638:     }
 8639: 
 8640: #
 8641: # Now user is definitely in a course
 8642: #
 8643: 
 8644: 
 8645: # Course preferences
 8646: 
 8647:    if ($thisallowed=~/C/) {
 8648:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8649:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8650:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8651: 	   =~/\Q$rolecode\E/) {
 8652: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8653: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8654: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8655: 			$env{'request.course.id'});
 8656: 	   }
 8657:            return '';
 8658:        }
 8659: 
 8660:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8661: 	   =~/\Q$unamedom\E/) {
 8662: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8663: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8664: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8665: 			$env{'request.course.id'});
 8666: 	   }
 8667:            return '';
 8668:        }
 8669:    }
 8670: 
 8671: # Resource preferences
 8672: 
 8673:    if ($thisallowed=~/R/) {
 8674:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8675:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8676: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8677: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8678: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8679: 	   }
 8680: 	   return '';
 8681:        }
 8682:    }
 8683: 
 8684: # Restricted for deeplinked session?
 8685: 
 8686:     if ($env{'request.deeplink.login'}) {
 8687:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
 8688:             if (!$symb) { $symb=&symbread($uri,1); }
 8689:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
 8690:                 return '';
 8691:             }
 8692:         }
 8693:     }
 8694: 
 8695: # Restricted by state or randomout?
 8696: 
 8697:    if ($thisallowed=~/X/) {
 8698:       if ($env{'acc.randomout'}) {
 8699: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8700:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8701:             return ''; 
 8702:          }
 8703:       }
 8704:       if (&condval($statecond)) {
 8705: 	 return '2';
 8706:       } else {
 8707:          return '';
 8708:       }
 8709:    }
 8710: 
 8711:     if ($thisallowed eq 'A') {
 8712: 	return 'A';
 8713:     } elsif ($thisallowed eq 'B') {
 8714:         return 'B';
 8715:     } elsif ($thisallowed eq 'D') {
 8716:         return 'D';
 8717:     }
 8718:    return 'F';
 8719: }
 8720: 
 8721: # ------------------------------------------- Check construction space access
 8722: 
 8723: sub constructaccess {
 8724:     my ($url,$setpriv)=@_;
 8725: 
 8726: # We do not allow editing of previous versions of files
 8727:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8728: 
 8729: # Get username and domain from URL
 8730:     my ($ownername,$ownerdomain,$ownerhome);
 8731: 
 8732:     ($ownerdomain,$ownername) =
 8733:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8734: 
 8735: # The URL does not really point to any authorspace, forget it
 8736:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8737: 
 8738: # Now we need to see if the user has access to the authorspace of
 8739: # $ownername at $ownerdomain
 8740: 
 8741:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8742: # Real author for this?
 8743:        $ownerhome = $env{'user.home'};
 8744:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8745:           return ($ownername,$ownerdomain,$ownerhome);
 8746:        }
 8747:     } else {
 8748: # Co-author for this?
 8749:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8750:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8751:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8752:             return ($ownername,$ownerdomain,$ownerhome);
 8753:         }
 8754:         if ($env{'request.course.id'}) {
 8755:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8756:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8757:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8758:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8759:                     return ($ownername,$ownerdomain,$ownerhome);
 8760:                 }
 8761:             }
 8762:         }
 8763:     }
 8764: 
 8765: # We don't have any access right now. If we are not possibly going to do anything about this,
 8766: # we might as well leave
 8767:    unless ($setpriv) { return ''; }
 8768: 
 8769: # Backdoor access?
 8770:     my $allowed=&allowed('eco',$ownerdomain);
 8771: # Nope
 8772:     unless ($allowed) { return ''; }
 8773: # Looks like we may have access, but could be locked by the owner of the construction space
 8774:     if ($allowed eq 'U') {
 8775:         my %blocked=&get('environment',['domcoord.author'],
 8776:                          $ownerdomain,$ownername);
 8777: # Is blocked by owner
 8778:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8779:     }
 8780:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8781: # Grant temporary access
 8782:         my $then=$env{'user.login.time'};
 8783:         my $update=$env{'user.update.time'};
 8784:         if (!$update) { $update = $then; }
 8785:         my $refresh=$env{'user.refresh.time'};
 8786:         if (!$refresh) { $refresh = $update; }
 8787:         my $now = time;
 8788:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8789:                            $now,'ca','constructaccess');
 8790:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8791:         return($ownername,$ownerdomain,$ownerhome);
 8792:     }
 8793: # No business here
 8794:     return '';
 8795: }
 8796: 
 8797: # ----------------------------------------------------------- Content Blocking
 8798: 
 8799: {
 8800: # Caches for faster Course Contents display where content blocking
 8801: # is in operation (i.e., interval param set) for timed quiz.
 8802: #
 8803: # User for whom data are being temporarily cached.
 8804: my $cacheduser='';
 8805: # Course for which data are being temporarily cached.
 8806: my $cachedcid='';
 8807: # Cached blockers for this user (a hash of blocking items). 
 8808: my %cachedblockers=();
 8809: # When the data were last cached.
 8810: my $cachedlast='';
 8811: 
 8812: sub load_all_blockers {
 8813:     my ($uname,$udom)=@_;
 8814:     if (($uname ne '') && ($udom ne '')) { 
 8815:         if (($cacheduser eq $uname.':'.$udom) &&
 8816:             ($cachedcid eq $env{'request.course.id'}) &&
 8817:             (abs($cachedlast-time)<5)) {
 8818:             return;
 8819:         }
 8820:     }
 8821:     $cachedlast=time;
 8822:     $cacheduser=$uname.':'.$udom;
 8823:     $cachedcid=$env{'request.course.id'};
 8824:     %cachedblockers = &get_commblock_resources();
 8825:     return;
 8826: }
 8827: 
 8828: sub get_comm_blocks {
 8829:     my ($cdom,$cnum) = @_;
 8830:     if ($cdom eq '' || $cnum eq '') {
 8831:         return unless ($env{'request.course.id'});
 8832:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8833:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8834:     }
 8835:     my %commblocks;
 8836:     my $hashid=$cdom.'_'.$cnum;
 8837:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8838:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8839:         %commblocks = %{$blocksref};
 8840:     } else {
 8841:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8842:         my $cachetime = 600;
 8843:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8844:     }
 8845:     return %commblocks;
 8846: }
 8847: 
 8848: sub get_commblock_resources {
 8849:     my ($blocks) = @_;
 8850:     my %blockers = ();
 8851:     return %blockers unless ($env{'request.course.id'});
 8852:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8853:     my %commblocks;
 8854:     if (ref($blocks) eq 'HASH') {
 8855:         %commblocks = %{$blocks};
 8856:     } else {
 8857:         %commblocks = &get_comm_blocks();
 8858:     }
 8859:     return %blockers unless (keys(%commblocks) > 0); 
 8860:     my $navmap = Apache::lonnavmaps::navmap->new();
 8861:     return %blockers unless (ref($navmap));
 8862:     my $now = time;
 8863:     foreach my $block (keys(%commblocks)) {
 8864:         if ($block =~ /^(\d+)____(\d+)$/) {
 8865:             my ($start,$end) = ($1,$2);
 8866:             if ($start <= $now && $end >= $now) {
 8867:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8868:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8869:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8870:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8871:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8872:                             }
 8873:                         }
 8874:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8875:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8876:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8877:                             }
 8878:                         }
 8879:                     }
 8880:                 }
 8881:             }
 8882:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8883:             my $item = $1;
 8884:             my @to_test;
 8885:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8886:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8887:                     my @interval;
 8888:                     my $type = 'map';
 8889:                     if ($item eq 'course') {
 8890:                         $type = 'course';
 8891:                         @interval=&EXT("resource.0.interval");
 8892:                     } else {
 8893:                         if ($item =~ /___\d+___/) {
 8894:                             $type = 'resource';
 8895:                             @interval=&EXT("resource.0.interval",$item);
 8896:                             if (ref($navmap)) {                        
 8897:                                 my $res = $navmap->getBySymb($item); 
 8898:                                 push(@to_test,$res);
 8899:                             }
 8900:                         } else {
 8901:                             my $mapsymb = &symbread($item,1);
 8902:                             if ($mapsymb) {
 8903:                                 if (ref($navmap)) {
 8904:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8905:                                     if (ref($mapres)) {
 8906:                                         my $first = $mapres->map_start();
 8907:                                         my $finish = $mapres->map_finish();
 8908:                                         my $it = $navmap->getIterator($first,$finish,undef,0,0);
 8909:                                         if (ref($it)) {
 8910:                                             my $res;
 8911:                                             while ($res = $it->next(undef,1)) {
 8912:                                                 next unless (ref($res));
 8913:                                                 my $symb = $res->symb();
 8914:                                                 next if (($symb eq $mapsymb) || ($symb eq ''));
 8915:                                                 @interval=&EXT("resource.0.interval",$symb);
 8916:                                                 if ($interval[1] eq 'map') {
 8917:                                                     if ($res->answerable()) {
 8918:                                                         push(@to_test,$res);
 8919:                                                         last;
 8920:                                                     }
 8921:                                                 }
 8922:                                             }
 8923:                                         }
 8924:                                     }
 8925:                                 }
 8926:                             }
 8927:                         }
 8928:                     }
 8929:                     if ($interval[0] =~ /^(\d+)/) {
 8930:                         my $timelimit = $1; 
 8931:                         my $first_access;
 8932:                         if ($type eq 'resource') {
 8933:                             $first_access=&get_first_access($interval[1],$item);
 8934:                         } elsif ($type eq 'map') {
 8935:                             $first_access=&get_first_access($interval[1],undef,$item);
 8936:                         } else {
 8937:                             $first_access=&get_first_access($interval[1]);
 8938:                         }
 8939:                         if ($first_access) {
 8940:                             my $timesup = $first_access+$timelimit;
 8941:                             if ($timesup > $now) {
 8942:                                 my $activeblock;
 8943:                                 foreach my $res (@to_test) {
 8944:                                     if ($res->answerable()) {
 8945:                                         $activeblock = 1;
 8946:                                         last;
 8947:                                     }
 8948:                                 }
 8949:                                 if ($activeblock) {
 8950:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8951:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8952:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8953:                                          }
 8954:                                     }
 8955:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8956:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8957:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8958:                                         }
 8959:                                     }
 8960:                                 }
 8961:                             }
 8962:                         }
 8963:                     }
 8964:                 }
 8965:             }
 8966:         }
 8967:     }
 8968:     return %blockers;
 8969: }
 8970: 
 8971: sub has_comm_blocking {
 8972:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 8973:     my @blockers;
 8974:     return unless ($env{'request.course.id'});
 8975:     return unless ($priv eq 'bre');
 8976:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8977:     return if ($env{'request.state'} eq 'construct');
 8978:     my %blockinfo;
 8979:     if (ref($blocks) eq 'HASH') {
 8980:         %blockinfo = &get_commblock_resources($blocks);
 8981:     } else {
 8982:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 8983:         %blockinfo = %cachedblockers;
 8984:     }
 8985:     return unless (keys(%blockinfo) > 0);
 8986:     my (%possibles,@symbs);
 8987:     if (!$symb) {
 8988:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 8989:     }
 8990:     if ($symb) {
 8991:         @symbs = ($symb);
 8992:     } elsif (keys(%possibles)) { 
 8993:         @symbs = keys(%possibles);
 8994:     }
 8995:     my $noblock;
 8996:     foreach my $symb (@symbs) {
 8997:         last if ($noblock);
 8998:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8999:         foreach my $block (keys(%blockinfo)) {
 9000:             if ($block =~ /^firstaccess____(.+)$/) {
 9001:                 my $item = $1;
 9002:                 unless ($blocked) {
 9003:                     if (($item eq $map) || ($item eq $symb)) {
 9004:                         $noblock = 1;
 9005:                         last;
 9006:                     }
 9007:                 }
 9008:             }
 9009:             if (ref($blockinfo{$block}) eq 'HASH') {
 9010:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 9011:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 9012:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9013:                             push(@blockers,$block);
 9014:                         }
 9015:                     }
 9016:                 }
 9017:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 9018:                     if ($blockinfo{$block}{'maps'}{$map}) {
 9019:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9020:                             push(@blockers,$block);
 9021:                         }
 9022:                     }
 9023:                 }
 9024:             }
 9025:         }
 9026:     }
 9027:     unless ($noblock) { 
 9028:         return @blockers;
 9029:     }
 9030:     return;
 9031: }
 9032: }
 9033: 
 9034: sub deeplink_check {
 9035:     my ($priv,$symb,$uri) = @_;
 9036:     return unless ($env{'request.course.id'});
 9037:     return unless ($priv eq 'bre');
 9038:     return if ($env{'request.state'} eq 'construct');
 9039:     return if ($env{'request.role.adv'});
 9040:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9041:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9042:     my (%possibles,@symbs);
 9043:     if (!$symb) {
 9044:         $symb = &symbread($uri,1,1,1,\%possibles);
 9045:     }
 9046:     if ($symb) {
 9047:         @symbs = ($symb);
 9048:     } elsif (keys(%possibles)) {
 9049:         @symbs = keys(%possibles);
 9050:     }
 9051: 
 9052:     my ($deeplink_symb,$allow);
 9053:     if ($env{'request.deeplink.login'}) {
 9054:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
 9055:     }
 9056:     foreach my $symb (@symbs) {
 9057:         last if ($allow);
 9058:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9059:         if ($deeplink eq '') {
 9060:             $allow = 1;
 9061:         } else {
 9062:             my ($listed,$scope,$access) = split(/,/,$deeplink);
 9063:             if ($access eq 'any') {
 9064:                 $allow = 1;
 9065:             } elsif ($deeplink_symb) {
 9066:                 if ($access eq 'only') {
 9067:                     if ($scope eq 'res') {
 9068:                         if ($symb eq $deeplink_symb) {
 9069:                             $allow = 1;
 9070:                         }
 9071:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
 9072:                         my ($map_from_symb,$map_from_login); 
 9073:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
 9074:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
 9075:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
 9076:                         } else {
 9077:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
 9078:                         }
 9079:                         if (($map_from_symb) && ($map_from_login)) {
 9080:                             if ($map_from_symb eq $map_from_login) {
 9081:                                 $allow = 1;
 9082:                             } elsif ($scope eq 'rec') {
 9083:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
 9084:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
 9085:                                     $allow = 1;
 9086:                                 }
 9087:                             }
 9088:                         }
 9089:                     }
 9090:                 } else {
 9091:                     my ($acctype,$item) = split(/:/,$access);
 9092:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
 9093:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
 9094:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 9095:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
 9096:                                 $allow = 1;
 9097:                             }
 9098:                         }
 9099:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9100:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9101:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 9102:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
 9103:                                 $allow = 1;
 9104:                             }
 9105:                         }
 9106:                     }
 9107:                 }
 9108:             }
 9109:         }
 9110:     }
 9111:     return if ($allow);
 9112:     return 1;
 9113: }
 9114: 
 9115: # -------------------------------- Deversion and split uri into path an filename   
 9116: 
 9117: #
 9118: #   Removes the version from a URI and
 9119: #   splits it in to its filename and path to the filename.
 9120: #   Seems like File::Basename could have done this more clearly.
 9121: #   Parameters:
 9122: #      $uri   - input URI
 9123: #   Returns:
 9124: #     Two element list consisting of 
 9125: #     $pathname  - the URI up to and excluding the trailing /
 9126: #     $filename  - The part of the URI following the last /
 9127: #  NOTE:
 9128: #    Another realization of this is simply:
 9129: #    use File::Basename;
 9130: #    ...
 9131: #    $uri = shift;
 9132: #    $filename = basename($uri);
 9133: #    $path     = dirname($uri);
 9134: #    return ($filename, $path);
 9135: #
 9136: #     The implementation below is probably faster however.
 9137: #
 9138: sub split_uri_for_cond {
 9139:     my $uri=&deversion(&declutter(shift));
 9140:     my @uriparts=split(/\//,$uri);
 9141:     my $filename=pop(@uriparts);
 9142:     my $pathname=join('/',@uriparts);
 9143:     return ($pathname,$filename);
 9144: }
 9145: # --------------------------------------------------- Is a resource on the map?
 9146: 
 9147: sub is_on_map {
 9148:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9149:     #Trying to find the conditional for the file
 9150:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9151: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9152:     if ($match) {
 9153: 	return (1,$1);
 9154:     } else {
 9155: 	return (0,0);
 9156:     }
 9157: }
 9158: 
 9159: # --------------------------------------------------------- Get symb from alias
 9160: 
 9161: sub get_symb_from_alias {
 9162:     my $symb=shift;
 9163:     my ($map,$resid,$url)=&decode_symb($symb);
 9164: # Already is a symb
 9165:     if ($url) { return $symb; }
 9166: # Must be an alias
 9167:     my $aliassymb='';
 9168:     my %bighash;
 9169:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9170:                             &GDBM_READER(),0640)) {
 9171:         my $rid=$bighash{'mapalias_'.$symb};
 9172: 	if ($rid) {
 9173: 	    my ($mapid,$resid)=split(/\./,$rid);
 9174: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9175: 				    $resid,$bighash{'src_'.$rid});
 9176: 	}
 9177:         untie %bighash;
 9178:     }
 9179:     return $aliassymb;
 9180: }
 9181: 
 9182: # ----------------------------------------------------------------- Define Role
 9183: 
 9184: sub definerole {
 9185:   if (allowed('mcr','/')) {
 9186:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9187:     foreach my $role (split(':',$sysrole)) {
 9188: 	my ($crole,$cqual)=split(/\&/,$role);
 9189:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9190:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9191: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9192:                return "refused:s:$crole&$cqual"; 
 9193:             }
 9194:         }
 9195:     }
 9196:     foreach my $role (split(':',$domrole)) {
 9197: 	my ($crole,$cqual)=split(/\&/,$role);
 9198:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9199:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9200: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9201:                return "refused:d:$crole&$cqual"; 
 9202:             }
 9203:         }
 9204:     }
 9205:     foreach my $role (split(':',$courole)) {
 9206: 	my ($crole,$cqual)=split(/\&/,$role);
 9207:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9208:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9209: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9210:                return "refused:c:$crole&$cqual"; 
 9211:             }
 9212:         }
 9213:     }
 9214:     my $uhome;
 9215:     if (($uname ne '') && ($udom ne '')) {
 9216:         $uhome = &homeserver($uname,$udom);
 9217:         return $uhome if ($uhome eq 'no_host');
 9218:     } else {
 9219:         $uname = $env{'user.name'};
 9220:         $udom = $env{'user.domain'};
 9221:         $uhome = $env{'user.home'};
 9222:     }
 9223:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9224:                 "$udom:$uname:rolesdef_$rolename=".
 9225:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9226:     return reply($command,$uhome);
 9227:   } else {
 9228:     return 'refused';
 9229:   }
 9230: }
 9231: 
 9232: # ---------------- Make a metadata query against the network of library servers
 9233: 
 9234: sub metadata_query {
 9235:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9236:     my %rhash;
 9237:     my %libserv = &all_library();
 9238:     my @server_list = (defined($server_array) ? @$server_array
 9239:                                               : keys(%libserv) );
 9240:     for my $server (@server_list) {
 9241:         my $domains = ''; 
 9242:         if (ref($domains_hash) eq 'HASH') {
 9243:             $domains = $domains_hash->{$server}; 
 9244:         }
 9245: 	unless ($custom or $customshow) {
 9246: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9247: 	    $rhash{$server}=$reply;
 9248: 	}
 9249: 	else {
 9250: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9251: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9252: 			     $server);
 9253: 	    $rhash{$server}=$reply;
 9254: 	}
 9255:     }
 9256:     return \%rhash;
 9257: }
 9258: 
 9259: # ----------------------------------------- Send log queries and wait for reply
 9260: 
 9261: sub log_query {
 9262:     my ($uname,$udom,$query,%filters)=@_;
 9263:     my $uhome=&homeserver($uname,$udom);
 9264:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9265:     my $uhost=&hostname($uhome);
 9266:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9267:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9268:                        $uhome);
 9269:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9270:     return get_query_reply($queryid);
 9271: }
 9272: 
 9273: # -------------------------- Update MySQL table for portfolio file
 9274: 
 9275: sub update_portfolio_table {
 9276:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9277:     if ($group ne '') {
 9278:         $file_name =~s /^\Q$group\E//;
 9279:     }
 9280:     my $homeserver = &homeserver($uname,$udom);
 9281:     my $queryid=
 9282:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9283:                ':'.&escape($file_name).':'.$action,$homeserver);
 9284:     my $reply = &get_query_reply($queryid);
 9285:     return $reply;
 9286: }
 9287: 
 9288: # -------------------------- Update MySQL allusers table
 9289: 
 9290: sub update_allusers_table {
 9291:     my ($uname,$udom,$names) = @_;
 9292:     my $homeserver = &homeserver($uname,$udom);
 9293:     my $queryid=
 9294:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9295:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9296:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9297:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9298:                'generation='.&escape($names->{'generation'}).'%%'.
 9299:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9300:                'id='.&escape($names->{'id'}),$homeserver);
 9301:     return;
 9302: }
 9303: 
 9304: # ------- Request retrieval of institutional classlists for course(s)
 9305: 
 9306: sub fetch_enrollment_query {
 9307:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9308:     my ($homeserver,$sleep,$loopmax);
 9309:     my $maxtries = 1;
 9310:     if ($context eq 'automated') {
 9311:         $homeserver = $perlvar{'lonHostID'};
 9312:         $sleep = 2;
 9313:         $loopmax = 100;
 9314:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9315:     } else {
 9316:         $homeserver = &homeserver($cnum,$dom);
 9317:     }
 9318:     my $host=&hostname($homeserver);
 9319:     my $cmd = '';
 9320:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9321:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9322:     }
 9323:     $cmd =~ s/%%$//;
 9324:     $cmd = &escape($cmd);
 9325:     my $query = 'fetchenrollment';
 9326:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9327:     unless ($queryid=~/^\Q$host\E\_/) { 
 9328:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9329:         return 'error: '.$queryid;
 9330:     }
 9331:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9332:     my $tries = 1;
 9333:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9334:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9335:         $tries ++;
 9336:     }
 9337:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9338:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9339:     } else {
 9340:         my @responses = split(/:/,$reply);
 9341:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9342:             foreach my $line (@responses) {
 9343:                 my ($key,$value) = split(/=/,$line,2);
 9344:                 $$replyref{$key} = $value;
 9345:             }
 9346:         } else {
 9347:             my $pathname = LONCAPA::tempdir();
 9348:             foreach my $line (@responses) {
 9349:                 my ($key,$value) = split(/=/,$line);
 9350:                 $$replyref{$key} = $value;
 9351:                 if ($value > 0) {
 9352:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9353:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9354:                         my $destname = $pathname.'/'.$filename;
 9355:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9356:                         if ($xml_classlist =~ /^error/) {
 9357:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9358:                         } else {
 9359:                             if ( open(FILE,">",$destname) ) {
 9360:                                 print FILE &unescape($xml_classlist);
 9361:                                 close(FILE);
 9362:                             } else {
 9363:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9364:                             }
 9365:                         }
 9366:                     }
 9367:                 }
 9368:             }
 9369:         }
 9370:         return 'ok';
 9371:     }
 9372:     return 'error';
 9373: }
 9374: 
 9375: sub get_query_reply {
 9376:     my ($queryid,$sleep,$loopmax) = @_;;
 9377:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9378:         $sleep = 0.2;
 9379:     }
 9380:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9381:         $loopmax = 100;
 9382:     }
 9383:     my $replyfile=LONCAPA::tempdir().$queryid;
 9384:     my $reply='';
 9385:     for (1..$loopmax) {
 9386: 	sleep($sleep);
 9387:         if (-e $replyfile.'.end') {
 9388: 	    if (open(my $fh,"<",$replyfile)) {
 9389: 		$reply = join('',<$fh>);
 9390: 		close($fh);
 9391: 	   } else { return 'error: reply_file_error'; }
 9392:            return &unescape($reply);
 9393: 	}
 9394:     }
 9395:     return 'timeout:'.$queryid;
 9396: }
 9397: 
 9398: sub courselog_query {
 9399: #
 9400: # possible filters:
 9401: # url: url or symb
 9402: # username
 9403: # domain
 9404: # action: view, submit, grade
 9405: # start: timestamp
 9406: # end: timestamp
 9407: #
 9408:     my (%filters)=@_;
 9409:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9410:     if ($filters{'url'}) {
 9411: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9412:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9413:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9414:     }
 9415:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9416:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9417:     return &log_query($cname,$cdom,'courselog',%filters);
 9418: }
 9419: 
 9420: sub userlog_query {
 9421: #
 9422: # possible filters:
 9423: # action: log check role
 9424: # start: timestamp
 9425: # end: timestamp
 9426: #
 9427:     my ($uname,$udom,%filters)=@_;
 9428:     return &log_query($uname,$udom,'userlog',%filters);
 9429: }
 9430: 
 9431: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9432: 
 9433: sub auto_run {
 9434:     my ($cnum,$cdom) = @_;
 9435:     my $response = 0;
 9436:     my $settings;
 9437:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9438:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9439:         $settings = $domconfig{'autoenroll'};
 9440:         if ($settings->{'run'} eq '1') {
 9441:             $response = 1;
 9442:         }
 9443:     } else {
 9444:         my $homeserver;
 9445:         if (&is_course($cdom,$cnum)) {
 9446:             $homeserver = &homeserver($cnum,$cdom);
 9447:         } else {
 9448:             $homeserver = &domain($cdom,'primary');
 9449:         }
 9450:         if ($homeserver ne 'no_host') {
 9451:             $response = &reply('autorun:'.$cdom,$homeserver);
 9452:         }
 9453:     }
 9454:     return $response;
 9455: }
 9456: 
 9457: sub auto_get_sections {
 9458:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9459:     my $homeserver;
 9460:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9461:         $homeserver = &homeserver($cnum,$cdom);
 9462:     }
 9463:     if (!defined($homeserver)) { 
 9464:         if ($cdom =~ /^$match_domain$/) {
 9465:             $homeserver = &domain($cdom,'primary');
 9466:         }
 9467:     }
 9468:     my @secs;
 9469:     if (defined($homeserver)) {
 9470:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9471:         unless ($response eq 'refused') {
 9472:             @secs = split(/:/,$response);
 9473:         }
 9474:     }
 9475:     return @secs;
 9476: }
 9477: 
 9478: sub auto_new_course {
 9479:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9480:     my $homeserver = &homeserver($cnum,$cdom);
 9481:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9482:     return $response;
 9483: }
 9484: 
 9485: sub auto_validate_courseID {
 9486:     my ($cnum,$cdom,$inst_course_id) = @_;
 9487:     my $homeserver = &homeserver($cnum,$cdom);
 9488:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9489:     return $response;
 9490: }
 9491: 
 9492: sub auto_validate_instcode {
 9493:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9494:     my ($homeserver,$response);
 9495:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9496:         $homeserver = &homeserver($cnum,$cdom);
 9497:     }
 9498:     if (!defined($homeserver)) {
 9499:         if ($cdom =~ /^$match_domain$/) {
 9500:             $homeserver = &domain($cdom,'primary');
 9501:         }
 9502:     }
 9503:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9504:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9505:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9506:     return ($outcome,$description,$defaultcredits);
 9507: }
 9508: 
 9509: sub auto_validate_inst_crosslist {
 9510:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
 9511:     my ($homeserver,$response);
 9512:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9513:         $homeserver = &homeserver($cnum,$cdom);
 9514:     }
 9515:     if (!defined($homeserver)) {
 9516:         if ($cdom =~ /^$match_domain$/) {
 9517:             $homeserver = &domain($cdom,'primary');
 9518:         }
 9519:     }
 9520:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
 9521:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
 9522:                          &escape($instcode).':'.&escape($inst_xlist).':'.
 9523:                          &escape($coowner),$homeserver);
 9524:     }
 9525:     return $response;
 9526: }
 9527: 
 9528: sub auto_create_password {
 9529:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9530:     my ($homeserver,$response);
 9531:     my $create_passwd = 0;
 9532:     my $authchk = '';
 9533:     if ($udom =~ /^$match_domain$/) {
 9534:         $homeserver = &domain($udom,'primary');
 9535:     }
 9536:     if ($homeserver eq '') {
 9537:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9538:             $homeserver = &homeserver($cnum,$cdom);
 9539:         }
 9540:     }
 9541:     if ($homeserver eq '') {
 9542:         $authchk = 'nodomain';
 9543:     } else {
 9544:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9545:         if ($response eq 'refused') {
 9546:             $authchk = 'refused';
 9547:         } else {
 9548:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9549:         }
 9550:     }
 9551:     return ($authparam,$create_passwd,$authchk);
 9552: }
 9553: 
 9554: sub auto_photo_permission {
 9555:     my ($cnum,$cdom,$students) = @_;
 9556:     my $homeserver = &homeserver($cnum,$cdom);
 9557:     my ($outcome,$perm_reqd,$conditions) = 
 9558: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9559:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9560: 	return (undef,undef);
 9561:     }
 9562:     return ($outcome,$perm_reqd,$conditions);
 9563: }
 9564: 
 9565: sub auto_checkphotos {
 9566:     my ($uname,$udom,$pid) = @_;
 9567:     my $homeserver = &homeserver($uname,$udom);
 9568:     my ($result,$resulttype);
 9569:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9570: 				   &escape($uname).':'.&escape($pid),
 9571: 				   $homeserver));
 9572:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9573: 	return (undef,undef);
 9574:     }
 9575:     if ($outcome) {
 9576:         ($result,$resulttype) = split(/:/,$outcome);
 9577:     } 
 9578:     return ($result,$resulttype);
 9579: }
 9580: 
 9581: sub auto_photochoice {
 9582:     my ($cnum,$cdom) = @_;
 9583:     my $homeserver = &homeserver($cnum,$cdom);
 9584:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9585: 						       &escape($cdom),
 9586: 						       $homeserver)));
 9587:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9588: 	return (undef,undef);
 9589:     }
 9590:     return ($update,$comment);
 9591: }
 9592: 
 9593: sub auto_photoupdate {
 9594:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9595:     my $homeserver = &homeserver($cnum,$dom);
 9596:     my $host=&hostname($homeserver);
 9597:     my $cmd = '';
 9598:     my $maxtries = 1;
 9599:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9600:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9601:     }
 9602:     $cmd =~ s/%%$//;
 9603:     $cmd = &escape($cmd);
 9604:     my $query = 'institutionalphotos';
 9605:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9606:     unless ($queryid=~/^\Q$host\E\_/) {
 9607:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9608:         return 'error: '.$queryid;
 9609:     }
 9610:     my $reply = &get_query_reply($queryid);
 9611:     my $tries = 1;
 9612:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9613:         $reply = &get_query_reply($queryid);
 9614:         $tries ++;
 9615:     }
 9616:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9617:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9618:     } else {
 9619:         my @responses = split(/:/,$reply);
 9620:         my $outcome = shift(@responses); 
 9621:         foreach my $item (@responses) {
 9622:             my ($key,$value) = split(/=/,$item);
 9623:             $$photo{$key} = $value;
 9624:         }
 9625:         return $outcome;
 9626:     }
 9627:     return 'error';
 9628: }
 9629: 
 9630: sub auto_instcode_format {
 9631:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9632: 	$cat_order) = @_;
 9633:     my $courses = '';
 9634:     my @homeservers;
 9635:     if ($caller eq 'global') {
 9636: 	my %servers = &get_servers($codedom,'library');
 9637: 	foreach my $tryserver (keys(%servers)) {
 9638: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9639: 		push(@homeservers,$tryserver);
 9640: 	    }
 9641:         }
 9642:     } elsif ($caller eq 'requests') {
 9643:         if ($codedom =~ /^$match_domain$/) {
 9644:             my $chome = &domain($codedom,'primary');
 9645:             unless ($chome eq 'no_host') {
 9646:                 push(@homeservers,$chome);
 9647:             }
 9648:         }
 9649:     } else {
 9650:         push(@homeservers,&homeserver($caller,$codedom));
 9651:     }
 9652:     foreach my $code (keys(%{$instcodes})) {
 9653:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9654:     }
 9655:     chop($courses);
 9656:     my $ok_response = 0;
 9657:     my $response;
 9658:     while (@homeservers > 0 && $ok_response == 0) {
 9659:         my $server = shift(@homeservers); 
 9660:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9661:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9662:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9663: 		split(/:/,$response);
 9664:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9665:             push(@{$codetitles},&str2array($codetitles_str));
 9666:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9667:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9668:             $ok_response = 1;
 9669:         }
 9670:     }
 9671:     if ($ok_response) {
 9672:         return 'ok';
 9673:     } else {
 9674:         return $response;
 9675:     }
 9676: }
 9677: 
 9678: sub auto_instcode_defaults {
 9679:     my ($domain,$returnhash,$code_order) = @_;
 9680:     my @homeservers;
 9681: 
 9682:     my %servers = &get_servers($domain,'library');
 9683:     foreach my $tryserver (keys(%servers)) {
 9684: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9685: 	    push(@homeservers,$tryserver);
 9686: 	}
 9687:     }
 9688: 
 9689:     my $response;
 9690:     foreach my $server (@homeservers) {
 9691:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9692:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9693: 	
 9694: 	foreach my $pair (split(/\&/,$response)) {
 9695: 	    my ($name,$value)=split(/\=/,$pair);
 9696: 	    if ($name eq 'code_order') {
 9697: 		@{$code_order} = split(/\&/,&unescape($value));
 9698: 	    } else {
 9699: 		$returnhash->{&unescape($name)}=&unescape($value);
 9700: 	    }
 9701: 	}
 9702: 	return 'ok';
 9703:     }
 9704: 
 9705:     return $response;
 9706: }
 9707: 
 9708: sub auto_possible_instcodes {
 9709:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9710:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9711:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9712:         return;
 9713:     }
 9714:     my (@homeservers,$uhome);
 9715:     if (defined(&domain($domain,'primary'))) {
 9716:         $uhome=&domain($domain,'primary');
 9717:         push(@homeservers,&domain($domain,'primary'));
 9718:     } else {
 9719:         my %servers = &get_servers($domain,'library');
 9720:         foreach my $tryserver (keys(%servers)) {
 9721:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9722:                 push(@homeservers,$tryserver);
 9723:             }
 9724:         }
 9725:     }
 9726:     my $response;
 9727:     foreach my $server (@homeservers) {
 9728:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9729:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9730:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9731:             split(':',$response);
 9732:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9733:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9734:         foreach my $item (split('&',$cat_title)) {   
 9735:             my ($name,$value)=split('=',$item);
 9736:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9737:         }
 9738:         foreach my $item (split('&',$cat_order)) {
 9739:             my ($name,$value)=split('=',$item);
 9740:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9741:         }
 9742:         return 'ok';
 9743:     }
 9744:     return $response;
 9745: }
 9746: 
 9747: sub auto_courserequest_checks {
 9748:     my ($dom) = @_;
 9749:     my ($homeserver,%validations);
 9750:     if ($dom =~ /^$match_domain$/) {
 9751:         $homeserver = &domain($dom,'primary');
 9752:     }
 9753:     unless ($homeserver eq 'no_host') {
 9754:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9755:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9756:             my @items = split(/&/,$response);
 9757:             foreach my $item (@items) {
 9758:                 my ($key,$value) = split('=',$item);
 9759:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9760:             }
 9761:         }
 9762:     }
 9763:     return %validations; 
 9764: }
 9765: 
 9766: sub auto_courserequest_validation {
 9767:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9768:     my ($homeserver,$response);
 9769:     if ($dom =~ /^$match_domain$/) {
 9770:         $homeserver = &domain($dom,'primary');
 9771:     }
 9772:     unless ($homeserver eq 'no_host') {
 9773:         my $customdata;
 9774:         if (ref($custominfo) eq 'HASH') {
 9775:             $customdata = &freeze_escape($custominfo);
 9776:         }
 9777:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9778:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9779:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9780:                                     $customdata,$homeserver));
 9781:     }
 9782:     return $response;
 9783: }
 9784: 
 9785: sub auto_validate_class_sec {
 9786:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9787:     my $homeserver = &homeserver($cnum,$cdom);
 9788:     my $ownerlist;
 9789:     if (ref($owners) eq 'ARRAY') {
 9790:         $ownerlist = join(',',@{$owners});
 9791:     } else {
 9792:         $ownerlist = $owners;
 9793:     }
 9794:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9795:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9796:     return $response;
 9797: }
 9798: 
 9799: sub auto_instsec_reformat {
 9800:     my ($cdom,$action,$instsecref) = @_;
 9801:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
 9802:     my @homeservers;
 9803:     if (defined(&domain($cdom,'primary'))) {
 9804:         push(@homeservers,&domain($cdom,'primary'));
 9805:     } else {
 9806:         my %servers = &get_servers($cdom,'library');
 9807:         foreach my $tryserver (keys(%servers)) {
 9808:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9809:                 push(@homeservers,$tryserver);
 9810:             }
 9811:         }
 9812:     }
 9813:     my $response;
 9814:     my %reformatted = %{$instsecref};
 9815:     foreach my $server (@homeservers) {
 9816:         if (ref($instsecref) eq 'HASH') {
 9817:             my $info = &freeze_escape($instsecref);
 9818:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
 9819:                                 $action.':'.$info,$server);
 9820:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
 9821:             my @items = split(/&/,$response);
 9822:             foreach my $item (@items) {
 9823:                 my ($key,$value) = split(/=/,$item);
 9824:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
 9825:             }
 9826:         }
 9827:     }
 9828:     return %reformatted;
 9829: }
 9830: 
 9831: sub auto_validate_instclasses {
 9832:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9833:     my ($homeserver,%validations);
 9834:     $homeserver = &homeserver($cnum,$cdom);
 9835:     unless ($homeserver eq 'no_host') {
 9836:         my $ownerlist;
 9837:         if (ref($owners) eq 'ARRAY') {
 9838:             $ownerlist = join(',',@{$owners});
 9839:         } else {
 9840:             $ownerlist = $owners;
 9841:         }
 9842:         if (ref($classesref) eq 'HASH') {
 9843:             my $classes = &freeze_escape($classesref);
 9844:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9845:                                 ':'.$cdom.':'.$classes,$homeserver);
 9846:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9847:                 my @items = split(/&/,$response);
 9848:                 foreach my $item (@items) {
 9849:                     my ($key,$value) = split('=',$item);
 9850:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9851:                 }
 9852:             }
 9853:         }
 9854:     }
 9855:     return %validations;
 9856: }
 9857: 
 9858: sub auto_crsreq_update {
 9859:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9860:         $code,$accessstart,$accessend,$inbound) = @_;
 9861:     my ($homeserver,%crsreqresponse);
 9862:     if ($cdom =~ /^$match_domain$/) {
 9863:         $homeserver = &domain($cdom,'primary');
 9864:     }
 9865:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9866:         my $info;
 9867:         if (ref($inbound) eq 'HASH') {
 9868:             $info = &freeze_escape($inbound);
 9869:         }
 9870:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9871:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9872:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9873:                             &escape($title).':'.&escape($code).':'.
 9874:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9875:                             $homeserver);
 9876:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9877:             my @items = split(/&/,$response);
 9878:             foreach my $item (@items) {
 9879:                 my ($key,$value) = split('=',$item);
 9880:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9881:             }
 9882:         }
 9883:     }
 9884:     return \%crsreqresponse;
 9885: }
 9886: 
 9887: sub auto_export_grades {
 9888:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9889:     my ($homeserver,%exportresponse);
 9890:     if ($cdom =~ /^$match_domain$/) {
 9891:         $homeserver = &domain($cdom,'primary');
 9892:     }
 9893:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9894:         my $info;
 9895:         if (ref($inforef) eq 'HASH') {
 9896:             $info = &freeze_escape($inforef);
 9897:         }
 9898:         if (ref($gradesref) eq 'HASH') {
 9899:             my $grades = &freeze_escape($gradesref);
 9900:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9901:                                 $info.':'.$grades,$homeserver);
 9902:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9903:                 my @items = split(/&/,$response);
 9904:                 foreach my $item (@items) {
 9905:                     my ($key,$value) = split('=',$item);
 9906:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9907:                 }
 9908:             }
 9909:         }
 9910:     }
 9911:     return \%exportresponse;
 9912: }
 9913: 
 9914: sub check_instcode_cloning {
 9915:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9916:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9917:         return;
 9918:     }
 9919:     my $canclone;
 9920:     if (@{$code_order} > 0) {
 9921:         my $instcoderegexp ='^';
 9922:         my @clonecodes = split(/\&/,$cloner);
 9923:         foreach my $item (@{$code_order}) {
 9924:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9925:                 foreach my $pair (@clonecodes) {
 9926:                     my ($key,$val) = split(/\=/,$pair,2);
 9927:                     $val = &unescape($val);
 9928:                     if ($key eq $item) {
 9929:                         $instcoderegexp .= '('.$val.')';
 9930:                         last;
 9931:                     }
 9932:                 }
 9933:             } else {
 9934:                 $instcoderegexp .= $codedefaults->{$item};
 9935:             }
 9936:         }
 9937:         $instcoderegexp .= '$';
 9938:         my (@from,@to);
 9939:         eval {
 9940:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9941:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9942:         };
 9943:         if ((@from > 0) && (@to > 0)) {
 9944:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9945:             if (!@diffs) {
 9946:                 $canclone = 1;
 9947:             }
 9948:         }
 9949:     }
 9950:     return $canclone;
 9951: }
 9952: 
 9953: sub default_instcode_cloning {
 9954:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9955:     my (%codedefaults,@code_order,$canclone);
 9956:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9957:         %codedefaults = %{$codedefaultsref};
 9958:         @code_order = @{$codeorderref};
 9959:     } elsif ($clonedom) {
 9960:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 9961:     }
 9962:     if (($domdefclone) && (@code_order)) {
 9963:         my @clonecodes = split(/\+/,$domdefclone);
 9964:         my $instcoderegexp ='^';
 9965:         foreach my $item (@code_order) {
 9966:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 9967:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 9968:             } else {
 9969:                 $instcoderegexp .= $codedefaults{$item};
 9970:             }
 9971:         }
 9972:         $instcoderegexp .= '$';
 9973:         my (@from,@to);
 9974:         eval {
 9975:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9976:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 9977:         };
 9978:         if ((@from > 0) && (@to > 0)) {
 9979:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9980:             if (!@diffs) {
 9981:                 $canclone = 1;
 9982:             }
 9983:         }
 9984:     }
 9985:     return $canclone;
 9986: }
 9987: 
 9988: # ------------------------------------------------------- Course Group routines
 9989: 
 9990: sub get_coursegroups {
 9991:     my ($cdom,$cnum,$group,$namespace) = @_;
 9992:     return(&dump($namespace,$cdom,$cnum,$group));
 9993: }
 9994: 
 9995: sub modify_coursegroup {
 9996:     my ($cdom,$cnum,$groupsettings) = @_;
 9997:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9998: }
 9999: 
10000: sub toggle_coursegroup_status {
10001:     my ($cdom,$cnum,$group,$action) = @_;
10002:     my ($from_namespace,$to_namespace);
10003:     if ($action eq 'delete') {
10004:         $from_namespace = 'coursegroups';
10005:         $to_namespace = 'deleted_groups';
10006:     } else {
10007:         $from_namespace = 'deleted_groups';
10008:         $to_namespace = 'coursegroups';
10009:     }
10010:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
10011:     if (my $tmp = &error(%curr_group)) {
10012:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10013:         return ('read error',$tmp);
10014:     } else {
10015:         my %savedsettings = %curr_group; 
10016:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
10017:         my $deloutcome;
10018:         if ($result eq 'ok') {
10019:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
10020:         } else {
10021:             return ('write error',$result);
10022:         }
10023:         if ($deloutcome eq 'ok') {
10024:             return 'ok';
10025:         } else {
10026:             return ('delete error',$deloutcome);
10027:         }
10028:     }
10029: }
10030: 
10031: sub modify_group_roles {
10032:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
10033:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10034:     my $role = 'gr/'.&escape($userprivs);
10035:     my ($uname,$udom) = split(/:/,$user);
10036:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
10037:     if ($result eq 'ok') {
10038:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10039:     }
10040:     return $result;
10041: }
10042: 
10043: sub modify_coursegroup_membership {
10044:     my ($cdom,$cnum,$membership) = @_;
10045:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
10046:     return $result;
10047: }
10048: 
10049: sub get_active_groups {
10050:     my ($udom,$uname,$cdom,$cnum) = @_;
10051:     my $now = time;
10052:     my %groups = ();
10053:     foreach my $key (keys(%env)) {
10054:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
10055:             my ($start,$end) = split(/\./,$env{$key});
10056:             if (($end!=0) && ($end<$now)) { next; }
10057:             if (($start!=0) && ($start>$now)) { next; }
10058:             if ($1 eq $cdom && $2 eq $cnum) {
10059:                 $groups{$3} = $env{$key} ;
10060:             }
10061:         }
10062:     }
10063:     return %groups;
10064: }
10065: 
10066: sub get_group_membership {
10067:     my ($cdom,$cnum,$group) = @_;
10068:     return(&dump('groupmembership',$cdom,$cnum,$group));
10069: }
10070: 
10071: sub get_users_groups {
10072:     my ($udom,$uname,$courseid) = @_;
10073:     my @usersgroups;
10074:     my $cachetime=1800;
10075: 
10076:     my $hashid="$udom:$uname:$courseid";
10077:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10078:     if (defined($cached)) {
10079:         @usersgroups = split(/:/,$grouplist);
10080:     } else {  
10081:         $grouplist = '';
10082:         my $courseurl = &courseid_to_courseurl($courseid);
10083:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
10084:         my $access_end = $env{'course.'.$courseid.
10085:                               '.default_enrollment_end_date'};
10086:         my $now = time;
10087:         foreach my $key (keys(%roleshash)) {
10088:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10089:                 my $group = $1;
10090:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10091:                     my $start = $2;
10092:                     my $end = $1;
10093:                     if ($start == -1) { next; } # deleted from group
10094:                     if (($start!=0) && ($start>$now)) { next; }
10095:                     if (($end!=0) && ($end<$now)) {
10096:                         if ($access_end && $access_end < $now) {
10097:                             if ($access_end - $end < 86400) {
10098:                                 push(@usersgroups,$group);
10099:                             }
10100:                         }
10101:                         next;
10102:                     }
10103:                     push(@usersgroups,$group);
10104:                 }
10105:             }
10106:         }
10107:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10108:         $grouplist = join(':',@usersgroups);
10109:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10110:     }
10111:     return @usersgroups;
10112: }
10113: 
10114: sub devalidate_getgroups_cache {
10115:     my ($udom,$uname,$cdom,$cnum)=@_;
10116:     my $courseid = $cdom.'_'.$cnum;
10117: 
10118:     my $hashid="$udom:$uname:$courseid";
10119:     &devalidate_cache_new('getgroups',$hashid);
10120: }
10121: 
10122: # ------------------------------------------------------------------ Plain Text
10123: 
10124: sub plaintext {
10125:     my ($short,$type,$cid,$forcedefault) = @_;
10126:     if ($short =~ m{^cr/}) {
10127: 	return (split('/',$short))[-1];
10128:     }
10129:     if (!defined($cid)) {
10130:         $cid = $env{'request.course.id'};
10131:     }
10132:     my %rolenames = (
10133:                       Course    => 'std',
10134:                       Community => 'alt1',
10135:                       Placement => 'std',
10136:                     );
10137:     if ($cid ne '') {
10138:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10139:             unless ($forcedefault) {
10140:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10141:                 &Apache::lonlocal::mt_escape(\$roletext);
10142:                 return &Apache::lonlocal::mt($roletext);
10143:             }
10144:         }
10145:     }
10146:     if ((defined($type)) && (defined($rolenames{$type})) &&
10147:         (defined($rolenames{$type})) && 
10148:         (defined($prp{$short}{$rolenames{$type}}))) {
10149:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10150:     } elsif ($cid ne '') {
10151:         my $crstype = $env{'course.'.$cid.'.type'};
10152:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10153:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10154:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10155:         }
10156:     }
10157:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10158: }
10159: 
10160: # ----------------------------------------------------------------- Assign Role
10161: 
10162: sub assignrole {
10163:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10164:         $context)=@_;
10165:     my $mrole;
10166:     if ($role =~ /^cr\//) {
10167:         my $cwosec=$url;
10168:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10169: 	unless (&allowed('ccr',$cwosec)) {
10170:            my $refused = 1;
10171:            if ($context eq 'requestcourses') {
10172:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10173:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10174:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10175:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10176:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10177:                            if ($crsenv{'internal.courseowner'} eq
10178:                                $env{'user.name'}.':'.$env{'user.domain'}) {
10179:                                $refused = '';
10180:                            }
10181:                        }
10182:                    }
10183:                }
10184:            }
10185:            if ($refused) {
10186:                &logthis('Refused custom assignrole: '.
10187:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10188:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10189:                return 'refused';
10190:            }
10191:         }
10192:         $mrole='cr';
10193:     } elsif ($role =~ /^gr\//) {
10194:         my $cwogrp=$url;
10195:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10196:         unless (&allowed('mdg',$cwogrp)) {
10197:             &logthis('Refused group assignrole: '.
10198:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10199:                     $env{'user.name'}.' at '.$env{'user.domain'});
10200:             return 'refused';
10201:         }
10202:         $mrole='gr';
10203:     } else {
10204:         my $cwosec=$url;
10205:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10206:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10207:             my $refused;
10208:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10209:                 if (!(&allowed('c'.$role,$url))) {
10210:                     $refused = 1;
10211:                 }
10212:             } else {
10213:                 $refused = 1;
10214:             }
10215:             if ($refused) {
10216:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10217:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10218:                     my %crsenv;
10219:                     if ($role eq 'cc' || $role eq 'co') {
10220:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10221:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10222:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10223:                                 if ($crsenv{'internal.courseowner'} eq 
10224:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10225:                                     $refused = '';
10226:                                 }
10227:                             }
10228:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10229:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10230:                                 if ($crsenv{'internal.courseowner'} eq 
10231:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10232:                                     $refused = '';
10233:                                 }
10234:                             }
10235:                         }
10236:                     }
10237:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10238:                     if ($role eq 'st') {
10239:                         $refused = '';
10240:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10241:                         $refused = '';
10242:                     }
10243:                 } elsif ($context eq 'requestcourses') {
10244:                     my @possroles = ('st','ta','ep','in','cc','co');
10245:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10246:                         my $wrongcc;
10247:                         if ($cnum =~ /^$match_community$/) {
10248:                             $wrongcc = 1 if ($role eq 'cc');
10249:                         } else {
10250:                             $wrongcc = 1 if ($role eq 'co');
10251:                         }
10252:                         unless ($wrongcc) {
10253:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10254:                             if ($crsenv{'internal.courseowner'} eq 
10255:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10256:                                 $refused = '';
10257:                             }
10258:                         }
10259:                     }
10260:                 } elsif ($context eq 'requestauthor') {
10261:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10262:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10263:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10264:                             $refused = '';
10265:                         } else {
10266:                             my %domdefaults = &get_domain_defaults($udom);
10267:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10268:                                 my $checkbystatus;
10269:                                 if ($env{'user.adv'}) { 
10270:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10271:                                     if ($disposition eq 'automatic') {
10272:                                         $refused = '';
10273:                                     } elsif ($disposition eq '') {
10274:                                         $checkbystatus = 1;
10275:                                     } 
10276:                                 } else {
10277:                                     $checkbystatus = 1;
10278:                                 }
10279:                                 if ($checkbystatus) {
10280:                                     if ($env{'environment.inststatus'}) {
10281:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10282:                                         foreach my $type (@inststatuses) {
10283:                                             if (($type ne '') &&
10284:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10285:                                                 $refused = '';
10286:                                             }
10287:                                         }
10288:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10289:                                         $refused = '';
10290:                                     }
10291:                                 }
10292:                             }
10293:                         }
10294:                     }
10295:                 }
10296:                 if ($refused) {
10297:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10298:                              ' '.$role.' '.$end.' '.$start.' by '.
10299: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10300:                     return 'refused';
10301:                 }
10302:             }
10303:         } elsif ($role eq 'au') {
10304:             if ($url ne '/'.$udom.'/') {
10305:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10306:                          ' to assign author role for '.$uname.':'.$udom.
10307:                          ' in domain: '.$url.' refused (wrong domain).');
10308:                 return 'refused';
10309:             }
10310:         }
10311:         $mrole=$role;
10312:     }
10313:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10314:                 "$udom:$uname:$url".'_'."$mrole=$role";
10315:     if ($end) { $command.='_'.$end; }
10316:     if ($start) {
10317: 	if ($end) { 
10318:            $command.='_'.$start; 
10319:         } else {
10320:            $command.='_0_'.$start;
10321:         }
10322:     }
10323:     my $origstart = $start;
10324:     my $origend = $end;
10325:     my $delflag;
10326: # actually delete
10327:     if ($deleteflag) {
10328: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10329: # modify command to delete the role
10330:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10331:                 "$udom:$uname:$url".'_'."$mrole";
10332: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10333: # set start and finish to negative values for userrolelog
10334:            $start=-1;
10335:            $end=-1;
10336:            $delflag = 1;
10337:         }
10338:     }
10339: # send command
10340:     my $answer=&reply($command,&homeserver($uname,$udom));
10341: # log new user role if status is ok
10342:     if ($answer eq 'ok') {
10343: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10344:         if (($role eq 'cc') || ($role eq 'in') ||
10345:             ($role eq 'ep') || ($role eq 'ad') ||
10346:             ($role eq 'ta') || ($role eq 'st') ||
10347:             ($role=~/^cr/) || ($role eq 'gr') ||
10348:             ($role eq 'co')) {
10349: # for course roles, perform group memberships changes triggered by role change.
10350:             unless ($role =~ /^gr/) {
10351:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10352:                                                  $origstart,$selfenroll,$context);
10353:             }
10354:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10355:                            $selfenroll,$context);
10356:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10357:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10358:                  ($role eq 'da')) {
10359:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10360:                            $context);
10361:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10362:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10363:                              $context); 
10364:         }
10365:         if ($role eq 'cc') {
10366:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10367:         }
10368:     }
10369:     return $answer;
10370: }
10371: 
10372: sub autoupdate_coowners {
10373:     my ($url,$end,$start,$uname,$udom) = @_;
10374:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10375:     if (($cdom ne '') && ($cnum ne '')) {
10376:         my $now = time;
10377:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10378:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10379:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10380:             my $instcode = $coursehash{'internal.coursecode'};
10381:             my $xlists = $coursehash{'internal.crosslistings'};
10382:             if ($instcode ne '') {
10383:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10384:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10385:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10386:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10387:                         unless ($result eq 'valid') {
10388:                             if ($xlists ne '') {
10389:                                 foreach my $xlist (split(',',$xlists)) {
10390:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
10391:                                     $result =
10392:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10393:                                                                       $inst_crosslist,$uname.':'.$udom);
10394:                                     last if ($result eq 'valid');
10395:                                 }
10396:                             }
10397:                         }
10398:                         if ($result eq 'valid') {
10399:                             if ($coursehash{'internal.co-owners'}) {
10400:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10401:                                     push(@newcoowners,$coowner);
10402:                                 }
10403:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10404:                                     push(@newcoowners,$uname.':'.$udom);
10405:                                 }
10406:                                 @newcoowners = sort(@newcoowners);
10407:                             } else {
10408:                                 push(@newcoowners,$uname.':'.$udom);
10409:                             }
10410:                         } elsif ($coursehash{'internal.co-owners'}) {
10411:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10412:                                 unless ($coowner eq $uname.':'.$udom) {
10413:                                     push(@newcoowners,$coowner);
10414:                                 }
10415:                             }
10416:                             unless (@newcoowners > 0) {
10417:                                 $delcoowners = 1;
10418:                                 $coowners = '';
10419:                             }
10420:                         }
10421:                         if (@newcoowners || $delcoowners) {
10422:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10423:                                             $delcoowners,@newcoowners);
10424:                         }
10425:                     }
10426:                 }
10427:             }
10428:         }
10429:     }
10430: }
10431: 
10432: sub store_coowners {
10433:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10434:     my $cid = $cdom.'_'.$cnum;
10435:     my ($coowners,$delresult,$putresult);
10436:     if (@newcoowners) {
10437:         $coowners = join(',',@newcoowners);
10438:         my %coownershash = (
10439:                             'internal.co-owners' => $coowners,
10440:                            );
10441:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10442:         if ($putresult eq 'ok') {
10443:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10444:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10445:             }
10446:         }
10447:     }
10448:     if ($delcoowners) {
10449:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10450:         if ($delresult eq 'ok') {
10451:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10452:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10453:             }
10454:         }
10455:     }
10456:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10457:         my %crsinfo =
10458:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10459:         if (ref($crsinfo{$cid}) eq 'HASH') {
10460:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10461:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10462:         }
10463:     }
10464: }
10465: 
10466: # -------------------------------------------------- Modify user authentication
10467: # Overrides without validation
10468: 
10469: sub modifyuserauth {
10470:     my ($udom,$uname,$umode,$upass)=@_;
10471:     my $uhome=&homeserver($uname,$udom);
10472:     my $allowed;
10473:     if (&allowed('mau',$udom)) {
10474:         $allowed = 1;
10475:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10476:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10477:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10478:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10479:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10480:         if (($cdom ne '') && ($cnum ne '')) {
10481:             my $is_owner = &is_course_owner($cdom,$cnum);
10482:             if ($is_owner) {
10483:                 $allowed = 1;
10484:             }
10485:         }
10486:     }
10487:     unless ($allowed) { return 'refused'; }
10488:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10489:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10490:              ' in domain '.$env{'request.role.domain'});  
10491:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10492: 		     &escape($upass),$uhome);
10493:     my $ip = &get_requestor_ip();
10494:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10495:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10496:          '(Remote '.$ip.'): '.$reply);
10497:     &log($udom,,$uname,$uhome,
10498:         'Authentication changed by '.$env{'user.domain'}.', '.
10499:                                      $env{'user.name'}.', '.$umode.
10500:          '(Remote '.$ip.'): '.$reply);
10501:     unless ($reply eq 'ok') {
10502:         &logthis('Authentication mode error: '.$reply);
10503: 	return 'error: '.$reply;
10504:     }   
10505:     return 'ok';
10506: }
10507: 
10508: # --------------------------------------------------------------- Modify a user
10509: 
10510: sub modifyuser {
10511:     my ($udom,    $uname, $uid,
10512:         $umode,   $upass, $first,
10513:         $middle,  $last,  $gene,
10514:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10515:     $udom= &LONCAPA::clean_domain($udom);
10516:     $uname=&LONCAPA::clean_username($uname);
10517:     my $showcandelete = 'none';
10518:     if (ref($candelete) eq 'ARRAY') {
10519:         if (@{$candelete} > 0) {
10520:             $showcandelete = join(', ',@{$candelete});
10521:         }
10522:     }
10523:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10524:              $umode.', '.$first.', '.$middle.', '.
10525: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10526:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10527:                                      ' desiredhome not specified'). 
10528:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10529:              ' in domain '.$env{'request.role.domain'});
10530:     my $uhome=&homeserver($uname,$udom,'true');
10531:     my $newuser;
10532:     if ($uhome eq 'no_host') {
10533:         $newuser = 1;
10534:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10535:                 ($umode eq 'lti')) {
10536:             return 'error: more information needed to create new user';
10537:         }
10538:     }
10539: # ----------------------------------------------------------------- Create User
10540:     if (($uhome eq 'no_host') && 
10541: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10542:         my $unhome='';
10543:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10544:             $unhome = $desiredhome;
10545: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10546: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10547:         } else { # load balancing routine for determining $unhome
10548:             my $loadm=10000000;
10549: 	    my %servers = &get_servers($udom,'library');
10550: 	    foreach my $tryserver (keys(%servers)) {
10551: 		my $answer=reply('load',$tryserver);
10552: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10553: 		    $loadm=$answer;
10554: 		    $unhome=$tryserver;
10555: 		}
10556: 	    }
10557:         }
10558:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10559: 	    return 'error: unable to find a home server for '.$uname.
10560:                    ' in domain '.$udom;
10561:         }
10562:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10563:                          &escape($upass),$unhome);
10564: 	unless ($reply eq 'ok') {
10565:             return 'error: '.$reply;
10566:         }   
10567:         $uhome=&homeserver($uname,$udom,'true');
10568:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10569: 	    return 'error: unable verify users home machine.';
10570:         }
10571:     }   # End of creation of new user
10572: # ---------------------------------------------------------------------- Add ID
10573:     if ($uid) {
10574:        $uid=~tr/A-Z/a-z/;
10575:        my %uidhash=&idrget($udom,$uname);
10576:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10577:          && (!$forceid)) {
10578: 	  unless ($uid eq $uidhash{$uname}) {
10579: 	      return 'error: user id "'.$uid.'" does not match '.
10580:                   'current user id "'.$uidhash{$uname}.'".';
10581:           }
10582:        } else {
10583: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10584:        }
10585:     }
10586: # -------------------------------------------------------------- Add names, etc
10587:     my @tmp=&get('environment',
10588: 		   ['firstname','middlename','lastname','generation','id',
10589:                     'permanentemail','inststatus'],
10590: 		   $udom,$uname);
10591:     my (%names,%oldnames);
10592:     if ($tmp[0] =~ m/^error:.*/) { 
10593:         %names=(); 
10594:     } else {
10595:         %names = @tmp;
10596:         %oldnames = %names;
10597:     }
10598: #
10599: # If name, email and/or uid are blank (e.g., because an uploaded file
10600: # of users did not contain them), do not overwrite existing values
10601: # unless field is in $candelete array ref.  
10602: #
10603: 
10604:     my @fields = ('firstname','middlename','lastname','generation',
10605:                   'permanentemail','id');
10606:     my %newvalues;
10607:     if (ref($candelete) eq 'ARRAY') {
10608:         foreach my $field (@fields) {
10609:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10610:                 if ($field eq 'firstname') {
10611:                     $names{$field} = $first;
10612:                 } elsif ($field eq 'middlename') {
10613:                     $names{$field} = $middle;
10614:                 } elsif ($field eq 'lastname') {
10615:                     $names{$field} = $last;
10616:                 } elsif ($field eq 'generation') { 
10617:                     $names{$field} = $gene;
10618:                 } elsif ($field eq 'permanentemail') {
10619:                     $names{$field} = $email;
10620:                 } elsif ($field eq 'id') {
10621:                     $names{$field}  = $uid;
10622:                 }
10623:             }
10624:         }
10625:     }
10626:     if ($first)  { $names{'firstname'}  = $first; }
10627:     if (defined($middle)) { $names{'middlename'} = $middle; }
10628:     if ($last)   { $names{'lastname'}   = $last; }
10629:     if (defined($gene))   { $names{'generation'} = $gene; }
10630:     if ($email) {
10631:        $email=~s/[^\w\@\.\-\,]//gs;
10632:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10633:     }
10634:     if ($uid) { $names{'id'}  = $uid; }
10635:     if (defined($inststatus)) {
10636:         $names{'inststatus'} = '';
10637:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10638:         if (ref($usertypes) eq 'HASH') {
10639:             my @okstatuses; 
10640:             foreach my $item (split(/:/,$inststatus)) {
10641:                 if (defined($usertypes->{$item})) {
10642:                     push(@okstatuses,$item);  
10643:                 }
10644:             }
10645:             if (@okstatuses) {
10646:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10647:             }
10648:         }
10649:     }
10650:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10651:                  $umode.', '.$first.', '.$middle.', '.
10652:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10653:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10654:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10655:     } else {
10656:         $logmsg .= ' during self creation';
10657:     }
10658:     my $changed;
10659:     if ($newuser) {
10660:         $changed = 1;
10661:     } else {
10662:         foreach my $field (@fields) {
10663:             if ($names{$field} ne $oldnames{$field}) {
10664:                 $changed = 1;
10665:                 last;
10666:             }
10667:         }
10668:     }
10669:     unless ($changed) {
10670:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10671:         &logthis($logmsg);
10672:         return 'ok';
10673:     }
10674:     my $reply = &put('environment', \%names, $udom,$uname);
10675:     if ($reply ne 'ok') { 
10676:         return 'error: '.$reply;
10677:     }
10678:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10679:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10680:     }
10681:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10682:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10683:     $logmsg = 'Success modifying user '.$logmsg;
10684:     &logthis($logmsg);
10685:     return 'ok';
10686: }
10687: 
10688: # -------------------------------------------------------------- Modify student
10689: 
10690: sub modifystudent {
10691:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10692:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10693:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10694:     if (!$cid) {
10695: 	unless ($cid=$env{'request.course.id'}) {
10696: 	    return 'not_in_class';
10697: 	}
10698:     }
10699: # --------------------------------------------------------------- Make the user
10700:     my $reply=&modifyuser
10701: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10702:          $desiredhome,$email,$inststatus);
10703:     unless ($reply eq 'ok') { return $reply; }
10704:     # This will cause &modify_student_enrollment to get the uid from the
10705:     # student's environment
10706:     $uid = undef if (!$forceid);
10707:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10708:                                         $gene,$usec,$end,$start,$type,$locktype,
10709:                                         $cid,$selfenroll,$context,$credits,$instsec);
10710:     return $reply;
10711: }
10712: 
10713: sub modify_student_enrollment {
10714:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10715:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10716:     my ($cdom,$cnum,$chome);
10717:     if (!$cid) {
10718: 	unless ($cid=$env{'request.course.id'}) {
10719: 	    return 'not_in_class';
10720: 	}
10721: 	$cdom=$env{'course.'.$cid.'.domain'};
10722: 	$cnum=$env{'course.'.$cid.'.num'};
10723:     } else {
10724: 	($cdom,$cnum)=split(/_/,$cid);
10725:     }
10726:     $chome=$env{'course.'.$cid.'.home'};
10727:     if (!$chome) {
10728: 	$chome=&homeserver($cnum,$cdom);
10729:     }
10730:     if (!$chome) { return 'unknown_course'; }
10731:     # Make sure the user exists
10732:     my $uhome=&homeserver($uname,$udom);
10733:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10734: 	return 'error: no such user';
10735:     }
10736:     # Get student data if we were not given enough information
10737:     if (!defined($first)  || $first  eq '' || 
10738:         !defined($last)   || $last   eq '' || 
10739:         !defined($uid)    || $uid    eq '' || 
10740:         !defined($middle) || $middle eq '' || 
10741:         !defined($gene)   || $gene   eq '') {
10742:         # They did not supply us with enough data to enroll the student, so
10743:         # we need to pick up more information.
10744:         my %tmp = &get('environment',
10745:                        ['firstname','middlename','lastname', 'generation','id']
10746:                        ,$udom,$uname);
10747: 
10748:         #foreach my $key (keys(%tmp)) {
10749:         #    &logthis("key $key = ".$tmp{$key});
10750:         #}
10751:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10752:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10753:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10754:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10755:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10756:     }
10757:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10758:     my $user = "$uname:$udom";
10759:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10760:     my $reply=cput('classlist',
10761: 		   {$user => 
10762: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10763: 		   $cdom,$cnum);
10764:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10765:         &devalidate_getsection_cache($udom,$uname,$cid);
10766:     } else { 
10767: 	return 'error: '.$reply;
10768:     }
10769:     # Add student role to user
10770:     my $uurl='/'.$cid;
10771:     $uurl=~s/\_/\//g;
10772:     if ($usec) {
10773: 	$uurl.='/'.$usec;
10774:     }
10775:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10776:                              $selfenroll,$context);
10777:     if ($result ne 'ok') {
10778:         if ($old_entry{$user} ne '') {
10779:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10780:         } else {
10781:             $reply = &del('classlist',[$user],$cdom,$cnum);
10782:         }
10783:     }
10784:     return $result; 
10785: }
10786: 
10787: sub format_name {
10788:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10789:     my $name;
10790:     if ($first ne 'lastname') {
10791: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10792:     } else {
10793: 	if ($lastname=~/\S/) {
10794: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10795: 	    $name=~s/\s+,/,/;
10796: 	} else {
10797: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10798: 	}
10799:     }
10800:     $name=~s/^\s+//;
10801:     $name=~s/\s+$//;
10802:     $name=~s/\s+/ /g;
10803:     return $name;
10804: }
10805: 
10806: # ------------------------------------------------- Write to course preferences
10807: 
10808: sub writecoursepref {
10809:     my ($courseid,%prefs)=@_;
10810:     $courseid=~s/^\///;
10811:     $courseid=~s/\_/\//g;
10812:     my ($cdomain,$cnum)=split(/\//,$courseid);
10813:     my $chome=homeserver($cnum,$cdomain);
10814:     if (($chome eq '') || ($chome eq 'no_host')) { 
10815: 	return 'error: no such course';
10816:     }
10817:     my $cstring='';
10818:     foreach my $pref (keys(%prefs)) {
10819: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10820:     }
10821:     $cstring=~s/\&$//;
10822:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10823: }
10824: 
10825: # ---------------------------------------------------------- Make/modify course
10826: 
10827: sub createcourse {
10828:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10829:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
10830:     $url=&declutter($url);
10831:     my $cid='';
10832:     if ($context eq 'requestcourses') {
10833:         my $can_create = 0;
10834:         my ($ownername,$ownerdom) = split(':',$course_owner);
10835:         if ($udom eq $ownerdom) {
10836:             my $reload;
10837:             if (($callercontext eq 'auto') &&
10838:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
10839:                 $reload = 'reload';
10840:             }
10841:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
10842:                                   $context)) {
10843:                 $can_create = 1;
10844:             }
10845:         } else {
10846:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10847:                                            $category);
10848:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10849:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10850:                 if (@curr > 0) {
10851:                     my @options = qw(approval validate autolimit);
10852:                     my $optregex = join('|',@options);
10853:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10854:                         $can_create = 1;
10855:                     }
10856:                 }
10857:             }
10858:         }
10859:         if ($can_create) {
10860:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10861:                 unless (&allowed('ccc',$udom)) {
10862:                     return 'refused'; 
10863:                 }
10864:             }
10865:         } else {
10866:             return 'refused';
10867:         }
10868:     } elsif (!&allowed('ccc',$udom)) {
10869:         return 'refused';
10870:     }
10871: # --------------------------------------------------------------- Get Unique ID
10872:     my $uname;
10873:     if ($cnum =~ /^$match_courseid$/) {
10874:         my $chome=&homeserver($cnum,$udom,'true');
10875:         if (($chome eq '') || ($chome eq 'no_host')) {
10876:             $uname = $cnum;
10877:         } else {
10878:             $uname = &generate_coursenum($udom,$crstype);
10879:         }
10880:     } else {
10881:         $uname = &generate_coursenum($udom,$crstype);
10882:     }
10883:     return $uname if ($uname =~ /^error/);
10884: # -------------------------------------------------- Check supplied server name
10885:     if (!defined($course_server)) {
10886:         if (defined(&domain($udom,'primary'))) {
10887:             $course_server = &domain($udom,'primary');
10888:         } else {
10889:             $course_server = $env{'user.home'}; 
10890:         }
10891:     }
10892:     my %host_servers =
10893:         &Apache::lonnet::get_servers($udom,'library');
10894:     unless ($host_servers{$course_server}) {
10895:         return 'error: invalid home server for course: '.$course_server;
10896:     }
10897: # ------------------------------------------------------------- Make the course
10898:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10899:                       $course_server);
10900:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10901:     my $uhome=&homeserver($uname,$udom,'true');
10902:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10903: 	return 'error: no such course';
10904:     }
10905: # ----------------------------------------------------------------- Course made
10906: # log existence
10907:     my $now = time;
10908:     my $newcourse = {
10909:                     $udom.'_'.$uname => {
10910:                                      description => $description,
10911:                                      inst_code   => $inst_code,
10912:                                      owner       => $course_owner,
10913:                                      type        => $crstype,
10914:                                      creator     => $env{'user.name'}.':'.
10915:                                                     $env{'user.domain'},
10916:                                      created     => $now,
10917:                                      context     => $context,
10918:                                                 },
10919:                     };
10920:     &courseidput($udom,$newcourse,$uhome,'notime');
10921: # set toplevel url
10922:     my $topurl=$url;
10923:     unless ($nonstandard) {
10924: # ------------------------------------------ For standard courses, make top url
10925:         my $mapurl=&clutter($url);
10926:         if ($mapurl eq '/res/') { $mapurl=''; }
10927:         $env{'form.initmap'}=(<<ENDINITMAP);
10928: <map>
10929: <resource id="1" type="start"></resource>
10930: <resource id="2" src="$mapurl"></resource>
10931: <resource id="3" type="finish"></resource>
10932: <link index="1" from="1" to="2"></link>
10933: <link index="2" from="2" to="3"></link>
10934: </map>
10935: ENDINITMAP
10936:         $topurl=&declutter(
10937:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10938:                           );
10939:     }
10940: # ----------------------------------------------------------- Write preferences
10941:     &writecoursepref($udom.'_'.$uname,
10942:                      ('description'              => $description,
10943:                       'url'                      => $topurl,
10944:                       'internal.creator'         => $env{'user.name'}.':'.
10945:                                                     $env{'user.domain'},
10946:                       'internal.created'         => $now,
10947:                       'internal.creationcontext' => $context)
10948:                     );
10949:     return '/'.$udom.'/'.$uname;
10950: }
10951: 
10952: # ------------------------------------------------------------------- Create ID
10953: sub generate_coursenum {
10954:     my ($udom,$crstype) = @_;
10955:     my $domdesc = &domain($udom);
10956:     return 'error: invalid domain' if ($domdesc eq '');
10957:     my $first;
10958:     if ($crstype eq 'Community') {
10959:         $first = '0';
10960:     } else {
10961:         $first = int(1+rand(9)); 
10962:     } 
10963:     my $uname=$first.
10964:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10965:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
10966:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10967: # ----------------------------------------------- Make sure that does not exist
10968:     my $uhome=&homeserver($uname,$udom,'true');
10969:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
10970:         if ($crstype eq 'Community') {
10971:             $first = '0';
10972:         } else {
10973:             $first = int(1+rand(9));
10974:         }
10975:         $uname=$first.
10976:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10977:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
10978:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10979:         $uhome=&homeserver($uname,$udom,'true');
10980:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
10981:             return 'error: unable to generate unique course-ID';
10982:         }
10983:     }
10984:     return $uname;
10985: }
10986: 
10987: sub is_course {
10988:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
10989:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
10990: 
10991:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
10992:     my $uhome=&homeserver($cnum,$cdom);
10993:     my $iscourse;
10994:     if (grep { $_ eq $uhome } current_machine_ids()) {
10995:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
10996:     } else {
10997:         my $hashid = $cdom.':'.$cnum;
10998:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
10999:         unless (defined($cached)) {
11000:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11001:                                         $cnum,undef,undef,'.');
11002:             $iscourse = 0;
11003:             if (exists($courses{$cdom.'_'.$cnum})) {
11004:                 $iscourse = 1;
11005:             }
11006:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
11007:         }
11008:     }
11009:     return unless ($iscourse);
11010:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
11011: }
11012: 
11013: sub store_userdata {
11014:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
11015:     my $result;
11016:     if ($datakey ne '') {
11017:         if (ref($storehash) eq 'HASH') {
11018:             if ($udom eq '' || $uname eq '') {
11019:                 $udom = $env{'user.domain'};
11020:                 $uname = $env{'user.name'};
11021:             }
11022:             my $uhome=&homeserver($uname,$udom);
11023:             if (($uhome eq '') || ($uhome eq 'no_host')) {
11024:                 $result = 'error: no_host';
11025:             } else {
11026:                 $storehash->{'ip'} = &get_requestor_ip();
11027:                 $storehash->{'host'} = $perlvar{'lonHostID'};
11028: 
11029:                 my $namevalue='';
11030:                 foreach my $key (keys(%{$storehash})) {
11031:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11032:                 }
11033:                 $namevalue=~s/\&$//;
11034:                 unless ($namespace eq 'courserequests') {
11035:                     $datakey = &escape($datakey);
11036:                 }
11037:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
11038:                                   $namevalue,$uhome);
11039:             }
11040:         } else {
11041:             $result = 'error: data to store was not a hash reference'; 
11042:         }
11043:     } else {
11044:         $result= 'error: invalid requestkey'; 
11045:     }
11046:     return $result;
11047: }
11048: 
11049: # ---------------------------------------------------------- Assign Custom Role
11050: 
11051: sub assigncustomrole {
11052:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
11053:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
11054:                        $end,$start,$deleteflag,$selfenroll,$context);
11055: }
11056: 
11057: # ----------------------------------------------------------------- Revoke Role
11058: 
11059: sub revokerole {
11060:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
11061:     my $now=time;
11062:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
11063: }
11064: 
11065: # ---------------------------------------------------------- Revoke Custom Role
11066: 
11067: sub revokecustomrole {
11068:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
11069:     my $now=time;
11070:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
11071:            $deleteflag,$selfenroll,$context);
11072: }
11073: 
11074: # ------------------------------------------------------------ Disk usage
11075: sub diskusage {
11076:     my ($udom,$uname,$directorypath,$getpropath)=@_;
11077:     $directorypath =~ s/\/$//;
11078:     my $listing=&reply('du2:'.&escape($directorypath).':'
11079:                        .&escape($getpropath).':'.&escape($uname).':'
11080:                        .&escape($udom),homeserver($uname,$udom));
11081:     if ($listing eq 'unknown_cmd') {
11082:         if ($getpropath) {
11083:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
11084:         }
11085:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11086:     }
11087:     return $listing;
11088: }
11089: 
11090: sub is_locked {
11091:     my ($file_name, $domain, $user, $which) = @_;
11092:     my @check;
11093:     my $is_locked;
11094:     push (@check,$file_name);
11095:     my %locked = &get('file_permissions',\@check,
11096: 		      $env{'user.domain'},$env{'user.name'});
11097:     my ($tmp)=keys(%locked);
11098:     if ($tmp=~/^error:/) { undef(%locked); }
11099:     
11100:     if (ref($locked{$file_name}) eq 'ARRAY') {
11101:         $is_locked = 'false';
11102:         foreach my $entry (@{$locked{$file_name}}) {
11103:            if (ref($entry) eq 'ARRAY') {
11104:                $is_locked = 'true';
11105:                if (ref($which) eq 'ARRAY') {
11106:                    push(@{$which},$entry);
11107:                } else {
11108:                    last;
11109:                }
11110:            }
11111:        }
11112:     } else {
11113:         $is_locked = 'false';
11114:     }
11115:     return $is_locked;
11116: }
11117: 
11118: sub declutter_portfile {
11119:     my ($file) = @_;
11120:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11121:     return $file;
11122: }
11123: 
11124: # ------------------------------------------------------------- Mark as Read Only
11125: 
11126: sub mark_as_readonly {
11127:     my ($domain,$user,$files,$what) = @_;
11128:     my %current_permissions = &dump('file_permissions',$domain,$user);
11129:     my ($tmp)=keys(%current_permissions);
11130:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11131:     foreach my $file (@{$files}) {
11132: 	$file = &declutter_portfile($file);
11133:         push(@{$current_permissions{$file}},$what);
11134:     }
11135:     &put('file_permissions',\%current_permissions,$domain,$user);
11136:     return;
11137: }
11138: 
11139: # ------------------------------------------------------------Save Selected Files
11140: 
11141: sub save_selected_files {
11142:     my ($user, $path, @files) = @_;
11143:     my $filename = $user."savedfiles";
11144:     my @other_files = &files_not_in_path($user, $path);
11145:     open (OUT,'>',LONCAPA::tempdir().$filename);
11146:     foreach my $file (@files) {
11147:         print (OUT $env{'form.currentpath'}.$file."\n");
11148:     }
11149:     foreach my $file (@other_files) {
11150:         print (OUT $file."\n");
11151:     }
11152:     close (OUT);
11153:     return 'ok';
11154: }
11155: 
11156: sub clear_selected_files {
11157:     my ($user) = @_;
11158:     my $filename = $user."savedfiles";
11159:     open (OUT,'>',LONCAPA::tempdir().$filename);
11160:     print (OUT undef);
11161:     close (OUT);
11162:     return ("ok");    
11163: }
11164: 
11165: sub files_in_path {
11166:     my ($user, $path) = @_;
11167:     my $filename = $user."savedfiles";
11168:     my %return_files;
11169:     open (IN,'<',LONCAPA::tempdir().$filename);
11170:     while (my $line_in = <IN>) {
11171:         chomp ($line_in);
11172:         my @paths_and_file = split (m!/!, $line_in);
11173:         my $file_part = pop (@paths_and_file);
11174:         my $path_part = join ('/', @paths_and_file);
11175:         $path_part.='/';
11176:         my $path_and_file = $path_part.$file_part;
11177:         if ($path_part eq $path) {
11178:             $return_files{$file_part}= 'selected';
11179:         }
11180:     }
11181:     close (IN);
11182:     return (\%return_files);
11183: }
11184: 
11185: # called in portfolio select mode, to show files selected NOT in current directory
11186: sub files_not_in_path {
11187:     my ($user, $path) = @_;
11188:     my $filename = $user."savedfiles";
11189:     my @return_files;
11190:     my $path_part;
11191:     open(IN, '<',LONCAPA::tempdir().$filename);
11192:     while (my $line = <IN>) {
11193:         #ok, I know it's clunky, but I want it to work
11194:         my @paths_and_file = split(m|/|, $line);
11195:         my $file_part = pop(@paths_and_file);
11196:         chomp($file_part);
11197:         my $path_part = join('/', @paths_and_file);
11198:         $path_part .= '/';
11199:         my $path_and_file = $path_part.$file_part;
11200:         if ($path_part ne $path) {
11201:             push(@return_files, ($path_and_file));
11202:         }
11203:     }
11204:     close(OUT);
11205:     return (@return_files);
11206: }
11207: 
11208: #------------------------------Submitted/Handedback Portfolio Files Versioning
11209:  
11210: sub portfiles_versioning {
11211:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11212:     my $portfolio_root = '/userfiles/portfolio';
11213:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11214:     foreach my $file (@{$portfiles}) {
11215:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11216:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11217:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11218:         my $getpropath = 1;
11219:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11220:                                              $stu_name,$getpropath);
11221:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11222:         my $new_answer = 
11223:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11224:         if ($new_answer ne 'problem getting file') {
11225:             push(@{$versioned_portfiles}, $directory.$new_answer);
11226:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11227:                               [$symb,$env{'request.course.id'},'graded']);
11228:         }
11229:     }
11230: }
11231: 
11232: sub get_next_version {
11233:     my ($answer_name, $answer_ext, $dir_list) = @_;
11234:     my $version;
11235:     if (ref($dir_list) eq 'ARRAY') {
11236:         foreach my $row (@{$dir_list}) {
11237:             my ($file) = split(/\&/,$row,2);
11238:             my ($file_name,$file_version,$file_ext) =
11239:                 &file_name_version_ext($file);
11240:             if (($file_name eq $answer_name) &&
11241:                 ($file_ext eq $answer_ext)) {
11242:                      # gets here if filename and extension match,
11243:                      # regardless of version
11244:                 if ($file_version ne '') {
11245:                     # a versioned file is found  so save it for later
11246:                     if ($file_version > $version) {
11247:                         $version = $file_version;
11248:                     }
11249:                 }
11250:             }
11251:         }
11252:     }
11253:     $version ++;
11254:     return($version);
11255: }
11256: 
11257: sub version_selected_portfile {
11258:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11259:     my ($answer_name,$answer_ver,$answer_ext) =
11260:         &file_name_version_ext($file_name);
11261:     my $new_answer;
11262:     $env{'form.copy'} =
11263:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11264:     if($env{'form.copy'} eq '-1') {
11265:         $new_answer = 'problem getting file';
11266:     } else {
11267:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11268:         my $copy_result = 
11269:             &finishuserfileupload($stu_name,$domain,'copy',
11270:                                   '/portfolio'.$directory.$new_answer);
11271:     }
11272:     undef($env{'form.copy'});
11273:     return ($new_answer);
11274: }
11275: 
11276: sub file_name_version_ext {
11277:     my ($file)=@_;
11278:     my @file_parts = split(/\./, $file);
11279:     my ($name,$version,$ext);
11280:     if (@file_parts > 1) {
11281:         $ext=pop(@file_parts);
11282:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11283:             $version=pop(@file_parts);
11284:         }
11285:         $name=join('.',@file_parts);
11286:     } else {
11287:         $name=join('.',@file_parts);
11288:     }
11289:     return($name,$version,$ext);
11290: }
11291: 
11292: #----------------------------------------------Get portfolio file permissions
11293: 
11294: sub get_portfile_permissions {
11295:     my ($domain,$user) = @_;
11296:     my %current_permissions = &dump('file_permissions',$domain,$user);
11297:     my ($tmp)=keys(%current_permissions);
11298:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11299:     return \%current_permissions;
11300: }
11301: 
11302: #---------------------------------------------Get portfolio file access controls
11303: 
11304: sub get_access_controls {
11305:     my ($current_permissions,$group,$file) = @_;
11306:     my %access;
11307:     my $real_file = $file;
11308:     $file =~ s/\.meta$//;
11309:     if (defined($file)) {
11310:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11311:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11312:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11313:             }
11314:         }
11315:     } else {
11316:         foreach my $key (keys(%{$current_permissions})) {
11317:             if ($key =~ /\0accesscontrol$/) {
11318:                 if (defined($group)) {
11319:                     if ($key !~ m-^\Q$group\E/-) {
11320:                         next;
11321:                     }
11322:                 }
11323:                 my ($fullpath) = split(/\0/,$key);
11324:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11325:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11326:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11327:                     }
11328:                 }
11329:             }
11330:         }
11331:     }
11332:     return %access;
11333: }
11334: 
11335: sub modify_access_controls {
11336:     my ($file_name,$changes,$domain,$user)=@_;
11337:     my ($outcome,$deloutcome);
11338:     my %store_permissions;
11339:     my %new_values;
11340:     my %new_control;
11341:     my %translation;
11342:     my @deletions = ();
11343:     my $now = time;
11344:     if (exists($$changes{'activate'})) {
11345:         if (ref($$changes{'activate'}) eq 'HASH') {
11346:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11347:             my $numnew = scalar(@newitems);
11348:             for (my $i=0; $i<$numnew; $i++) {
11349:                 my $newkey = $newitems[$i];
11350:                 my $newid = &Apache::loncommon::get_cgi_id();
11351:                 if ($newkey =~ /^\d+:/) { 
11352:                     $newkey =~ s/^(\d+)/$newid/;
11353:                     $translation{$1} = $newid;
11354:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11355:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11356:                     $translation{$1} = $newid;
11357:                 }
11358:                 $new_values{$file_name."\0".$newkey} = 
11359:                                           $$changes{'activate'}{$newitems[$i]};
11360:                 $new_control{$newkey} = $now;
11361:             }
11362:         }
11363:     }
11364:     my %todelete;
11365:     my %changed_items;
11366:     foreach my $action ('delete','update') {
11367:         if (exists($$changes{$action})) {
11368:             if (ref($$changes{$action}) eq 'HASH') {
11369:                 foreach my $key (keys(%{$$changes{$action}})) {
11370:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11371:                     if ($action eq 'delete') { 
11372:                         $todelete{$itemnum} = 1;
11373:                     } else {
11374:                         $changed_items{$itemnum} = $key;
11375:                     }
11376:                 }
11377:             }
11378:         }
11379:     }
11380:     # get lock on access controls for file.
11381:     my $lockhash = {
11382:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11383:                                                        ':'.$env{'user.domain'},
11384:                    }; 
11385:     my $tries = 0;
11386:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11387:    
11388:     while (($gotlock ne 'ok') && $tries < 10) {
11389:         $tries ++;
11390:         sleep(0.1);
11391:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11392:     }
11393:     if ($gotlock eq 'ok') {
11394:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11395:         my ($tmp)=keys(%curr_permissions);
11396:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11397:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11398:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11399:             if (ref($curr_controls) eq 'HASH') {
11400:                 foreach my $control_item (keys(%{$curr_controls})) {
11401:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11402:                     if (defined($todelete{$itemnum})) {
11403:                         push(@deletions,$file_name."\0".$control_item);
11404:                     } else {
11405:                         if (defined($changed_items{$itemnum})) {
11406:                             $new_control{$changed_items{$itemnum}} = $now;
11407:                             push(@deletions,$file_name."\0".$control_item);
11408:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11409:                         } else {
11410:                             $new_control{$control_item} = $$curr_controls{$control_item};
11411:                         }
11412:                     }
11413:                 }
11414:             }
11415:         }
11416:         my ($group);
11417:         if (&is_course($domain,$user)) {
11418:             ($group,my $file) = split(/\//,$file_name,2);
11419:         }
11420:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11421:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11422:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11423:         #  remove lock
11424:         my @del_lock = ($file_name."\0".'locked_access_records');
11425:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11426:         my $sqlresult =
11427:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11428:                                     $group);
11429:     } else {
11430:         $outcome = "error: could not obtain lockfile\n";  
11431:     }
11432:     return ($outcome,$deloutcome,\%new_values,\%translation);
11433: }
11434: 
11435: sub make_public_indefinitely {
11436:     my (@requrl) = @_;
11437:     return &automated_portfile_access('public',\@requrl);
11438: }
11439: 
11440: sub automated_portfile_access {
11441:     my ($accesstype,$addsref,$delsref,$info) = @_;
11442:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11443:         return 'invalid';
11444:     }
11445:     my %urls;
11446:     if (ref($addsref) eq 'ARRAY') {
11447:         foreach my $requrl (@{$addsref}) {
11448:             if (&is_portfolio_url($requrl)) {
11449:                 unless (exists($urls{$requrl})) {
11450:                     $urls{$requrl} = 'add';
11451:                 }
11452:             }
11453:         }
11454:     }
11455:     if (ref($delsref) eq 'ARRAY') {
11456:         foreach my $requrl (@{$delsref}) { 
11457:             if (&is_portfolio_url($requrl)) {
11458:                 unless (exists($urls{$requrl})) {
11459:                     $urls{$requrl} = 'delete'; 
11460:                 }
11461:             }
11462:         }
11463:     }
11464:     unless (keys(%urls)) {
11465:         return 'invalid';
11466:     }
11467:     my $ip;
11468:     if ($accesstype eq 'ip') {
11469:         if (ref($info) eq 'HASH') {
11470:             if ($info->{'ip'} ne '') {
11471:                 $ip = $info->{'ip'};
11472:             }
11473:         }
11474:         if ($ip eq '') {
11475:             return 'invalid';
11476:         }
11477:     }
11478:     my $errors;
11479:     my $now = time;
11480:     my %current_perms;
11481:     foreach my $requrl (sort(keys(%urls))) {
11482:         my $action;
11483:         if ($urls{$requrl} eq 'add') {
11484:             $action = 'activate';
11485:         } else {
11486:             $action = 'none';
11487:         }
11488:         my $aclnum = 0;
11489:         my (undef,$udom,$unum,$file_name,$group) =
11490:             &parse_portfolio_url($requrl);
11491:         unless (exists($current_perms{$unum.':'.$udom})) {
11492:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11493:         }
11494:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11495:                                                    $group,$file_name);
11496:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11497:             my ($num,$scope,$end,$start) = 
11498:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11499:             if ($scope eq $accesstype) {
11500:                 if (($start <= $now) && ($end == 0)) {
11501:                     if ($accesstype eq 'ip') {
11502:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11503:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11504:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11505:                                     if ($urls{$requrl} eq 'add') {
11506:                                         $action = 'none';
11507:                                         last;
11508:                                     } else {
11509:                                         $action = 'delete';
11510:                                         $aclnum = $num;
11511:                                         last;
11512:                                     }
11513:                                 }
11514:                             }
11515:                         }
11516:                     } elsif ($accesstype eq 'public') {
11517:                         if ($urls{$requrl} eq 'add') {
11518:                             $action = 'none';
11519:                             last;
11520:                         } else {
11521:                             $action = 'delete';
11522:                             $aclnum = $num;
11523:                             last;
11524:                         }
11525:                     }
11526:                 } elsif ($accesstype eq 'public') {
11527:                     $action = 'update';
11528:                     $aclnum = $num;
11529:                     last;
11530:                 }
11531:             }
11532:         }
11533:         if ($action eq 'none') {
11534:             next;
11535:         } else {
11536:             my %changes;
11537:             my $newend = 0;
11538:             my $newstart = $now;
11539:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11540:             $changes{$action}{$newkey} = {
11541:                 type => $accesstype,
11542:                 time => {
11543:                     start => $newstart,
11544:                     end   => $newend,
11545:                 },
11546:             };
11547:             if ($accesstype eq 'ip') {
11548:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11549:             }
11550:             my ($outcome,$deloutcome,$new_values,$translation) =
11551:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11552:             unless ($outcome eq 'ok') {
11553:                 $errors .= $outcome.' ';
11554:             }
11555:         }
11556:     }
11557:     if ($errors) {
11558:         $errors =~ s/\s$//;
11559:         return $errors;
11560:     } else {
11561:         return 'ok';
11562:     }
11563: }
11564: 
11565: #------------------------------------------------------Get Marked as Read Only
11566: 
11567: sub get_marked_as_readonly {
11568:     my ($domain,$user,$what,$group) = @_;
11569:     my $current_permissions = &get_portfile_permissions($domain,$user);
11570:     my @readonly_files;
11571:     my $cmp1=$what;
11572:     if (ref($what)) { $cmp1=join('',@{$what}) };
11573:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11574:         if (defined($group)) {
11575:             if ($file_name !~ m-^\Q$group\E/-) {
11576:                 next;
11577:             }
11578:         }
11579:         if (ref($value) eq "ARRAY"){
11580:             foreach my $stored_what (@{$value}) {
11581:                 my $cmp2=$stored_what;
11582:                 if (ref($stored_what) eq 'ARRAY') {
11583:                     $cmp2=join('',@{$stored_what});
11584:                 }
11585:                 if ($cmp1 eq $cmp2) {
11586:                     push(@readonly_files, $file_name);
11587:                     last;
11588:                 } elsif (!defined($what)) {
11589:                     push(@readonly_files, $file_name);
11590:                     last;
11591:                 }
11592:             }
11593:         }
11594:     }
11595:     return @readonly_files;
11596: }
11597: #-----------------------------------------------------------Get Marked as Read Only Hash
11598: 
11599: sub get_marked_as_readonly_hash {
11600:     my ($current_permissions,$group,$what) = @_;
11601:     my %readonly_files;
11602:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11603:         if (defined($group)) {
11604:             if ($file_name !~ m-^\Q$group\E/-) {
11605:                 next;
11606:             }
11607:         }
11608:         if (ref($value) eq "ARRAY"){
11609:             foreach my $stored_what (@{$value}) {
11610:                 if (ref($stored_what) eq 'ARRAY') {
11611:                     foreach my $lock_descriptor(@{$stored_what}) {
11612:                         if ($lock_descriptor eq 'graded') {
11613:                             $readonly_files{$file_name} = 'graded';
11614:                         } elsif ($lock_descriptor eq 'handback') {
11615:                             $readonly_files{$file_name} = 'handback';
11616:                         } else {
11617:                             if (!exists($readonly_files{$file_name})) {
11618:                                 $readonly_files{$file_name} = 'locked';
11619:                             }
11620:                         }
11621:                     }
11622:                 } 
11623:             }
11624:         } 
11625:     }
11626:     return %readonly_files;
11627: }
11628: # ------------------------------------------------------------ Unmark as Read Only
11629: 
11630: sub unmark_as_readonly {
11631:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11632:     # for portfolio submissions, $what contains [$symb,$crsid] 
11633:     my ($domain,$user,$what,$file_name,$group) = @_;
11634:     $file_name = &declutter_portfile($file_name);
11635:     my $symb_crs = $what;
11636:     if (ref($what)) { $symb_crs=join('',@$what); }
11637:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11638:     my ($tmp)=keys(%current_permissions);
11639:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11640:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11641:     foreach my $file (@readonly_files) {
11642: 	my $clean_file = &declutter_portfile($file);
11643: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11644: 	my $current_locks = $current_permissions{$file};
11645:         my @new_locks;
11646:         my @del_keys;
11647:         if (ref($current_locks) eq "ARRAY"){
11648:             foreach my $locker (@{$current_locks}) {
11649:                 my $compare=$locker;
11650:                 if (ref($locker) eq 'ARRAY') {
11651:                     $compare=join('',@{$locker});
11652:                     if ($compare ne $symb_crs) {
11653:                         push(@new_locks, $locker);
11654:                     }
11655:                 }
11656:             }
11657:             if (scalar(@new_locks) > 0) {
11658:                 $current_permissions{$file} = \@new_locks;
11659:             } else {
11660:                 push(@del_keys, $file);
11661:                 &del('file_permissions',\@del_keys, $domain, $user);
11662:                 delete($current_permissions{$file});
11663:             }
11664:         }
11665:     }
11666:     &put('file_permissions',\%current_permissions,$domain,$user);
11667:     return;
11668: }
11669: 
11670: # ------------------------------------------------------------ Directory lister
11671: 
11672: sub dirlist {
11673:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11674:     $uri=~s/^\///;
11675:     $uri=~s/\/$//;
11676:     my ($udom, $uname);
11677:     if ($getuserdir) {
11678:         $udom = $userdomain;
11679:         $uname = $username;
11680:     } else {
11681:         (undef,$udom,$uname)=split(/\//,$uri);
11682:         if(defined($userdomain)) {
11683:             $udom = $userdomain;
11684:         }
11685:         if(defined($username)) {
11686:             $uname = $username;
11687:         }
11688:     }
11689:     my ($dirRoot,$listing,@listing_results);
11690: 
11691:     $dirRoot = $perlvar{'lonDocRoot'};
11692:     if (defined($getpropath)) {
11693:         $dirRoot = &propath($udom,$uname);
11694:         $dirRoot =~ s/\/$//;
11695:     } elsif (defined($getuserdir)) {
11696:         my $subdir=$uname.'__';
11697:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11698:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11699:                    ."/$udom/$subdir/$uname";
11700:     } elsif (defined($alternateRoot)) {
11701:         $dirRoot = $alternateRoot;
11702:     }
11703: 
11704:     if($udom) {
11705:         if($uname) {
11706:             my $uhome = &homeserver($uname,$udom);
11707:             if ($uhome eq 'no_host') {
11708:                 return ([],'no_host');
11709:             }
11710:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11711:                               .$getuserdir.':'.&escape($dirRoot)
11712:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11713:             if ($listing eq 'unknown_cmd') {
11714:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11715:             } else {
11716:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11717:             }
11718:             if ($listing eq 'unknown_cmd') {
11719:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11720:                 @listing_results = split(/:/,$listing);
11721:             } else {
11722:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11723:             }
11724:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11725:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11726:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11727:                 return ([],$listing);
11728:             } else {
11729:                 return (\@listing_results);
11730:             }
11731:         } elsif(!$alternateRoot) {
11732:             my (%allusers,%listerror);
11733: 	    my %servers = &get_servers($udom,'library');
11734:  	    foreach my $tryserver (keys(%servers)) {
11735:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11736:                                   &escape($udom),$tryserver);
11737:                 if ($listing eq 'unknown_cmd') {
11738: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11739: 				      $udom, $tryserver);
11740:                 } else {
11741:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11742:                 }
11743: 		if ($listing eq 'unknown_cmd') {
11744: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11745: 				      $udom, $tryserver);
11746: 		    @listing_results = split(/:/,$listing);
11747: 		} else {
11748: 		    @listing_results =
11749: 			map { &unescape($_); } split(/:/,$listing);
11750: 		}
11751:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11752:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11753:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11754:                     $listerror{$tryserver} = $listing;
11755:                 } else {
11756: 		    foreach my $line (@listing_results) {
11757: 			my ($entry) = split(/&/,$line,2);
11758: 			$allusers{$entry} = 1;
11759: 		    }
11760: 		}
11761:             }
11762:             my @alluserslist=();
11763:             foreach my $user (sort(keys(%allusers))) {
11764:                 push(@alluserslist,$user.'&user');
11765:             }
11766: 
11767:             if (!%listerror) {
11768:                 # no errors
11769:                 return (\@alluserslist);
11770:             } elsif (scalar(keys(%servers)) == 1) {
11771:                 # one library server, one error 
11772:                 my ($key) = keys(%listerror);
11773:                 return (\@alluserslist, $listerror{$key});
11774:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11775:                 # con_lost indicates that we might miss data from at least one
11776:                 # library server
11777:                 return (\@alluserslist, 'con_lost');
11778:             } else {
11779:                 # multiple library servers and no con_lost -> data should be
11780:                 # complete. 
11781:                 return (\@alluserslist);
11782:             }
11783: 
11784:         } else {
11785:             return ([],'missing username');
11786:         }
11787:     } elsif(!defined($getpropath)) {
11788:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11789:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11790:         return (\@all_domains);
11791:     } else {
11792:         return ([],'missing domain');
11793:     }
11794: }
11795: 
11796: # --------------------------------------------- GetFileTimestamp
11797: # This function utilizes dirlist and returns the date stamp for
11798: # when it was last modified.  It will also return an error of -1
11799: # if an error occurs
11800: 
11801: sub GetFileTimestamp {
11802:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11803:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11804:     $studentName   = &LONCAPA::clean_username($studentName);
11805:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11806:                                     undef,$getuserdir);
11807:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11808:         return -1;
11809:     }
11810:     if (ref($fileref) eq 'ARRAY') {
11811:         my @stats = split('&',$fileref->[0]);
11812:         # @stats contains first the filename, then the stat output
11813:         return $stats[10]; # so this is 10 instead of 9.
11814:     } else {
11815:         return -1;
11816:     }
11817: }
11818: 
11819: sub stat_file {
11820:     my ($uri) = @_;
11821:     $uri = &clutter_with_no_wrapper($uri);
11822: 
11823:     my ($udom,$uname,$file);
11824:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11825: 	($udom,$uname,$file) =
11826: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11827: 	$file = 'userfiles/'.$file;
11828:     }
11829:     if ($uri =~ m-^/res/-) {
11830: 	($udom,$uname) = 
11831: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11832: 	$file = $uri;
11833:     }
11834: 
11835:     if (!$udom || !$uname || !$file) {
11836: 	# unable to handle the uri
11837: 	return ();
11838:     }
11839:     my $getpropath;
11840:     if ($file =~ /^userfiles\//) {
11841:         $getpropath = 1;
11842:     }
11843:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11844:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11845:         return ();
11846:     } else {
11847:         if (ref($listref) eq 'ARRAY') {
11848:             my @stats = split('&',$listref->[0]);
11849: 	    shift(@stats); #filename is first
11850: 	    return @stats;
11851:         }
11852:     }
11853:     return ();
11854: }
11855: 
11856: # --------------------------------------------------------- recursedirs
11857: # Recursive function to traverse either a specific user's Authoring Space
11858: # or corresponding Published Resource Space, and populate the hash ref:
11859: # $dirhashref with URLs of all directories, and if $filehashref hash
11860: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11861: # or .rights files in resource space, and .meta, .save, .log, and .bak
11862: # files in Authoring Space.
11863: #
11864: # Inputs:
11865: #
11866: # $is_home - true if current server is home server for user's space
11867: # $context - either: priv, or res respectively for Authoring or Resource Space.
11868: # $docroot - Document root (i.e., /home/httpd/html
11869: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11870: # $relpath - Current path (relative to top level).
11871: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11872: # $filehashref - reference to hash to populate with URLs of files (Optional)
11873: #
11874: # Returns: nothing
11875: #
11876: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11877: #
11878: # Currently used by interface/londocs.pm to create linked select boxes for
11879: # directory and filename to import a Course "Author" resource into a course, and
11880: # also to create linked select boxes for Authoring Space and Directory to choose
11881: # save location for creation of a new "standard" problem from the Course Editor.
11882: #
11883: 
11884: sub recursedirs {
11885:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11886:     return unless (ref($dirhashref) eq 'HASH');
11887:     my $currpath = $docroot.$toppath;
11888:     if ($relpath) {
11889:         $currpath .= "/$relpath";
11890:     }
11891:     my $savefile;
11892:     if (ref($filehashref)) {
11893:         $savefile = 1;
11894:     }
11895:     if ($is_home) {
11896:         if (opendir(my $dirh,$currpath)) {
11897:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11898:                 next if ($item eq '');
11899:                 if (-d "$currpath/$item") {
11900:                     my $newpath;
11901:                     if ($relpath) {
11902:                         $newpath = "$relpath/$item";
11903:                     } else {
11904:                         $newpath = $item;
11905:                     }
11906:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11907:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11908:                 } elsif ($savefile) {
11909:                     if ($context eq 'priv') {
11910:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11911:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11912:                         }
11913:                     } else {
11914:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11915:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11916:                         }
11917:                     }
11918:                 }
11919:             }
11920:             closedir($dirh);
11921:         }
11922:     } else {
11923:         my ($dirlistref,$listerror) =
11924:             &dirlist($toppath.$relpath);
11925:         my @dir_lines;
11926:         my $dirptr=16384;
11927:         if (ref($dirlistref) eq 'ARRAY') {
11928:             foreach my $dir_line (sort
11929:                               {
11930:                                   my ($afile)=split('&',$a,2);
11931:                                   my ($bfile)=split('&',$b,2);
11932:                                   return (lc($afile) cmp lc($bfile));
11933:                               } (@{$dirlistref})) {
11934:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11935:                     split(/\&/,$dir_line,16);
11936:                 $item =~ s/\s+$//;
11937:                 next if (($item =~ /^\.\.?$/) || ($obs));
11938:                 if ($dirptr&$testdir) {
11939:                     my $newpath;
11940:                     if ($relpath) {
11941:                         $newpath = "$relpath/$item";
11942:                     } else {
11943:                         $relpath = '/';
11944:                         $newpath = $item;
11945:                     }
11946:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11947:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11948:                 } elsif ($savefile) {
11949:                     if ($context eq 'priv') {
11950:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11951:                             $filehashref->{$relpath}{$item} = 1;
11952:                         }
11953:                     } else {
11954:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11955:                             $filehashref->{$relpath}{$item} = 1;
11956:                         }
11957:                     }
11958:                 }
11959:             }
11960:         }
11961:     }
11962:     return;
11963: }
11964: 
11965: # -------------------------------------------------------- Value of a Condition
11966: 
11967: # gets the value of a specific preevaluated condition
11968: #    stored in the string  $env{user.state.<cid>}
11969: # or looks up a condition reference in the bighash and if if hasn't
11970: # already been evaluated recurses into docondval to get the value of
11971: # the condition, then memoizing it to 
11972: #   $env{user.state.<cid>.<condition>}
11973: sub directcondval {
11974:     my $number=shift;
11975:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
11976: 	&Apache::lonuserstate::evalstate();
11977:     }
11978:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
11979: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
11980:     } elsif ($number =~ /^_/) {
11981: 	my $sub_condition;
11982: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11983: 		&GDBM_READER(),0640)) {
11984: 	    $sub_condition=$bighash{'conditions'.$number};
11985: 	    untie(%bighash);
11986: 	}
11987: 	my $value = &docondval($sub_condition);
11988: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11989: 	return $value;
11990:     }
11991:     if ($env{'user.state.'.$env{'request.course.id'}}) {
11992:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11993:     } else {
11994:        return 2;
11995:     }
11996: }
11997: 
11998: # get the collection of conditions for this resource
11999: sub condval {
12000:     my $condidx=shift;
12001:     my $allpathcond='';
12002:     foreach my $cond (split(/\|/,$condidx)) {
12003: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12004: 	    $allpathcond.=
12005: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12006: 	}
12007:     }
12008:     $allpathcond=~s/\|$//;
12009:     return &docondval($allpathcond);
12010: }
12011: 
12012: #evaluates an expression of conditions
12013: sub docondval {
12014:     my ($allpathcond) = @_;
12015:     my $result=0;
12016:     if ($env{'request.course.id'}
12017: 	&& defined($allpathcond)) {
12018: 	my $operand='|';
12019: 	my @stack;
12020: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12021: 	    if ($chunk eq '(') {
12022: 		push @stack,($operand,$result);
12023: 	    } elsif ($chunk eq ')') {
12024: 		my $before=pop @stack;
12025: 		if (pop @stack eq '&') {
12026: 		    $result=$result>$before?$before:$result;
12027: 		} else {
12028: 		    $result=$result>$before?$result:$before;
12029: 		}
12030: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
12031: 		$operand=$chunk;
12032: 	    } else {
12033: 		my $new=directcondval($chunk);
12034: 		if ($operand eq '&') {
12035: 		    $result=$result>$new?$new:$result;
12036: 		} else {
12037: 		    $result=$result>$new?$result:$new;
12038: 		}
12039: 	    }
12040: 	}
12041:     }
12042:     return $result;
12043: }
12044: 
12045: # ---------------------------------------------------- Devalidate courseresdata
12046: 
12047: sub devalidatecourseresdata {
12048:     my ($coursenum,$coursedomain)=@_;
12049:     my $hashid=$coursenum.':'.$coursedomain;
12050:     &devalidate_cache_new('courseres',$hashid);
12051: }
12052: 
12053: 
12054: # --------------------------------------------------- Course Resourcedata Query
12055: #
12056: #  Parameters:
12057: #      $coursenum    - Number of the course.
12058: #      $coursedomain - Domain at which the course was created.
12059: #  Returns:
12060: #     A hash of the course parameters along (I think) with timestamps
12061: #     and version info.
12062: 
12063: sub get_courseresdata {
12064:     my ($coursenum,$coursedomain)=@_;
12065:     my $coursehom=&homeserver($coursenum,$coursedomain);
12066:     my $hashid=$coursenum.':'.$coursedomain;
12067:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
12068:     my %dumpreply;
12069:     unless (defined($cached)) {
12070: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
12071: 	$result=\%dumpreply;
12072: 	my ($tmp) = keys(%dumpreply);
12073: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12074: 	    &do_cache_new('courseres',$hashid,$result,600);
12075: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12076: 	    return $tmp;
12077: 	} elsif ($tmp =~ /^(error)/) {
12078: 	    $result=undef;
12079: 	    &do_cache_new('courseres',$hashid,$result,600);
12080: 	}
12081:     }
12082:     return $result;
12083: }
12084: 
12085: sub devalidateuserresdata {
12086:     my ($uname,$udom)=@_;
12087:     my $hashid="$udom:$uname";
12088:     &devalidate_cache_new('userres',$hashid);
12089: }
12090: 
12091: sub get_userresdata {
12092:     my ($uname,$udom)=@_;
12093:     #most student don\'t have any data set, check if there is some data
12094:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12095: 
12096:     my $hashid="$udom:$uname";
12097:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12098:     if (!defined($cached)) {
12099: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12100: 	$result=\%resourcedata;
12101: 	&do_cache_new('userres',$hashid,$result,600);
12102:     }
12103:     my ($tmp)=keys(%$result);
12104:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12105: 	return $result;
12106:     }
12107:     #error 2 occurs when the .db doesn't exist
12108:     if ($tmp!~/error: 2 /) {
12109:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12110: 	    &logthis("<font color=\"blue\">WARNING:".
12111: 		     " Trying to get resource data for ".
12112: 		     $uname." at ".$udom.": ".
12113: 		     $tmp."</font>");
12114:         }
12115:     } elsif ($tmp=~/error: 2 /) {
12116: 	#&EXT_cache_set($udom,$uname);
12117: 	&do_cache_new('userres',$hashid,undef,600);
12118: 	undef($tmp); # not really an error so don't send it back
12119:     }
12120:     return $tmp;
12121: }
12122: #----------------------------------------------- resdata - return resource data
12123: #  Purpose:
12124: #    Return resource data for either users or for a course.
12125: #  Parameters:
12126: #     $name      - Course/user name.
12127: #     $domain    - Name of the domain the user/course is registered on.
12128: #     $type      - Type of thing $name is (must be 'course' or 'user')
12129: #     $mapp      - decluttered URL of enclosing map  
12130: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12131: #     $recurseup - Ref to array of map URLs, starting with map containing
12132: #                  $mapp up through hierarchy of nested maps to top level map.  
12133: #     $courseid  - CourseID (first part of param identifier).
12134: #     $modifier  - Middle part of param identifier.
12135: #     $what      - Last part of param identifier.
12136: #     @which     - Array of names of resources desired.
12137: #  Returns:
12138: #     The value of the first reasource in @which that is found in the
12139: #     resource hash.
12140: #  Exceptional Conditions:
12141: #     If the $type passed in is not valid (not the string 'course' or 
12142: #     'user', an undefined  reference is returned.
12143: #     If none of the resources are found, an undef is returned
12144: sub resdata {
12145:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12146:         $modifier,$what,@which)=@_;
12147:     my $result;
12148:     if ($type eq 'course') {
12149: 	$result=&get_courseresdata($name,$domain);
12150:     } elsif ($type eq 'user') {
12151: 	$result=&get_userresdata($name,$domain);
12152:     }
12153:     if (!ref($result)) { return $result; }    
12154:     foreach my $item (@which) {
12155:         if ($item->[1] eq 'course') {
12156:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12157:                 unless ($$recursed) {
12158:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12159:                     $$recursed = 1;
12160:                 }
12161:                 foreach my $item (@${recurseup}) {
12162:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12163:                     last if (defined($result->{$norecursechk}));
12164:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12165:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12166:                 }
12167:             }
12168:         }
12169:         if (defined($result->{$item->[0]})) {
12170: 	    return [$result->{$item->[0]},$item->[1]];
12171: 	}
12172:     }
12173:     return undef;
12174: }
12175: 
12176: sub get_domain_lti {
12177:     my ($cdom,$context) = @_;
12178:     my ($name,%lti);
12179:     if ($context eq 'consumer') {
12180:         $name = 'ltitools';
12181:     } elsif ($context eq 'provider') {
12182:         $name = 'lti';
12183:     } else {
12184:         return %lti;
12185:     }
12186:     my ($result,$cached)=&is_cached_new($name,$cdom);
12187:     if (defined($cached)) {
12188:         if (ref($result) eq 'HASH') {
12189:             %lti = %{$result};
12190:         }
12191:     } else {
12192:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12193:         if (ref($domconfig{$name}) eq 'HASH') {
12194:             %lti = %{$domconfig{$name}};
12195:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
12196:             if (ref($encdomconfig{$name}) eq 'HASH') {
12197:                 foreach my $id (keys(%lti)) {
12198:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
12199:                         foreach my $item ('key','secret') {
12200:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
12201:                         }
12202:                     }
12203:                 }
12204:             }
12205:         }
12206:         my $cachetime = 24*60*60;
12207:         &do_cache_new($name,$cdom,\%lti,$cachetime);
12208:     }
12209:     return %lti;
12210: }
12211: 
12212: sub get_numsuppfiles {
12213:     my ($cnum,$cdom,$ignorecache)=@_;
12214:     my $hashid=$cnum.':'.$cdom;
12215:     my ($suppcount,$cached);
12216:     unless ($ignorecache) {
12217:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
12218:     }
12219:     unless (defined($cached)) {
12220:         my $chome=&homeserver($cnum,$cdom);
12221:         unless ($chome eq 'no_host') {
12222:             ($suppcount,my $supptools,my $errors) = (0,0,0);
12223:             my $suppmap = 'supplemental.sequence';
12224:             ($suppcount,$supptools,$errors) =
12225:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
12226:                                                          $supptools,$errors);
12227:         }
12228:         &do_cache_new('suppcount',$hashid,$suppcount,600);
12229:     }
12230:     return $suppcount;
12231: }
12232: 
12233: #
12234: # EXT resource caching routines
12235: #
12236: 
12237: {
12238: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12239: #
12240: # The course for which we cache
12241: my $cachedmapkey='';
12242: # The cached recursive maps for this course
12243: my %cachedmaps=();
12244: # When this was last done
12245: my $cachedmaptime='';
12246: 
12247: sub clear_EXT_cache_status {
12248:     &delenv('cache.EXT.');
12249: }
12250: 
12251: sub EXT_cache_status {
12252:     my ($target_domain,$target_user) = @_;
12253:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12254:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12255:         # We know already the user has no data
12256:         return 1;
12257:     } else {
12258:         return 0;
12259:     }
12260: }
12261: 
12262: sub EXT_cache_set {
12263:     my ($target_domain,$target_user) = @_;
12264:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12265:     #&appenv({$cachename => time});
12266: }
12267: 
12268: # --------------------------------------------------------- Value of a Variable
12269: sub EXT {
12270: 
12271:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
12272:     unless ($varname) { return ''; }
12273:     #get real user name/domain, courseid and symb
12274:     my $courseid;
12275:     my $publicuser;
12276:     if ($symbparm) {
12277: 	$symbparm=&get_symb_from_alias($symbparm);
12278:     }
12279:     if (!($uname && $udom)) {
12280:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12281:       if (!$symbparm) {	$symbparm=$cursymb; }
12282:     } else {
12283: 	$courseid=$env{'request.course.id'};
12284:     }
12285:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12286:     my $rest;
12287:     if (defined($therest[0])) {
12288:        $rest=join('.',@therest);
12289:     } else {
12290:        $rest='';
12291:     }
12292: 
12293:     my $qualifierrest=$qualifier;
12294:     if ($rest) { $qualifierrest.='.'.$rest; }
12295:     my $spacequalifierrest=$space;
12296:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12297:     if ($realm eq 'user') {
12298: # --------------------------------------------------------------- user.resource
12299: 	if ($space eq 'resource') {
12300: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12301: 		  || defined($Apache::lonhomework::parsing_a_task))
12302: 		 &&
12303: 		 ($symbparm eq &symbread()) ) {	
12304: 		# if we are in the middle of processing the resource the
12305: 		# get the value we are planning on committing
12306:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12307:                     return $Apache::lonhomework::results{$qualifierrest};
12308:                 } else {
12309:                     return $Apache::lonhomework::history{$qualifierrest};
12310:                 }
12311: 	    } else {
12312: 		my %restored;
12313: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12314: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12315: 		} else {
12316: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12317: 		}
12318: 		return $restored{$qualifierrest};
12319: 	    }
12320: # ----------------------------------------------------------------- user.access
12321:         } elsif ($space eq 'access') {
12322: 	    # FIXME - not supporting calls for a specific user
12323:             return &allowed($qualifier,$rest);
12324: # ------------------------------------------ user.preferences, user.environment
12325:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12326: 	    if (($uname eq $env{'user.name'}) &&
12327: 		($udom eq $env{'user.domain'})) {
12328: 		return $env{join('.',('environment',$qualifierrest))};
12329: 	    } else {
12330: 		my %returnhash;
12331: 		if (!$publicuser) {
12332: 		    %returnhash=&userenvironment($udom,$uname,
12333: 						 $qualifierrest);
12334: 		}
12335: 		return $returnhash{$qualifierrest};
12336: 	    }
12337: # ----------------------------------------------------------------- user.course
12338:         } elsif ($space eq 'course') {
12339: 	    # FIXME - not supporting calls for a specific user
12340:             return $env{join('.',('request.course',$qualifier))};
12341: # ------------------------------------------------------------------- user.role
12342:         } elsif ($space eq 'role') {
12343: 	    # FIXME - not supporting calls for a specific user
12344:             my ($role,$where)=split(/\./,$env{'request.role'});
12345:             if ($qualifier eq 'value') {
12346: 		return $role;
12347:             } elsif ($qualifier eq 'extent') {
12348:                 return $where;
12349:             }
12350: # ----------------------------------------------------------------- user.domain
12351:         } elsif ($space eq 'domain') {
12352:             return $udom;
12353: # ------------------------------------------------------------------- user.name
12354:         } elsif ($space eq 'name') {
12355:             return $uname;
12356: # ---------------------------------------------------- Any other user namespace
12357:         } else {
12358: 	    my %reply;
12359: 	    if (!$publicuser) {
12360: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12361: 	    }
12362: 	    return $reply{$qualifierrest};
12363:         }
12364:     } elsif ($realm eq 'query') {
12365: # ---------------------------------------------- pull stuff out of query string
12366:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12367: 						[$spacequalifierrest]);
12368: 	return $env{'form.'.$spacequalifierrest}; 
12369:    } elsif ($realm eq 'request') {
12370: # ------------------------------------------------------------- request.browser
12371:         if ($space eq 'browser') {
12372:             return $env{'browser.'.$qualifier};
12373: # ------------------------------------------------------------ request.filename
12374:         } else {
12375:             return $env{'request.'.$spacequalifierrest};
12376:         }
12377:     } elsif ($realm eq 'course') {
12378: # ---------------------------------------------------------- course.description
12379:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12380:     } elsif ($realm eq 'resource') {
12381: 
12382: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12383: 	    if (!$symbparm) { $symbparm=&symbread(); }
12384: 	}
12385: 
12386:         if ($qualifier eq '') {
12387: 	    if ($space eq 'title') {
12388: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12389: 	        return &gettitle($symbparm);
12390: 	    }
12391: 	
12392: 	    if ($space eq 'map') {
12393: 	        my ($map) = &decode_symb($symbparm);
12394: 	        return &symbread($map);
12395: 	    }
12396:             if ($space eq 'maptitle') {
12397:                 my ($map) = &decode_symb($symbparm);
12398:                 return &gettitle($map);
12399:             }
12400: 	    if ($space eq 'filename') {
12401: 	        if ($symbparm) {
12402: 		    return &clutter((&decode_symb($symbparm))[2]);
12403: 	        }
12404: 	        return &hreflocation('',$env{'request.filename'});
12405: 	    }
12406: 
12407:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12408:                 if ($space eq 'visibleparts') {
12409:                     my $navmap = Apache::lonnavmaps::navmap->new();
12410:                     my $item;
12411:                     if (ref($navmap)) {
12412:                         my $res = $navmap->getBySymb($symbparm);
12413:                         my $parts = $res->parts();
12414:                         if (ref($parts) eq 'ARRAY') {
12415:                             $item = join(',',@{$parts});
12416:                         }
12417:                         undef($navmap);
12418:                     }
12419:                     return $item;
12420:                 }
12421:             }
12422:         }
12423: 
12424: 	my ($section, $group, @groups, @recurseup, $recursed);
12425:         if (ref($recurseupref) eq 'ARRAY') {
12426:             @recurseup = @{$recurseupref};
12427:             $recursed = 1;
12428:         }
12429: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12430:         if (($courseid eq '') && ($cid)) {
12431:             $courseid = $cid;
12432:         }
12433: 	if (($symbparm && $courseid) && 
12434: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12435: 
12436: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12437: 
12438: # ----------------------------------------------------- Cascading lookup scheme
12439: 	    my $symbp=$symbparm;
12440: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12441: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12442:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12443: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12444: 	    if (($env{'user.name'} eq $uname) &&
12445: 		($env{'user.domain'} eq $udom)) {
12446: 		$section=$env{'request.course.sec'};
12447:                 @groups = split(/:/,$env{'request.course.groups'});  
12448:                 @groups=&sort_course_groups($courseid,@groups); 
12449: 	    } else {
12450: 		if (! defined($usection)) {
12451: 		    $section=&getsection($udom,$uname,$courseid);
12452: 		} else {
12453: 		    $section = $usection;
12454: 		}
12455:                 @groups = &get_users_groups($udom,$uname,$courseid);
12456: 	    }
12457: 
12458: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12459: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12460:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12461: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12462: 
12463: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12464: 	    my $courselevelr=$courseid.'.'.$symbparm;
12465:             $courseleveli=$courseid.'.'.$recurseparm;
12466: 	    $courselevelm=$courseid.'.'.$mapparm;
12467: 
12468: # ----------------------------------------------------------- first, check user
12469: 
12470: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12471:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12472: 				       ([$courselevelr,'resource'],
12473: 					[$courselevelm,'map'     ],
12474:                                         [$courseleveli,'map'     ],
12475: 					[$courselevel, 'course'  ]));
12476: 	    if (defined($userreply)) { return &get_reply($userreply); }
12477: 
12478: # ------------------------------------------------ second, check some of course
12479:             my $coursereply;
12480:             if (@groups > 0) {
12481:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12482:                                        $recurseparm,$mapparm,$spacequalifierrest,
12483:                                        $mapp,\$recursed,\@recurseup);
12484:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12485:             }
12486: 
12487: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12488: 				  $env{'course.'.$courseid.'.domain'},
12489: 				  'course',$mapp,\$recursed,\@recurseup,
12490:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12491: 				  ([$seclevelr,   'resource'],
12492: 				   [$seclevelm,   'map'     ],
12493:                                    [$secleveli,   'map'     ],
12494: 				   [$seclevel,    'course'  ],
12495: 				   [$courselevelr,'resource']));
12496: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12497: 
12498: # ------------------------------------------------------ third, check map parms
12499: 	    my %parmhash=();
12500: 	    my $thisparm='';
12501: 	    if (tie(%parmhash,'GDBM_File',
12502: 		    $env{'request.course.fn'}.'_parms.db',
12503: 		    &GDBM_READER(),0640)) {
12504: 		$thisparm=$parmhash{$symbparm};
12505: 		untie(%parmhash);
12506: 	    }
12507: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12508: 	}
12509: # ------------------------------------------ fourth, look in resource metadata
12510:  
12511:         my $what = $spacequalifierrest;
12512: 	$what=~s/\./\_/;
12513: 	my $filename;
12514: 	if (!$symbparm) { $symbparm=&symbread(); }
12515: 	if ($symbparm) {
12516: 	    $filename=(&decode_symb($symbparm))[2];
12517: 	} else {
12518: 	    $filename=$env{'request.filename'};
12519: 	}
12520:         my $toolsymb;
12521:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12522:             $toolsymb = $symbparm;
12523:         }
12524: 	my $metadata=&metadata($filename,$what,$toolsymb);
12525: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12526: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12527: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12528: 
12529: # ----------------------------------------------- fifth, look in rest of course
12530: 	if ($symbparm && defined($courseid) && 
12531: 	    $courseid eq $env{'request.course.id'}) {
12532: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12533: 				     $env{'course.'.$courseid.'.domain'},
12534: 				     'course',$mapp,\$recursed,\@recurseup,
12535:                                      $courseid,'.',$spacequalifierrest,
12536: 				     ([$courselevelm,'map'   ],
12537:                                       [$courseleveli,'map'   ],
12538: 				      [$courselevel, 'course']));
12539: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12540: 	}
12541: # ------------------------------------------------------------------ Cascade up
12542: 	unless ($space eq '0') {
12543: 	    my @parts=split(/_/,$space);
12544: 	    my $id=pop(@parts);
12545: 	    my $part=join('_',@parts);
12546: 	    if ($part eq '') { $part='0'; }
12547: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12548: 				 $symbparm,$udom,$uname,$section,1);
12549: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12550: 	}
12551: 	if ($recurse) { return undef; }
12552: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12553: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12554: # ---------------------------------------------------- Any other user namespace
12555:     } elsif ($realm eq 'environment') {
12556: # ----------------------------------------------------------------- environment
12557: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12558: 	    return $env{'environment.'.$spacequalifierrest};
12559: 	} else {
12560: 	    if ($uname eq 'anonymous' && $udom eq '') {
12561: 		return '';
12562: 	    }
12563: 	    my %returnhash=&userenvironment($udom,$uname,
12564: 					    $spacequalifierrest);
12565: 	    return $returnhash{$spacequalifierrest};
12566: 	}
12567:     } elsif ($realm eq 'system') {
12568: # ----------------------------------------------------------------- system.time
12569: 	if ($space eq 'time') {
12570: 	    return time;
12571:         }
12572:     } elsif ($realm eq 'server') {
12573: # ----------------------------------------------------------------- system.time
12574: 	if ($space eq 'name') {
12575: 	    return $ENV{'SERVER_NAME'};
12576:         }
12577:     } elsif ($realm eq 'client') {
12578:         if ($space eq 'remote_addr') {
12579:             return &get_requestor_ip();
12580:         }
12581:     }
12582:     return '';
12583: }
12584: 
12585: sub get_reply {
12586:     my ($reply_value) = @_;
12587:     if (ref($reply_value) eq 'ARRAY') {
12588:         if (wantarray) {
12589: 	    return @$reply_value;
12590:         }
12591:         return $reply_value->[0];
12592:     } else {
12593:         return $reply_value;
12594:     }
12595: }
12596: 
12597: sub check_group_parms {
12598:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12599:         $recursed,$recurseupref) = @_;
12600:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12601:                   [$what,'course']);
12602:     my $coursereply;
12603:     foreach my $group (@{$groups}) {
12604:         my @groupitems = ();
12605:         foreach my $level (@levels) {
12606:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12607:              push(@groupitems,[$item,$level->[1]]);
12608:         }
12609:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12610:                                    $env{'course.'.$courseid.'.domain'},
12611:                                    'course',$mapp,$recursed,$recurseupref,
12612:                                    $courseid,'.['.$group.'].',$what,
12613:                                    @groupitems);
12614:         last if (defined($coursereply));
12615:     }
12616:     return $coursereply;
12617: }
12618: 
12619: sub get_map_hierarchy {
12620:     my ($mapname,$courseid) = @_;
12621:     my @recurseup = ();
12622:     if ($mapname) {
12623:         if (($cachedmapkey eq $courseid) &&
12624:             (abs($cachedmaptime-time)<5)) {
12625:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12626:                 return @{$cachedmaps{$mapname}};
12627:             }
12628:         }
12629:         my $navmap = Apache::lonnavmaps::navmap->new();
12630:         if (ref($navmap)) {
12631:             @recurseup = $navmap->recurseup_maps($mapname);
12632:             undef($navmap);
12633:             $cachedmaps{$mapname} = \@recurseup;
12634:             $cachedmaptime=time;
12635:             $cachedmapkey=$courseid;
12636:         }
12637:     }
12638:     return @recurseup;
12639: }
12640: 
12641: }
12642: 
12643: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12644:     my ($courseid,@groups) = @_;
12645:     @groups = sort(@groups);
12646:     return @groups;
12647: }
12648: 
12649: sub packages_tab_default {
12650:     my ($uri,$varname,$toolsymb)=@_;
12651:     my (undef,$part,$name)=split(/\./,$varname);
12652: 
12653:     my (@extension,@specifics,$do_default);
12654:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12655: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12656: 	if ($pack_type eq 'default') {
12657: 	    $do_default=1;
12658: 	} elsif ($pack_type eq 'extension') {
12659: 	    push(@extension,[$package,$pack_type,$pack_part]);
12660: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12661: 	    # only look at packages defaults for packages that this id is
12662: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12663: 	}
12664:     }
12665:     # first look for a package that matches the requested part id
12666:     foreach my $package (@specifics) {
12667: 	my (undef,$pack_type,$pack_part)=@{$package};
12668: 	next if ($pack_part ne $part);
12669: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12670: 	    return $packagetab{"$pack_type&$name&default"};
12671: 	}
12672:     }
12673:     # look for any possible matching non extension_ package
12674:     foreach my $package (@specifics) {
12675: 	my (undef,$pack_type,$pack_part)=@{$package};
12676: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12677: 	    return $packagetab{"$pack_type&$name&default"};
12678: 	}
12679: 	if ($pack_type eq 'part') { $pack_part='0'; }
12680: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12681: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12682: 	}
12683:     }
12684:     # look for any posible extension_ match
12685:     foreach my $package (@extension) {
12686: 	my ($package,$pack_type)=@{$package};
12687: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12688: 	    return $packagetab{"$pack_type&$name&default"};
12689: 	}
12690: 	if (defined($packagetab{$package."&$name&default"})) {
12691: 	    return $packagetab{$package."&$name&default"};
12692: 	}
12693:     }
12694:     # look for a global default setting
12695:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12696: 	return $packagetab{"default&$name&default"};
12697:     }
12698:     return undef;
12699: }
12700: 
12701: sub add_prefix_and_part {
12702:     my ($prefix,$part)=@_;
12703:     my $keyroot;
12704:     if (defined($prefix) && $prefix !~ /^__/) {
12705: 	# prefix that has a part already
12706: 	$keyroot=$prefix;
12707:     } elsif (defined($prefix)) {
12708: 	# prefix that is missing a part
12709: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12710:     } else {
12711: 	# no prefix at all
12712: 	if (defined($part)) { $keyroot='_'.$part; }
12713:     }
12714:     return $keyroot;
12715: }
12716: 
12717: # ---------------------------------------------------------------- Get metadata
12718: 
12719: my %metaentry;
12720: my %importedpartids;
12721: my %importedrespids;
12722: sub metadata {
12723:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12724:     $uri=&declutter($uri);
12725:     # if it is a non metadata possible uri return quickly
12726:     if (($uri eq '') || 
12727: 	(($uri =~ m|^/*adm/|) && 
12728: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12729:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12730: 	return undef;
12731:     }
12732:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12733: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12734: 	return undef;
12735:     }
12736:     my $filename=$uri;
12737:     $uri=~s/\.meta$//;
12738: #
12739: # Is the metadata already cached?
12740: # Look at timestamp of caching
12741: # Everything is cached by the main uri, libraries are never directly cached
12742: #
12743:     if (!defined($liburi)) {
12744: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12745: 	if (defined($cached)) { return $result->{':'.$what}; }
12746:     }
12747: 
12748: #
12749: # If the uri is for an external tool the file from
12750: # which metadata should be retrieved depends on whether
12751: # the tool had been configured to be gradable (set in the Course
12752: # Editor or Resource Editor).
12753: #
12754: # If a valid symb has been included as the third arg in the call
12755: # to &metadata() that can be used to retrieve the value of
12756: # parameter_0_gradable set for the resource, and included in the
12757: # uploaded map containing the tool. The value is retrieved via
12758: # &EXT(), if a valid symb is available.  Otherwise the value of
12759: # gradable in the exttool_$marker.db file for the tool instance
12760: # is retrieved via &get().
12761: #
12762: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12763: # hiddenresource and encrypturl (during course initialization)
12764: # the map-level parameter for resource.0.gradable included in the 
12765: # uploaded map containing the tool will not yet have been stored
12766: # in the user_course_parms.db file for the user's session, so in 
12767: # this case fall back to retrieving gradable status from the
12768: # exttool_$marker.db file.
12769: #
12770: # In order to avoid an infinite loop, &metadata() will return
12771: # before a call to &EXT(), if the uri is for an external tool
12772: # and the $what for which metadata is being requested is
12773: # parameter_0_gradable or 0_gradable.
12774: #
12775: 
12776:     if ($uri =~ /ext\.tool$/) {
12777:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12778:             return;
12779:         } else {
12780:             my ($checked,$use_passback);
12781:             if ($toolsymb ne '') {
12782:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12783:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12784:                     $checked = 1;
12785:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12786:                         $use_passback = 1;
12787:                     }
12788:                 }
12789:             }
12790:             unless ($checked) {
12791:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12792:                 $marker=~s/\D//g;
12793:                 if ($marker) {
12794:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12795:                     $use_passback = $toolsettings{'gradable'};
12796:                 }
12797:             }
12798:             if ($use_passback) {
12799:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12800:             } else {
12801:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12802:             }
12803:         }
12804:     }
12805: 
12806:     {
12807: # Imported parts would go here
12808:         my @origfiletagids=();
12809:         my $importedparts=0;
12810: 
12811: # Imported responseids would go here
12812:         my $importedresponses=0;
12813: #
12814: # Is this a recursive call for a library?
12815: #
12816: #	if (! exists($metacache{$uri})) {
12817: #	    $metacache{$uri}={};
12818: #	}
12819: 	my $cachetime = 60*60;
12820:         if ($liburi) {
12821: 	    $liburi=&declutter($liburi);
12822:             $filename=$liburi;
12823:         } else {
12824: 	    &devalidate_cache_new('meta',$uri);
12825: 	    undef(%metaentry);
12826: 	}
12827:         my %metathesekeys=();
12828:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12829: 	my $metastring;
12830: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12831: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12832: 	    $metastring = 
12833: 		&Apache::lonnet::ssi_body($which,
12834: 					  ('grade_target' => 'meta'));
12835: 	    $cachetime = 1; # only want this cached in the child not long term
12836: 	} elsif (($uri !~ m -^(editupload)/-) && 
12837:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12838: 	    my $file=&filelocation('',&clutter($filename));
12839: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12840: 	    $metastring=&getfile($file);
12841: 	}
12842:         my $parser=HTML::LCParser->new(\$metastring);
12843:         my $token;
12844:         undef %metathesekeys;
12845:         while ($token=$parser->get_token) {
12846: 	    if ($token->[0] eq 'S') {
12847: 		if (defined($token->[2]->{'package'})) {
12848: #
12849: # This is a package - get package info
12850: #
12851: 		    my $package=$token->[2]->{'package'};
12852: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12853: 		    if (defined($token->[2]->{'id'})) { 
12854: 			$keyroot.='_'.$token->[2]->{'id'}; 
12855: 		    }
12856: 		    if ($metaentry{':packages'}) {
12857: 			$metaentry{':packages'}.=','.$package.$keyroot;
12858: 		    } else {
12859: 			$metaentry{':packages'}=$package.$keyroot;
12860: 		    }
12861: 		    foreach my $pack_entry (keys(%packagetab)) {
12862: 			my $part=$keyroot;
12863: 			$part=~s/^\_//;
12864: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12865: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12866: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12867: 			    # ignore package.tab specified default values
12868:                             # here &package_tab_default() will fetch those
12869: 			    if ($subp eq 'default') { next; }
12870: 			    my $value=$packagetab{$pack_entry};
12871: 			    my $unikey;
12872: 			    if ($pack =~ /_0$/) {
12873: 				$unikey='parameter_0_'.$name;
12874: 				$part=0;
12875: 			    } else {
12876: 				$unikey='parameter'.$keyroot.'_'.$name;
12877: 			    }
12878: 			    if ($subp eq 'display') {
12879: 				$value.=' [Part: '.$part.']';
12880: 			    }
12881: 			    $metaentry{':'.$unikey.'.part'}=$part;
12882: 			    $metathesekeys{$unikey}=1;
12883: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12884: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12885: 			    }
12886: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12887: 				$metaentry{':'.$unikey}=
12888: 				    $metaentry{':'.$unikey.'.default'};
12889: 			    }
12890: 			}
12891: 		    }
12892: 		} else {
12893: #
12894: # This is not a package - some other kind of start tag
12895: #
12896: 		    my $entry=$token->[1];
12897: 		    my $unikey='';
12898: 
12899: 		    if ($entry eq 'import') {
12900: #
12901: # Importing a library here
12902: #
12903:                         my $location=$parser->get_text('/import');
12904:                         my $dir=$filename;
12905:                         $dir=~s|[^/]*$||;
12906:                         $location=&filelocation($dir,$location);
12907: 
12908:                         my $importid=$token->[2]->{'id'};
12909:                         my $importmode=$token->[2]->{'importmode'};
12910: #
12911: # Check metadata for imported file to
12912: # see if it contained response items
12913: #
12914:                         my ($origfile,@libfilekeys);
12915:                         my %currmetaentry = %metaentry;
12916:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12917:                                                            $depthcount+1));
12918:                         if (grep(/^responseorder$/,@libfilekeys)) {
12919:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12920:                                                              undef,$depthcount+1);
12921:                             if ($libresponseorder ne '') {
12922:                                 if ($#origfiletagids<0) {
12923:                                     undef(%importedrespids);
12924:                                     undef(%importedpartids);
12925:                                 }
12926:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12927:                                 if (@respids) {
12928:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12929:                                 }
12930:                                 if ($importedrespids{$importid} ne '') {
12931:                                     $importedresponses = 1;
12932: # We need to get the original file and the imported file to get the response order correct
12933: # Load and inspect original file
12934:                                     if ($#origfiletagids<0) {
12935:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12936:                                         $origfile=&getfile($origfilelocation);
12937:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12938:                                     }
12939:                                 }
12940:                             }
12941:                         }
12942: # Do not overwrite contents of %metaentry hash for resource itself with 
12943: # hash populated for imported library file
12944:                         %metaentry = %currmetaentry;
12945:                         undef(%currmetaentry);
12946:                         if ($importmode eq 'part') {
12947: # Import as part(s)
12948:                            $importedparts=1;
12949: # We need to get the original file and the imported file to get the part order correct
12950: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
12951: # Load and inspect original file if we didn't do that already
12952:                            if ($#origfiletagids<0) {
12953:                                undef(%importedrespids);
12954:                                undef(%importedpartids);
12955:                                if ($origfile eq '') {
12956:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12957:                                    $origfile=&getfile($origfilelocation);
12958:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12959:                                }
12960:                            }
12961:                            my @impfilepartids;
12962: # If <partorder> tag is included in metadata for the imported file
12963: # get the parts in the imported file from that.
12964:                            if (grep(/^partorder$/,@libfilekeys)) {
12965:                                %currmetaentry = %metaentry;
12966:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12967:                                                             $depthcount+1);
12968:                                %metaentry = %currmetaentry;
12969:                                undef(%currmetaentry);
12970:                                if ($libpartorder ne '') {
12971:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
12972:                                }
12973:                            } else {
12974: # If no <partorder> tag available, load and inspect imported file
12975:                                my $impfile=&getfile($location);
12976:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12977:                            }
12978:                            if ($#impfilepartids>=0) {
12979: # This problem had parts
12980:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
12981:                            } else {
12982: # Importing by turning a single problem into a problem part
12983: # It gets the import-tags ID as part-ID
12984:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
12985:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
12986:                            }
12987:                         } else {
12988: # Import as problem or as normal import
12989:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12990:                             unless ($importmode eq 'problem') {
12991: # Normal import
12992:                                 if (defined($token->[2]->{'id'})) {
12993:                                     $unikey.='_'.$token->[2]->{'id'};
12994:                                 }
12995:                             }
12996: # Check metadata for imported file to
12997: # see if it contained parts
12998:                             if (grep(/^partorder$/,@libfilekeys)) {
12999:                                 %currmetaentry = %metaentry;
13000:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13001:                                                              $depthcount+1);
13002:                                 %metaentry = %currmetaentry;
13003:                                 undef(%currmetaentry);
13004:                                 if ($libpartorder ne '') {
13005:                                     $importedparts = 1;
13006:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
13007:                                 }
13008:                             }
13009:                         }
13010: 			if ($depthcount<20) {
13011: 			    my $metadata = 
13012: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
13013: 					  $depthcount+1);
13014: 			    foreach my $meta (split(',',$metadata)) {
13015: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
13016: 				$metathesekeys{$meta}=1;
13017: 			    }
13018:                         }
13019: 		    } else {
13020: #
13021: # Not importing, some other kind of non-package, non-library start tag
13022: # 
13023:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13024:                         if (defined($token->[2]->{'id'})) {
13025:                             $unikey.='_'.$token->[2]->{'id'};
13026:                         }
13027: 			if (defined($token->[2]->{'name'})) { 
13028: 			    $unikey.='_'.$token->[2]->{'name'}; 
13029: 			}
13030: 			$metathesekeys{$unikey}=1;
13031: 			foreach my $param (@{$token->[3]}) {
13032: 			    $metaentry{':'.$unikey.'.'.$param} =
13033: 				$token->[2]->{$param};
13034: 			}
13035: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
13036: 			my $default=$metaentry{':'.$unikey.'.default'};
13037: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13038: 		 # only ws inside the tag, and not in default, so use default
13039: 		 # as value
13040: 			    $metaentry{':'.$unikey}=$default;
13041: 			} elsif ( $internaltext =~ /\S/ ) {
13042: 		  # something interesting inside the tag
13043: 			    $metaentry{':'.$unikey}=$internaltext;
13044: 			} else {
13045: 		  # no interesting values, don't set a default
13046: 			}
13047: # end of not-a-package not-a-library import
13048: 		    }
13049: # end of not-a-package start tag
13050: 		}
13051: # the next is the end of "start tag"
13052: 	    }
13053: 	}
13054: 	my ($extension) = ($uri =~ /\.(\w+)$/);
13055: 	$extension = lc($extension);
13056: 	if ($extension eq 'htm') { $extension='html'; }
13057: 
13058: 	foreach my $key (keys(%packagetab)) {
13059: 	    #no specific packages #how's our extension
13060: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
13061: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
13062: 					 \%metathesekeys);
13063: 	}
13064: 
13065: 	if (!exists($metaentry{':packages'})
13066: 	    || $packagetab{"import_defaults&extension_$extension"}) {
13067: 	    foreach my $key (keys(%packagetab)) {
13068: 		#no specific packages well let's get default then
13069: 		if ($key!~/^default&/) { next; }
13070: 		&metadata_create_package_def($uri,$key,'default',
13071: 					     \%metathesekeys);
13072: 	    }
13073: 	}
13074: # are there custom rights to evaluate
13075: 	if ($metaentry{':copyright'} eq 'custom') {
13076: 
13077:     #
13078:     # Importing a rights file here
13079:     #
13080: 	    unless ($depthcount) {
13081: 		my $location=$metaentry{':customdistributionfile'};
13082: 		my $dir=$filename;
13083: 		$dir=~s|[^/]*$||;
13084: 		$location=&filelocation($dir,$location);
13085: 		my $rights_metadata =
13086: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
13087: 			      $depthcount+1);
13088: 		foreach my $rights (split(',',$rights_metadata)) {
13089: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13090: 		    $metathesekeys{$rights}=1;
13091: 		}
13092: 	    }
13093: 	}
13094: 	# uniqifiy package listing
13095: 	my %seen;
13096: 	my @uniq_packages =
13097: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13098: 	$metaentry{':packages'} = join(',',@uniq_packages);
13099: 
13100:         if (($importedresponses) || ($importedparts)) {
13101:             if ($importedparts) {
13102: # We had imported parts and need to rebuild partorder
13103:                 $metaentry{':partorder'}='';
13104:                 $metathesekeys{'partorder'}=1;
13105:             }
13106:             if ($importedresponses) {
13107: # We had imported responses and need to rebuil responseorder
13108:                 $metaentry{':responseorder'}='';
13109:                 $metathesekeys{'responseorder'}=1;
13110:             }
13111:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
13112:                 my $origid = $origfiletagids[$index+1];
13113:                 if ($origfiletagids[$index] eq 'part') {
13114: # Original part, part of the problem
13115:                     if ($importedparts) {
13116:                         $metaentry{':partorder'}.=','.$origid;
13117:                     }
13118:                 } elsif ($origfiletagids[$index] eq 'import') {
13119:                     if ($importedparts) {
13120: # We have imported parts at this position
13121:                         if ($importedpartids{$origid} ne '') {
13122:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
13123:                         }
13124:                     }
13125:                     if ($importedresponses) {
13126: # We have imported responses at this position
13127:                         if ($importedrespids{$origid} ne '') {
13128:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
13129:                         }
13130:                     }
13131:                 } else {
13132: # Original response item, part of the problem
13133:                     if ($importedresponses) {
13134:                         $metaentry{':responseorder'}.=','.$origid;
13135:                     }
13136:                 }
13137:             }
13138:             if ($importedparts) {
13139:                 $metaentry{':partorder'}=~s/^\,//;
13140:             }
13141:             if ($importedresponses) {
13142:                 $metaentry{':responseorder'}=~s/^\,//;
13143:             }
13144:         }
13145: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
13146: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
13147: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
13148:         unless ($liburi) {
13149: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
13150:         }
13151: # this is the end of "was not already recently cached
13152:     }
13153:     return $metaentry{':'.$what};
13154: }
13155: 
13156: sub metadata_create_package_def {
13157:     my ($uri,$key,$package,$metathesekeys)=@_;
13158:     my ($pack,$name,$subp)=split(/\&/,$key);
13159:     if ($subp eq 'default') { next; }
13160:     
13161:     if (defined($metaentry{':packages'})) {
13162: 	$metaentry{':packages'}.=','.$package;
13163:     } else {
13164: 	$metaentry{':packages'}=$package;
13165:     }
13166:     my $value=$packagetab{$key};
13167:     my $unikey;
13168:     $unikey='parameter_0_'.$name;
13169:     $metaentry{':'.$unikey.'.part'}=0;
13170:     $$metathesekeys{$unikey}=1;
13171:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13172: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
13173:     }
13174:     if (defined($metaentry{':'.$unikey.'.default'})) {
13175: 	$metaentry{':'.$unikey}=
13176: 	    $metaentry{':'.$unikey.'.default'};
13177:     }
13178: }
13179: 
13180: sub metadata_generate_part0 {
13181:     my ($metadata,$metacache,$uri) = @_;
13182:     my %allnames;
13183:     foreach my $metakey (keys(%$metadata)) {
13184: 	if ($metakey=~/^parameter\_(.*)/) {
13185: 	  my $part=$$metacache{':'.$metakey.'.part'};
13186: 	  my $name=$$metacache{':'.$metakey.'.name'};
13187: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
13188: 	    $allnames{$name}=$part;
13189: 	  }
13190: 	}
13191:     }
13192:     foreach my $name (keys(%allnames)) {
13193:       $$metadata{"parameter_0_$name"}=1;
13194:       my $key=":parameter_0_$name";
13195:       $$metacache{"$key.part"}='0';
13196:       $$metacache{"$key.name"}=$name;
13197:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
13198: 					   $allnames{$name}.'_'.$name.
13199: 					   '.type'};
13200:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
13201: 			     '.display'};
13202:       my $expr='[Part: '.$allnames{$name}.']';
13203:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
13204:       $$metacache{"$key.display"}=$olddis;
13205:     }
13206: }
13207: 
13208: # ------------------------------------------------------ Devalidate title cache
13209: 
13210: sub devalidate_title_cache {
13211:     my ($url)=@_;
13212:     if (!$env{'request.course.id'}) { return; }
13213:     my $symb=&symbread($url);
13214:     if (!$symb) { return; }
13215:     my $key=$env{'request.course.id'}."\0".$symb;
13216:     &devalidate_cache_new('title',$key);
13217: }
13218: 
13219: # ------------------------------------------------- Get the title of a course
13220: 
13221: sub current_course_title {
13222:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13223: }
13224: # ------------------------------------------------- Get the title of a resource
13225: 
13226: sub gettitle {
13227:     my $urlsymb=shift;
13228:     my $symb=&symbread($urlsymb);
13229:     if ($symb) {
13230: 	my $key=$env{'request.course.id'}."\0".$symb;
13231: 	my ($result,$cached)=&is_cached_new('title',$key);
13232: 	if (defined($cached)) { 
13233: 	    return $result;
13234: 	}
13235: 	my ($map,$resid,$url)=&decode_symb($symb);
13236: 	my $title='';
13237: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13238: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13239: 	} else {
13240: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13241: 		    &GDBM_READER(),0640)) {
13242: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13243: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13244: 		untie(%bighash);
13245: 	    }
13246: 	}
13247: 	$title=~s/\&colon\;/\:/gs;
13248: 	if ($title) {
13249: # Remember both $symb and $title for dynamic metadata
13250:             $accesshash{$symb.'___crstitle'}=$title;
13251:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13252: # Cache this title and then return it
13253: 	    return &do_cache_new('title',$key,$title,600);
13254: 	}
13255: 	$urlsymb=$url;
13256:     }
13257:     my $title=&metadata($urlsymb,'title');
13258:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13259:     return $title;
13260: }
13261: 
13262: sub get_slot {
13263:     my ($which,$cnum,$cdom)=@_;
13264:     if (!$cnum || !$cdom) {
13265: 	(undef,my $courseid)=&whichuser();
13266: 	$cdom=$env{'course.'.$courseid.'.domain'};
13267: 	$cnum=$env{'course.'.$courseid.'.num'};
13268:     }
13269:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13270:     my %slotinfo;
13271:     if (exists($remembered{$key})) {
13272: 	$slotinfo{$which} = $remembered{$key};
13273:     } else {
13274: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13275: 	&Apache::lonhomework::showhash(%slotinfo);
13276: 	my ($tmp)=keys(%slotinfo);
13277: 	if ($tmp=~/^error:/) { return (); }
13278: 	$remembered{$key} = $slotinfo{$which};
13279:     }
13280:     if (ref($slotinfo{$which}) eq 'HASH') {
13281: 	return %{$slotinfo{$which}};
13282:     }
13283:     return $slotinfo{$which};
13284: }
13285: 
13286: sub get_reservable_slots {
13287:     my ($cnum,$cdom,$uname,$udom) = @_;
13288:     my $now = time;
13289:     my $reservable_info;
13290:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13291:     if (exists($remembered{$key})) {
13292:         $reservable_info = $remembered{$key};
13293:     } else {
13294:         my %resv;
13295:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13296:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13297:         $reservable_info = \%resv;
13298:         $remembered{$key} = $reservable_info;
13299:     }
13300:     return $reservable_info;
13301: }
13302: 
13303: sub get_course_slots {
13304:     my ($cnum,$cdom) = @_;
13305:     my $hashid=$cnum.':'.$cdom;
13306:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13307:     if (defined($cached)) {
13308:         if (ref($result) eq 'HASH') {
13309:             return %{$result};
13310:         }
13311:     } else {
13312:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13313:         my ($tmp) = keys(%slots);
13314:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13315:             &do_cache_new('allslots',$hashid,\%slots,600);
13316:             return %slots;
13317:         }
13318:     }
13319:     return;
13320: }
13321: 
13322: sub devalidate_slots_cache {
13323:     my ($cnum,$cdom)=@_;
13324:     my $hashid=$cnum.':'.$cdom;
13325:     &devalidate_cache_new('allslots',$hashid);
13326: }
13327: 
13328: sub get_coursechange {
13329:     my ($cdom,$cnum) = @_;
13330:     if ($cdom eq '' || $cnum eq '') {
13331:         return unless ($env{'request.course.id'});
13332:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13333:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13334:     }
13335:     my $hashid=$cdom.'_'.$cnum;
13336:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13337:     if ((defined($cached)) && ($change ne '')) {
13338:         return $change;
13339:     } else {
13340:         my %crshash;
13341:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13342:         if ($crshash{'internal.contentchange'} eq '') {
13343:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13344:             if ($change eq '') {
13345:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13346:                 $change = $crshash{'internal.created'};
13347:             }
13348:         } else {
13349:             $change = $crshash{'internal.contentchange'};
13350:         }
13351:         my $cachetime = 600;
13352:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13353:     }
13354:     return $change;
13355: }
13356: 
13357: sub devalidate_coursechange_cache {
13358:     my ($cnum,$cdom)=@_;
13359:     my $hashid=$cnum.':'.$cdom;
13360:     &devalidate_cache_new('crschange',$hashid);
13361: }
13362: 
13363: # ------------------------------------------------- Update symbolic store links
13364: 
13365: sub symblist {
13366:     my ($mapname,%newhash)=@_;
13367:     $mapname=&deversion(&declutter($mapname));
13368:     my %hash;
13369:     if (($env{'request.course.fn'}) && (%newhash)) {
13370:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13371:                       &GDBM_WRCREAT(),0640)) {
13372: 	    foreach my $url (keys(%newhash)) {
13373: 		next if ($url eq 'last_known'
13374: 			 && $env{'form.no_update_last_known'});
13375: 		$hash{declutter($url)}=&encode_symb($mapname,
13376: 						    $newhash{$url}->[1],
13377: 						    $newhash{$url}->[0]);
13378:             }
13379:             if (untie(%hash)) {
13380: 		return 'ok';
13381:             }
13382:         }
13383:     }
13384:     return 'error';
13385: }
13386: 
13387: # --------------------------------------------------------------- Verify a symb
13388: 
13389: sub symbverify {
13390:     my ($symb,$thisurl,$encstate)=@_;
13391:     my $thisfn=$thisurl;
13392:     $thisfn=&declutter($thisfn);
13393: # direct jump to resource in page or to a sequence - will construct own symbs
13394:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13395: # check URL part
13396:     my ($map,$resid,$url)=&decode_symb($symb);
13397: 
13398:     unless ($url eq $thisfn) { return 0; }
13399: 
13400:     $symb=&symbclean($symb);
13401:     $thisurl=&deversion($thisurl);
13402:     $thisfn=&deversion($thisfn);
13403: 
13404:     my %bighash;
13405:     my $okay=0;
13406: 
13407:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13408:                             &GDBM_READER(),0640)) {
13409:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13410:             $thisurl =~ s/\?.+$//;
13411:             if ($map =~ m{^uploaded/.+\.page$}) {
13412:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13413:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13414:             }
13415:         }
13416:         my $ids;
13417:         if ($map =~ m{^uploaded/.+\.page$}) {
13418:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13419:         } else {
13420:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13421:         }
13422:         unless ($ids) {
13423:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13424:             $ids=$bighash{$idkey};
13425:         }
13426:         if ($ids) {
13427: # ------------------------------------------------------------------- Has ID(s)
13428:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13429:                 $symb =~ s/\?.+$//;
13430:             }
13431: 	    foreach my $id (split(/\,/,$ids)) {
13432: 	       my ($mapid,$resid)=split(/\./,$id);
13433:                if (
13434:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13435:    eq $symb) {
13436:                    if (ref($encstate)) {
13437:                        $$encstate = $bighash{'encrypted_'.$id};
13438:                    }
13439: 		   if (($env{'request.role.adv'}) ||
13440: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13441:                        ($thisurl eq '/adm/navmaps')) {
13442: 		       $okay=1;
13443:                        last;
13444: 		   }
13445: 	       }
13446: 	   }
13447:         }
13448: 	untie(%bighash);
13449:     }
13450:     return $okay;
13451: }
13452: 
13453: # --------------------------------------------------------------- Clean-up symb
13454: 
13455: sub symbclean {
13456:     my $symb=shift;
13457:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13458: # remove version from map
13459:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13460: 
13461: # remove version from URL
13462:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13463: 
13464: # remove wrapper
13465: 
13466:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13467:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13468:     return $symb;
13469: }
13470: 
13471: # ---------------------------------------------- Split symb to find map and url
13472: 
13473: sub encode_symb {
13474:     my ($map,$resid,$url)=@_;
13475:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13476: }
13477: 
13478: sub decode_symb {
13479:     my $symb=shift;
13480:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13481:     my ($map,$resid,$url)=split(/___/,$symb);
13482:     return (&fixversion($map),$resid,&fixversion($url));
13483: }
13484: 
13485: sub fixversion {
13486:     my $fn=shift;
13487:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13488:     my %bighash;
13489:     my $uri=&clutter($fn);
13490:     my $key=$env{'request.course.id'}.'_'.$uri;
13491: # is this cached?
13492:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13493:     if (defined($cached)) { return $result; }
13494: # unfortunately not cached, or expired
13495:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13496: 	    &GDBM_READER(),0640)) {
13497:  	if ($bighash{'version_'.$uri}) {
13498:  	    my $version=$bighash{'version_'.$uri};
13499:  	    unless (($version eq 'mostrecent') || 
13500: 		    ($version==&getversion($uri))) {
13501:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13502:  	    }
13503:  	}
13504:  	untie %bighash;
13505:     }
13506:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13507: }
13508: 
13509: sub deversion {
13510:     my $url=shift;
13511:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13512:     return $url;
13513: }
13514: 
13515: # ------------------------------------------------------ Return symb list entry
13516: 
13517: sub symbread {
13518:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
13519:         $ignoresymbdb,$noenccheck)=@_;
13520:     my $cache_str='request.symbread.cached.'.$thisfn;
13521:     if (defined($env{$cache_str})) {
13522:         unless (ref($possibles) eq 'HASH') {
13523:             if ($ignorecachednull) {
13524:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
13525:             } else {
13526:                 return $env{$cache_str};
13527:             }
13528:         }
13529:     }
13530: # no filename provided? try from environment
13531:     unless ($thisfn) {
13532:         if ($env{'request.symb'}) {
13533:             return $env{$cache_str}=&symbclean($env{'request.symb'});
13534: 	}
13535: 	$thisfn=$env{'request.filename'};
13536:     }
13537:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13538: # is that filename actually a symb? Verify, clean, and return
13539:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13540: 	if (&symbverify($thisfn,$1)) {
13541: 	    return $env{$cache_str}=&symbclean($thisfn);
13542: 	}
13543:     }
13544:     $thisfn=declutter($thisfn);
13545:     my %hash;
13546:     my %bighash;
13547:     my $syval='';
13548:     if (($env{'request.course.fn'}) && ($thisfn)) {
13549:         my $targetfn = $thisfn;
13550:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13551:             $targetfn = 'adm/wrapper/'.$thisfn;
13552:         }
13553: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13554: 	    $targetfn=$1;
13555: 	}
13556:         unless ($ignoresymbdb) {
13557:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13558:                           &GDBM_READER(),0640)) {
13559: 	        $syval=$hash{$targetfn};
13560:                 untie(%hash);
13561:             }
13562:             if ($syval && $checkforblock) {
13563:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
13564:                 if (@blockers) {
13565:                     $syval='';
13566:                 }
13567:             }
13568:         }
13569: # ---------------------------------------------------------- There was an entry
13570:         if ($syval) {
13571: 	    #unless ($syval=~/\_\d+$/) {
13572: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13573: 		    #&appenv({'request.ambiguous' => $thisfn});
13574: 		    #return $env{$cache_str}='';
13575: 		#}    
13576: 		#$syval.=$1;
13577: 	    #}
13578:         } else {
13579: # ------------------------------------------------------- Was not in symb table
13580:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13581:                             &GDBM_READER(),0640)) {
13582: # ---------------------------------------------- Get ID(s) for current resource
13583:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13584:               unless ($ids) { 
13585:                  $ids=$bighash{'ids_/'.$thisfn};
13586:               }
13587:               unless ($ids) {
13588: # alias?
13589: 		  $ids=$bighash{'mapalias_'.$thisfn};
13590:               }
13591:               if ($ids) {
13592: # ------------------------------------------------------------------- Has ID(s)
13593:                  my @possibilities=split(/\,/,$ids);
13594:                  if ($#possibilities==0) {
13595: # ----------------------------------------------- There is only one possibility
13596: 		     my ($mapid,$resid)=split(/\./,$ids);
13597: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13598: 						    $resid,$thisfn);
13599:                      if (ref($possibles) eq 'HASH') {
13600:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13601:                              $possibles->{$syval} = 1;
13602:                          }
13603:                      }
13604:                      if ($checkforblock) {
13605:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13606:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
13607:                              if (@blockers) {
13608:                                  $syval = '';
13609:                                  untie(%bighash);
13610:                                  return $env{$cache_str}='';
13611:                              }
13612:                          }
13613:                      }
13614:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13615: # ------------------------------------------ There is more than one possibility
13616:                      my $realpossible=0;
13617:                      foreach my $id (@possibilities) {
13618: 			 my $file=$bighash{'src_'.$id};
13619:                          my $canaccess;
13620:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13621:                              $canaccess = 1;
13622:                          } else { 
13623:                              $canaccess = &allowed('bre',$file);
13624:                          }
13625:                          if ($canaccess) {
13626:          		     my ($mapid,$resid)=split(/\./,$id);
13627:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13628:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13629: 						             $resid,$thisfn);
13630:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
13631:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
13632:                                  if ($checkforblock) {
13633:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
13634:                                      if (@blockers > 0) {
13635:                                          $syval = '';
13636:                                      } else {
13637:                                          $syval = $poss_syval;
13638:                                          $realpossible++;
13639:                                      }
13640:                                  } else {
13641:                                      $syval = $poss_syval;
13642:                                      $realpossible++;
13643:                                  }
13644:                                  if ($syval) {
13645:                                      if (ref($possibles) eq 'HASH') {
13646:                                          $possibles->{$syval} = 1;
13647:                                      }
13648:                                  }
13649:                              }
13650: 			 }
13651:                      }
13652: 		     if ($realpossible!=1) { $syval=''; }
13653:                  } else {
13654:                      $syval='';
13655:                  }
13656: 	      }
13657:               untie(%bighash);
13658:            }
13659:         }
13660:         if ($syval) {
13661: 	    return $env{$cache_str}=$syval;
13662:         }
13663:     }
13664:     &appenv({'request.ambiguous' => $thisfn});
13665:     return $env{$cache_str}='';
13666: }
13667: 
13668: # ---------------------------------------------------------- Return random seed
13669: 
13670: sub numval {
13671:     my $txt=shift;
13672:     $txt=~tr/A-J/0-9/;
13673:     $txt=~tr/a-j/0-9/;
13674:     $txt=~tr/K-T/0-9/;
13675:     $txt=~tr/k-t/0-9/;
13676:     $txt=~tr/U-Z/0-5/;
13677:     $txt=~tr/u-z/0-5/;
13678:     $txt=~s/\D//g;
13679:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13680:     return int($txt);
13681: }
13682: 
13683: sub numval2 {
13684:     my $txt=shift;
13685:     $txt=~tr/A-J/0-9/;
13686:     $txt=~tr/a-j/0-9/;
13687:     $txt=~tr/K-T/0-9/;
13688:     $txt=~tr/k-t/0-9/;
13689:     $txt=~tr/U-Z/0-5/;
13690:     $txt=~tr/u-z/0-5/;
13691:     $txt=~s/\D//g;
13692:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13693:     my $total;
13694:     foreach my $val (@txts) { $total+=$val; }
13695:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13696:     return int($total);
13697: }
13698: 
13699: sub numval3 {
13700:     use integer;
13701:     my $txt=shift;
13702:     $txt=~tr/A-J/0-9/;
13703:     $txt=~tr/a-j/0-9/;
13704:     $txt=~tr/K-T/0-9/;
13705:     $txt=~tr/k-t/0-9/;
13706:     $txt=~tr/U-Z/0-5/;
13707:     $txt=~tr/u-z/0-5/;
13708:     $txt=~s/\D//g;
13709:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13710:     my $total;
13711:     foreach my $val (@txts) { $total+=$val; }
13712:     if ($_64bit) { $total=(($total<<32)>>32); }
13713:     return $total;
13714: }
13715: 
13716: sub digest {
13717:     my ($data)=@_;
13718:     my $digest=&Digest::MD5::md5($data);
13719:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13720:     my ($e,$f);
13721:     {
13722:         use integer;
13723:         $e=($a+$b);
13724:         $f=($c+$d);
13725:         if ($_64bit) {
13726:             $e=(($e<<32)>>32);
13727:             $f=(($f<<32)>>32);
13728:         }
13729:     }
13730:     if (wantarray) {
13731: 	return ($e,$f);
13732:     } else {
13733: 	my $g;
13734: 	{
13735: 	    use integer;
13736: 	    $g=($e+$f);
13737: 	    if ($_64bit) {
13738: 		$g=(($g<<32)>>32);
13739: 	    }
13740: 	}
13741: 	return $g;
13742:     }
13743: }
13744: 
13745: sub latest_rnd_algorithm_id {
13746:     return '64bit5';
13747: }
13748: 
13749: sub get_rand_alg {
13750:     my ($courseid)=@_;
13751:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13752:     if ($courseid) {
13753: 	return $env{"course.$courseid.rndseed"};
13754:     }
13755:     return &latest_rnd_algorithm_id();
13756: }
13757: 
13758: sub validCODE {
13759:     my ($CODE)=@_;
13760:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13761:     return 0;
13762: }
13763: 
13764: sub getCODE {
13765:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13766:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13767: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13768: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13769: 	return $Apache::lonhomework::history{'resource.CODE'};
13770:     }
13771:     return undef;
13772: }
13773: #
13774: #  Determines the random seed for a specific context:
13775: #
13776: # parameters:
13777: #   symb      - in course context the symb for the seed.
13778: #   course_id - The course id of the form domain_coursenum.
13779: #   domain    - Domain for the user.
13780: #   course    - Course for the user.
13781: #   cenv      - environment of the course.
13782: #
13783: # NOTE:
13784: #   All parameters are picked out of the environment if missing
13785: #   or not defined.
13786: #   If a symb cannot be determined the current time is used instead.
13787: #
13788: #  For a given well defined symb, courside, domain, username,
13789: #  and course environment, the seed is reproducible.
13790: #
13791: sub rndseed {
13792:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13793:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13794:     if (!defined($symb)) {
13795: 	unless ($symb=$wsymb) { return time; }
13796:     }
13797:     if (!defined $courseid) { 
13798: 	$courseid=$wcourseid; 
13799:     }
13800:     if (!defined $domain) { $domain=$wdomain; }
13801:     if (!defined $username) { $username=$wusername }
13802: 
13803:     my $which;
13804:     if (defined($cenv->{'rndseed'})) {
13805: 	$which = $cenv->{'rndseed'};
13806:     } else {
13807: 	$which =&get_rand_alg($courseid);
13808:     }
13809:     if (defined(&getCODE())) {
13810: 
13811: 	if ($which eq '64bit5') {
13812: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13813: 	} elsif ($which eq '64bit4') {
13814: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13815: 	} else {
13816: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13817: 	}
13818:     } elsif ($which eq '64bit5') {
13819: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13820:     } elsif ($which eq '64bit4') {
13821: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13822:     } elsif ($which eq '64bit3') {
13823: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13824:     } elsif ($which eq '64bit2') {
13825: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13826:     } elsif ($which eq '64bit') {
13827: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13828:     }
13829:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13830: }
13831: 
13832: sub rndseed_32bit {
13833:     my ($symb,$courseid,$domain,$username)=@_;
13834:     {
13835: 	use integer;
13836: 	my $symbchck=unpack("%32C*",$symb) << 27;
13837: 	my $symbseed=numval($symb) << 22;
13838: 	my $namechck=unpack("%32C*",$username) << 17;
13839: 	my $nameseed=numval($username) << 12;
13840: 	my $domainseed=unpack("%32C*",$domain) << 7;
13841: 	my $courseseed=unpack("%32C*",$courseid);
13842: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13843: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13844: 	#&logthis("rndseed :$num:$symb");
13845: 	if ($_64bit) { $num=(($num<<32)>>32); }
13846: 	return $num;
13847:     }
13848: }
13849: 
13850: sub rndseed_64bit {
13851:     my ($symb,$courseid,$domain,$username)=@_;
13852:     {
13853: 	use integer;
13854: 	my $symbchck=unpack("%32S*",$symb) << 21;
13855: 	my $symbseed=numval($symb) << 10;
13856: 	my $namechck=unpack("%32S*",$username);
13857: 	
13858: 	my $nameseed=numval($username) << 21;
13859: 	my $domainseed=unpack("%32S*",$domain) << 10;
13860: 	my $courseseed=unpack("%32S*",$courseid);
13861: 	
13862: 	my $num1=$symbchck+$symbseed+$namechck;
13863: 	my $num2=$nameseed+$domainseed+$courseseed;
13864: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13865: 	#&logthis("rndseed :$num:$symb");
13866: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13867: 	return "$num1,$num2";
13868:     }
13869: }
13870: 
13871: sub rndseed_64bit2 {
13872:     my ($symb,$courseid,$domain,$username)=@_;
13873:     {
13874: 	use integer;
13875: 	# strings need to be an even # of cahracters long, it it is odd the
13876:         # last characters gets thrown away
13877: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13878: 	my $symbseed=numval($symb) << 10;
13879: 	my $namechck=unpack("%32S*",$username.' ');
13880: 	
13881: 	my $nameseed=numval($username) << 21;
13882: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13883: 	my $courseseed=unpack("%32S*",$courseid.' ');
13884: 	
13885: 	my $num1=$symbchck+$symbseed+$namechck;
13886: 	my $num2=$nameseed+$domainseed+$courseseed;
13887: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13888: 	#&logthis("rndseed :$num:$symb");
13889: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13890: 	return "$num1,$num2";
13891:     }
13892: }
13893: 
13894: sub rndseed_64bit3 {
13895:     my ($symb,$courseid,$domain,$username)=@_;
13896:     {
13897: 	use integer;
13898: 	# strings need to be an even # of cahracters long, it it is odd the
13899:         # last characters gets thrown away
13900: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13901: 	my $symbseed=numval2($symb) << 10;
13902: 	my $namechck=unpack("%32S*",$username.' ');
13903: 	
13904: 	my $nameseed=numval2($username) << 21;
13905: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13906: 	my $courseseed=unpack("%32S*",$courseid.' ');
13907: 	
13908: 	my $num1=$symbchck+$symbseed+$namechck;
13909: 	my $num2=$nameseed+$domainseed+$courseseed;
13910: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13911: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13912: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13913: 	
13914: 	return "$num1:$num2";
13915:     }
13916: }
13917: 
13918: sub rndseed_64bit4 {
13919:     my ($symb,$courseid,$domain,$username)=@_;
13920:     {
13921: 	use integer;
13922: 	# strings need to be an even # of cahracters long, it it is odd the
13923:         # last characters gets thrown away
13924: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13925: 	my $symbseed=numval3($symb) << 10;
13926: 	my $namechck=unpack("%32S*",$username.' ');
13927: 	
13928: 	my $nameseed=numval3($username) << 21;
13929: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13930: 	my $courseseed=unpack("%32S*",$courseid.' ');
13931: 	
13932: 	my $num1=$symbchck+$symbseed+$namechck;
13933: 	my $num2=$nameseed+$domainseed+$courseseed;
13934: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13935: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13936: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13937: 	
13938: 	return "$num1:$num2";
13939:     }
13940: }
13941: 
13942: sub rndseed_64bit5 {
13943:     my ($symb,$courseid,$domain,$username)=@_;
13944:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
13945:     return "$num1:$num2";
13946: }
13947: 
13948: sub rndseed_CODE_64bit {
13949:     my ($symb,$courseid,$domain,$username)=@_;
13950:     {
13951: 	use integer;
13952: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13953: 	my $symbseed=numval2($symb);
13954: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13955: 	my $CODEseed=numval(&getCODE());
13956: 	my $courseseed=unpack("%32S*",$courseid.' ');
13957: 	my $num1=$symbseed+$CODEchck;
13958: 	my $num2=$CODEseed+$courseseed+$symbchck;
13959: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13960: 	#&logthis("rndseed :$num1:$num2:$symb");
13961: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13962: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13963: 	return "$num1:$num2";
13964:     }
13965: }
13966: 
13967: sub rndseed_CODE_64bit4 {
13968:     my ($symb,$courseid,$domain,$username)=@_;
13969:     {
13970: 	use integer;
13971: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13972: 	my $symbseed=numval3($symb);
13973: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13974: 	my $CODEseed=numval3(&getCODE());
13975: 	my $courseseed=unpack("%32S*",$courseid.' ');
13976: 	my $num1=$symbseed+$CODEchck;
13977: 	my $num2=$CODEseed+$courseseed+$symbchck;
13978: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13979: 	#&logthis("rndseed :$num1:$num2:$symb");
13980: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13981: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13982: 	return "$num1:$num2";
13983:     }
13984: }
13985: 
13986: sub rndseed_CODE_64bit5 {
13987:     my ($symb,$courseid,$domain,$username)=@_;
13988:     my $code = &getCODE();
13989:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
13990:     return "$num1:$num2";
13991: }
13992: 
13993: sub setup_random_from_rndseed {
13994:     my ($rndseed)=@_;
13995:     if ($rndseed =~/([,:])/) {
13996:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
13997:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
13998:             &Math::Random::random_set_seed_from_phrase($rndseed);
13999:         } else {
14000:             &Math::Random::random_set_seed($num1,$num2);
14001:         }
14002:     } else {
14003: 	&Math::Random::random_set_seed_from_phrase($rndseed);
14004:     }
14005: }
14006: 
14007: sub latest_receipt_algorithm_id {
14008:     return 'receipt3';
14009: }
14010: 
14011: sub recunique {
14012:     my $fucourseid=shift;
14013:     my $unique;
14014:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14015: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14016: 	$unique=$env{"course.$fucourseid.internal.encseed"};
14017:     } else {
14018: 	$unique=$perlvar{'lonReceipt'};
14019:     }
14020:     return unpack("%32C*",$unique);
14021: }
14022: 
14023: sub recprefix {
14024:     my $fucourseid=shift;
14025:     my $prefix;
14026:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14027: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14028: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
14029:     } else {
14030: 	$prefix=$perlvar{'lonHostID'};
14031:     }
14032:     return unpack("%32C*",$prefix);
14033: }
14034: 
14035: sub ireceipt {
14036:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
14037: 
14038:     my $return =&recprefix($fucourseid).'-';
14039: 
14040:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14041: 	$env{'request.state'} eq 'construct') {
14042: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14043: 	return $return;
14044:     }
14045: 
14046:     my $cuname=unpack("%32C*",$funame);
14047:     my $cudom=unpack("%32C*",$fudom);
14048:     my $cucourseid=unpack("%32C*",$fucourseid);
14049:     my $cusymb=unpack("%32C*",$fusymb);
14050:     my $cunique=&recunique($fucourseid);
14051:     my $cpart=unpack("%32S*",$part);
14052:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14053: 
14054: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
14055: 			       
14056: 	$return.= ($cunique%$cuname+
14057: 		   $cunique%$cudom+
14058: 		   $cusymb%$cuname+
14059: 		   $cusymb%$cudom+
14060: 		   $cucourseid%$cuname+
14061: 		   $cucourseid%$cudom+
14062: 		   $cpart%$cuname+
14063: 		   $cpart%$cudom);
14064:     } else {
14065: 	$return.= ($cunique%$cuname+
14066: 		   $cunique%$cudom+
14067: 		   $cusymb%$cuname+
14068: 		   $cusymb%$cudom+
14069: 		   $cucourseid%$cuname+
14070: 		   $cucourseid%$cudom);
14071:     }
14072:     return $return;
14073: }
14074: 
14075: sub receipt {
14076:     my ($part)=@_;
14077:     my ($symb,$courseid,$domain,$name) = &whichuser();
14078:     return &ireceipt($name,$domain,$courseid,$symb,$part);
14079: }
14080: 
14081: sub whichuser {
14082:     my ($passedsymb)=@_;
14083:     my ($symb,$courseid,$domain,$name,$publicuser);
14084:     if (defined($env{'form.grade_symb'})) {
14085: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14086: 	my $allowed=&allowed('vgr',$tmp_courseid);
14087: 	if (!$allowed &&
14088: 	    exists($env{'request.course.sec'}) &&
14089: 	    $env{'request.course.sec'} !~ /^\s*$/) {
14090: 	    $allowed=&allowed('vgr',$tmp_courseid.
14091: 			      '/'.$env{'request.course.sec'});
14092: 	}
14093: 	if ($allowed) {
14094: 	    ($symb)=&get_env_multiple('form.grade_symb');
14095: 	    $courseid=$tmp_courseid;
14096: 	    ($domain)=&get_env_multiple('form.grade_domain');
14097: 	    ($name)=&get_env_multiple('form.grade_username');
14098: 	    return ($symb,$courseid,$domain,$name,$publicuser);
14099: 	}
14100:     }
14101:     if (!$passedsymb) {
14102: 	$symb=&symbread();
14103:     } else {
14104: 	$symb=$passedsymb;
14105:     }
14106:     $courseid=$env{'request.course.id'};
14107:     $domain=$env{'user.domain'};
14108:     $name=$env{'user.name'};
14109:     if ($name eq 'public' && $domain eq 'public') {
14110: 	if (!defined($env{'form.username'})) {
14111: 	    $env{'form.username'}.=time.rand(10000000);
14112: 	}
14113: 	$name.=$env{'form.username'};
14114:     }
14115:     return ($symb,$courseid,$domain,$name,$publicuser);
14116: 
14117: }
14118: 
14119: # ------------------------------------------------------------ Serves up a file
14120: # returns either the contents of the file or 
14121: # -1 if the file doesn't exist
14122: #
14123: # if the target is a file that was uploaded via DOCS, 
14124: # a check will be made to see if a current copy exists on the local server,
14125: # if it does this will be served, otherwise a copy will be retrieved from
14126: # the home server for the course and stored in /home/httpd/html/userfiles on
14127: # the local server.   
14128: 
14129: sub getfile {
14130:     my ($file) = @_;
14131:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
14132:     &repcopy($file);
14133:     return &readfile($file);
14134: }
14135: 
14136: sub repcopy_userfile {
14137:     my ($file)=@_;
14138:     my $londocroot = $perlvar{'lonDocRoot'};
14139:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
14140:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
14141:     my ($cdom,$cnum,$filename) = 
14142: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
14143:     my $uri="/uploaded/$cdom/$cnum/$filename";
14144:     if (-e "$file") {
14145: # we already have a local copy, check it out
14146: 	my @fileinfo = stat($file);
14147: 	my $rtncode;
14148: 	my $info;
14149: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
14150: 	if ($lwpresp ne 'ok') {
14151: # there is no such file anymore, even though we had a local copy
14152: 	    if ($rtncode eq '404') {
14153: 		unlink($file);
14154: 	    }
14155: 	    return -1;
14156: 	}
14157: 	if ($info < $fileinfo[9]) {
14158: # nice, the file we have is up-to-date, just say okay
14159: 	    return 'ok';
14160: 	} else {
14161: # the file is outdated, get rid of it
14162: 	    unlink($file);
14163: 	}
14164:     }
14165: # one way or the other, at this point, we don't have the file
14166: # construct the correct path for the file
14167:     my @parts = ($cdom,$cnum); 
14168:     if ($filename =~ m|^(.+)/[^/]+$|) {
14169: 	push @parts, split(/\//,$1);
14170:     }
14171:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14172:     foreach my $part (@parts) {
14173: 	$path .= '/'.$part;
14174: 	if (!-e $path) {
14175: 	    mkdir($path,0770);
14176: 	}
14177:     }
14178: # now the path exists for sure
14179: # get a user agent
14180:     my $transferfile=$file.'.in.transfer';
14181: # FIXME: this should flock
14182:     if (-e $transferfile) { return 'ok'; }
14183:     my $request;
14184:     $uri=~s/^\///;
14185:     my $homeserver = &homeserver($cnum,$cdom);
14186:     my $hostname = &hostname($homeserver);
14187:     my $protocol = $protocol{$homeserver};
14188:     $protocol = 'http' if ($protocol ne 'https');
14189:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
14190:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
14191: # did it work?
14192:     if ($response->is_error()) {
14193: 	unlink($transferfile);
14194: 	&logthis("Userfile repcopy failed for $uri");
14195: 	return -1;
14196:     }
14197: # worked, rename the transfer file
14198:     rename($transferfile,$file);
14199:     return 'ok';
14200: }
14201: 
14202: sub tokenwrapper {
14203:     my $uri=shift;
14204:     $uri=~s|^https?\://([^/]+)||;
14205:     $uri=~s|^/||;
14206:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14207:     my $token=$1;
14208:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14209:     if ($udom && $uname && $file) {
14210: 	$file=~s|(\?\.*)*$||;
14211:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14212:         my $homeserver = &homeserver($uname,$udom);
14213:         my $hostname = &hostname($homeserver);
14214:         my $protocol = $protocol{$homeserver};
14215:         $protocol = 'http' if ($protocol ne 'https');
14216:         return $protocol.'://'.$hostname.'/'.$uri.
14217:                (($uri=~/\?/)?'&':'?').'token='.$token.
14218:                                '&tokenissued='.$perlvar{'lonHostID'};
14219:     } else {
14220:         return '/adm/notfound.html';
14221:     }
14222: }
14223: 
14224: # call with reqtype HEAD: get last modification time
14225: # call with reqtype GET: get the file contents
14226: # Do not call this with reqtype GET for large files! It loads everything into memory
14227: #
14228: sub getuploaded {
14229:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14230:     $uri=~s/^\///;
14231:     my $homeserver = &homeserver($cnum,$cdom);
14232:     my $hostname = &hostname($homeserver);
14233:     my $protocol = $protocol{$homeserver};
14234:     $protocol = 'http' if ($protocol ne 'https');
14235:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14236:     my $request=new HTTP::Request($reqtype,$uri);
14237:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14238:     $$rtncode = $response->code;
14239:     if (! $response->is_success()) {
14240: 	return 'failed';
14241:     }      
14242:     if ($reqtype eq 'HEAD') {
14243: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14244:     } elsif ($reqtype eq 'GET') {
14245: 	$$info = $response->content;
14246:     }
14247:     return 'ok';
14248: }
14249: 
14250: sub readfile {
14251:     my $file = shift;
14252:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14253:     my $fh;
14254:     open($fh,"<",$file);
14255:     my $a='';
14256:     while (my $line = <$fh>) { $a .= $line; }
14257:     return $a;
14258: }
14259: 
14260: sub filelocation {
14261:     my ($dir,$file) = @_;
14262:     my $location;
14263:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14264: 
14265:     if ($file =~ m-^/adm/-) {
14266: 	$file=~s-^/adm/wrapper/-/-;
14267: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14268:     }
14269: 
14270:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14271:         $location = $file;
14272:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14273:         my ($udom,$uname,$filename)=
14274:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14275:         my $home=&homeserver($uname,$udom);
14276:         my $is_me=0;
14277:         my @ids=&current_machine_ids();
14278:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14279:         if ($is_me) {
14280:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14281:         } else {
14282:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14283:   	      $udom.'/'.$uname.'/'.$filename;
14284:         }
14285:     } elsif ($file =~ m-^/adm/-) {
14286: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14287:     } else {
14288:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14289:         $file=~s:^/(res|priv)/:/:;
14290:         my $space=$1;
14291:         if ( !( $file =~ m:^/:) ) {
14292:             $location = $dir. '/'.$file;
14293:         } else {
14294:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14295:         }
14296:     }
14297:     $location=~s://+:/:g; # remove duplicate /
14298:     while ($location=~m{/\.\./}) {
14299: 	if ($location =~ m{/[^/]+/\.\./}) {
14300: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14301: 	} else {
14302: 	    $location=~ s{/\.\./}{/}g;
14303: 	}
14304:     } #remove dir/..
14305:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14306:     return $location;
14307: }
14308: 
14309: sub hreflocation {
14310:     my ($dir,$file)=@_;
14311:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14312: 	$file=filelocation($dir,$file);
14313:     } elsif ($file=~m-^/adm/-) {
14314: 	$file=~s-^/adm/wrapper/-/-;
14315: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14316:     }
14317:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14318: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14319:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14320: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14321: 	        {/uploaded/$1/$2/}x;
14322:     }
14323:     if ($file=~ m{^/userfiles/}) {
14324: 	$file =~ s{^/userfiles/}{/uploaded/};
14325:     }
14326:     return $file;
14327: }
14328: 
14329: 
14330: 
14331: 
14332: 
14333: sub current_machine_domains {
14334:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14335: }
14336: 
14337: sub machine_domains {
14338:     my ($hostname) = @_;
14339:     my @domains;
14340:     my %hostname = &all_hostnames();
14341:     while( my($id, $name) = each(%hostname)) {
14342: #	&logthis("-$id-$name-$hostname-");
14343: 	if ($hostname eq $name) {
14344: 	    push(@domains,&host_domain($id));
14345: 	}
14346:     }
14347:     return @domains;
14348: }
14349: 
14350: sub current_machine_ids {
14351:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14352: }
14353: 
14354: sub machine_ids {
14355:     my ($hostname) = @_;
14356:     $hostname ||= &hostname($perlvar{'lonHostID'});
14357:     my @ids;
14358:     my %name_to_host = &all_names();
14359:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14360: 	return @{ $name_to_host{$hostname} };
14361:     }
14362:     return;
14363: }
14364: 
14365: sub additional_machine_domains {
14366:     my @domains;
14367:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
14368:     while( my $line = <$fh>) {
14369:         $line =~ s/\s//g;
14370:         push(@domains,$line);
14371:     }
14372:     return @domains;
14373: }
14374: 
14375: sub default_login_domain {
14376:     my $domain = $perlvar{'lonDefDomain'};
14377:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14378:     foreach my $posdom (&current_machine_domains(),
14379:                         &additional_machine_domains()) {
14380:         if (lc($posdom) eq lc($testdomain)) {
14381:             $domain=$posdom;
14382:             last;
14383:         }
14384:     }
14385:     return $domain;
14386: }
14387: 
14388: sub shared_institution {
14389:     my ($dom,$lonhost) = @_;
14390:     if ($lonhost eq '') {
14391:         $lonhost = $perlvar{'lonHostID'};
14392:     }
14393:     my $same_intdom;
14394:     my $hostintdom = &internet_dom($lonhost);
14395:     if ($hostintdom ne '') {
14396:         my %iphost = &get_iphost();
14397:         my $primary_id = &domain($dom,'primary');
14398:         my $primary_ip = &get_host_ip($primary_id);
14399:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14400:             foreach my $id (@{$iphost{$primary_ip}}) {
14401:                 my $intdom = &internet_dom($id);
14402:                 if ($intdom eq $hostintdom) {
14403:                     $same_intdom = 1;
14404:                     last;
14405:                 }
14406:             }
14407:         }
14408:     }
14409:     return $same_intdom;
14410: }
14411: 
14412: sub uses_sts {
14413:     my ($ignore_cache) = @_;
14414:     my $lonhost = $perlvar{'lonHostID'};
14415:     my $hostname = &hostname($lonhost);
14416:     my $sts_on;
14417:     if ($protocol{$lonhost} eq 'https') {
14418:         my $cachetime = 12*3600;
14419:         if (!$ignore_cache) {
14420:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14421:             if (defined($cached)) {
14422:                 return $sts_on;
14423:             }
14424:         }
14425:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14426:         my $request=new HTTP::Request('HEAD',$url);
14427:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14428:         if ($response->is_success) {
14429:             my $has_sts = $response->header('Strict-Transport-Security');
14430:             if ($has_sts eq '') {
14431:                 $sts_on = 0;
14432:             } else {
14433:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14434:                     my $maxage = $1;
14435:                     if ($maxage) {
14436:                         $sts_on = 1;
14437:                     } else {
14438:                         $sts_on = 0;
14439:                     }
14440:                 } else {
14441:                     $sts_on = 0;
14442:                 }
14443:             }
14444:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14445:         }
14446:     }
14447:     return;
14448: }
14449: 
14450: sub waf_allssl {
14451:     my ($host_name) = @_;
14452:     my $alias = &get_proxy_alias();
14453:     if ($host_name eq '') {
14454:         $host_name = $ENV{'SERVER_NAME'};
14455:     }
14456:     if (($host_name ne '') && ($alias eq $host_name)) {
14457:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14458:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
14459:         if ($defdomdefaults{'waf_sslopt'}) {
14460:             return $defdomdefaults{'waf_sslopt'};
14461:         }
14462:     }
14463:     return;
14464: }
14465: 
14466: sub get_requestor_ip {
14467:     my ($r,$nolookup,$noproxy) = @_;
14468:     my $from_ip;
14469:     if (ref($r)) {
14470:         if ($r->can('useragent_ip')) {
14471:             if ($noproxy && $r->can('client_ip')) {
14472:                 $from_ip = $r->client_ip();
14473:             } else {
14474:                 $from_ip = $r->useragent_ip();
14475:             }
14476:         } elsif ($r->connection->can('remote_ip')) {
14477:             $from_ip = $r->connection->remote_ip();
14478:         } else {
14479:             $from_ip = $r->get_remote_host($nolookup);
14480:         }
14481:     } else {
14482:         $from_ip = $ENV{'REMOTE_ADDR'};
14483:     }
14484:     return $from_ip if ($noproxy); 
14485:     # Who controls proxy settings for server
14486:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14487:     my $proxyinfo = &get_proxy_settings($dom_in_use);
14488:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14489:         if ($proxyinfo->{'vpnint'}) {
14490:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14491:                 return $from_ip;
14492:             }
14493:         }
14494:         if ($proxyinfo->{'trusted'}) {
14495:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14496:                 my $ipheader = $proxyinfo->{'ipheader'};
14497:                 my ($ip,$xfor);
14498:                 if (ref($r)) {
14499:                     if ($ipheader) {
14500:                         $ip = $r->headers_in->{$ipheader};
14501:                     }
14502:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
14503:                 } else {
14504:                     if ($ipheader) {
14505:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
14506:                     }
14507:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14508:                 }
14509:                 if (($ip eq '') && ($xfor ne '')) {
14510:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14511:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14512:                             $ip = $poss_ip;
14513:                             last;
14514:                         }
14515:                     }
14516:                 }
14517:                 if ($ip ne '') {
14518:                     return $ip;
14519:                 }
14520:             }
14521:         }
14522:     }
14523:     return $from_ip;
14524: }
14525: 
14526: sub get_proxy_settings {
14527:     my ($dom_in_use) = @_;
14528:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14529:     my $proxyinfo = {
14530:                        ipheader => $domdefaults{'waf_ipheader'},
14531:                        trusted  => $domdefaults{'waf_trusted'},
14532:                        vpnint   => $domdefaults{'waf_vpnint'},
14533:                        vpnext   => $domdefaults{'waf_vpnext'},
14534:                        sslopt   => $domdefaults{'waf_sslopt'},
14535:                     };
14536:     return $proxyinfo;
14537: }
14538: 
14539: sub ip_match {
14540:     my ($ip,$pattern_str) = @_;
14541:     $ip=Net::CIDR::cidrvalidate($ip);
14542:     if ($ip) {
14543:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14544:     }
14545:     return;
14546: }
14547: 
14548: sub get_proxy_alias {
14549:     my ($lonid) = @_;
14550:     if ($lonid eq '') {
14551:         $lonid = $perlvar{'lonHostID'};
14552:     }
14553:     if (!defined(&hostname($lonid))) {
14554:         return;
14555:     }
14556:     if ($lonid ne '') {
14557:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14558:         if ($cached) {
14559:             return $alias;
14560:         }
14561:         my $dom = &Apache::lonnet::host_domain($lonid);
14562:         if ($dom ne '') {
14563:             my $cachetime = 60*60*24;
14564:             my %domconfig =
14565:                 &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14566:             my $alias;
14567:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14568:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14569:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14570:                 }
14571:             }
14572:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14573:         }
14574:     }
14575:     return;
14576: }
14577: 
14578: sub use_proxy_alias {
14579:     my ($r,$lonid) = @_;
14580:     my $alias = &get_proxy_alias($lonid);
14581:     if ($alias) {
14582:         my $dom = &host_domain($lonid);
14583:         if ($dom ne '') {
14584:             my $proxyinfo = &get_proxy_settings($dom );
14585:             my ($vpnint,$remote_ip);
14586:             if (ref($proxyinfo) eq 'HASH') {
14587:                 $vpnint = $proxyinfo->{'vpnint'};
14588:                 if ($vpnint) {
14589:                     $remote_ip = &get_requestor_ip($r,1,1);
14590:                 }
14591:             }
14592:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14593:                 return $alias;
14594:             }
14595:         }
14596:     }
14597:     return;
14598: }
14599: 
14600: # ------------------------------------------------------------- Declutters URLs
14601: 
14602: sub declutter {
14603:     my $thisfn=shift;
14604:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14605:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14606:         $thisfn=~s{^/home/httpd/html}{};
14607:     }
14608:     $thisfn=~s/^\///;
14609:     $thisfn=~s|^adm/wrapper/||;
14610:     $thisfn=~s|^adm/coursedocs/showdoc/||;
14611:     $thisfn=~s/^res\///;
14612:     $thisfn=~s/^priv\///;
14613:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14614:         $thisfn=~s/\?.+$//;
14615:     }
14616:     return $thisfn;
14617: }
14618: 
14619: # ------------------------------------------------------------- Clutter up URLs
14620: 
14621: sub clutter {
14622:     my $thisfn='/'.&declutter(shift);
14623:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14624: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14625:        $thisfn='/res'.$thisfn; 
14626:     }
14627:     if ($thisfn !~m|^/adm|) {
14628: 	if ($thisfn =~ m|^/ext/|) {
14629: 	    $thisfn='/adm/wrapper'.$thisfn;
14630: 	} else {
14631: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14632: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14633: 	    if ($embstyle eq 'ssi'
14634: 		|| ($embstyle eq 'hdn')
14635: 		|| ($embstyle eq 'rat')
14636: 		|| ($embstyle eq 'prv')
14637: 		|| ($embstyle eq 'ign')) {
14638: 		#do nothing with these
14639: 	    } elsif (($embstyle eq 'img') 
14640: 		|| ($embstyle eq 'emb')
14641: 		|| ($embstyle eq 'wrp')) {
14642: 		$thisfn='/adm/wrapper'.$thisfn;
14643: 	    } elsif ($embstyle eq 'unk'
14644: 		     && $thisfn!~/\.(sequence|page)$/) {
14645: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14646: 	    } else {
14647: #		&logthis("Got a blank emb style");
14648: 	    }
14649: 	}
14650:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14651:         $thisfn='/adm/wrapper'.$thisfn;
14652:     }
14653:     return $thisfn;
14654: }
14655: 
14656: sub clutter_with_no_wrapper {
14657:     my $uri = &clutter(shift);
14658:     if ($uri =~ m-^/adm/-) {
14659: 	$uri =~ s-^/adm/wrapper/-/-;
14660: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14661:     }
14662:     return $uri;
14663: }
14664: 
14665: sub freeze_escape {
14666:     my ($value)=@_;
14667:     if (ref($value)) {
14668: 	$value=&nfreeze($value);
14669: 	return '__FROZEN__'.&escape($value);
14670:     }
14671:     return &escape($value);
14672: }
14673: 
14674: 
14675: sub thaw_unescape {
14676:     my ($value)=@_;
14677:     if ($value =~ /^__FROZEN__/) {
14678: 	substr($value,0,10,undef);
14679: 	$value=&unescape($value);
14680: 	return &thaw($value);
14681:     }
14682:     return &unescape($value);
14683: }
14684: 
14685: sub correct_line_ends {
14686:     my ($result)=@_;
14687:     $$result =~s/\r\n/\n/mg;
14688:     $$result =~s/\r/\n/mg;
14689: }
14690: # ================================================================ Main Program
14691: 
14692: sub goodbye {
14693:    &logthis("Starting Shut down");
14694: #not converted to using infrastruture and probably shouldn't be
14695:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14696: #converted
14697: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14698:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14699: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14700: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14701: #1.1 only
14702: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14703: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14704: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14705: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14706:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14707:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14708:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14709:    &flushcourselogs();
14710:    &logthis("Shutting down");
14711: }
14712: 
14713: sub get_dns {
14714:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14715:     if (!$ignore_cache) {
14716: 	my ($content,$cached)=
14717: 	    &Apache::lonnet::is_cached_new('dns',$url);
14718: 	if ($cached) {
14719: 	    &$func($content,$hashref);
14720: 	    return;
14721: 	}
14722:     }
14723: 
14724:     my %alldns;
14725:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14726:         foreach my $dns (<$config>) {
14727: 	    next if ($dns !~ /^\^(\S*)/x);
14728:             my $line = $1;
14729:             my ($host,$protocol) = split(/:/,$line);
14730:             if ($protocol ne 'https') {
14731:                 $protocol = 'http';
14732:             }
14733: 	    $alldns{$host} = $protocol;
14734:         }
14735:         close($config);
14736:     }
14737:     while (%alldns) {
14738: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14739:         my ($contents,@content);
14740:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
14741:             my $command = (split('/',$url))[3];
14742:             my ($dir,$file) = &parse_getdns_url($command,$url);
14743:             delete($alldns{$dns});
14744:             next if (($dir eq '') || ($file eq ''));
14745:             if (open(my $config,'<',"$dir/$file")) {
14746:                 @content = <$config>;
14747:                 close($config);
14748:             }
14749:             if ($url eq '/adm/dns/loncapaCRL') {
14750:                 $contents = join('',@content);
14751:             }
14752:         } else {
14753: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14754:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14755:             delete($alldns{$dns});
14756: 	    next if ($response->is_error());
14757:             if ($url eq '/adm/dns/loncapaCRL') {
14758:                 $contents = $response->content;
14759:             } else {
14760:                 @content = split("\n",$response->content);
14761:             }
14762:         }
14763:         if ($url eq '/adm/dns/loncapaCRL') {
14764:             return &$func($contents);
14765:         } else {
14766: 	    unless ($nocache) {
14767: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14768: 	    }
14769: 	    &$func(\@content,$hashref);
14770:             return;
14771:         }
14772:     }
14773:     my $which = (split('/',$url,4))[3];
14774:     if ($which eq 'loncapaCRL') {
14775:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14776:         if (-e $diskfile) {
14777:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14778:         } else {
14779:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14780:         }
14781:     } else {
14782:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14783:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14784:             my @content = <$config>;
14785:             close($config);
14786:             &$func(\@content,$hashref);
14787:         }
14788:     }
14789:     return;
14790: }
14791: 
14792: # ------------------------------------------------------Get DNS checksums file
14793: sub parse_dns_checksums_tab {
14794:     my ($lines,$hashref) = @_;
14795:     my $lonhost = $perlvar{'lonHostID'};
14796:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14797:     my $loncaparev = &get_server_loncaparev($machine_dom);
14798:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14799:     my $webconfdir = '/etc/httpd/conf';
14800:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14801:         $webconfdir = '/etc/apache2';
14802:     } elsif ($distro =~ /^sles(\d+)$/) {
14803:         if ($1 >= 10) {
14804:             $webconfdir = '/etc/apache2';
14805:         }
14806:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14807:         if ($1 >= 10.0) {
14808:             $webconfdir = '/etc/apache2';
14809:         }
14810:     }
14811:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14812:     my (%chksum,%revnum);
14813:     if (ref($lines) eq 'ARRAY') {
14814:         chomp(@{$lines});
14815:         my $version = shift(@{$lines});
14816:         if ($version eq $release) {  
14817:             foreach my $line (@{$lines}) {
14818:                 my ($file,$version,$shasum) = split(/,/,$line);
14819:                 if ($file =~ m{^/etc/httpd/conf}) {
14820:                     if ($webconfdir eq '/etc/apache2') {
14821:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14822:                     }
14823:                 }
14824:                 $chksum{$file} = $shasum;
14825:                 $revnum{$file} = $version;
14826:             }
14827:             if (ref($hashref) eq 'HASH') {
14828:                 %{$hashref} = (
14829:                                 sums     => \%chksum,
14830:                                 versions => \%revnum,
14831:                               );
14832:             }
14833:         }
14834:     }
14835:     return;
14836: }
14837: 
14838: sub fetch_dns_checksums {
14839:     my %checksums;
14840:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14841:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14842:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14843:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14844:              \%checksums);
14845:     return \%checksums;
14846: }
14847: 
14848: sub fetch_crl_pemfile {
14849:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14850: }
14851: 
14852: sub save_crl_pem {
14853:     my ($content) = @_;
14854:     my ($msg,$hadchanges);
14855:     if ($content ne '') {
14856:         my $now = time;
14857:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14858:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14859:         if (open(my $fh,'>',"$tmpcrl")) {
14860:             print $fh $content;
14861:             close($fh);
14862:             if (-e $lonca) {
14863:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14864:                     my $check = <PIPE>;
14865:                     close(PIPE);
14866:                     chomp($check);
14867:                     if ($check eq 'verify OK') {
14868:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14869:                         my $backup;
14870:                         if (-e $dest) {
14871:                             if (&File::Copy::move($dest,"$dest.bak")) {
14872:                                 $backup = 'ok';
14873:                             }
14874:                         }
14875:                         if (&File::Copy::move($tmpcrl,$dest)) {
14876:                             $msg = 'ok';
14877:                             if ($backup) {
14878:                                 my (%oldnums,%newnums);
14879:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
14880:                                     while (<PIPE>) {
14881:                                         $oldnums{(split(/:/))[1]} = 1;
14882:                                     }
14883:                                     close(PIPE);
14884:                                 }
14885:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
14886:                                     while(<PIPE>) {
14887:                                         $newnums{(split(/:/))[1]} = 1;
14888:                                     }
14889:                                     close(PIPE);
14890:                                 }
14891:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
14892:                                     unless (exists($oldnums{$key})) {
14893:                                         $hadchanges = 1;
14894:                                         last;
14895:                                     }
14896:                                 }
14897:                                 unless ($hadchanges) {
14898:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
14899:                                         unless (exists($newnums{$key})) {
14900:                                             $hadchanges = 1;
14901:                                             last;
14902:                                         }
14903:                                     }
14904:                                 }
14905:                             }
14906:                         }
14907:                     } else {
14908:                         unlink($tmpcrl);
14909:                     }
14910:                 } else {
14911:                     unlink($tmpcrl);
14912:                 }
14913:             } else {
14914:                 unlink($tmpcrl);
14915:             }
14916:         }
14917:     }
14918:     return ($msg,$hadchanges);
14919: }
14920: 
14921: sub parse_getdns_url {
14922:     my ($command,$url) = @_;
14923:     my $dir = $perlvar{'lonTabDir'};
14924:     my $file;
14925:     if ($command eq 'hosts') {
14926:         $file = 'dns_hosts.tab';
14927:     } elsif ($command eq 'domain') {
14928:         $file = 'dns_domain.tab';
14929:     } elsif ($command eq 'checksums') {
14930:         my $version = (split('/',$url))[4];
14931:         $file = "dns_checksums/$version.tab",
14932:     } elsif ($command eq 'loncapaCRL') {
14933:         $dir = $perlvar{'lonCertificateDirectory'};
14934:         $file = $perlvar{'lonnetCertRevocationList'};
14935:     }
14936:     return ($dir,$file);
14937: }
14938: 
14939: # ------------------------------------------------------------ Read domain file
14940: {
14941:     my $loaded;
14942:     my %domain;
14943: 
14944:     sub parse_domain_tab {
14945: 	my ($lines) = @_;
14946: 	foreach my $line (@$lines) {
14947: 	    next if ($line =~ /^(\#|\s*$ )/x);
14948: 
14949: 	    chomp($line);
14950: 	    my ($name,@elements) = split(/:/,$line,9);
14951: 	    my %this_domain;
14952: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
14953: 			       'lang_def', 'city', 'longi', 'lati',
14954: 			       'primary') {
14955: 		$this_domain{$field} = shift(@elements);
14956: 	    }
14957: 	    $domain{$name} = \%this_domain;
14958: 	}
14959:     }
14960: 
14961:     sub reset_domain_info {
14962: 	undef($loaded);
14963: 	undef(%domain);
14964:     }
14965: 
14966:     sub load_domain_tab {
14967: 	my ($ignore_cache,$nocache) = @_;
14968: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
14969: 	my $fh;
14970: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
14971: 	    my @lines = <$fh>;
14972: 	    &parse_domain_tab(\@lines);
14973: 	}
14974: 	close($fh);
14975: 	$loaded = 1;
14976:     }
14977: 
14978:     sub domain {
14979: 	&load_domain_tab() if (!$loaded);
14980: 
14981: 	my ($name,$what) = @_;
14982: 	return if ( !exists($domain{$name}) );
14983: 
14984: 	if (!$what) {
14985: 	    return $domain{$name}{'description'};
14986: 	}
14987: 	return $domain{$name}{$what};
14988:     }
14989: 
14990:     sub domain_info {
14991:         &load_domain_tab() if (!$loaded);
14992:         return %domain;
14993:     }
14994: 
14995: }
14996: 
14997: 
14998: # ------------------------------------------------------------- Read hosts file
14999: {
15000:     my %hostname;
15001:     my %hostdom;
15002:     my %libserv;
15003:     my $loaded;
15004:     my %name_to_host;
15005:     my %internetdom;
15006:     my %LC_dns_serv;
15007: 
15008:     sub parse_hosts_tab {
15009: 	my ($file) = @_;
15010: 	foreach my $configline (@$file) {
15011: 	    next if ($configline =~ /^(\#|\s*$ )/x);
15012:             chomp($configline);
15013: 	    if ($configline =~ /^\^/) {
15014:                 if ($configline =~ /^\^([\w.\-]+)/) {
15015:                     $LC_dns_serv{$1} = 1;
15016:                 }
15017:                 next;
15018:             }
15019: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
15020: 	    $name=~s/\s//g;
15021: 	    if ($id && $domain && $role && $name) {
15022:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15023:                     my $curr = $hostname{$id};
15024:                     my $skip;
15025:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
15026:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15027:                             $skip = 1;
15028:                         } else {
15029:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15030:                         }
15031:                     }
15032:                     unless ($skip) {
15033:                         push(@{$name_to_host{$name}},$id);
15034:                     }
15035:                 } else {
15036:                     push(@{$name_to_host{$name}},$id);
15037:                 }
15038: 		$hostname{$id}=$name;
15039: 		$hostdom{$id}=$domain;
15040: 		if ($role eq 'library') { $libserv{$id}=$name; }
15041:                 if (defined($protocol)) {
15042:                     if ($protocol eq 'https') {
15043:                         $protocol{$id} = $protocol;
15044:                     } else {
15045:                         $protocol{$id} = 'http'; 
15046:                     }
15047:                 } else {
15048:                     $protocol{$id} = 'http';
15049:                 }
15050:                 if (defined($intdom)) {
15051:                     $internetdom{$id} = $intdom;
15052:                 }
15053: 	    }
15054: 	}
15055:     }
15056:     
15057:     sub reset_hosts_info {
15058: 	&purge_remembered();
15059: 	&reset_domain_info();
15060: 	&reset_hosts_ip_info();
15061:         undef(%internetdom);
15062: 	undef(%name_to_host);
15063: 	undef(%hostname);
15064: 	undef(%hostdom);
15065: 	undef(%libserv);
15066: 	undef($loaded);
15067:     }
15068: 
15069:     sub load_hosts_tab {
15070: 	my ($ignore_cache,$nocache) = @_;
15071: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
15072: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
15073: 	my @config = <$config>;
15074: 	&parse_hosts_tab(\@config);
15075: 	close($config);
15076: 	$loaded=1;
15077:     }
15078: 
15079:     sub hostname {
15080: 	&load_hosts_tab() if (!$loaded);
15081: 
15082: 	my ($lonid) = @_;
15083: 	return $hostname{$lonid};
15084:     }
15085: 
15086:     sub all_hostnames {
15087: 	&load_hosts_tab() if (!$loaded);
15088: 
15089: 	return %hostname;
15090:     }
15091: 
15092:     sub all_names {
15093:         my ($ignore_cache,$nocache) = @_;
15094: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
15095: 
15096: 	return %name_to_host;
15097:     }
15098: 
15099:     sub all_host_domain {
15100:         &load_hosts_tab() if (!$loaded);
15101:         return %hostdom;
15102:     }
15103: 
15104:     sub all_host_intdom {
15105:         &load_hosts_tab() if (!$loaded);
15106:         return %internetdom;
15107:     }
15108: 
15109:     sub is_library {
15110: 	&load_hosts_tab() if (!$loaded);
15111: 
15112: 	return exists($libserv{$_[0]});
15113:     }
15114: 
15115:     sub all_library {
15116: 	&load_hosts_tab() if (!$loaded);
15117: 
15118: 	return %libserv;
15119:     }
15120: 
15121:     sub unique_library {
15122: 	#2x reverse removes all hostnames that appear more than once
15123:         my %unique = reverse &all_library();
15124:         return reverse %unique;
15125:     }
15126: 
15127:     sub get_servers {
15128: 	&load_hosts_tab() if (!$loaded);
15129: 
15130: 	my ($domain,$type) = @_;
15131: 	my %possible_hosts = ($type eq 'library') ? %libserv
15132: 	                                          : %hostname;
15133: 	my %result;
15134: 	if (ref($domain) eq 'ARRAY') {
15135: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15136: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
15137: 		    $result{$host} = $hostname;
15138: 		}
15139: 	    }
15140: 	} else {
15141: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15142: 		if ($hostdom{$host} eq $domain) {
15143: 		    $result{$host} = $hostname;
15144: 		}
15145: 	    }
15146: 	}
15147: 	return %result;
15148:     }
15149: 
15150:     sub get_unique_servers {
15151:         my %unique = reverse &get_servers(@_);
15152: 	return reverse %unique;
15153:     }
15154: 
15155:     sub host_domain {
15156: 	&load_hosts_tab() if (!$loaded);
15157: 
15158: 	my ($lonid) = @_;
15159: 	return $hostdom{$lonid};
15160:     }
15161: 
15162:     sub all_domains {
15163: 	&load_hosts_tab() if (!$loaded);
15164: 
15165: 	my %seen;
15166: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
15167: 	return @uniq;
15168:     }
15169: 
15170:     sub internet_dom {
15171:         &load_hosts_tab() if (!$loaded);
15172: 
15173:         my ($lonid) = @_;
15174:         return $internetdom{$lonid};
15175:     }
15176: 
15177:     sub is_LC_dns {
15178:         &load_hosts_tab() if (!$loaded);
15179: 
15180:         my ($hostname) = @_;
15181:         return exists($LC_dns_serv{$hostname});
15182:     }
15183: 
15184: }
15185: 
15186: { 
15187:     my %iphost;
15188:     my %name_to_ip;
15189:     my %lonid_to_ip;
15190: 
15191:     sub get_hosts_from_ip {
15192: 	my ($ip) = @_;
15193: 	my %iphosts = &get_iphost();
15194: 	if (ref($iphosts{$ip})) {
15195: 	    return @{$iphosts{$ip}};
15196: 	}
15197: 	return;
15198:     }
15199:     
15200:     sub reset_hosts_ip_info {
15201: 	undef(%iphost);
15202: 	undef(%name_to_ip);
15203: 	undef(%lonid_to_ip);
15204:     }
15205: 
15206:     sub get_host_ip {
15207: 	my ($lonid) = @_;
15208: 	if (exists($lonid_to_ip{$lonid})) {
15209: 	    return $lonid_to_ip{$lonid};
15210: 	}
15211: 	my $name=&hostname($lonid);
15212:    	my $ip = gethostbyname($name);
15213: 	return if (!$ip || length($ip) ne 4);
15214: 	$ip=inet_ntoa($ip);
15215: 	$name_to_ip{$name}   = $ip;
15216: 	$lonid_to_ip{$lonid} = $ip;
15217: 	return $ip;
15218:     }
15219:     
15220:     sub get_iphost {
15221: 	my ($ignore_cache,$nocache) = @_;
15222: 
15223: 	if (!$ignore_cache) {
15224: 	    if (%iphost) {
15225: 		return %iphost;
15226: 	    }
15227: 	    my ($ip_info,$cached)=
15228: 		&Apache::lonnet::is_cached_new('iphost','iphost');
15229: 	    if ($cached) {
15230: 		%iphost      = %{$ip_info->[0]};
15231: 		%name_to_ip  = %{$ip_info->[1]};
15232: 		%lonid_to_ip = %{$ip_info->[2]};
15233: 		return %iphost;
15234: 	    }
15235: 	}
15236: 
15237: 	# get yesterday's info for fallback
15238: 	my %old_name_to_ip;
15239: 	my ($ip_info,$cached)=
15240: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
15241: 	if ($cached) {
15242: 	    %old_name_to_ip = %{$ip_info->[1]};
15243: 	}
15244: 
15245: 	my %name_to_host = &all_names($ignore_cache,$nocache);
15246: 	foreach my $name (keys(%name_to_host)) {
15247: 	    my $ip;
15248: 	    if (!exists($name_to_ip{$name})) {
15249: 		$ip = gethostbyname($name);
15250: 		if (!$ip || length($ip) ne 4) {
15251: 		    if (defined($old_name_to_ip{$name})) {
15252: 			$ip = $old_name_to_ip{$name};
15253: 			&logthis("Can't find $name defaulting to old $ip");
15254: 		    } else {
15255: 			&logthis("Name $name no IP found");
15256: 			next;
15257: 		    }
15258: 		} else {
15259: 		    $ip=inet_ntoa($ip);
15260: 		}
15261: 		$name_to_ip{$name} = $ip;
15262: 	    } else {
15263: 		$ip = $name_to_ip{$name};
15264: 	    }
15265: 	    foreach my $id (@{ $name_to_host{$name} }) {
15266: 		$lonid_to_ip{$id} = $ip;
15267: 	    }
15268: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
15269: 	}
15270:         unless ($nocache) {
15271: 	    &do_cache_new('iphost','iphost',
15272: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
15273: 		          48*60*60);
15274:         }
15275: 
15276: 	return %iphost;
15277:     }
15278: 
15279:     #
15280:     #  Given a DNS returns the loncapa host name for that DNS 
15281:     # 
15282:     sub host_from_dns {
15283:         my ($dns) = @_;
15284:         my @hosts;
15285:         my $ip;
15286: 
15287:         if (exists($name_to_ip{$dns})) {
15288:             $ip = $name_to_ip{$dns};
15289:         }
15290:         if (!$ip) {
15291:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15292:             if (length($ip) == 4) { 
15293: 	        $ip   = &IO::Socket::inet_ntoa($ip);
15294:             }
15295:         }
15296:         if ($ip) {
15297: 	    @hosts = get_hosts_from_ip($ip);
15298: 	    return $hosts[0];
15299:         }
15300:         return undef;
15301:     }
15302: 
15303:     sub get_internet_names {
15304:         my ($lonid) = @_;
15305:         return if ($lonid eq '');
15306:         my ($idnref,$cached)=
15307:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
15308:         if ($cached) {
15309:             return $idnref;
15310:         }
15311:         my $ip = &get_host_ip($lonid);
15312:         my @hosts = &get_hosts_from_ip($ip);
15313:         my %iphost = &get_iphost();
15314:         my (@idns,%seen);
15315:         foreach my $id (@hosts) {
15316:             my $dom = &host_domain($id);
15317:             my $prim_id = &domain($dom,'primary');
15318:             my $prim_ip = &get_host_ip($prim_id);
15319:             next if ($seen{$prim_ip});
15320:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15321:                 foreach my $id (@{$iphost{$prim_ip}}) {
15322:                     my $intdom = &internet_dom($id);
15323:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
15324:                         push(@idns,$intdom);
15325:                     }
15326:                 }
15327:             }
15328:             $seen{$prim_ip} = 1;
15329:         }
15330:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
15331:     }
15332: 
15333: }
15334: 
15335: sub all_loncaparevs {
15336:     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);
15337: }
15338: 
15339: # ---------------------------------------------------------- Read loncaparev table
15340: {
15341:     sub load_loncaparevs { 
15342:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
15343:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
15344:                 while (my $configline=<$config>) {
15345:                     chomp($configline);
15346:                     my ($hostid,$loncaparev)=split(/:/,$configline);
15347:                     $loncaparevs{$hostid}=$loncaparev;
15348:                 }
15349:                 close($config);
15350:             }
15351:         }
15352:     }
15353: }
15354: 
15355: # ---------------------------------------------------------- Read serverhostID table
15356: {
15357:     sub load_serverhomeIDs {
15358:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
15359:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
15360:                 while (my $configline=<$config>) {
15361:                     chomp($configline);
15362:                     my ($name,$id)=split(/:/,$configline);
15363:                     $serverhomeIDs{$name}=$id;
15364:                 }
15365:                 close($config);
15366:             }
15367:         }
15368:     }
15369: }
15370: 
15371: 
15372: BEGIN {
15373: 
15374: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15375:     unless ($readit) {
15376: {
15377:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15378:     %perlvar = (%perlvar,%{$configvars});
15379: }
15380: 
15381: 
15382: # ------------------------------------------------------ Read spare server file
15383: {
15384:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
15385: 
15386:     while (my $configline=<$config>) {
15387:        chomp($configline);
15388:        if ($configline) {
15389: 	   my ($host,$type) = split(':',$configline,2);
15390: 	   if (!defined($type) || $type eq '') { $type = 'default' };
15391: 	   push(@{ $spareid{$type} }, $host);
15392:        }
15393:     }
15394:     close($config);
15395: }
15396: # ------------------------------------------------------------ Read permissions
15397: {
15398:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
15399: 
15400:     while (my $configline=<$config>) {
15401: 	chomp($configline);
15402: 	if ($configline) {
15403: 	    my ($role,$perm)=split(/ /,$configline);
15404: 	    if ($perm ne '') { $pr{$role}=$perm; }
15405: 	}
15406:     }
15407:     close($config);
15408: }
15409: 
15410: # -------------------------------------------- Read plain texts for permissions
15411: {
15412:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
15413: 
15414:     while (my $configline=<$config>) {
15415: 	chomp($configline);
15416: 	if ($configline) {
15417: 	    my ($short,@plain)=split(/:/,$configline);
15418:             %{$prp{$short}} = ();
15419: 	    if (@plain > 0) {
15420:                 $prp{$short}{'std'} = $plain[0];
15421:                 for (my $i=1; $i<@plain; $i++) {
15422:                     $prp{$short}{'alt'.$i} = $plain[$i];  
15423:                 }
15424:             }
15425: 	}
15426:     }
15427:     close($config);
15428: }
15429: 
15430: # ---------------------------------------------------------- Read package table
15431: {
15432:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
15433: 
15434:     while (my $configline=<$config>) {
15435: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
15436: 	chomp($configline);
15437: 	my ($short,$plain)=split(/:/,$configline);
15438: 	my ($pack,$name)=split(/\&/,$short);
15439: 	if ($plain ne '') {
15440: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
15441: 	    $packagetab{$short}=$plain; 
15442: 	}
15443:     }
15444:     close($config);
15445: }
15446: 
15447: # ---------------------------------------------------------- Read loncaparev table
15448: 
15449: &load_loncaparevs();
15450: 
15451: # ---------------------------------------------------------- Read serverhostID table
15452: 
15453: &load_serverhomeIDs();
15454: 
15455: # ---------------------------------------------------------- Read releaseslist XML
15456: {
15457:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15458:     if (-e $file) {
15459:         my $parser = HTML::LCParser->new($file);
15460:         while (my $token = $parser->get_token()) {
15461:             if ($token->[0] eq 'S') {
15462:                 my $item = $token->[1];
15463:                 my $name = $token->[2]{'name'};
15464:                 my $value = $token->[2]{'value'};
15465:                 my $valuematch = $token->[2]{'valuematch'};
15466:                 my $namematch = $token->[2]{'namematch'};
15467:                 if ($item eq 'parameter') {
15468:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
15469:                         my $release = $parser->get_text();
15470:                         $release =~ s/(^\s*|\s*$ )//gx;
15471:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
15472:                     }
15473:                 } elsif ($item ne '' && $name ne '') {
15474:                     my $release = $parser->get_text();
15475:                     $release =~ s/(^\s*|\s*$ )//gx;
15476:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
15477:                 }
15478:             }
15479:         }
15480:     }
15481: }
15482: 
15483: # ---------------------------------------------------------- Read managers table
15484: {
15485:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
15486:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
15487:             while (my $configline=<$config>) {
15488:                 chomp($configline);
15489:                 next if ($configline =~ /^\#/);
15490:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15491:                     $managerstab{$configline} = 1;
15492:                 }
15493:             }
15494:             close($config);
15495:         }
15496:     }
15497: }
15498: 
15499: # ------------- set up temporary directory
15500: {
15501:     $tmpdir = LONCAPA::tempdir();
15502: 
15503: }
15504: 
15505: # ------------- set default texengine (domain default overrides this)
15506: {
15507:     $deftex = LONCAPA::texengine();
15508: }
15509: 
15510: # ------------- set default minimum length for passwords for internal auth users
15511: {
15512:     $passwdmin = LONCAPA::passwd_min();
15513: }
15514: 
15515: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
15516: 				'compress_threshold'=> 20_000,
15517:  			        });
15518: 
15519: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
15520: $dumpcount=0;
15521: $locknum=0;
15522: 
15523: &logtouch();
15524: &logthis('<font color="yellow">INFO: Read configuration</font>');
15525: $readit=1;
15526:     {
15527: 	use integer;
15528: 	my $test=(2**32)+1;
15529: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
15530: 	&logthis(" Detected 64bit platform ($_64bit)");
15531:     }
15532: }
15533: }
15534: 
15535: 1;
15536: __END__
15537: 
15538: =pod
15539: 
15540: =head1 NAME
15541: 
15542: Apache::lonnet - Subroutines to ask questions about things in the network.
15543: 
15544: =head1 SYNOPSIS
15545: 
15546: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
15547: 
15548:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15549: 
15550: Common parameters:
15551: 
15552: =over 4
15553: 
15554: =item *
15555: 
15556: $uname : an internal username (if $cname expecting a course Id specifically)
15557: 
15558: =item *
15559: 
15560: $udom : a domain (if $cdom expecting a course's domain specifically)
15561: 
15562: =item *
15563: 
15564: $symb : a resource instance identifier
15565: 
15566: =item *
15567: 
15568: $namespace : the name of a .db file that contains the data needed or
15569: being set.
15570: 
15571: =back
15572: 
15573: =head1 OVERVIEW
15574: 
15575: lonnet provides subroutines which interact with the
15576: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15577: about classes, users, and resources.
15578: 
15579: For many of these objects you can also use this to store data about
15580: them or modify them in various ways.
15581: 
15582: =head2 Symbs
15583: 
15584: To identify a specific instance of a resource, LON-CAPA uses symbols
15585: or "symbs"X<symb>. These identifiers are built from the URL of the
15586: map, the resource number of the resource in the map, and the URL of
15587: the resource itself. The latter is somewhat redundant, but might help
15588: if maps change.
15589: 
15590: An example is
15591: 
15592:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15593: 
15594: The respective map entry is
15595: 
15596:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
15597:   title="Problem 2">
15598:  </resource>
15599: 
15600: Symbs are used by the random number generator, as well as to store and
15601: restore data specific to a certain instance of for example a problem.
15602: 
15603: =head2 Storing And Retrieving Data
15604: 
15605: X<store()>X<cstore()>X<restore()>Three of the most important functions
15606: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15607: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15608: is is the non-critical message twin of cstore. These functions are for
15609: handlers to store a perl hash to a user's permanent data space in an
15610: easy manner, and to retrieve it again on another call. It is expected
15611: that a handler would use this once at the beginning to retrieve data,
15612: and then again once at the end to send only the new data back.
15613: 
15614: The data is stored in the user's data directory on the user's
15615: homeserver under the ID of the course.
15616: 
15617: The hash that is returned by restore will have all of the previous
15618: value for all of the elements of the hash.
15619: 
15620: Example:
15621: 
15622:  #creating a hash
15623:  my %hash;
15624:  $hash{'foo'}='bar';
15625: 
15626:  #storing it
15627:  &Apache::lonnet::cstore(\%hash);
15628: 
15629:  #changing a value
15630:  $hash{'foo'}='notbar';
15631: 
15632:  #adding a new value
15633:  $hash{'bar'}='foo';
15634:  &Apache::lonnet::cstore(\%hash);
15635: 
15636:  #retrieving the hash
15637:  my %history=&Apache::lonnet::restore();
15638: 
15639:  #print the hash
15640:  foreach my $key (sort(keys(%history))) {
15641:    print("\%history{$key} = $history{$key}");
15642:  }
15643: 
15644: Will print out:
15645: 
15646:  %history{1:foo} = bar
15647:  %history{1:keys} = foo:timestamp
15648:  %history{1:timestamp} = 990455579
15649:  %history{2:bar} = foo
15650:  %history{2:foo} = notbar
15651:  %history{2:keys} = foo:bar:timestamp
15652:  %history{2:timestamp} = 990455580
15653:  %history{bar} = foo
15654:  %history{foo} = notbar
15655:  %history{timestamp} = 990455580
15656:  %history{version} = 2
15657: 
15658: Note that the special hash entries C<keys>, C<version> and
15659: C<timestamp> were added to the hash. C<version> will be equal to the
15660: total number of versions of the data that have been stored. The
15661: C<timestamp> attribute will be the UNIX time the hash was
15662: stored. C<keys> is available in every historical section to list which
15663: keys were added or changed at a specific historical revision of a
15664: hash.
15665: 
15666: B<Warning>: do not store the hash that restore returns directly. This
15667: will cause a mess since it will restore the historical keys as if the
15668: were new keys. I.E. 1:foo will become 1:1:foo etc.
15669: 
15670: Calling convention:
15671: 
15672:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15673:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15674: 
15675: For more detailed information, see lonnet specific documentation.
15676: 
15677: =head1 RETURN MESSAGES
15678: 
15679: =over 4
15680: 
15681: =item * B<con_lost>: unable to contact remote host
15682: 
15683: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15684: when the connection is brought back up
15685: 
15686: =item * B<con_failed>: unable to contact remote host and unable to save message
15687: for later delivery
15688: 
15689: =item * B<error:>: an error a occurred, a description of the error follows the :
15690: 
15691: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15692: that was requested
15693: 
15694: =back
15695: 
15696: =head1 PUBLIC SUBROUTINES
15697: 
15698: =head2 Session Environment Functions
15699: 
15700: =over 4
15701: 
15702: =item * 
15703: X<appenv()>
15704: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15705: the user envirnoment file, and will be restored for each access this
15706: user makes during this session, also modifies the %env for the current
15707: process. Optional rolesarrayref - if defined contains a reference to an array
15708: of roles which are exempt from the restriction on modifying user.role entries 
15709: in the user's environment.db and in %env.    
15710: 
15711: =item *
15712: X<delenv()>
15713: B<delenv($delthis,$regexp)>: removes all items from the session
15714: environment file that begin with $delthis. If the 
15715: optional second arg - $regexp - is true, $delthis is treated as a 
15716: regular expression, otherwise \Q$delthis\E is used. 
15717: The values are also deleted from the current processes %env.
15718: 
15719: =item * get_env_multiple($name) 
15720: 
15721: gets $name from the %env hash, it seemlessly handles the cases where multiple
15722: values may be defined and end up as an array ref.
15723: 
15724: returns an array of values
15725: 
15726: =back
15727: 
15728: =head2 User Information
15729: 
15730: =over 4
15731: 
15732: =item *
15733: X<queryauthenticate()>
15734: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15735: authentication scheme
15736: 
15737: =item *
15738: X<authenticate()>
15739: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15740: authenticate user from domain's lib servers (first use the current
15741: one). C<$upass> should be the users password.
15742: $checkdefauth is optional (value is 1 if a check should be made to
15743:    authenticate user using default authentication method, and allow
15744:    account creation if username does not have account in the domain).
15745: $clientcancheckhost is optional (value is 1 if checking whether the
15746:    server can host will occur on the client side in lonauth.pm).   
15747: 
15748: =item *
15749: X<homeserver()>
15750: B<homeserver($uname,$udom)>: find the server which has
15751: the user's directory and files (there must be only one), this caches
15752: the answer, and also caches if there is a borken connection.
15753: 
15754: =item *
15755: X<idget()>
15756: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15757: a list of student/employee IDs or clicker IDs
15758: (student/employee IDs are a unique resource in a domain, there must be 
15759: only 1 ID per username, and only 1 username per ID in a specific domain).
15760: clickerIDs are not necessarily unique, as students might share clickers.
15761: (returns hash: id=>name,id=>name)
15762: 
15763: =item *
15764: X<idrget()>
15765: B<idrget($udom,@unames)>: find the IDs behind a list of
15766: usernames (returns hash: name=>id,name=>id)
15767: 
15768: =item *
15769: X<idput()>
15770: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15771: names and associated student/employee IDs or clicker IDs.
15772: 
15773: =item *
15774: X<iddel()>
15775: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15776: student/employee ID or clicker ID username look-ups from domain.
15777: The homeserver ($uhome) and namespace ($namespace) are optional.
15778: If no $uhome is provided, it will be determined usig &homeserver()
15779: for each user.  If no $namespace is provided, the default is ids.
15780: 
15781: =item *
15782: X<updateclickers()>
15783: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15784: clicker ID-to-username look-ups in clickers.db on library server.
15785: Permitted actions are add or del (i.e., add or delete). The 
15786: clickers.db contains clickerID as keys (escaped), and each corresponding
15787: value is an escaped comma-separated list of usernames (for whom the
15788: library server is the homeserver), who registered that particular ID.
15789: If $critical is true, the update will be sent via &critical, otherwise
15790: &reply() will be used.
15791: 
15792: =item *
15793: X<rolesinit()>
15794: B<rolesinit($udom,$username)>: get user privileges.
15795: returns user role, first access and timer interval hashes
15796: 
15797: =item *
15798: X<privileged()>
15799: B<privileged($username,$domain)>: returns a true if user has a
15800: privileged and active role (i.e. su or dc), false otherwise.
15801: 
15802: =item *
15803: X<getsection()>
15804: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15805: course $cname, return section name/number or '' for "not in course"
15806: and '-1' for "no section"
15807: 
15808: =item *
15809: X<userenvironment()>
15810: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15811: passed in @what from the requested user's environment, returns a hash
15812: 
15813: =item * 
15814: X<userlog_query()>
15815: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15816: activity.log file. %filters defines filters applied when parsing the
15817: log file. These can be start or end timestamps, or the type of action
15818: - log to look for Login or Logout events, check for Checkin or
15819: Checkout, role for role selection. The response is in the form
15820: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15821: escaped strings of the action recorded in the activity.log file.
15822: 
15823: =back
15824: 
15825: =head2 User Roles
15826: 
15827: =over 4
15828: 
15829: =item *
15830: 
15831: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15832: returns codes for allowed actions.
15833: 
15834: The first argument is required, all others are optional.
15835: 
15836: $priv is the privilege being checked.
15837: $uri contains additional information about what is being checked for access (e.g.,
15838: URL, course ID etc.). 
15839: $symb is the unique resource instance identifier in a course; if needed,
15840: but not provided, it will be retrieved via a call to &symbread(). 
15841: $role is the role for which a priv is being checked (only used if priv is evb). 
15842: $clientip is the user's IP address (only used when checking for access to portfolio 
15843: files).
15844: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15845: prevents recursive calls to &allowed.
15846: 
15847:  F: full access
15848:  U,I,K: authentication modes (cxx only)
15849:  '': forbidden
15850:  1: user needs to choose course
15851:  2: browse allowed
15852:  A: passphrase authentication needed
15853:  B: access temporarily blocked because of a blocking event in a course.
15854:  D: access blocked because access is required via session initiated via deep-link 
15855: 
15856: =item *
15857: 
15858: constructaccess($url,$setpriv) : check for access to construction space URL
15859: 
15860: See if the owner domain and name in the URL match those in the
15861: expected environment.  If so, return three element list
15862: ($ownername,$ownerdomain,$ownerhome).
15863: 
15864: Otherwise return the null string.
15865: 
15866: If second argument 'setpriv' is true, it assigns the privileges,
15867: and returns the same three element list, unless the owner has
15868: blocked "ad hoc" Domain Coordinator access to the Author Space,
15869: in which case the null string is returned.
15870: 
15871: =item *
15872: 
15873: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
15874: define a custom role rolename set privileges in format of lonTabs/roles.tab
15875: for system, domain, and course level. $uname and $udom are optional (current
15876: user's username and domain will be used when either of $uname or $udom are absent.
15877: 
15878: =item *
15879: 
15880: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
15881: (rolesplain.tab); plain text explanation of a user role term.
15882: $type is Course (default) or Community.
15883: If $forcedefault evaluates to true, text returned will be default 
15884: text for $type. Otherwise, if this is a course, the text returned 
15885: will be a custom name for the role (if defined in the course's 
15886: environment).  If no custom name is defined the default is returned.
15887:    
15888: =item *
15889: 
15890: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
15891: All arguments are optional. Returns a hash of a roles, either for
15892: co-author/assistant author roles for a user's Construction Space
15893: (default), or if $context is 'userroles', roles for the user himself,
15894: In the hash, keys are set to colon-separated $uname,$udom,$role, and
15895: (optionally) if $withsec is true, a fourth colon-separated item - $section.
15896: For each key, value is set to colon-separated start and end times for
15897: the role.  If no username and domain are specified, will default to
15898: current user/domain. Types, roles, and roledoms are references to arrays
15899: of role statuses (active, future or previous), roles 
15900: (e.g., cc,in, st etc.) and domains of the roles which can be used
15901: to restrict the list of roles reported. If no array ref is 
15902: provided for types, will default to return only active roles.
15903: 
15904: =item *
15905: 
15906: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
15907: user: $uname:$udom has a role in the course: $cdom_$cnum. 
15908: 
15909: Additional optional arguments are: $type (if role checking is to be restricted 
15910: to certain user status types -- previous (expired roles), active (currently
15911: available roles) or future (roles available in the future), and
15912: $hideprivileged -- if true will not report course roles for users who
15913: have active Domain Coordinator role in course's domain or in additional
15914: domains (specified in 'Domains to check for privileged users' in course
15915: environment -- set via:  Course Settings -> Classlists and staff listing).
15916: 
15917: =item *
15918: 
15919: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
15920: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
15921: $possdomains and $possroles are optional array refs -- to domains to check and
15922: roles to check.  If $possdomains is not specified, a dump will be done of the
15923: users' roles.db to check for a dc or su role in any domain. This can be
15924: time consuming if &privileged is called repeatedly (e.g., when displaying a
15925: classlist), so in such cases, supplying a $possdomains array is preferred, as
15926: this then allows &privileged_by_domain() to be used, which caches the identity
15927: of privileged users, eliminating the need for repeated calls to &dump().
15928: 
15929: =item *
15930: 
15931: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
15932: where the outer hash keys are domains specified in the $possdomains array ref,
15933: next inner hash keys are privileged roles specified in the $roles array ref,
15934: and the innermost hash contains key = value pairs for username:domain = end:start
15935: for active or future "privileged" users with that role in that domain. To avoid
15936: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
15937: innerhash are cached using priv_$role and $dom as the identifiers.
15938: 
15939: =back
15940: 
15941: =head2 User Modification
15942: 
15943: =over 4
15944: 
15945: =item *
15946: 
15947: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
15948: user for the level given by URL.  Optional start and end dates (leave empty
15949: string or zero for "no date")
15950: 
15951: =item *
15952: 
15953: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
15954: change a users, password, possible return values are: ok,
15955: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
15956: refused
15957: 
15958: =item *
15959: 
15960: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
15961: 
15962: =item *
15963: 
15964: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
15965:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
15966: 
15967: will update user information (firstname,middlename,lastname,generation,
15968: permanentemail), and if forceid is true, student/employee ID also.
15969: A user's institutional affiliation(s) can also be updated.
15970: User information fields will not be overwritten with empty entries 
15971: unless the field is included in the $candelete array reference.
15972: This array is included when a single user is modified via "Manage Users",
15973: or when Autoupdate.pl is run by cron in a domain.
15974: 
15975: =item *
15976: 
15977: modifystudent
15978: 
15979: modify a student's enrollment and identification information.
15980: The course id is resolved based on the current user's environment.  
15981: This means the invoking user must be a course coordinator or otherwise
15982: associated with a course.
15983: 
15984: This call is essentially a wrapper for lonnet::modifyuser and
15985: lonnet::modify_student_enrollment
15986: 
15987: Inputs: 
15988: 
15989: =over 4
15990: 
15991: =item B<$udom> Student's loncapa domain
15992: 
15993: =item B<$uname> Student's loncapa login name
15994: 
15995: =item B<$uid> Student/Employee ID
15996: 
15997: =item B<$umode> Student's authentication mode
15998: 
15999: =item B<$upass> Student's password
16000: 
16001: =item B<$first> Student's first name
16002: 
16003: =item B<$middle> Student's middle name
16004: 
16005: =item B<$last> Student's last name
16006: 
16007: =item B<$gene> Student's generation
16008: 
16009: =item B<$usec> Student's section in course
16010: 
16011: =item B<$end> Unix time of the roles expiration
16012: 
16013: =item B<$start> Unix time of the roles start date
16014: 
16015: =item B<$forceid> If defined, allow $uid to be changed
16016: 
16017: =item B<$desiredhome> server to use as home server for student
16018: 
16019: =item B<$email> Student's permanent e-mail address
16020: 
16021: =item B<$type> Type of enrollment (auto or manual)
16022: 
16023: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
16024: 
16025: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
16026: 
16027: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
16028: 
16029: =item B<$context> role change context (shown in User Management Logs display in a course)
16030: 
16031: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16032: 
16033: =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.
16034: 
16035: =back
16036: 
16037: =item *
16038: 
16039: modify_student_enrollment
16040: 
16041: Change a student's enrollment status in a class.  The environment variable
16042: 'role.request.course' must be defined for this function to proceed.
16043: 
16044: Inputs:
16045: 
16046: =over 4
16047: 
16048: =item $udom, student's domain
16049: 
16050: =item $uname, student's name
16051: 
16052: =item $uid, student's user id
16053: 
16054: =item $first, student's first name
16055: 
16056: =item $middle
16057: 
16058: =item $last
16059: 
16060: =item $gene
16061: 
16062: =item $usec
16063: 
16064: =item $end
16065: 
16066: =item $start
16067: 
16068: =item $type
16069: 
16070: =item $locktype
16071: 
16072: =item $cid
16073: 
16074: =item $selfenroll
16075: 
16076: =item $context
16077: 
16078: =item $credits, number of credits student will earn from this class
16079: 
16080: =item $instsec, institutional course section code for student
16081: 
16082: =back
16083: 
16084: 
16085: =item *
16086: 
16087: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16088: custom role; give a custom role to a user for the level given by URL.  Specify
16089: name and domain of role author, and role name
16090: 
16091: =item *
16092: 
16093: revokerole($udom,$uname,$url,$role) : revoke a role for url
16094: 
16095: =item *
16096: 
16097: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16098: 
16099: =back
16100: 
16101: =head2 Course Infomation
16102: 
16103: =over 4
16104: 
16105: =item *
16106: 
16107: coursedescription($courseid,$options) : returns a hash of information about the
16108: specified course id, including all environment settings for the
16109: course, the description of the course will be in the hash under the
16110: key 'description'
16111: 
16112: $options is an optional parameter that if supplied is a hash reference that controls
16113: what how this function works.  It has the following key/values:
16114: 
16115: =over 4
16116: 
16117: =item freshen_cache
16118: 
16119: If defined, and the environment cache for the course is valid, it is 
16120: returned in the returned hash.
16121: 
16122: =item one_time
16123: 
16124: If defined, the last cache time is set to _now_
16125: 
16126: =item user
16127: 
16128: If defined, the supplied username is used instead of the current user.
16129: 
16130: 
16131: =back
16132: 
16133: =item *
16134: 
16135: resdata($name,$domain,$type,@which) : request for current parameter
16136: setting for a specific $type, where $type is either 'course' or 'user',
16137: @what should be a list of parameters to ask about. This routine caches
16138: answers for 10 minutes.
16139: 
16140: =item *
16141: 
16142: get_courseresdata($courseid, $domain) : dump the entire course resource
16143: data base, returning a hash that is keyed by the resource name and has
16144: values that are the resource value.  I believe that the timestamps and
16145: versions are also returned.
16146: 
16147: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
16148: supplemental content area. This routine caches the number of files for 
16149: 10 minutes.
16150: 
16151: =back
16152: 
16153: =head2 Course Modification
16154: 
16155: =over 4
16156: 
16157: =item *
16158: 
16159: writecoursepref($courseid,%prefs) : write preferences (environment
16160: database) for a course
16161: 
16162: =item *
16163: 
16164: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16165: 
16166: =item *
16167: 
16168: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
16169: 
16170: =item *
16171: 
16172: is_course($courseid), is_course($cdom, $cnum)
16173: 
16174: Accepts either a combined $courseid (in the form of domain_courseid) or the
16175: two component version $cdom, $cnum. It checks if the specified course exists.
16176: 
16177: Returns:
16178:     undef if the course doesn't exist, otherwise
16179:     in scalar context the combined courseid.
16180:     in list context the two components of the course identifier, domain and 
16181:     courseid.    
16182: 
16183: =back
16184: 
16185: =head2 Bubblesheet Configuration
16186: 
16187: =over 4
16188: 
16189: =item *
16190: 
16191: get_scantron_config($which)
16192: 
16193: $which - the name of the configuration to parse from the file.
16194: 
16195: Parses and returns the bubblesheet configuration line selected as a
16196: hash of configuration file fields.
16197: 
16198: 
16199: Returns:
16200:     If the named configuration is not in the file, an empty
16201:     hash is returned.
16202: 
16203:     a hash with the fields
16204:       name         - internal name for the this configuration setup
16205:       description  - text to display to operator that describes this config
16206:       CODElocation - if 0 or the string 'none'
16207:                           - no CODE exists for this config
16208:                      if -1 || the string 'letter'
16209:                           - a CODE exists for this config and is
16210:                             a string of letters
16211:                      Unsupported value (but planned for future support)
16212:                           if a positive integer
16213:                                - The CODE exists as the first n items from
16214:                                  the question section of the form
16215:                           if the string 'number'
16216:                                - The CODE exists for this config and is
16217:                                  a string of numbers
16218:       CODEstart   - (only matter if a CODE exists) column in the line where
16219:                      the CODE starts
16220:       CODElength  - length of the CODE
16221:       IDstart     - column where the student/employee ID starts
16222:       IDlength    - length of the student/employee ID info
16223:       Qstart      - column where the information from the bubbled
16224:                     'questions' start
16225:       Qlength     - number of columns comprising a single bubble line from
16226:                     the sheet. (usually either 1 or 10)
16227:       Qon         - either a single character representing the character used
16228:                     to signal a bubble was chosen in the positional setup, or
16229:                     the string 'letter' if the letter of the chosen bubble is
16230:                     in the final, or 'number' if a number representing the
16231:                     chosen bubble is in the file (1->A 0->J)
16232:       Qoff        - the character used to represent that a bubble was
16233:                     left blank
16234:       PaperID     - if the scanning process generates a unique number for each
16235:                     sheet scanned the column that this ID number starts in
16236:       PaperIDlength - number of columns that comprise the unique ID number
16237:                       for the sheet of paper
16238:       FirstName   - column that the first name starts in
16239:       FirstNameLength - number of columns that the first name spans
16240:       LastName    - column that the last name starts in
16241:       LastNameLength - number of columns that the last name spans
16242:       BubblesPerRow - number of bubbles available in each row used to
16243:                       bubble an answer. (If not specified, 10 assumed).
16244: 
16245: 
16246: =item *
16247: 
16248: get_scantronformat_file($cdom)
16249: 
16250: $cdom - the course's domain (optional); if not supplied, uses
16251: domain for current $env{'request.course.id'}.
16252: 
16253: Returns an array containing lines from the scantron format file for
16254: the domain of the course.
16255: 
16256: If a url for a custom.tab file is listed in domain's configuration.db,
16257: lines are from this file.
16258: 
16259: Otherwise, if a default.tab has been published in RES space by the
16260: domainconfig user, lines are from this file.
16261: 
16262: Otherwise, fall back to getting lines from the legacy file on the
16263: local server:  /home/httpd/lonTabs/default_scantronformat.tab
16264: 
16265: =back
16266: 
16267: =head2 Resource Subroutines
16268: 
16269: =over 4
16270: 
16271: =item *
16272: 
16273: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
16274: 
16275: =item *
16276: 
16277: repcopy($filename) : subscribes to the requested file, and attempts to
16278: replicate from the owning library server, Might return
16279: 'unavailable', 'not_found', 'forbidden', 'ok', or
16280: 'bad_request', also attempts to grab the metadata for the
16281: resource. Expects the local filesystem pathname
16282: (/home/httpd/html/res/....)
16283: 
16284: =back
16285: 
16286: =head2 Resource Information
16287: 
16288: =over 4
16289: 
16290: =item *
16291: 
16292: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
16293: and returns the value of a variety of different possible values,
16294: $varname should be a request string, and the other parameters can be
16295: used to specify who and what one is asking about. Ordinarily, $cid 
16296: does not need to be specified, as it is retrived from 
16297: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16298: within lonuserstate::loadmap() when initializing a course, before
16299: $env{'request.course.id'} has been set, so it needs to be provided
16300: in that one case.
16301: 
16302: Possible values for $varname are environment.lastname (or other item
16303: from the envirnment hash), user.name (or someother aspect about the
16304: user), resource.0.maxtries (or some other part and parameter of a
16305: resource)
16306: 
16307: =item *
16308: 
16309: directcondval($number) : get current value of a condition; reads from a state
16310: string
16311: 
16312: =item *
16313: 
16314: condval($condidx) : value of condition index based on state
16315: 
16316: =item *
16317: 
16318: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
16319: resource's metadata, $what should be either a specific key, or either
16320: 'keys' (to get a list of possible keys) or 'packages' to get a list of
16321: packages that this resource currently uses, the last 3 arguments are 
16322: only used internally for recursive metadata.
16323: 
16324: the toolsymb is only used where the uri is for an external tool (for which
16325: the uri as well as the symb are guaranteed to be unique).
16326: 
16327: this function automatically caches all requests except any made recursively
16328: to retrieve a list of metadata keys for an imported library file ($liburi is 
16329: defined).
16330: 
16331: =item *
16332: 
16333: metadata_query($query,$custom,$customshow) : make a metadata query against the
16334: network of library servers; returns file handle of where SQL and regex results
16335: will be stored for query
16336: 
16337: =item *
16338: 
16339: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
16340: return symbolic list entry (all arguments optional). 
16341: 
16342: Args: filename is the filename (including path) for the file for which a symb 
16343: is required; donotrecurse, if true will prevent calls to allowed() being made 
16344: to check access status if more than one resource was found in the bighash 
16345: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
16346: a randompick); ignorecachednull, if true will prevent a symb of '' being 
16347: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16348: cause possible symbs to be checked to determine if they are subject to content
16349: blocking, if so they will not be included as possible symbs; possibles is a
16350: ref to a hash, which, as a side effect, will be populated with all possible 
16351: symbs (content blocking not tested).
16352:  
16353: returns the data handle
16354: 
16355: =item *
16356: 
16357: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
16358: and is a possible symb for the URL in $thisfn, and if is an encrypted
16359: resource that the user accessed using /enc/ returns a 1 on success, 0
16360: on failure, user must be in a course, as it assumes the existence of
16361: the course initial hash, and uses $env('request.course.id'}.  The third
16362: arg is an optional reference to a scalar.  If this arg is passed in the 
16363: call to symbverify, it will be set to 1 if the symb has been set to be 
16364: encrypted; otherwise it will be null.  
16365: 
16366: =item *
16367: 
16368: symbclean($symb) : removes versions numbers from a symb, returns the
16369: cleaned symb
16370: 
16371: =item *
16372: 
16373: is_on_map($uri) : checks if the $uri is somewhere on the current
16374: course map, user must be in a course for it to work.
16375: 
16376: =item *
16377: 
16378: numval($salt) : return random seed value (addend for rndseed)
16379: 
16380: =item *
16381: 
16382: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16383: a random seed, all arguments are optional, if they aren't sent it uses the
16384: environment to derive them. Note: if symb isn't sent and it can't get one
16385: from &symbread it will use the current time as its return value
16386: 
16387: =item *
16388: 
16389: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16390: unfakeable, receipt
16391: 
16392: =item *
16393: 
16394: receipt() : API to ireceipt working off of env values; given out to users
16395: 
16396: =item *
16397: 
16398: countacc($url) : count the number of accesses to a given URL
16399: 
16400: =item *
16401: 
16402: 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
16403: 
16404: =item *
16405: 
16406: 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)
16407: 
16408: =item *
16409: 
16410: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
16411: 
16412: =item *
16413: 
16414: devalidate($symb) : devalidate temporary spreadsheet calculations,
16415: forcing spreadsheet to reevaluate the resource scores next time.
16416: 
16417: =item * 
16418: 
16419: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16420: when viewing in course context.
16421: 
16422:  input: six args -- filename (decluttered), course number, course domain,
16423:                     url, symb (if registered) and group (if this is a 
16424:                     group item -- e.g., bulletin board, group page etc.).
16425: 
16426:  output: array of five scalars --
16427:          $cfile -- url for file editing if editable on current server
16428:          $home -- homeserver of resource (i.e., for author if published,
16429:                                           or course if uploaded.).
16430:          $switchserver --  1 if server switch will be needed.
16431:          $forceedit -- 1 if icon/link should be to go to edit mode 
16432:          $forceview -- 1 if icon/link should be to go to view mode
16433: 
16434: =item *
16435: 
16436: is_course_upload($file,$cnum,$cdom)
16437: 
16438: Used in course context to determine if current file was uploaded to 
16439: the course (i.e., would be found in /userfiles/docs on the course's 
16440: homeserver.
16441: 
16442:   input: 3 args -- filename (decluttered), course number and course domain.
16443:   output: boolean -- 1 if file was uploaded.
16444: 
16445: =back
16446: 
16447: =head2 Storing/Retreiving Data
16448: 
16449: =over 4
16450: 
16451: =item *
16452: 
16453: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16454: permanently for this url; hashref needs to be given and should be a \%hashname;
16455: the remaining args aren't required and if they aren't passed or are '' they will
16456: be derived from the env (with the exception of $laststore, which is an 
16457: optional arg used when a user's submission is stored in grading).
16458: $laststore is $version=$timestamp, where $version is the most recent version
16459: number retrieved for the corresponding $symb in the $namespace db file, and
16460: $timestamp is the timestamp for that transaction (UNIX time).
16461: $laststore is currently only passed when cstore() is called by 
16462: structuretags::finalize_storage().
16463: 
16464: =item *
16465: 
16466: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16467: but uses critical subroutine
16468: 
16469: =item *
16470: 
16471: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16472: all args are optional
16473: 
16474: =item *
16475: 
16476: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
16477: dumps the complete (or key matching regexp) namespace into a hash
16478: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16479: normally &store()ed into
16480: 
16481: $range should be either an integer '100' (give me the first 100
16482:                                            matching records)
16483:               or be  two integers sperated by a - with no spaces
16484:                  '30-50' (give me the 30th through the 50th matching
16485:                           records)
16486: 
16487: 
16488: =item *
16489: 
16490: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
16491: replaces a &store() version of data with a replacement set of data
16492: for a particular resource in a namespace passed in the $storehash hash 
16493: reference. If $tolog is true, the transaction is logged in the courselog
16494: with an action=PUTSTORE.
16495: 
16496: =item *
16497: 
16498: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16499: works very similar to store/cstore, but all data is stored in a
16500: temporary location and can be reset using tmpreset, $storehash should
16501: be a hash reference, returns nothing on success
16502: 
16503: =item *
16504: 
16505: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16506: similar to restore, but all data is stored in a temporary location and
16507: can be reset using tmpreset. Returns a hash of values on success,
16508: error string otherwise.
16509: 
16510: =item *
16511: 
16512: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16513: deltes all keys for $symb form the temporary storage hash.
16514: 
16515: =item *
16516: 
16517: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16518: reference filled in from namesp ($udom and $uname are optional)
16519: 
16520: =item *
16521: 
16522: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16523: namesp ($udom and $uname are optional)
16524: 
16525: =item *
16526: 
16527: dump($namespace,$udom,$uname,$regexp,$range) : 
16528: dumps the complete (or key matching regexp) namespace into a hash
16529: ($udom, $uname, $regexp, $range are optional)
16530: 
16531: $range should be either an integer '100' (give me the first 100
16532:                                            matching records)
16533:               or be  two integers sperated by a - with no spaces
16534:                  '30-50' (give me the 30th through the 50th matching
16535:                           records)
16536: =item *
16537: 
16538: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16539: $store can be a scalar, an array reference, or if the amount to be 
16540: incremented is > 1, a hash reference.
16541: 
16542: ($udom and $uname are optional)
16543: 
16544: =item *
16545: 
16546: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16547: ($udom and $uname are optional)
16548: 
16549: =item *
16550: 
16551: cput($namespace,$storehash,$udom,$uname) : critical put
16552: ($udom and $uname are optional)
16553: 
16554: =item *
16555: 
16556: newput($namespace,$storehash,$udom,$uname) :
16557: 
16558: Attempts to store the items in the $storehash, but only if they don't
16559: currently exist, if this succeeds you can be certain that you have 
16560: successfully created a new key value pair in the $namespace db.
16561: 
16562: 
16563: Args:
16564:  $namespace: name of database to store values to
16565:  $storehash: hashref to store to the db
16566:  $udom: (optional) domain of user containing the db
16567:  $uname: (optional) name of user caontaining the db
16568: 
16569: Returns:
16570:  'ok' -> succeeded in storing all keys of $storehash
16571:  'key_exists: <key>' -> failed to anything out of $storehash, as at
16572:                         least <key> already existed in the db (other
16573:                         requested keys may also already exist)
16574:  'error: <msg>' -> unable to tie the DB or other error occurred
16575:  'con_lost' -> unable to contact request server
16576:  'refused' -> action was not allowed by remote machine
16577: 
16578: 
16579: =item *
16580: 
16581: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16582: reference filled in from namesp (encrypts the return communication)
16583: ($udom and $uname are optional)
16584: 
16585: =item *
16586: 
16587: log($udom,$name,$home,$message) : write to permanent log for user; use
16588: critical subroutine
16589: 
16590: =item *
16591: 
16592: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16593: array reference filled in from namespace found in domain level on either
16594: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
16595: 
16596: =item *
16597: 
16598: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
16599: domain level either on specified domain server ($uhome) or primary domain 
16600: server ($udom and $uhome are optional)
16601: 
16602: =item * 
16603: 
16604: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
16605: for: authentication, language, quotas, timezone, date locale, and portal URL in
16606: the target domain.
16607: 
16608: May also include additional key => value pairs for the following groups:
16609: 
16610: =over
16611: 
16612: =item
16613: disk quotas (MB allocated by default to portfolios and authoring spaces).
16614: 
16615: =over
16616: 
16617: =item defaultquota, authorquota
16618: 
16619: =back
16620: 
16621: =item
16622: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16623: portfolio for users).
16624: 
16625: =over
16626: 
16627: =item
16628: aboutme, blog, webdav, portfolio
16629: 
16630: =back
16631: 
16632: =item
16633: requestcourses: ability to request courses, and how requests are processed.
16634: 
16635: =over
16636: 
16637: =item
16638: official, unofficial, community, textbook, placement
16639: 
16640: =back
16641: 
16642: =item
16643: inststatus: types of institutional affiliation, and order in which they are displayed.
16644: 
16645: =over
16646: 
16647: =item
16648: inststatustypes, inststatusorder, inststatusguest
16649: 
16650: =back
16651: 
16652: =item
16653: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16654: for course's uploaded content.
16655: 
16656: =over
16657: 
16658: =item
16659: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16660: communityquota, textbookquota, placementquota
16661: 
16662: =back
16663: 
16664: =item
16665: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16666: on your servers.
16667: 
16668: =over
16669: 
16670: =item 
16671: remotesessions, hostedsessions
16672: 
16673: =back
16674: 
16675: =back
16676: 
16677: In cases where a domain coordinator has never used the "Set Domain Configuration"
16678: utility to create a configuration.db file on a domain's primary library server 
16679: only the following domain defaults: auth_def, auth_arg_def, lang_def
16680: -- corresponding values are authentication type (internal, krb4, krb5,
16681: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16682: will be available. Values are retrieved from cache (if current), unless the
16683: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16684: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16685: 
16686: Typical usage:
16687: 
16688: %domdefaults = &get_domain_defaults($target_domain);
16689: 
16690: =back
16691: 
16692: =head2 Network Status Functions
16693: 
16694: =over 4
16695: 
16696: =item *
16697: 
16698: dirlist() : return directory list based on URI (first arg).
16699: 
16700: Inputs: 1 required, 5 optional.
16701: 
16702: =over
16703: 
16704: =item 
16705: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16706: 
16707: =item
16708: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16709: 
16710: =item
16711: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16712: 
16713: =item
16714: $getpropath - boolean: 1 if prepend path using &propath(). 
16715: 
16716: =item
16717: $getuserdir - boolean: 1 if prepend path for "userfiles".
16718: 
16719: =item 
16720: $alternateRoot - path to prepend in place of path from $uri.
16721: 
16722: =back
16723: 
16724: Returns: Array of up to two items.
16725: 
16726: =over
16727: 
16728: a reference to an array of files/subdirectories
16729: 
16730: =over
16731: 
16732: Each element in the array of files/subdirectories is a & separated list of
16733: item name and the result of running stat on the item.  If dirlist was requested
16734: for a file instead of a directory, the item name will be ''. For a directory 
16735: listing, if the item is a metadata file, the element will end &N&M 
16736: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16737: default copyright set (1).  
16738: 
16739: =back
16740: 
16741: a scalar containing error condition (if encountered).
16742: 
16743: =over
16744: 
16745: =item 
16746: no_host (no homeserver identified for $username:$domain).
16747: 
16748: =item 
16749: no_such_host (server contacted for listing not identified as valid host).
16750: 
16751: =item 
16752: con_lost (connection to remote server failed).
16753: 
16754: =item 
16755: refused (invalid $username:$domain received on lond side).
16756: 
16757: =item 
16758: no_such_dir (directory at specified path on lond side does not exist). 
16759: 
16760: =item 
16761: empty (directory at specified path on lond side is empty).
16762: 
16763: =over
16764: 
16765: This is currently not encountered because the &ls3, &ls2, 
16766: &ls (_handler) routines on the lond side do not filter out
16767: . and .. from a directory listing. 
16768: 
16769: =back
16770: 
16771: =back
16772: 
16773: =back
16774: 
16775: =item *
16776: 
16777: spareserver() : find server with least workload from spare.tab
16778: 
16779: 
16780: =item *
16781: 
16782: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16783: if there is no corresponding loncapa host.
16784: 
16785: =back
16786: 
16787: 
16788: =head2 Apache Request
16789: 
16790: =over 4
16791: 
16792: =item *
16793: 
16794: ssi($url,%hash) : server side include, does a complete request cycle on url to
16795: localhost, posts hash
16796: 
16797: =back
16798: 
16799: =head2 Data to String to Data
16800: 
16801: =over 4
16802: 
16803: =item *
16804: 
16805: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16806: and '&' separators, supports elements that are arrayrefs and hashrefs
16807: 
16808: =item *
16809: 
16810: hashref2str($hashref) : convert a hashref into a string complete with
16811: escaping and '=' and '&' separators, supports elements that are
16812: arrayrefs and hashrefs
16813: 
16814: =item *
16815: 
16816: arrayref2str($arrayref) : convert an arrayref into a string complete
16817: with escaping and '&' separators, supports elements that are arrayrefs
16818: and hashrefs
16819: 
16820: =item *
16821: 
16822: str2hash($string) : convert string to hash using unescaping and
16823: splitting on '=' and '&', supports elements that are arrayrefs and
16824: hashrefs
16825: 
16826: =item *
16827: 
16828: str2array($string) : convert string to hash using unescaping and
16829: splitting on '&', supports elements that are arrayrefs and hashrefs
16830: 
16831: =back
16832: 
16833: =head2 Logging Routines
16834: 
16835: 
16836: These routines allow one to make log messages in the lonnet.log and
16837: lonnet.perm logfiles.
16838: 
16839: =over 4
16840: 
16841: =item *
16842: 
16843: logtouch() : make sure the logfile, lonnet.log, exists
16844: 
16845: =item *
16846: 
16847: logthis() : append message to the normal lonnet.log file, it gets
16848: preiodically rolled over and deleted.
16849: 
16850: =item *
16851: 
16852: logperm() : append a permanent message to lonnet.perm.log, this log
16853: file never gets deleted by any automated portion of the system, only
16854: messages of critical importance should go in here.
16855: 
16856: 
16857: =back
16858: 
16859: =head2 General File Helper Routines
16860: 
16861: =over 4
16862: 
16863: =item *
16864: 
16865: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
16866: (a) files in /uploaded
16867:   (i) If a local copy of the file exists - 
16868:       compares modification date of local copy with last-modified date for 
16869:       definitive version stored on home server for course. If local copy is 
16870:       stale, requests a new version from the home server and stores it. 
16871:       If the original has been removed from the home server, then local copy 
16872:       is unlinked.
16873:   (ii) If local copy does not exist -
16874:       requests the file from the home server and stores it. 
16875:   
16876:   If $caller is 'uploadrep':  
16877:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
16878:     for request for files originally uploaded via DOCS. 
16879:      - returns 'ok' if fresh local copy now available, -1 otherwise.
16880:   
16881:   Otherwise:
16882:      This indicates a call from the content generation phase of the request.
16883:      -  returns the entire contents of the file or -1.
16884:      
16885: (b) files in /res
16886:    - returns the entire contents of a file or -1; 
16887:    it properly subscribes to and replicates the file if neccessary.
16888: 
16889: 
16890: =item *
16891: 
16892: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
16893:                   reference
16894: 
16895: returns either a stat() list of data about the file or an empty list
16896: if the file doesn't exist or couldn't find out about it (connection
16897: problems or user unknown)
16898: 
16899: =item *
16900: 
16901: filelocation($dir,$file) : returns file system location of a file
16902: based on URI; meant to be "fairly clean" absolute reference, $dir is a
16903: directory that relative $file lookups are to looked in ($dir of /a/dir
16904: and a file of ../bob will become /a/bob)
16905: 
16906: =item *
16907: 
16908: hreflocation($dir,$file) : returns file system location or a URL; same as
16909: filelocation except for hrefs
16910: 
16911: =item *
16912: 
16913: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
16914: also removes beginning /home/httpd/html unless /priv/ follows it.
16915: 
16916: =back
16917: 
16918: =head2 Usererfile file routines (/uploaded*)
16919: 
16920: =over 4
16921: 
16922: =item *
16923: 
16924: userfileupload(): main rotine for putting a file in a user or course's
16925:                   filespace, arguments are,
16926: 
16927:  formname - required - this is the name of the element in $env where the
16928:            filename, and the contents of the file to create/modifed exist
16929:            the filename is in $env{'form.'.$formname.'.filename'} and the
16930:            contents of the file is located in $env{'form.'.$formname}
16931:  context - if coursedoc, store the file in the course of the active role
16932:              of the current user; 
16933:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
16934:            if 'canceloverwrite': delete file in tmp/overwrites directory
16935:  subdir - required - subdirectory to put the file in under ../userfiles/
16936:          if undefined, it will be placed in "unknown"
16937: 
16938:  (This routine calls clean_filename() to remove any dangerous
16939:  characters from the filename, and then calls finuserfileupload() to
16940:  complete the transaction)
16941: 
16942:  returns either the url of the uploaded file (/uploaded/....) if successful
16943:  and /adm/notfound.html if unsuccessful
16944: 
16945: =item *
16946: 
16947: clean_filename(): routine for cleaing a filename up for storage in
16948:                  userfile space, argument is:
16949: 
16950:  filename - proposed filename
16951: 
16952: returns: the new clean filename
16953: 
16954: =item *
16955: 
16956: finishuserfileupload(): routine that creates and sends the file to
16957: userspace, probably shouldn't be called directly
16958: 
16959:   docuname: username or courseid of destination for the file
16960:   docudom: domain of user/course of destination for the file
16961:   formname: same as for userfileupload()
16962:   fname: filename (including subdirectories) for the file
16963:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
16964:           if hashref, and context is scantron, will convert csv format to standard format
16965:   allfiles: reference to hash used to store objects found by parser
16966:   codebase: reference to hash used for codebases of java objects found by parser
16967:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
16968:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
16969:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
16970:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
16971:   context: if 'overwrite', will move the uploaded file from its temporary location to
16972:             userfiles to facilitate overwriting a previously uploaded file with same name.
16973:   mimetype: reference to scalar to accommodate mime type determined
16974:             from File::MMagic if $parser = parse.
16975: 
16976:  returns either the url of the uploaded file (/uploaded/....) if successful
16977:  and /adm/notfound.html if unsuccessful (or an error message if context 
16978:  was 'overwrite').
16979:  
16980: 
16981: =item *
16982: 
16983: renameuserfile(): renames an existing userfile to a new name
16984: 
16985:   Args:
16986:    docuname: username or courseid of destination for the file
16987:    docudom: domain of user/course of destination for the file
16988:    old: current file name (including any subdirs under userfiles)
16989:    new: desired file name (including any subdirs under userfiles)
16990: 
16991: =item *
16992: 
16993: mkdiruserfile(): creates a directory is a userfiles dir
16994: 
16995:   Args:
16996:    docuname: username or courseid of destination for the file
16997:    docudom: domain of user/course of destination for the file
16998:    dir: dir to create (including any subdirs under userfiles)
16999: 
17000: =item *
17001: 
17002: removeuserfile(): removes a file that exists in userfiles
17003: 
17004:   Args:
17005:    docuname: username or courseid of destination for the file
17006:    docudom: domain of user/course of destination for the file
17007:    fname: filname to delete (including any subdirs under userfiles)
17008: 
17009: =item *
17010: 
17011: removeuploadedurl(): convience function for removeuserfile()
17012: 
17013:   Args:
17014:    url:  a full /uploaded/... url to delete
17015: 
17016: =item * 
17017: 
17018: get_portfile_permissions():
17019:   Args:
17020:     domain: domain of user or course contain the portfolio files
17021:     user: name of user or num of course contain the portfolio files
17022:   Returns:
17023:     hashref of a dump of the proper file_permissions.db
17024:    
17025: 
17026: =item * 
17027: 
17028: get_access_controls():
17029: 
17030: Args:
17031:   current_permissions: the hash ref returned from get_portfile_permissions()
17032:   group: (optional) the group you want the files associated with
17033:   file: (optional) the file you want access info on
17034: 
17035: Returns:
17036:     a hash (keys are file names) of hashes containing
17037:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17038:         values are XML containing access control settings (see below) 
17039: 
17040: Internal notes:
17041: 
17042:  access controls are stored in file_permissions.db as key=value pairs.
17043:     key -> path to file/file_name\0uniqueID:scope_end_start
17044:         where scope -> public,guest,course,group,domains or users.
17045:               end -> UNIX time for end of access (0 -> no end date)
17046:               start -> UNIX time for start of access
17047: 
17048:     value -> XML description of access control
17049:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17050:             <start></start>
17051:             <end></end>
17052: 
17053:             <password></password>  for scope type = guest
17054: 
17055:             <domain></domain>     for scope type = course or group
17056:             <number></number>
17057:             <roles id="">
17058:              <role></role>
17059:              <access></access>
17060:              <section></section>
17061:              <group></group>
17062:             </roles>
17063: 
17064:             <dom></dom>         for scope type = domains
17065: 
17066:             <users>             for scope type = users
17067:              <user>
17068:               <uname></uname>
17069:               <udom></udom>
17070:              </user>
17071:             </users>
17072:            </scope> 
17073:               
17074:  Access data is also aggregated for each file in an additional key=value pair:
17075:  key -> path to file/file_name\0accesscontrol 
17076:  value -> reference to hash
17077:           hash contains key = value pairs
17078:           where key = uniqueID:scope_end_start
17079:                 value = UNIX time record was last updated
17080: 
17081:           Used to improve speed of look-ups of access controls for each file.  
17082:  
17083:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17084: 
17085: =item *
17086: 
17087: modify_access_controls():
17088: 
17089: Modifies access controls for a portfolio file
17090: Args
17091: 1. file name
17092: 2. reference to hash of required changes,
17093: 3. domain
17094: 4. username
17095:   where domain,username are the domain of the portfolio owner 
17096:   (either a user or a course) 
17097: 
17098: Returns:
17099: 1. result of additions or updates ('ok' or 'error', with error message). 
17100: 2. result of deletions ('ok' or 'error', with error message).
17101: 3. reference to hash of any new or updated access controls.
17102: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17103:    key = integer (inbound ID)
17104:    value = uniqueID
17105: 
17106: =item *
17107: 
17108: get_timebased_id():
17109: 
17110: Attempts to get a unique timestamp-based suffix for use with items added to a 
17111: course via the Course Editor (e.g., folders, composite pages, 
17112: group bulletin boards).
17113: 
17114: Args: (first three required; six others optional)
17115: 
17116: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17117:    docssequence, or name of group
17118: 
17119: 2. keyid (alphanumeric): name of temporary locking key in hash,
17120:    e.g., num, boardids
17121: 
17122: 3. namespace: name of gdbm file used to store suffixes already assigned;  
17123:    file will be named nohist_namespace.db
17124: 
17125: 4. cdom: domain of course; default is current course domain from %env
17126: 
17127: 5. cnum: course number; default is current course number from %env
17128: 
17129: 6. idtype: set to concat if an additional digit is to be appended to the 
17130:    unix timestamp to form the suffix, if the plain timestamp is already
17131:    in use.  Default is to not do this, but simply increment the unix 
17132:    timestamp by 1 until a unique key is obtained.
17133: 
17134: 7. who: holder of locking key; defaults to user:domain for user.
17135: 
17136: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
17137:    retrying); default is 3.
17138: 
17139: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
17140: 
17141: Returns:
17142: 
17143: 1. suffix obtained (numeric)
17144: 
17145: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17146: 
17147: 3. error: contains (localized) error message if an error occurred.
17148: 
17149: 
17150: =back
17151: 
17152: =head2 HTTP Helper Routines
17153: 
17154: =over 4
17155: 
17156: =item *
17157: 
17158: escape() : unpack non-word characters into CGI-compatible hex codes
17159: 
17160: =item *
17161: 
17162: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17163: 
17164: =back
17165: 
17166: =head1 PRIVATE SUBROUTINES
17167: 
17168: =head2 Underlying communication routines (Shouldn't call)
17169: 
17170: =over 4
17171: 
17172: =item *
17173: 
17174: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17175: 
17176: =item *
17177: 
17178: reply() : uses subreply to send a message to remote machine, logs all failures
17179: 
17180: =item *
17181: 
17182: critical() : passes a critical message to another server; if cannot
17183: get through then place message in connection buffer directory and
17184: returns con_delayed, if incapable of saving message, returns
17185: con_failed
17186: 
17187: =item *
17188: 
17189: reconlonc() : tries to reconnect lonc client processes.
17190: 
17191: =back
17192: 
17193: =head2 Resource Access Logging
17194: 
17195: =over 4
17196: 
17197: =item *
17198: 
17199: flushcourselogs() : flush (save) buffer logs and access logs
17200: 
17201: =item *
17202: 
17203: courselog($what) : save message for course in hash
17204: 
17205: =item *
17206: 
17207: courseacclog($what) : save message for course using &courselog().  Perform
17208: special processing for specific resource types (problems, exams, quizzes, etc).
17209: 
17210: =item *
17211: 
17212: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17213: as a PerlChildExitHandler
17214: 
17215: =back
17216: 
17217: =head2 Other
17218: 
17219: =over 4
17220: 
17221: =item *
17222: 
17223: symblist($mapname,%newhash) : update symbolic storage links
17224: 
17225: =back
17226: 
17227: =cut
17228: 

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