File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1476: download - view: text, annotated - select for diffs
Fri Dec 24 11:07:43 2021 UTC (2 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add option to choose inline preview (new) or pop-up (old) for Chemical
  Reaction response items.  Default is inline preview. Domain setting can
  be overridden in a course.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1476 2021/12/24 11:07:43 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:                 ($subcmd eq 'put')) {
  473:                 (undef,undef,my @rest) = split(/:/,$cmd);
  474:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  475:                     splice(@rest,2,1,'Hidden');
  476:                 } elsif ($subcmd eq 'passwd') {
  477:                     splice(@rest,2,2,('Hidden','Hidden'));
  478:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  479:                          ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
  480:                     splice(@rest,3,1,'Hidden');
  481:                 }
  482:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  483:             }
  484:         }
  485:         &logthis("<font color=\"blue\">WARNING:".
  486:                  " $logged to $server returned $answer</font>");
  487:     }
  488:     return $answer;
  489: }
  490: 
  491: # ----------------------------------------------------------- Send USR1 to lonc
  492: 
  493: sub reconlonc {
  494:     my ($lonid) = @_;
  495:     if ($lonid) {
  496:         my $hostname = &hostname($lonid);
  497: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  498: 	if ($hostname && -e $peerfile) {
  499: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  500: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  501: 					     Type    => SOCK_STREAM,
  502: 					     Timeout => 10);
  503: 	    if ($client) {
  504: 		print $client ("reset_retries\n");
  505: 		my $answer=<$client>;
  506: 		#reset just this one.
  507: 	    }
  508: 	}
  509: 	return;
  510:     }
  511: 
  512:     &logthis("Trying to reconnect lonc");
  513:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  514:     if (open(my $fh,"<",$loncfile)) {
  515: 	my $loncpid=<$fh>;
  516:         chomp($loncpid);
  517:         if (kill 0 => $loncpid) {
  518: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  519:             kill USR1 => $loncpid;
  520:             sleep 1;
  521:         } else {
  522: 	    &logthis(
  523:                "<font color=\"blue\">WARNING:".
  524:                " lonc at pid $loncpid not responding, giving up</font>");
  525:         }
  526:     } else {
  527: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  528:     }
  529: }
  530: 
  531: # ------------------------------------------------------ Critical communication
  532: 
  533: sub critical {
  534:     my ($cmd,$server)=@_;
  535:     unless (&hostname($server)) {
  536:         &logthis("<font color=\"blue\">WARNING:".
  537:                " Critical message to unknown server ($server)</font>");
  538:         return 'no_such_host';
  539:     }
  540:     my $answer=reply($cmd,$server);
  541:     if ($answer eq 'con_lost') {
  542: 	&reconlonc($server);
  543: 	my $answer=reply($cmd,$server);
  544:         if ($answer eq 'con_lost') {
  545:             my $now=time;
  546:             my $middlename=$cmd;
  547:             $middlename=substr($middlename,0,16);
  548:             $middlename=~s/\W//g;
  549:             my $dfilename=
  550:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  551:             $dumpcount++;
  552:             {
  553: 		my $dfh;
  554: 		if (open($dfh,">",$dfilename)) {
  555: 		    print $dfh "$cmd\n"; 
  556: 		    close($dfh);
  557: 		}
  558:             }
  559:             sleep 1;
  560:             my $wcmd='';
  561:             {
  562: 		my $dfh;
  563: 		if (open($dfh,"<",$dfilename)) {
  564: 		    $wcmd=<$dfh>; 
  565: 		    close($dfh);
  566: 		}
  567:             }
  568:             chomp($wcmd);
  569:             if ($wcmd eq $cmd) {
  570: 		&logthis("<font color=\"blue\">WARNING: ".
  571:                          "Connection buffer $dfilename: $cmd</font>");
  572:                 &logperm("D:$server:$cmd");
  573: 	        return 'con_delayed';
  574:             } else {
  575:                 &logthis("<font color=\"red\">CRITICAL:"
  576:                         ." Critical connection failed: $server $cmd</font>");
  577:                 &logperm("F:$server:$cmd");
  578:                 return 'con_failed';
  579:             }
  580:         }
  581:     }
  582:     return $answer;
  583: }
  584: 
  585: # ------------------------------------------- check if return value is an error
  586: 
  587: sub error {
  588:     my ($result) = @_;
  589:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  590: 	if ($2 == 2) { return undef; }
  591: 	return $1;
  592:     }
  593:     return undef;
  594: }
  595: 
  596: sub convert_and_load_session_env {
  597:     my ($lonidsdir,$handle)=@_;
  598:     my @profile;
  599:     {
  600: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  601: 	if (!$opened) {
  602: 	    return 0;
  603: 	}
  604: 	flock($idf,LOCK_SH);
  605: 	@profile=<$idf>;
  606: 	close($idf);
  607:     }
  608:     my %temp_env;
  609:     foreach my $line (@profile) {
  610: 	if ($line !~ m/=/) {
  611: 	    return 0;
  612: 	}
  613: 	chomp($line);
  614: 	my ($envname,$envvalue)=split(/=/,$line,2);
  615: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  616:     }
  617:     unlink("$lonidsdir/$handle.id");
  618:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  619: 	    0640)) {
  620: 	%disk_env = %temp_env;
  621: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  622: 	untie(%disk_env);
  623:     }
  624:     return 1;
  625: }
  626: 
  627: # ------------------------------------------- Transfer profile into environment
  628: my $env_loaded;
  629: sub transfer_profile_to_env {
  630:     my ($lonidsdir,$handle,$force_transfer) = @_;
  631:     if (!$force_transfer && $env_loaded) { return; } 
  632: 
  633:     if (!defined($lonidsdir)) {
  634: 	$lonidsdir = $perlvar{'lonIDsDir'};
  635:     }
  636:     if (!defined($handle)) {
  637:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  638:     }
  639: 
  640:     my $convert;
  641:     {
  642:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  643: 	if (!$opened) {
  644: 	    return;
  645: 	}
  646: 	flock($idf,LOCK_SH);
  647: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  648: 		&GDBM_READER(),0640)) {
  649: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  650: 	    untie(%disk_env);
  651: 	} else {
  652: 	    $convert = 1;
  653: 	}
  654:     }
  655:     if ($convert) {
  656: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  657: 	    &logthis("Failed to load session, or convert session.");
  658: 	}
  659:     }
  660: 
  661:     my %remove;
  662:     while ( my $envname = each(%env) ) {
  663:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  664:             if ($time < time-300) {
  665:                 $remove{$key}++;
  666:             }
  667:         }
  668:     }
  669: 
  670:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  671:     $env_loaded=1;
  672:     foreach my $expired_key (keys(%remove)) {
  673:         &delenv($expired_key);
  674:     }
  675: }
  676: 
  677: # ---------------------------------------------------- Check for valid session 
  678: sub check_for_valid_session {
  679:     my ($r,$name,$userhashref,$domref) = @_;
  680:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  681:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  682:     if ($name eq 'lonDAV') {
  683:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  684:     } else {
  685:         $lonidsdir=$r->dir_config('lonIDsDir');
  686:         if ($name eq '') {
  687:             $name = 'lonID';
  688:         }
  689:     }
  690:     if ($name eq 'lonID') {
  691:         $secure = 'lonSID';
  692:         $linkname = 'lonLinkID';
  693:         $pubname = 'lonPubID';
  694:         if (exists($cookies{$secure})) {
  695:             $lonid=$cookies{$secure};
  696:         } elsif (exists($cookies{$name})) {
  697:             $lonid=$cookies{$name};
  698:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  699:             $lonid=$cookies{$linkname};
  700:         } elsif (exists($cookies{$pubname})) {
  701:             $lonid=$cookies{$pubname};
  702:         }
  703:     } else {
  704:         $lonid=$cookies{$name};
  705:     }
  706:     return undef if (!$lonid);
  707: 
  708:     my $handle=&LONCAPA::clean_handle($lonid->value);
  709:     if (-l "$lonidsdir/$handle.id") {
  710:         my $link = readlink("$lonidsdir/$handle.id");
  711:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  712:             $handle = $1;
  713:         }
  714:     }
  715:     if (!-e "$lonidsdir/$handle.id") {
  716:         if ((ref($domref)) && ($name eq 'lonID') && 
  717:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  718:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  719:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  720:                 $$domref = $possudom;
  721:             }
  722:         }
  723:         return undef;
  724:     }
  725: 
  726:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  727:     return undef if (!$opened);
  728: 
  729:     flock($idf,LOCK_SH);
  730:     my %disk_env;
  731:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  732: 	    &GDBM_READER(),0640)) {
  733: 	return undef;	
  734:     }
  735: 
  736:     if (!defined($disk_env{'user.name'})
  737: 	|| !defined($disk_env{'user.domain'})) {
  738:         untie(%disk_env);
  739: 	return undef;
  740:     }
  741: 
  742:     if (ref($userhashref) eq 'HASH') {
  743:         $userhashref->{'name'} = $disk_env{'user.name'};
  744:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  745:         if ($disk_env{'request.role'}) {
  746:             $userhashref->{'role'} = $disk_env{'request.role'};
  747:         }
  748:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  749:         if ($userhashref->{'lti'}) {
  750:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  751:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  752:         }
  753:     }
  754:     untie(%disk_env);
  755: 
  756:     return $handle;
  757: }
  758: 
  759: sub timed_flock {
  760:     my ($file,$lock_type) = @_;
  761:     my $failed=0;
  762:     eval {
  763: 	local $SIG{__DIE__}='DEFAULT';
  764: 	local $SIG{ALRM}=sub {
  765: 	    $failed=1;
  766: 	    die("failed lock");
  767: 	};
  768: 	alarm(13);
  769: 	flock($file,$lock_type);
  770: 	alarm(0);
  771:     };
  772:     if ($failed) {
  773: 	return undef;
  774:     } else {
  775: 	return 1;
  776:     }
  777: }
  778: 
  779: sub get_sessionfile_vars {
  780:     my ($handle,$lonidsdir,$storearr) = @_;
  781:     my %returnhash;
  782:     unless (ref($storearr) eq 'ARRAY') {
  783:         return %returnhash;
  784:     }
  785:     if (-l "$lonidsdir/$handle.id") {
  786:         my $link = readlink("$lonidsdir/$handle.id");
  787:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  788:             $handle = $1;
  789:         }
  790:     }
  791:     if ((-e "$lonidsdir/$handle.id") &&
  792:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  793:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  794:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  795:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  796:                 flock($idf,LOCK_SH);
  797:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  798:                         &GDBM_READER(),0640)) {
  799:                     foreach my $item (@{$storearr}) {
  800:                         $returnhash{$item} = $disk_env{$item};
  801:                     }
  802:                     untie(%disk_env);
  803:                 }
  804:             }
  805:         }
  806:     }
  807:     return %returnhash;
  808: }
  809: 
  810: # ---------------------------------------------------------- Append Environment
  811: 
  812: sub appenv {
  813:     my ($newenv,$roles) = @_;
  814:     if (ref($newenv) eq 'HASH') {
  815:         foreach my $key (keys(%{$newenv})) {
  816:             my $refused = 0;
  817: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  818:                 $refused = 1;
  819:                 if (ref($roles) eq 'ARRAY') {
  820:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  821:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  822:                         $refused = 0;
  823:                     }
  824:                 }
  825:             }
  826:             if ($refused) {
  827:                 &logthis("<font color=\"blue\">WARNING: ".
  828:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  829:                          .'</font>');
  830: 	        delete($newenv->{$key});
  831:             } else {
  832:                 $env{$key}=$newenv->{$key};
  833:             }
  834:         }
  835:         my $lonids = $perlvar{'lonIDsDir'};
  836:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  837:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  838:             if ($opened
  839: 	        && &timed_flock($env_file,LOCK_EX)
  840: 	        &&
  841: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  842: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  843: 	        while (my ($key,$value) = each(%{$newenv})) {
  844: 	            $disk_env{$key} = $value;
  845: 	        }
  846: 	        untie(%disk_env);
  847:             }
  848:         }
  849:     }
  850:     return 'ok';
  851: }
  852: # ----------------------------------------------------- Delete from Environment
  853: 
  854: sub delenv {
  855:     my ($delthis,$regexp,$roles) = @_;
  856:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  857:         my $refused = 1;
  858:         if (ref($roles) eq 'ARRAY') {
  859:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  860:             if (grep(/^\Q$role\E$/,@{$roles})) {
  861:                 $refused = 0;
  862:             }
  863:         }
  864:         if ($refused) {
  865:             &logthis("<font color=\"blue\">WARNING: ".
  866:                      "Attempt to delete from environment ".$delthis);
  867:             return 'error';
  868:         }
  869:     }
  870:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  871:     if ($opened
  872: 	&& &timed_flock($env_file,LOCK_EX)
  873: 	&&
  874: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  875: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  876: 	foreach my $key (keys(%disk_env)) {
  877: 	    if ($regexp) {
  878:                 if ($key=~/^$delthis/) {
  879:                     delete($env{$key});
  880:                     delete($disk_env{$key});
  881:                 } 
  882:             } else {
  883:                 if ($key=~/^\Q$delthis\E/) {
  884: 		    delete($env{$key});
  885: 		    delete($disk_env{$key});
  886: 	        }
  887:             }
  888: 	}
  889: 	untie(%disk_env);
  890:     }
  891:     return 'ok';
  892: }
  893: 
  894: sub get_env_multiple {
  895:     my ($name) = @_;
  896:     my @values;
  897:     if (defined($env{$name})) {
  898:         # exists is it an array
  899:         if (ref($env{$name})) {
  900:             @values=@{ $env{$name} };
  901:         } else {
  902:             $values[0]=$env{$name};
  903:         }
  904:     }
  905:     return(@values);
  906: }
  907: 
  908: # ------------------------------------------------------------------- Locking
  909: 
  910: sub set_lock {
  911:     my ($text)=@_;
  912:     $locknum++;
  913:     my $id=$$.'-'.$locknum;
  914:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  915:              'session.lock.'.$id => $text});
  916:     return $id;
  917: }
  918: 
  919: sub get_locks {
  920:     my $num=0;
  921:     my %texts=();
  922:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  923:        if ($lock=~/\w/) {
  924:           $num++;
  925:           $texts{$lock}=$env{'session.lock.'.$lock};
  926:        }
  927:    }
  928:    return ($num,%texts);
  929: }
  930: 
  931: sub remove_lock {
  932:     my ($id)=@_;
  933:     my $newlocks='';
  934:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  935:        if (($lock=~/\w/) && ($lock ne $id)) {
  936:           $newlocks.=','.$lock;
  937:        }
  938:     }
  939:     &appenv({'session.locks' => $newlocks});
  940:     &delenv('session.lock.'.$id);
  941: }
  942: 
  943: sub remove_all_locks {
  944:     my $activelocks=$env{'session.locks'};
  945:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  946:        if ($lock=~/\w/) {
  947:           &remove_lock($lock);
  948:        }
  949:     }
  950: }
  951: 
  952: 
  953: # ------------------------------------------ Find out current server userload
  954: sub userload {
  955:     my $numusers=0;
  956:     {
  957: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  958: 	my $filename;
  959: 	my $curtime=time;
  960: 	while ($filename=readdir(LONIDS)) {
  961: 	    next if ($filename eq '.' || $filename eq '..');
  962: 	    next if ($filename =~ /publicuser_\d+\.id/);
  963:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
  964: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  965: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  966: 	}
  967: 	closedir(LONIDS);
  968:     }
  969:     my $userloadpercent=0;
  970:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  971:     if ($maxuserload) {
  972: 	$userloadpercent=100*$numusers/$maxuserload;
  973:     }
  974:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  975:     return $userloadpercent;
  976: }
  977: 
  978: # ------------------------------ Find server with least workload from spare.tab
  979: 
  980: sub spareserver {
  981:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  982:     my $spare_server;
  983:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  984:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  985:                                                      :  $userloadpercent;
  986:     my ($uint_dom,$remotesessions);
  987:     if (($udom ne '') && (&domain($udom) ne '')) {
  988:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  989:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  990:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  991:         $remotesessions = $udomdefaults{'remotesessions'};
  992:     }
  993:     my $spareshash = &this_host_spares($udom);
  994:     if (ref($spareshash) eq 'HASH') {
  995:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  996:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  997:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  998:                                              $try_server));
  999: 	        ($spare_server, $lowest_load) =
 1000: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
 1001:             }
 1002:         }
 1003: 
 1004:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1005: 
 1006:         if (!$found_server) {
 1007:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1008: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1009:                     next unless (&spare_can_host($udom,$uint_dom,
 1010:                                                  $remotesessions,$try_server));
 1011: 	            ($spare_server, $lowest_load) =
 1012: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1013:                 }
 1014: 	    }
 1015:         }
 1016:     }
 1017: 
 1018:     if (!$want_server_name) {
 1019:         if (defined($spare_server)) {
 1020:             my $hostname = &hostname($spare_server);
 1021:             if (defined($hostname)) {
 1022:                 my $protocol = 'http';
 1023:                 if ($protocol{$spare_server} eq 'https') {
 1024:                     $protocol = $protocol{$spare_server};
 1025:                 }
 1026:                 my $alias = &Apache::lonnet::use_proxy_alias($r,$spare_server);
 1027:                 $hostname = $alias if ($alias ne '');
 1028: 	        $spare_server = $protocol.'://'.$hostname;
 1029:             }
 1030:         }
 1031:     }
 1032:     return $spare_server;
 1033: }
 1034: 
 1035: sub compare_server_load {
 1036:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1037: 
 1038:     if ($required) {
 1039:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1040:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1041:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1042:         if (($major eq '' && $minor eq '') ||
 1043:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1044:             return ($spare_server,$lowest_load);
 1045:         }
 1046:     }
 1047: 
 1048:     my $loadans     = &reply('load',    $try_server);
 1049:     my $userloadans = &reply('userload',$try_server);
 1050: 
 1051:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1052: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1053:     }
 1054: 
 1055:     my $load;
 1056:     if ($loadans =~ /\d/) {
 1057: 	if ($userloadans =~ /\d/) {
 1058: 	    #both are numbers, pick the bigger one
 1059: 	    $load = ($loadans > $userloadans) ? $loadans 
 1060: 		                              : $userloadans;
 1061: 	} else {
 1062: 	    $load = $loadans;
 1063: 	}
 1064:     } else {
 1065: 	$load = $userloadans;
 1066:     }
 1067: 
 1068:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1069: 	$spare_server = $try_server;
 1070: 	$lowest_load  = $load;
 1071:     }
 1072:     return ($spare_server,$lowest_load);
 1073: }
 1074: 
 1075: # --------------------------- ask offload servers if user already has a session
 1076: sub find_existing_session {
 1077:     my ($udom,$uname) = @_;
 1078:     my $spareshash = &this_host_spares($udom);
 1079:     if (ref($spareshash) eq 'HASH') {
 1080:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1081:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1082:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1083:             }
 1084:         }
 1085:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1086:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1087:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1088:             }
 1089:         }
 1090:     }
 1091:     return;
 1092: }
 1093: 
 1094: sub delusersession {
 1095:     my ($lonid,$udom,$uname) = @_;
 1096:     my $uprimary_id = &domain($udom,'primary');
 1097:     my $uintdom = &internet_dom($uprimary_id);
 1098:     my $intdom = &internet_dom($lonid);
 1099:     my $serverhomedom = &host_domain($lonid);
 1100:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1101:         return &reply(join(':','delusersession',
 1102:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1103:     }
 1104:     return;
 1105: }
 1106: 
 1107: # check if user's browser sent load balancer cookie and server still has session
 1108: # and is not overloaded.
 1109: sub check_for_balancer_cookie {
 1110:     my ($r,$update_mtime) = @_;
 1111:     my ($otherserver,$cookie);
 1112:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1113:     if (exists($cookies{'balanceID'})) {
 1114:         my $balid = $cookies{'balanceID'};
 1115:         $cookie=&LONCAPA::clean_handle($balid->value);
 1116:         my $balancedir=$r->dir_config('lonBalanceDir');
 1117:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1118:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1119:                 my ($possudom,$possuname) = ($1,$2);
 1120:                 my $has_session = 0;
 1121:                 if ((&domain($possudom) ne '') &&
 1122:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1123:                     my $try_server;
 1124:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1125:                     if ($opened) {
 1126:                         flock($idf,LOCK_SH);
 1127:                         while (my $line = <$idf>) {
 1128:                             chomp($line);
 1129:                             if (&hostname($line) ne '') {
 1130:                                 $try_server = $line;
 1131:                                 last;
 1132:                             }
 1133:                         }
 1134:                         close($idf);
 1135:                         if (($try_server) &&
 1136:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1137:                             my $lowest_load = 30000;
 1138:                             ($otherserver,$lowest_load) =
 1139:                                 &compare_server_load($try_server,undef,$lowest_load);
 1140:                             if ($otherserver ne '' && $lowest_load < 100) {
 1141:                                 $has_session = 1;
 1142:                             } else {
 1143:                                 undef($otherserver);
 1144:                             }
 1145:                         }
 1146:                     }
 1147:                 }
 1148:                 if ($has_session) {
 1149:                     if ($update_mtime) {
 1150:                         my $atime = my $mtime = time;
 1151:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1152:                     }
 1153:                 } else {
 1154:                     unlink("$balancedir/$cookie.id");
 1155:                 }
 1156:             }
 1157:         }
 1158:     }
 1159:     return ($otherserver,$cookie);
 1160: }
 1161: 
 1162: sub updatebalcookie {
 1163:     my ($cookie,$balancer,$lastentry)=@_;
 1164:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1165:         my ($udom,$uname) = ($1,$2);
 1166:         my $uprimary_id = &domain($udom,'primary');
 1167:         my $uintdom = &internet_dom($uprimary_id);
 1168:         my $intdom = &internet_dom($balancer);
 1169:         my $serverhomedom = &host_domain($balancer);
 1170:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1171:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1172:         }
 1173:     }
 1174:     return;
 1175: }
 1176: 
 1177: sub delbalcookie {
 1178:     my ($cookie,$balancer) =@_;
 1179:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1180:         my ($udom,$uname) = ($1,$2);
 1181:         my $uprimary_id = &domain($udom,'primary');
 1182:         my $uintdom = &internet_dom($uprimary_id);
 1183:         my $intdom = &internet_dom($balancer);
 1184:         my $serverhomedom = &host_domain($balancer);
 1185:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1186:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1187:         }
 1188:     }
 1189: }
 1190: 
 1191: # -------------------------------- ask if server already has a session for user
 1192: sub has_user_session {
 1193:     my ($lonid,$udom,$uname) = @_;
 1194:     my $result = &reply(join(':','userhassession',
 1195: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1196:     return 1 if ($result eq 'ok');
 1197: 
 1198:     return 0;
 1199: }
 1200: 
 1201: # --------- determine least loaded server in a user's domain which allows login
 1202: 
 1203: sub choose_server {
 1204:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1205:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1206:     my %servers = &get_servers($udom);
 1207:     my $lowest_load = 30000;
 1208:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1209:     if ($skiploadbal) {
 1210:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1211:         unless (defined($cached)) {
 1212:             my $cachetime = 60*60*24;
 1213:             my %domconfig =
 1214:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1215:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1216:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1217:                                            $cachetime);
 1218:             }
 1219:         }
 1220:     }
 1221:     foreach my $lonhost (keys(%servers)) {
 1222:         if ($skiploadbal) {
 1223:             if (ref($balancers) eq 'HASH') {
 1224:                 next if (exists($balancers->{$lonhost}));
 1225:             }
 1226:         }
 1227:         my $loginvia;
 1228:         if ($checkloginvia) {
 1229:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1230:             if ($loginvia) {
 1231:                 my ($server,$path) = split(/:/,$loginvia);
 1232:                 ($login_host, $lowest_load) =
 1233:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1234:                 if ($login_host eq $server) {
 1235:                     $portal_path = $path;
 1236:                     $isredirect = 1;
 1237:                 }
 1238:             } else {
 1239:                 ($login_host, $lowest_load) =
 1240:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1241:                 if ($login_host eq $lonhost) {
 1242:                     $portal_path = '';
 1243:                     $isredirect = ''; 
 1244:                 }
 1245:             }
 1246:         } else {
 1247:             ($login_host, $lowest_load) =
 1248:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1249:         }
 1250:     }
 1251:     if ($login_host ne '') {
 1252:         $hostname = &hostname($login_host);
 1253:     }
 1254:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1255: }
 1256: 
 1257: sub get_course_sessions {
 1258:     my ($cnum,$cdom,$lastactivity) = @_;
 1259:     my %servers = &internet_dom_servers($cdom);
 1260:     my %returnhash;
 1261:     foreach my $server (sort(keys(%servers))) {
 1262:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1263:         my @pairs=split(/\&/,$rep);
 1264:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1265:             foreach my $item (@pairs) {
 1266:                 my ($key,$value)=split(/=/,$item,2);
 1267:                 $key = &unescape($key);
 1268:                 next if ($key =~ /^error: 2 /);
 1269:                 if (exists($returnhash{$key})) {
 1270:                     next if ($value < $returnhash{$key});
 1271:                 }
 1272:                 $returnhash{$key}=$value;
 1273:             }
 1274:         }
 1275:     }
 1276:     return %returnhash;
 1277: }
 1278: 
 1279: # --------------------------------------------- Try to change a user's password
 1280: 
 1281: sub changepass {
 1282:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1283:     $currentpass = &escape($currentpass);
 1284:     $newpass     = &escape($newpass);
 1285:     my $lonhost = $perlvar{'lonHostID'};
 1286:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1287: 		       $server);
 1288:     if (! $answer) {
 1289: 	&logthis("No reply on password change request to $server ".
 1290: 		 "by $uname in domain $udom.");
 1291:     } elsif ($answer =~ "^ok") {
 1292:         &logthis("$uname in $udom successfully changed their password ".
 1293: 		 "on $server.");
 1294:     } elsif ($answer =~ "^pwchange_failure") {
 1295: 	&logthis("$uname in $udom was unable to change their password ".
 1296: 		 "on $server.  The action was blocked by either lcpasswd ".
 1297: 		 "or pwchange");
 1298:     } elsif ($answer =~ "^non_authorized") {
 1299:         &logthis("$uname in $udom did not get their password correct when ".
 1300: 		 "attempting to change it on $server.");
 1301:     } elsif ($answer =~ "^auth_mode_error") {
 1302:         &logthis("$uname in $udom attempted to change their password despite ".
 1303: 		 "not being locally or internally authenticated on $server.");
 1304:     } elsif ($answer =~ "^unknown_user") {
 1305:         &logthis("$uname in $udom attempted to change their password ".
 1306: 		 "on $server but were unable to because $server is not ".
 1307: 		 "their home server.");
 1308:     } elsif ($answer =~ "^refused") {
 1309: 	&logthis("$server refused to change $uname in $udom password because ".
 1310: 		 "it was sent an unencrypted request to change the password.");
 1311:     } elsif ($answer =~ "invalid_client") {
 1312:         &logthis("$server refused to change $uname in $udom password because ".
 1313:                  "it was a reset by e-mail originating from an invalid server.");
 1314:     } elsif ($answer =~ "^prioruse") {
 1315:        &logthis("$server refused to change $uname in $udom password because ".
 1316:                 "the password had been used before");
 1317:     }
 1318:     return $answer;
 1319: }
 1320: 
 1321: # ----------------------- Try to determine user's current authentication scheme
 1322: 
 1323: sub queryauthenticate {
 1324:     my ($uname,$udom)=@_;
 1325:     my $uhome=&homeserver($uname,$udom);
 1326:     if (!$uhome) {
 1327: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1328: 	return 'no_host';
 1329:     }
 1330:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1331:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1332: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1333:     }
 1334:     return $answer;
 1335: }
 1336: 
 1337: # --------- Try to authenticate user from domain's lib servers (first this one)
 1338: 
 1339: sub authenticate {
 1340:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1341:     $upass=&escape($upass);
 1342:     $uname= &LONCAPA::clean_username($uname);
 1343:     my $uhome=&homeserver($uname,$udom,1);
 1344:     my $newhome;
 1345:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1346: # Maybe the machine was offline and only re-appeared again recently?
 1347:         &reconlonc();
 1348: # One more
 1349: 	$uhome=&homeserver($uname,$udom,1);
 1350:         if (($uhome eq 'no_host') && $checkdefauth) {
 1351:             if (defined(&domain($udom,'primary'))) {
 1352:                 $newhome=&domain($udom,'primary');
 1353:             }
 1354:             if ($newhome ne '') {
 1355:                 $uhome = $newhome;
 1356:             }
 1357:         }
 1358: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1359: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1360: 	    return 'no_host';
 1361:         }
 1362:     }
 1363:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1364:     if ($answer eq 'authorized') {
 1365:         if ($newhome) {
 1366:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1367:             return 'no_account_on_host'; 
 1368:         } else {
 1369:             &logthis("User $uname at $udom authorized by $uhome");
 1370:             return $uhome;
 1371:         }
 1372:     }
 1373:     if ($answer eq 'non_authorized') {
 1374: 	&logthis("User $uname at $udom rejected by $uhome");
 1375: 	return 'no_host'; 
 1376:     }
 1377:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1378:     return 'no_host';
 1379: }
 1380: 
 1381: sub can_host_session {
 1382:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1383:     my $canhost = 1;
 1384:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1385:     if (ref($remotesessions) eq 'HASH') {
 1386:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1387:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1388:                 $canhost = 0;
 1389:             } else {
 1390:                 $canhost = 1;
 1391:             }
 1392:         }
 1393:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1394:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1395:                 $canhost = 1;
 1396:             } else {
 1397:                 $canhost = 0;
 1398:             }
 1399:         }
 1400:         if ($canhost) {
 1401:             if ($remotesessions->{'version'} ne '') {
 1402:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1403:                 if ($reqmajor ne '' && $reqminor ne '') {
 1404:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1405:                         my $major = $1;
 1406:                         my $minor = $2;
 1407:                         if (($major < $reqmajor ) ||
 1408:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1409:                             $canhost = 0;
 1410:                         }
 1411:                     } else {
 1412:                         $canhost = 0;
 1413:                     }
 1414:                 }
 1415:             }
 1416:         }
 1417:     }
 1418:     if ($canhost) {
 1419:         if (ref($hostedsessions) eq 'HASH') {
 1420:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1421:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1422:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1423:                 if (($uint_dom ne '') && 
 1424:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1425:                     $canhost = 0;
 1426:                 } else {
 1427:                     $canhost = 1;
 1428:                 }
 1429:             }
 1430:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1431:                 if (($uint_dom ne '') && 
 1432:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1433:                     $canhost = 1;
 1434:                 } else {
 1435:                     $canhost = 0;
 1436:                 }
 1437:             }
 1438:         }
 1439:     }
 1440:     return $canhost;
 1441: }
 1442: 
 1443: sub spare_can_host {
 1444:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1445:     my $canhost=1;
 1446:     my $try_server_hostname = &hostname($try_server);
 1447:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1448:     my $serverhomedom = &host_domain($serverhomeID);
 1449:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1450:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1451:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1452:             $canhost = 0;
 1453:         }
 1454:     }
 1455:     if ($canhost) {
 1456:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1457:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1458:                 unless (&shared_institution($udom,$try_server)) {
 1459:                     $canhost = 0;
 1460:                 }
 1461:             }
 1462:         }
 1463:     }
 1464:     if (($canhost) && ($uint_dom)) {
 1465:         my @intdoms;
 1466:         my $internet_names = &get_internet_names($try_server);
 1467:         if (ref($internet_names) eq 'ARRAY') {
 1468:             @intdoms = @{$internet_names};
 1469:         }
 1470:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1471:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1472:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1473:                                          $remotesessions,
 1474:                                          $defdomdefaults{'hostedsessions'});
 1475:         }
 1476:     }
 1477:     return $canhost;
 1478: }
 1479: 
 1480: sub this_host_spares {
 1481:     my ($dom) = @_;
 1482:     my ($dom_in_use,$lonhost_in_use,$result);
 1483:     my @hosts = &current_machine_ids();
 1484:     foreach my $lonhost (@hosts) {
 1485:         if (&host_domain($lonhost) eq $dom) {
 1486:             $dom_in_use = $dom;
 1487:             $lonhost_in_use = $lonhost;
 1488:             last;
 1489:         }
 1490:     }
 1491:     if ($dom_in_use ne '') {
 1492:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1493:     }
 1494:     if (ref($result) ne 'HASH') {
 1495:         $lonhost_in_use = $perlvar{'lonHostID'};
 1496:         $dom_in_use = &host_domain($lonhost_in_use);
 1497:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1498:         if (ref($result) ne 'HASH') {
 1499:             $result = \%spareid;
 1500:         }
 1501:     }
 1502:     return $result;
 1503: }
 1504: 
 1505: sub spares_for_offload  {
 1506:     my ($dom_in_use,$lonhost_in_use) = @_;
 1507:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1508:     if (defined($cached)) {
 1509:         return $result;
 1510:     } else {
 1511:         my $cachetime = 60*60*24;
 1512:         my %domconfig =
 1513:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1514:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1515:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1516:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1517:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1518:                 }
 1519:             }
 1520:         }
 1521:     }
 1522:     return;
 1523: }
 1524: 
 1525: sub get_lonbalancer_config {
 1526:     my ($servers) = @_;
 1527:     my ($currbalancer,$currtargets);
 1528:     if (ref($servers) eq 'HASH') {
 1529:         foreach my $server (keys(%{$servers})) {
 1530:             my %what = (
 1531:                          spareid => 1,
 1532:                          perlvar => 1,
 1533:                        );
 1534:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1535:             if ($result eq 'ok') {
 1536:                 if (ref($returnhash) eq 'HASH') {
 1537:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1538:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1539:                             $currbalancer = $server;
 1540:                             $currtargets = {};
 1541:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1542:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1543:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1544:                                 }
 1545:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1546:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1547:                                 }
 1548:                             }
 1549:                             last;
 1550:                         }
 1551:                     }
 1552:                 }
 1553:             }
 1554:         }
 1555:     }
 1556:     return ($currbalancer,$currtargets);
 1557: }
 1558: 
 1559: sub check_loadbalancing {
 1560:     my ($uname,$udom,$caller) = @_;
 1561:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1562:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1563:     my $lonhost = $perlvar{'lonHostID'};
 1564:     my @hosts = &current_machine_ids();
 1565:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1566:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1567:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1568:     my $serverhomedom = &host_domain($lonhost);
 1569:     my $domneedscache;
 1570:     my $cachetime = 60*60*24;
 1571: 
 1572:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1573:         $dom_in_use = $udom;
 1574:         $homeintdom = 1;
 1575:     } else {
 1576:         $dom_in_use = $serverhomedom;
 1577:     }
 1578:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1579:     unless (defined($cached)) {
 1580:         my %domconfig =
 1581:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1582:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1583:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1584:         } else {
 1585:             $domneedscache = $dom_in_use;
 1586:         }
 1587:     }
 1588:     if (ref($result) eq 'HASH') {
 1589:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1590:             &check_balancer_result($result,@hosts);
 1591:         if ($is_balancer) {
 1592:             if (ref($currrules) eq 'HASH') {
 1593:                 if ($homeintdom) {
 1594:                     if ($uname ne '') {
 1595:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1596:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1597:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1598:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1599:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1600:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1601:                             }
 1602:                         }
 1603:                         if ($rule_in_effect eq '') {
 1604:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1605:                             if ($userenv{'inststatus'} ne '') {
 1606:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1607:                                 my ($othertitle,$usertypes,$types) =
 1608:                                     &Apache::loncommon::sorted_inst_types($udom);
 1609:                                 if (ref($types) eq 'ARRAY') {
 1610:                                     foreach my $type (@{$types}) {
 1611:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1612:                                             if (exists($currrules->{$type})) {
 1613:                                                 $rule_in_effect = $currrules->{$type};
 1614:                                             }
 1615:                                         }
 1616:                                     }
 1617:                                 }
 1618:                             } else {
 1619:                                 if (exists($currrules->{'default'})) {
 1620:                                     $rule_in_effect = $currrules->{'default'};
 1621:                                 }
 1622:                             }
 1623:                         }
 1624:                     } else {
 1625:                         if (exists($currrules->{'default'})) {
 1626:                             $rule_in_effect = $currrules->{'default'};
 1627:                         }
 1628:                     }
 1629:                 } else {
 1630:                     if ($currrules->{'_LC_external'} ne '') {
 1631:                         $rule_in_effect = $currrules->{'_LC_external'};
 1632:                     }
 1633:                 }
 1634:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1635:                                                        $uname,$udom);
 1636:             }
 1637:         }
 1638:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1639:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1640:         unless (defined($cached)) {
 1641:             my %domconfig =
 1642:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1643:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1644:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1645:             } else {
 1646:                 $domneedscache = $serverhomedom;
 1647:             }
 1648:         }
 1649:         if (ref($result) eq 'HASH') {
 1650:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1651:                 &check_balancer_result($result,@hosts);
 1652:             if ($is_balancer) {
 1653:                 if (ref($currrules) eq 'HASH') {
 1654:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1655:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1656:                     }
 1657:                 }
 1658:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1659:                                                        $uname,$udom);
 1660:             }
 1661:         } else {
 1662:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1663:                 $is_balancer = 1;
 1664:                 $offloadto = &this_host_spares($dom_in_use);
 1665:             }
 1666:             unless (defined($cached)) {
 1667:                 $domneedscache = $serverhomedom;
 1668:             }
 1669:         }
 1670:     } else {
 1671:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1672:             $is_balancer = 1;
 1673:             $offloadto = &this_host_spares($dom_in_use);
 1674:         }
 1675:         unless (defined($cached)) {
 1676:             $domneedscache = $serverhomedom;
 1677:         }
 1678:     }
 1679:     if ($domneedscache) {
 1680:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1681:     }
 1682:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1683:         my $lowest_load = 30000;
 1684:         if (ref($offloadto) eq 'HASH') {
 1685:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1686:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1687:                     ($otherserver,$lowest_load) =
 1688:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1689:                 }
 1690:             }
 1691:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1692: 
 1693:             if (!$found_server) {
 1694:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1695:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1696:                         ($otherserver,$lowest_load) =
 1697:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1698:                     }
 1699:                 }
 1700:             }
 1701:         } elsif (ref($offloadto) eq 'ARRAY') {
 1702:             if (@{$offloadto} == 1) {
 1703:                 $otherserver = $offloadto->[0];
 1704:             } elsif (@{$offloadto} > 1) {
 1705:                 foreach my $try_server (@{$offloadto}) {
 1706:                     ($otherserver,$lowest_load) =
 1707:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1708:                 }
 1709:             }
 1710:         }
 1711:         unless ($caller eq 'login') {
 1712:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1713:                 $is_balancer = 0;
 1714:                 if ($uname ne '' && $udom ne '') {
 1715:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1716:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1717:                                  'user.loadbalcheck.time' => time});
 1718:                     }
 1719:                 }
 1720:             }
 1721:         }
 1722:     }
 1723:     if (($is_balancer) && (!$homeintdom)) {
 1724:         undef($setcookie);
 1725:     }
 1726:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1727: }
 1728: 
 1729: sub check_balancer_result {
 1730:     my ($result,@hosts) = @_;
 1731:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1732:     if (ref($result) eq 'HASH') {
 1733:         if ($result->{'lonhost'} ne '') {
 1734:             my $currbalancer = $result->{'lonhost'};
 1735:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1736:                 $is_balancer = 1;
 1737:                 $currtargets = $result->{'targets'};
 1738:                 $currrules = $result->{'rules'};
 1739:             }
 1740:             $dom_balancers = $currbalancer;
 1741:         } else {
 1742:             if (keys(%{$result})) {
 1743:                 foreach my $key (keys(%{$result})) {
 1744:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1745:                         (ref($result->{$key}) eq 'HASH')) {
 1746:                         $is_balancer = 1;
 1747:                         $currrules = $result->{$key}{'rules'};
 1748:                         $currtargets = $result->{$key}{'targets'};
 1749:                         $setcookie = $result->{$key}{'cookie'};
 1750:                         last;
 1751:                     }
 1752:                 }
 1753:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1754:             }
 1755:         }
 1756:     }
 1757:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1758: }
 1759: 
 1760: sub get_loadbalancer_targets {
 1761:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1762:     my $offloadto;
 1763:     if ($rule_in_effect eq 'none') {
 1764:         return [$perlvar{'lonHostID'}];
 1765:     } elsif ($rule_in_effect eq '') {
 1766:         $offloadto = $currtargets;
 1767:     } else {
 1768:         if ($rule_in_effect eq 'homeserver') {
 1769:             my $homeserver = &homeserver($uname,$udom);
 1770:             if ($homeserver ne 'no_host') {
 1771:                 $offloadto = [$homeserver];
 1772:             }
 1773:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1774:             my %domconfig =
 1775:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1776:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1777:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1778:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1779:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1780:                     }
 1781:                 }
 1782:             } else {
 1783:                 my %servers = &internet_dom_servers($udom);
 1784:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1785:                 if (&hostname($remotebalancer) ne '') {
 1786:                     $offloadto = [$remotebalancer];
 1787:                 }
 1788:             }
 1789:         } elsif (&hostname($rule_in_effect) ne '') {
 1790:             $offloadto = [$rule_in_effect];
 1791:         }
 1792:     }
 1793:     return $offloadto;
 1794: }
 1795: 
 1796: sub internet_dom_servers {
 1797:     my ($dom) = @_;
 1798:     my (%uniqservers,%servers);
 1799:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1800:     my @machinedoms = &machine_domains($primaryserver);
 1801:     foreach my $mdom (@machinedoms) {
 1802:         my %currservers = %servers;
 1803:         my %server = &get_servers($mdom);
 1804:         %servers = (%currservers,%server);
 1805:     }
 1806:     my %by_hostname;
 1807:     foreach my $id (keys(%servers)) {
 1808:         push(@{$by_hostname{$servers{$id}}},$id);
 1809:     }
 1810:     foreach my $hostname (sort(keys(%by_hostname))) {
 1811:         if (@{$by_hostname{$hostname}} > 1) {
 1812:             my $match = 0;
 1813:             foreach my $id (@{$by_hostname{$hostname}}) {
 1814:                 if (&host_domain($id) eq $dom) {
 1815:                     $uniqservers{$id} = $hostname;
 1816:                     $match = 1;
 1817:                 }
 1818:             }
 1819:             unless ($match) {
 1820:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1821:             }
 1822:         } else {
 1823:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1824:         }
 1825:     }
 1826:     return %uniqservers;
 1827: }
 1828: 
 1829: sub trusted_domains {
 1830:     my ($cmdtype,$calldom) = @_;
 1831:     my ($trusted,$untrusted);
 1832:     if (&domain($calldom) eq '') {
 1833:         return ($trusted,$untrusted);
 1834:     }
 1835:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1836:         return ($trusted,$untrusted);
 1837:     }
 1838:     my $callprimary = &domain($calldom,'primary');
 1839:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1840:     if ($intcalldom eq '') {
 1841:         return ($trusted,$untrusted);
 1842:     }
 1843: 
 1844:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1845:     unless (defined($cached)) {
 1846:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1847:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1848:         $trustconfig = $domconfig{'trust'};
 1849:     }
 1850:     if (ref($trustconfig)) {
 1851:         my (%possexc,%possinc,@allexc,@allinc); 
 1852:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1853:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1854:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1855:             }
 1856:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1857:                 $possinc{$intcalldom} = 1;
 1858:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1859:             }
 1860:         }
 1861:         if (keys(%possexc)) {
 1862:             if (keys(%possinc)) {
 1863:                 foreach my $key (sort(keys(%possexc))) {
 1864:                     next if ($key eq $intcalldom);
 1865:                     unless ($possinc{$key}) {
 1866:                         push(@allexc,$key);
 1867:                     }
 1868:                 }
 1869:             } else {
 1870:                 @allexc = sort(keys(%possexc));
 1871:             }
 1872:         }
 1873:         if (keys(%possinc)) {
 1874:             $possinc{$intcalldom} = 1;
 1875:             @allinc = sort(keys(%possinc));
 1876:         }
 1877:         if ((@allexc > 0) || (@allinc > 0)) {
 1878:             my %doms_by_intdom;
 1879:             my %allintdoms = &all_host_intdom();
 1880:             my %alldoms = &all_host_domain();
 1881:             foreach my $key (%allintdoms) {
 1882:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1883:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1884:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1885:                     }
 1886:                 } else {
 1887:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1888:                 }
 1889:             }
 1890:             foreach my $exc (@allexc) {
 1891:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1892:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1893:                 }
 1894:             }
 1895:             foreach my $inc (@allinc) {
 1896:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1897:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1898:                 }
 1899:             }
 1900:         }
 1901:     }
 1902:     return ($trusted,$untrusted);
 1903: }
 1904: 
 1905: sub will_trust {
 1906:     my ($cmdtype,$domain,$possdom) = @_;
 1907:     return 1 if ($domain eq $possdom);
 1908:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1909:     my $willtrust; 
 1910:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1911:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1912:             $willtrust = 1;
 1913:         }
 1914:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1915:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1916:             $willtrust = 1;
 1917:         }
 1918:     } else {
 1919:         $willtrust = 1;
 1920:     }
 1921:     return $willtrust;
 1922: }
 1923: 
 1924: # ---------------------- Find the homebase for a user from domain's lib servers
 1925: 
 1926: my %homecache;
 1927: sub homeserver {
 1928:     my ($uname,$udom,$ignoreBadCache)=@_;
 1929:     my $index="$uname:$udom";
 1930: 
 1931:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1932: 
 1933:     my %servers = &get_servers($udom,'library');
 1934:     foreach my $tryserver (keys(%servers)) {
 1935:         next if ($ignoreBadCache ne 'true' && 
 1936: 		 exists($badServerCache{$tryserver}));
 1937: 
 1938: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1939: 	if ($answer eq 'found') {
 1940: 	    delete($badServerCache{$tryserver}); 
 1941: 	    return $homecache{$index}=$tryserver;
 1942: 	} elsif ($answer eq 'no_host') {
 1943: 	    $badServerCache{$tryserver}=1;
 1944: 	}
 1945:     }    
 1946:     return 'no_host';
 1947: }
 1948: 
 1949: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1950: 
 1951: sub idget {
 1952:     my ($udom,$idsref,$namespace)=@_;
 1953:     my %returnhash=();
 1954:     my @ids=(); 
 1955:     if (ref($idsref) eq 'ARRAY') {
 1956:         @ids = @{$idsref};
 1957:     } else {
 1958:         return %returnhash; 
 1959:     }
 1960:     if ($namespace eq '') {
 1961:         $namespace = 'ids';
 1962:     }
 1963:     
 1964:     my %servers = &get_servers($udom,'library');
 1965:     foreach my $tryserver (keys(%servers)) {
 1966: 	my $idlist=join('&', map { &escape($_); } @ids);
 1967: 	if ($namespace eq 'ids') {
 1968: 	    $idlist=~tr/A-Z/a-z/;
 1969: 	}
 1970: 	my $reply;
 1971: 	if ($namespace eq 'ids') {
 1972: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1973: 	} else {
 1974: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1975: 	}
 1976: 	my @answer=();
 1977: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1978: 	    @answer=split(/\&/,$reply);
 1979: 	}                    ;
 1980: 	my $i;
 1981: 	for ($i=0;$i<=$#ids;$i++) {
 1982: 	    if ($answer[$i]) {
 1983: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1984: 	    }
 1985: 	}
 1986:     }
 1987:     return %returnhash;
 1988: }
 1989: 
 1990: # ------------------------------------- Find the IDs behind a list of usernames
 1991: 
 1992: sub idrget {
 1993:     my ($udom,@unames)=@_;
 1994:     my %returnhash=();
 1995:     foreach my $uname (@unames) {
 1996:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1997:     }
 1998:     return %returnhash;
 1999: }
 2000: 
 2001: # Store away a list of names and associated student/employee IDs or clicker IDs
 2002: 
 2003: sub idput {
 2004:     my ($udom,$idsref,$uhom,$namespace)=@_;
 2005:     my %servers=();
 2006:     my %ids=();
 2007:     my %byid = ();
 2008:     if (ref($idsref) eq 'HASH') {
 2009:         %ids=%{$idsref};
 2010:     }
 2011:     if ($namespace eq '') {
 2012:         $namespace = 'ids'; 
 2013:     }
 2014:     foreach my $uname (keys(%ids)) {
 2015: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2016:         if ($uhom eq '') {
 2017:             $uhom=&homeserver($uname,$udom);
 2018:         }
 2019:         if ($uhom ne 'no_host') {
 2020:             my $esc_unam=&escape($uname);
 2021:             if ($namespace eq 'ids') {
 2022:                 my $id=&escape($ids{$uname});
 2023:                 $id=~tr/A-Z/a-z/;
 2024:                 my $esc_unam=&escape($uname);
 2025:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2026:             } else {
 2027:                 my @currids = split(/,/,$ids{$uname});
 2028:                 foreach my $id (@currids) {
 2029:                     $byid{$uhom}{$id} .= $uname.',';
 2030:                 }
 2031:             }
 2032:         }
 2033:     }
 2034:     if ($namespace eq 'clickers') {
 2035:         foreach my $server (keys(%byid)) {
 2036:             if (ref($byid{$server}) eq 'HASH') {
 2037:                 foreach my $id (keys(%{$byid{$server}})) {
 2038:                     $byid{$server} =~ s/,$//;
 2039:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2040:                 }
 2041:             }
 2042:         }
 2043:     }
 2044:     foreach my $server (keys(%servers)) {
 2045:         $servers{$server} =~ s/\&$//;
 2046:         if ($namespace eq 'ids') {     
 2047:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2048:         } else {
 2049:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2050:         }
 2051:     }
 2052: }
 2053: 
 2054: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2055: 
 2056: sub iddel {
 2057:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2058:     my %result=();
 2059:     my %ids=();
 2060:     my %byid = ();
 2061:     if (ref($idshashref) eq 'HASH') {
 2062:         %ids=%{$idshashref};
 2063:     } else {
 2064:         return %result;
 2065:     }
 2066:     if ($namespace eq '') {
 2067:         $namespace = 'ids';
 2068:     }
 2069:     my %servers=();
 2070:     while (my ($id,$unamestr) = each(%ids)) {
 2071:         if ($namespace eq 'ids') {
 2072:             my $uhom = $uhome;
 2073:             if ($uhom eq '') { 
 2074:                 $uhom=&homeserver($unamestr,$udom);
 2075:             }
 2076:             if ($uhom ne 'no_host') {
 2077:                 $servers{$uhom}.='&'.&escape($id);
 2078:             }
 2079:          } else {
 2080:             my @curritems = split(/,/,$ids{$id});
 2081:             foreach my $uname (@curritems) {
 2082:                 my $uhom = $uhome;
 2083:                 if ($uhom eq '') {
 2084:                     $uhom=&homeserver($uname,$udom);
 2085:                 }
 2086:                 if ($uhom ne 'no_host') { 
 2087:                     $byid{$uhom}{$id} .= $uname.',';
 2088:                 }
 2089:             }
 2090:         }
 2091:     }
 2092:     if ($namespace eq 'clickers') {
 2093:         foreach my $server (keys(%byid)) {
 2094:             if (ref($byid{$server}) eq 'HASH') {
 2095:                 foreach my $id (keys(%{$byid{$server}})) {
 2096:                     $byid{$server}{$id} =~ s/,$//;
 2097:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2098:                 }
 2099:             }
 2100:         }
 2101:     }
 2102:     foreach my $server (keys(%servers)) {
 2103:         $servers{$server} =~ s/\&$//;
 2104:         if ($namespace eq 'ids') {
 2105:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2106:         } elsif ($namespace eq 'clickers') {
 2107:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2108:         }
 2109:     }
 2110:     return %result;
 2111: }
 2112: 
 2113: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2114: 
 2115: sub updateclickers {
 2116:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2117:     my %clickers;
 2118:     if (ref($idshashref) eq 'HASH') {
 2119:         %clickers=%{$idshashref};
 2120:     } else {
 2121:         return;
 2122:     }
 2123:     my $items='';
 2124:     foreach my $item (keys(%clickers)) {
 2125:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2126:     }
 2127:     $items=~s/\&$//;
 2128:     my $request = "updateclickers:$udom:$action:$items";
 2129:     if ($critical) {
 2130:         return &critical($request,$uhome);
 2131:     } else {
 2132:         return &reply($request,$uhome);
 2133:     }
 2134: }
 2135: 
 2136: # ------------------------------dump from db file owned by domainconfig user
 2137: sub dump_dom {
 2138:     my ($namespace, $udom, $regexp) = @_;
 2139: 
 2140:     $udom ||= $env{'user.domain'};
 2141: 
 2142:     return () unless $udom;
 2143: 
 2144:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2145: }
 2146: 
 2147: # ------------------------------------------ get items from domain db files   
 2148: 
 2149: sub get_dom {
 2150:     my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
 2151:     return if ($udom eq 'public');
 2152:     my $items='';
 2153:     foreach my $item (@$storearr) {
 2154:         $items.=&escape($item).'&';
 2155:     }
 2156:     $items=~s/\&$//;
 2157:     if (!$udom) {
 2158:         $udom=$env{'user.domain'};
 2159:         return if ($udom eq 'public');
 2160:         if (defined(&domain($udom,'primary'))) {
 2161:             $uhome=&domain($udom,'primary');
 2162:         } else {
 2163:             undef($uhome);
 2164:         }
 2165:     } else {
 2166:         if (!$uhome) {
 2167:             if (defined(&domain($udom,'primary'))) {
 2168:                 $uhome=&domain($udom,'primary');
 2169:             }
 2170:         }
 2171:     }
 2172:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2173:         my $rep;
 2174:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2175:             # domain information is hosted on this machine
 2176:             $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
 2177:         } else {
 2178:             if ($encrypt) {
 2179:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2180:             } else {
 2181:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2182:             }
 2183:         }
 2184:         my %returnhash;
 2185:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2186:             return %returnhash;
 2187:         }
 2188:         my @pairs=split(/\&/,$rep);
 2189:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2190:             return @pairs;
 2191:         }
 2192:         my $i=0;
 2193:         foreach my $item (@$storearr) {
 2194:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2195:             $i++;
 2196:         }
 2197:         return %returnhash;
 2198:     } else {
 2199:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2200:     }
 2201: }
 2202: 
 2203: # -------------------------------------------- put items in domain db files 
 2204: 
 2205: sub put_dom {
 2206:     my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
 2207:     if (!$udom) {
 2208:         $udom=$env{'user.domain'};
 2209:         if (defined(&domain($udom,'primary'))) {
 2210:             $uhome=&domain($udom,'primary');
 2211:         } else {
 2212:             undef($uhome);
 2213:         }
 2214:     } else {
 2215:         if (!$uhome) {
 2216:             if (defined(&domain($udom,'primary'))) {
 2217:                 $uhome=&domain($udom,'primary');
 2218:             }
 2219:         }
 2220:     } 
 2221:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2222:         my $items='';
 2223:         foreach my $item (keys(%$storehash)) {
 2224:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2225:         }
 2226:         $items=~s/\&$//;
 2227:         if ($encrypt) {
 2228:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2229:         } else {
 2230:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2231:         }
 2232:     } else {
 2233:         &logthis("put_dom failed - no homeserver and/or domain");
 2234:     }
 2235: }
 2236: 
 2237: # --------------------- newput for items in db file owned by domainconfig user
 2238: sub newput_dom {
 2239:     my ($namespace,$storehash,$udom) = @_;
 2240:     my $result;
 2241:     if (!$udom) {
 2242:         $udom=$env{'user.domain'};
 2243:     }
 2244:     if ($udom) {
 2245:         my $uname = &get_domainconfiguser($udom);
 2246:         $result = &newput($namespace,$storehash,$udom,$uname);
 2247:     }
 2248:     return $result;
 2249: }
 2250: 
 2251: # --------------------- delete for items in db file owned by domainconfig user
 2252: sub del_dom {
 2253:     my ($namespace,$storearr,$udom)=@_;
 2254:     if (ref($storearr) eq 'ARRAY') {
 2255:         if (!$udom) {
 2256:             $udom=$env{'user.domain'};
 2257:         }
 2258:         if ($udom) {
 2259:             my $uname = &get_domainconfiguser($udom); 
 2260:             return &del($namespace,$storearr,$udom,$uname);
 2261:         }
 2262:     }
 2263: }
 2264: 
 2265: # ----------------------------------construct domainconfig user for a domain 
 2266: sub get_domainconfiguser {
 2267:     my ($udom) = @_;
 2268:     return $udom.'-domainconfig';
 2269: }
 2270: 
 2271: sub retrieve_inst_usertypes {
 2272:     my ($udom) = @_;
 2273:     my (%returnhash,@order);
 2274:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2275:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2276:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2277:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2278:     } else {
 2279:         if (defined(&domain($udom,'primary'))) {
 2280:             my $uhome=&domain($udom,'primary');
 2281:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2282:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2283:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2284:                 return (\%returnhash,\@order);
 2285:             }
 2286:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2287:             my @pairs=split(/\&/,$hashitems);
 2288:             foreach my $item (@pairs) {
 2289:                 my ($key,$value)=split(/=/,$item,2);
 2290:                 $key = &unescape($key);
 2291:                 next if ($key =~ /^error: 2 /);
 2292:                 $returnhash{$key}=&thaw_unescape($value);
 2293:             }
 2294:             my @esc_order = split(/\&/,$orderitems);
 2295:             foreach my $item (@esc_order) {
 2296:                 push(@order,&unescape($item));
 2297:             }
 2298:         } else {
 2299:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2300:         }
 2301:         return (\%returnhash,\@order);
 2302:     }
 2303: }
 2304: 
 2305: sub is_domainimage {
 2306:     my ($url) = @_;
 2307:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
 2308:         if (&domain($1) ne '') {
 2309:             return '1';
 2310:         }
 2311:     }
 2312:     return;
 2313: }
 2314: 
 2315: sub inst_directory_query {
 2316:     my ($srch) = @_;
 2317:     my $udom = $srch->{'srchdomain'};
 2318:     my %results;
 2319:     my $homeserver = &domain($udom,'primary');
 2320:     my $outcome;
 2321:     if ($homeserver ne '') {
 2322:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2323:             if ($srch->{'srchby'} eq 'email') {
 2324:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2325:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2326:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2327:                     (($major == 2) && ($minor < 12))) {
 2328:                     return;
 2329:                 }
 2330:             }
 2331:         }
 2332: 	my $queryid=&reply("querysend:instdirsearch:".
 2333: 			   &escape($srch->{'srchby'}).':'.
 2334: 			   &escape($srch->{'srchterm'}).':'.
 2335: 			   &escape($srch->{'srchtype'}),$homeserver);
 2336: 	my $host=&hostname($homeserver);
 2337: 	if ($queryid !~/^\Q$host\E\_/) {
 2338: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2339: 	    return;
 2340: 	}
 2341: 	my $response = &get_query_reply($queryid);
 2342: 	my $maxtries = 5;
 2343: 	my $tries = 1;
 2344: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2345: 	    $response = &get_query_reply($queryid);
 2346: 	    $tries ++;
 2347: 	}
 2348: 
 2349:         if (!&error($response) && $response ne 'refused') {
 2350:             if ($response eq 'unavailable') {
 2351:                 $outcome = $response;
 2352:             } else {
 2353:                 $outcome = 'ok';
 2354:                 my @matches = split(/\n/,$response);
 2355:                 foreach my $match (@matches) {
 2356:                     my ($key,$value) = split(/=/,$match);
 2357:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2358:                 }
 2359:             }
 2360:         }
 2361:     }
 2362:     return ($outcome,%results);
 2363: }
 2364: 
 2365: sub usersearch {
 2366:     my ($srch) = @_;
 2367:     my $dom = $srch->{'srchdomain'};
 2368:     my %results;
 2369:     my %libserv = &all_library();
 2370:     my $query = 'usersearch';
 2371:     foreach my $tryserver (keys(%libserv)) {
 2372:         if (&host_domain($tryserver) eq $dom) {
 2373:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2374:                 if ($srch->{'srchby'} eq 'email') {
 2375:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2376:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2377:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2378:                              (($major == 2) && ($minor < 12)));
 2379:                 }
 2380:             }
 2381:             my $host=&hostname($tryserver);
 2382:             my $queryid=
 2383:                 &reply("querysend:".&escape($query).':'.
 2384:                        &escape($srch->{'srchby'}).':'.
 2385:                        &escape($srch->{'srchtype'}).':'.
 2386:                        &escape($srch->{'srchterm'}),$tryserver);
 2387:             if ($queryid !~/^\Q$host\E\_/) {
 2388:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2389:                 next;
 2390:             }
 2391:             my $reply = &get_query_reply($queryid);
 2392:             my $maxtries = 1;
 2393:             my $tries = 1;
 2394:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2395:                 $reply = &get_query_reply($queryid);
 2396:                 $tries ++;
 2397:             }
 2398:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2399:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2400:             } else {
 2401:                 my @matches;
 2402:                 if ($reply =~ /\n/) {
 2403:                     @matches = split(/\n/,$reply);
 2404:                 } else {
 2405:                     @matches = split(/\&/,$reply);
 2406:                 }
 2407:                 foreach my $match (@matches) {
 2408:                     my ($uname,$udom,%userhash);
 2409:                     foreach my $entry (split(/:/,$match)) {
 2410:                         my ($key,$value) =
 2411:                             map {&unescape($_);} split(/=/,$entry);
 2412:                         $userhash{$key} = $value;
 2413:                         if ($key eq 'username') {
 2414:                             $uname = $value;
 2415:                         } elsif ($key eq 'domain') {
 2416:                             $udom = $value;
 2417:                         }
 2418:                     }
 2419:                     $results{$uname.':'.$udom} = \%userhash;
 2420:                 }
 2421:             }
 2422:         }
 2423:     }
 2424:     return %results;
 2425: }
 2426: 
 2427: sub get_instuser {
 2428:     my ($udom,$uname,$id) = @_;
 2429:     my $homeserver = &domain($udom,'primary');
 2430:     my ($outcome,%results);
 2431:     if ($homeserver ne '') {
 2432:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2433:                            &escape($id).':'.&escape($udom),$homeserver);
 2434:         my $host=&hostname($homeserver);
 2435:         if ($queryid !~/^\Q$host\E\_/) {
 2436:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2437:             return;
 2438:         }
 2439:         my $response = &get_query_reply($queryid);
 2440:         my $maxtries = 5;
 2441:         my $tries = 1;
 2442:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2443:             $response = &get_query_reply($queryid);
 2444:             $tries ++;
 2445:         }
 2446:         if (!&error($response) && $response ne 'refused') {
 2447:             if ($response eq 'unavailable') {
 2448:                 $outcome = $response;
 2449:             } else {
 2450:                 $outcome = 'ok';
 2451:                 my @matches = split(/\n/,$response);
 2452:                 foreach my $match (@matches) {
 2453:                     my ($key,$value) = split(/=/,$match);
 2454:                     $results{&unescape($key)} = &thaw_unescape($value);
 2455:                 }
 2456:             }
 2457:         }
 2458:     }
 2459:     my %userinfo;
 2460:     if (ref($results{$uname}) eq 'HASH') {
 2461:         %userinfo = %{$results{$uname}};
 2462:     } 
 2463:     return ($outcome,%userinfo);
 2464: }
 2465: 
 2466: sub get_multiple_instusers {
 2467:     my ($udom,$users,$caller) = @_;
 2468:     my ($outcome,$results);
 2469:     if (ref($users) eq 'HASH') {
 2470:         my $count = keys(%{$users}); 
 2471:         my $requested = &freeze_escape($users);
 2472:         my $homeserver = &domain($udom,'primary');
 2473:         if ($homeserver ne '') {
 2474:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2475:             my $host=&hostname($homeserver);
 2476:             if ($queryid !~/^\Q$host\E\_/) {
 2477:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2478:                          ' for host: '.$homeserver.'in domain '.$udom);
 2479:                 return ($outcome,$results);
 2480:             }
 2481:             my $response = &get_query_reply($queryid);
 2482:             my $maxtries = 5;
 2483:             if ($count > 100) {
 2484:                 $maxtries = 1+int($count/20);
 2485:             }
 2486:             my $tries = 1;
 2487:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2488:                 $response = &get_query_reply($queryid);
 2489:                 $tries ++;
 2490:             }
 2491:             if ($response eq '') {
 2492:                 $results = {};
 2493:                 foreach my $key (keys(%{$users})) {
 2494:                     my ($uname,$id);
 2495:                     if ($caller eq 'id') {
 2496:                         $id = $key;
 2497:                     } else {
 2498:                         $uname = $key;
 2499:                     }
 2500:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2501:                     $outcome = $resp;
 2502:                     if ($resp eq 'ok') {
 2503:                         %{$results} = (%{$results}, %info);
 2504:                     } else {
 2505:                         last;
 2506:                     }
 2507:                 }
 2508:             } elsif(!&error($response) && ($response ne 'refused')) {
 2509:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2510:                     $outcome = $response;
 2511:                 } else {
 2512:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2513:                     if ($outcome eq 'ok') {
 2514:                         $results = &thaw_unescape($userdata); 
 2515:                     }
 2516:                 }
 2517:             }
 2518:         }
 2519:     }
 2520:     return ($outcome,$results);
 2521: }
 2522: 
 2523: sub inst_rulecheck {
 2524:     my ($udom,$uname,$id,$item,$rules) = @_;
 2525:     my %returnhash;
 2526:     if ($udom ne '') {
 2527:         if (ref($rules) eq 'ARRAY') {
 2528:             @{$rules} = map {&escape($_);} (@{$rules});
 2529:             my $rulestr = join(':',@{$rules});
 2530:             my $homeserver=&domain($udom,'primary');
 2531:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2532:                 my $response;
 2533:                 if ($item eq 'username') {                
 2534:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2535:                                               ':'.&escape($uname).':'.$rulestr,
 2536:                                               $homeserver));
 2537:                 } elsif ($item eq 'id') {
 2538:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2539:                                               ':'.&escape($id).':'.$rulestr,
 2540:                                               $homeserver));
 2541:                 } elsif ($item eq 'selfcreate') {
 2542:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2543:                                                &escape($udom).':'.&escape($uname).
 2544:                                               ':'.$rulestr,$homeserver));
 2545:                 }
 2546:                 if ($response ne 'refused') {
 2547:                     my @pairs=split(/\&/,$response);
 2548:                     foreach my $item (@pairs) {
 2549:                         my ($key,$value)=split(/=/,$item,2);
 2550:                         $key = &unescape($key);
 2551:                         next if ($key =~ /^error: 2 /);
 2552:                         $returnhash{$key}=&thaw_unescape($value);
 2553:                     }
 2554:                 }
 2555:             }
 2556:         }
 2557:     }
 2558:     return %returnhash;
 2559: }
 2560: 
 2561: sub inst_userrules {
 2562:     my ($udom,$check) = @_;
 2563:     my (%ruleshash,@ruleorder);
 2564:     if ($udom ne '') {
 2565:         my $homeserver=&domain($udom,'primary');
 2566:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2567:             my $response;
 2568:             if ($check eq 'id') {
 2569:                 $response=&reply('instidrules:'.&escape($udom),
 2570:                                  $homeserver);
 2571:             } elsif ($check eq 'email') {
 2572:                 $response=&reply('instemailrules:'.&escape($udom),
 2573:                                  $homeserver);
 2574:             } else {
 2575:                 $response=&reply('instuserrules:'.&escape($udom),
 2576:                                  $homeserver);
 2577:             }
 2578:             if (($response ne 'refused') && ($response ne 'error') && 
 2579:                 ($response ne 'unknown_cmd') && 
 2580:                 ($response ne 'no_such_host')) {
 2581:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2582:                 my @pairs=split(/\&/,$hashitems);
 2583:                 foreach my $item (@pairs) {
 2584:                     my ($key,$value)=split(/=/,$item,2);
 2585:                     $key = &unescape($key);
 2586:                     next if ($key =~ /^error: 2 /);
 2587:                     $ruleshash{$key}=&thaw_unescape($value);
 2588:                 }
 2589:                 my @esc_order = split(/\&/,$orderitems);
 2590:                 foreach my $item (@esc_order) {
 2591:                     push(@ruleorder,&unescape($item));
 2592:                 }
 2593:             }
 2594:         }
 2595:     }
 2596:     return (\%ruleshash,\@ruleorder);
 2597: }
 2598: 
 2599: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2600: 
 2601: sub get_domain_defaults {
 2602:     my ($domain,$ignore_cache) = @_;
 2603:     return if (($domain eq '') || ($domain eq 'public'));
 2604:     my $cachetime = 60*60*24;
 2605:     unless ($ignore_cache) {
 2606:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2607:         if (defined($cached)) {
 2608:             if (ref($result) eq 'HASH') {
 2609:                 return %{$result};
 2610:             }
 2611:         }
 2612:     }
 2613:     my %domdefaults;
 2614:     my %domconfig =
 2615:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2616:                                   'requestcourses','inststatus',
 2617:                                   'coursedefaults','usersessions',
 2618:                                   'requestauthor','selfenrollment',
 2619:                                   'coursecategories','ssl','autoenroll',
 2620:                                   'trust','helpsettings','wafproxy'],$domain);
 2621:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2622:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2623:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2624:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2625:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2626:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2627:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2628:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2629:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2630:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2631:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2632:     } else {
 2633:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2634:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2635:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2636:     }
 2637:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2638:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2639:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2640:         } else {
 2641:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2642:         }
 2643:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2644:         foreach my $item (@usertools) {
 2645:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2646:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2647:             }
 2648:         }
 2649:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2650:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2651:         }
 2652:     }
 2653:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2654:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2655:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2656:         }
 2657:     }
 2658:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2659:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2660:     }
 2661:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2662:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2663:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2664:         }
 2665:     }
 2666:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2667:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2668:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2669:         $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
 2670:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2671:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2672:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2673:         }
 2674:         foreach my $type (@coursetypes) {
 2675:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2676:                 unless ($type eq 'community') {
 2677:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2678:                 }
 2679:             }
 2680:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2681:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2682:             }
 2683:             if ($domdefaults{'postsubmit'} eq 'on') {
 2684:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2685:                     $domdefaults{$type.'postsubtimeout'} = 
 2686:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2687:                 }
 2688:             }
 2689:         }
 2690:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2691:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2692:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2693:                 if (@clonecodes) {
 2694:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2695:                 }
 2696:             }
 2697:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2698:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2699:         }
 2700:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2701:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2702:         } 
 2703:     }
 2704:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2705:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2706:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2707:         }
 2708:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2709:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2710:         }
 2711:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2712:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2713:         }
 2714:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2715:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2716:         }
 2717:     }
 2718:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2719:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2720:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2721:                             'approval','limit');
 2722:             foreach my $type (@coursetypes) {
 2723:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2724:                     my @mgrdc = ();
 2725:                     foreach my $item (@settings) {
 2726:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2727:                             push(@mgrdc,$item);
 2728:                         }
 2729:                     }
 2730:                     if (@mgrdc) {
 2731:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2732:                     }
 2733:                 }
 2734:             }
 2735:         }
 2736:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2737:             foreach my $type (@coursetypes) {
 2738:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2739:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2740:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2741:                     }
 2742:                 }
 2743:             }
 2744:         }
 2745:     }
 2746:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2747:         $domdefaults{'catauth'} = 'std';
 2748:         $domdefaults{'catunauth'} = 'std';
 2749:         if ($domconfig{'coursecategories'}{'auth'}) {
 2750:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2751:         }
 2752:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2753:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2754:         }
 2755:     }
 2756:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2757:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2758:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2759:         }
 2760:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2761:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2762:         }
 2763:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2764:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2765:         }
 2766:     }
 2767:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2768:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2769:         foreach my $prefix (@prefixes) {
 2770:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2771:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2772:             }
 2773:         }
 2774:     }
 2775:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2776:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2777:     }
 2778:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2779:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2780:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2781:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2782:         }
 2783:     }
 2784:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2785:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
 2786:             if ($domconfig{'wafproxy'}{$item}) {
 2787:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2788:             }
 2789:         }
 2790:     } 
 2791:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2792:     return %domdefaults;
 2793: }
 2794: 
 2795: sub get_dom_cats {
 2796:     my ($dom) = @_;
 2797:     return unless (&domain($dom));
 2798:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2799:     unless (defined($cached)) {
 2800:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2801:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2802:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2803:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2804:             } else {
 2805:                 $cats = {};
 2806:             }
 2807:         } else {
 2808:             $cats = {};
 2809:         }
 2810:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
 2811:     }
 2812:     return $cats;
 2813: }
 2814: 
 2815: sub get_dom_instcats {
 2816:     my ($dom) = @_;
 2817:     return unless (&domain($dom));
 2818:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2819:     unless (defined($cached)) {
 2820:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 2821:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 2822:         if ($totcodes > 0) {
 2823:             my $caller = 'global';
 2824:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 2825:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 2826:                 $instcats = {
 2827:                                 codes => \%codes,
 2828:                                 codetitles => \@codetitles,
 2829:                                 cat_titles => \%cat_titles,
 2830:                                 cat_order => \%cat_order,
 2831:                             };
 2832:                 &do_cache_new('instcats',$dom,$instcats,3600);
 2833:             }
 2834:         }
 2835:     }
 2836:     return $instcats;
 2837: }
 2838: 
 2839: sub retrieve_instcodes {
 2840:     my ($coursecodes,$dom) = @_;
 2841:     my $totcodes;
 2842:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 2843:     foreach my $course (keys(%courses)) {
 2844:         if (ref($courses{$course}) eq 'HASH') {
 2845:             if ($courses{$course}{'inst_code'} ne '') {
 2846:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 2847:                 $totcodes ++;
 2848:             }
 2849:         }
 2850:     }
 2851:     return $totcodes;
 2852: }
 2853: 
 2854: sub course_portal_url {
 2855:     my ($cnum,$cdom,$r) = @_;
 2856:     my $chome = &homeserver($cnum,$cdom);
 2857:     my $hostname = &hostname($chome);
 2858:     my $protocol = $protocol{$chome};
 2859:     $protocol = 'http' if ($protocol ne 'https');
 2860:     my %domdefaults = &get_domain_defaults($cdom);
 2861:     my $firsturl;
 2862:     if ($domdefaults{'portal_def'}) {
 2863:         $firsturl = $domdefaults{'portal_def'};
 2864:     } else {
 2865:         my $alias = &Apache::lonnet::use_proxy_alias($r,$chome);
 2866:         $hostname = $alias if ($alias ne '');
 2867:         $firsturl = $protocol.'://'.$hostname;
 2868:     }
 2869:     return $firsturl;
 2870: }
 2871: 
 2872: # --------------------------------------------- Get domain config for passwords
 2873: 
 2874: sub get_passwdconf {
 2875:     my ($dom) = @_;
 2876:     my (%passwdconf,$gotconf,$lookup);
 2877:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2878:     if (defined($cached)) {
 2879:         if (ref($result) eq 'HASH') {
 2880:             %passwdconf = %{$result};
 2881:             $gotconf = 1;
 2882:         }
 2883:     }
 2884:     unless ($gotconf) {
 2885:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2886:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2887:             %passwdconf = %{$domconfig{'passwords'}};
 2888:         }
 2889:         my $cachetime = 24*60*60;
 2890:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2891:     }
 2892:     return %passwdconf;
 2893: }
 2894: 
 2895: # --------------------------------------------------- Assign a key to a student
 2896: 
 2897: sub assign_access_key {
 2898: #
 2899: # a valid key looks like uname:udom#comments
 2900: # comments are being appended
 2901: #
 2902:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2903:     $kdom=
 2904:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2905:     $knum=
 2906:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2907:     $cdom=
 2908:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2909:     $cnum=
 2910:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2911:     $udom=$env{'user.name'} unless (defined($udom));
 2912:     $uname=$env{'user.domain'} unless (defined($uname));
 2913:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2914:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2915:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2916:                                                   # assigned to this person
 2917:                                                   # - this should not happen,
 2918:                                                   # unless something went wrong
 2919:                                                   # the first time around
 2920: # ready to assign
 2921:         $logentry=$1.'; '.$logentry;
 2922:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2923:                                                  $kdom,$knum) eq 'ok') {
 2924: # key now belongs to user
 2925: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2926:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2927:                 &appenv({'environment.'.$envkey => $ckey});
 2928:                 return 'ok';
 2929:             } else {
 2930:                 return 
 2931:   'error: Count not permanently assign key, will need to be re-entered later.';
 2932: 	    }
 2933:         } else {
 2934:             return 'error: Could not assign key, try again later.';
 2935:         }
 2936:     } elsif (!$existing{$ckey}) {
 2937: # the key does not exist
 2938: 	return 'error: The key does not exist';
 2939:     } else {
 2940: # the key is somebody else's
 2941: 	return 'error: The key is already in use';
 2942:     }
 2943: }
 2944: 
 2945: # ------------------------------------------ put an additional comment on a key
 2946: 
 2947: sub comment_access_key {
 2948: #
 2949: # a valid key looks like uname:udom#comments
 2950: # comments are being appended
 2951: #
 2952:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2953:     $cdom=
 2954:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2955:     $cnum=
 2956:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2957:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2958:     if ($existing{$ckey}) {
 2959:         $existing{$ckey}.='; '.$logentry;
 2960: # ready to assign
 2961:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2962:                                                  $cdom,$cnum) eq 'ok') {
 2963: 	    return 'ok';
 2964:         } else {
 2965: 	    return 'error: Count not store comment.';
 2966:         }
 2967:     } else {
 2968: # the key does not exist
 2969: 	return 'error: The key does not exist';
 2970:     }
 2971: }
 2972: 
 2973: # ------------------------------------------------------ Generate a set of keys
 2974: 
 2975: sub generate_access_keys {
 2976:     my ($number,$cdom,$cnum,$logentry)=@_;
 2977:     $cdom=
 2978:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2979:     $cnum=
 2980:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2981:     unless (&allowed('mky',$cdom)) { return 0; }
 2982:     unless (($cdom) && ($cnum)) { return 0; }
 2983:     if ($number>10000) { return 0; }
 2984:     sleep(2); # make sure don't get same seed twice
 2985:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2986:     my $total=0;
 2987:     for (my $i=1;$i<=$number;$i++) {
 2988:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2989:                   sprintf("%lx",int(100000*rand)).'-'.
 2990:                   sprintf("%lx",int(100000*rand));
 2991:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2992:        $newkey=~s/0/h/g; # and also 0 and O
 2993:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2994:        if ($existing{$newkey}) {
 2995:            $i--;
 2996:        } else {
 2997: 	  if (&put('accesskeys',
 2998:               { $newkey => '# generated '.localtime().
 2999:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 3000:                            '; '.$logentry },
 3001: 		   $cdom,$cnum) eq 'ok') {
 3002:               $total++;
 3003: 	  }
 3004:        }
 3005:     }
 3006:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3007:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3008:     return $total;
 3009: }
 3010: 
 3011: # ------------------------------------------------------- Validate an accesskey
 3012: 
 3013: sub validate_access_key {
 3014:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3015:     $cdom=
 3016:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3017:     $cnum=
 3018:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3019:     $udom=$env{'user.domain'} unless (defined($udom));
 3020:     $uname=$env{'user.name'} unless (defined($uname));
 3021:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3022:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3023: }
 3024: 
 3025: # ------------------------------------- Find the section of student in a course
 3026: sub devalidate_getsection_cache {
 3027:     my ($udom,$unam,$courseid)=@_;
 3028:     my $hashid="$udom:$unam:$courseid";
 3029:     &devalidate_cache_new('getsection',$hashid);
 3030: }
 3031: 
 3032: sub courseid_to_courseurl {
 3033:     my ($courseid) = @_;
 3034:     #already url style courseid
 3035:     return $courseid if ($courseid =~ m{^/});
 3036: 
 3037:     if (exists($env{'course.'.$courseid.'.num'})) {
 3038: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3039: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3040: 	return "/$cdom/$cnum";
 3041:     }
 3042: 
 3043:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 3044:     if (exists($courseinfo{'num'})) {
 3045: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3046:     }
 3047: 
 3048:     return undef;
 3049: }
 3050: 
 3051: sub getsection {
 3052:     my ($udom,$unam,$courseid)=@_;
 3053:     my $cachetime=1800;
 3054: 
 3055:     my $hashid="$udom:$unam:$courseid";
 3056:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3057:     if (defined($cached)) { return $result; }
 3058: 
 3059:     my %Pending; 
 3060:     my %Expired;
 3061:     #
 3062:     # Each role can either have not started yet (pending), be active, 
 3063:     #    or have expired.
 3064:     #
 3065:     # If there is an active role, we are done.
 3066:     #
 3067:     # If there is more than one role which has not started yet, 
 3068:     #     choose the one which will start sooner
 3069:     # If there is one role which has not started yet, return it.
 3070:     #
 3071:     # If there is more than one expired role, choose the one which ended last.
 3072:     # If there is a role which has expired, return it.
 3073:     #
 3074:     $courseid = &courseid_to_courseurl($courseid);
 3075:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3076:     foreach my $key (keys(%roleshash)) {
 3077:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3078:         my $section=$1;
 3079:         if ($key eq $courseid.'_st') { $section=''; }
 3080:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3081:         my $now=time;
 3082:         if (defined($end) && $end && ($now > $end)) {
 3083:             $Expired{$end}=$section;
 3084:             next;
 3085:         }
 3086:         if (defined($start) && $start && ($now < $start)) {
 3087:             $Pending{$start}=$section;
 3088:             next;
 3089:         }
 3090:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3091:     }
 3092:     #
 3093:     # Presumedly there will be few matching roles from the above
 3094:     # loop and the sorting time will be negligible.
 3095:     if (scalar(keys(%Pending))) {
 3096:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3097:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3098:     } 
 3099:     if (scalar(keys(%Expired))) {
 3100:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3101:         my $time = pop(@sorted);
 3102:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3103:     }
 3104:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3105: }
 3106: 
 3107: sub save_cache {
 3108:     &purge_remembered();
 3109:     #&Apache::loncommon::validate_page();
 3110:     undef(%env);
 3111:     undef($env_loaded);
 3112: }
 3113: 
 3114: my $to_remember=-1;
 3115: my %remembered;
 3116: my %accessed;
 3117: my $kicks=0;
 3118: my $hits=0;
 3119: sub make_key {
 3120:     my ($name,$id) = @_;
 3121:     if (length($id) > 65 
 3122: 	&& length(&escape($id)) > 200) {
 3123: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3124:     }
 3125:     return &escape($name.':'.$id);
 3126: }
 3127: 
 3128: sub devalidate_cache_new {
 3129:     my ($name,$id,$debug) = @_;
 3130:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3131:     my $remembered_id=$name.':'.$id;
 3132:     $id=&make_key($name,$id);
 3133:     $memcache->delete($id);
 3134:     delete($remembered{$remembered_id});
 3135:     delete($accessed{$remembered_id});
 3136: }
 3137: 
 3138: sub is_cached_new {
 3139:     my ($name,$id,$debug) = @_;
 3140:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3141:     if (exists($remembered{$remembered_id})) {
 3142: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3143: 	$accessed{$remembered_id}=[&gettimeofday()];
 3144: 	$hits++;
 3145: 	return ($remembered{$remembered_id},1);
 3146:     }
 3147:     $id=&make_key($name,$id);
 3148:     my $value = $memcache->get($id);
 3149:     if (!(defined($value))) {
 3150: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3151: 	return (undef,undef);
 3152:     }
 3153:     if ($value eq '__undef__') {
 3154: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3155: 	$value=undef;
 3156:     }
 3157:     &make_room($remembered_id,$value,$debug);
 3158:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3159:     return ($value,1);
 3160: }
 3161: 
 3162: sub do_cache_new {
 3163:     my ($name,$id,$value,$time,$debug) = @_;
 3164:     my $remembered_id=$name.':'.$id;
 3165:     $id=&make_key($name,$id);
 3166:     my $setvalue=$value;
 3167:     if (!defined($setvalue)) {
 3168: 	$setvalue='__undef__';
 3169:     }
 3170:     if (!defined($time) ) {
 3171: 	$time=600;
 3172:     }
 3173:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3174:     my $result = $memcache->set($id,$setvalue,$time);
 3175:     if (! $result) {
 3176: 	&logthis("caching of id -> $id  failed");
 3177: 	$memcache->disconnect_all();
 3178:     }
 3179:     # need to make a copy of $value
 3180:     &make_room($remembered_id,$value,$debug);
 3181:     return $value;
 3182: }
 3183: 
 3184: sub make_room {
 3185:     my ($remembered_id,$value,$debug)=@_;
 3186: 
 3187:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3188:                                     : $value;
 3189:     if ($to_remember<0) { return; }
 3190:     $accessed{$remembered_id}=[&gettimeofday()];
 3191:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3192:     my $to_kick;
 3193:     my $max_time=0;
 3194:     foreach my $other (keys(%accessed)) {
 3195: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3196: 	    $to_kick=$other;
 3197: 	    $max_time=&tv_interval($accessed{$other});
 3198: 	}
 3199:     }
 3200:     delete($remembered{$to_kick});
 3201:     delete($accessed{$to_kick});
 3202:     $kicks++;
 3203:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3204:     return;
 3205: }
 3206: 
 3207: sub purge_remembered {
 3208:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3209:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3210:     undef(%remembered);
 3211:     undef(%accessed);
 3212: }
 3213: # ------------------------------------- Read an entry from a user's environment
 3214: 
 3215: sub userenvironment {
 3216:     my ($udom,$unam,@what)=@_;
 3217:     my $items;
 3218:     foreach my $item (@what) {
 3219:         $items.=&escape($item).'&';
 3220:     }
 3221:     $items=~s/\&$//;
 3222:     my %returnhash=();
 3223:     my $uhome = &homeserver($unam,$udom);
 3224:     unless ($uhome eq 'no_host') {
 3225:         my @answer=split(/\&/, 
 3226:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3227:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3228:             return %returnhash;
 3229:         }
 3230:         my $i;
 3231:         for ($i=0;$i<=$#what;$i++) {
 3232: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3233:         }
 3234:     }
 3235:     return %returnhash;
 3236: }
 3237: 
 3238: # ---------------------------------------------------------- Get a studentphoto
 3239: sub studentphoto {
 3240:     my ($udom,$unam,$ext) = @_;
 3241:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3242:     if (defined($env{'request.course.id'})) {
 3243:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3244:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3245:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3246:             } else {
 3247:                 my ($result,$perm_reqd)=
 3248: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3249:                 if ($result eq 'ok') {
 3250:                     if (!($perm_reqd eq 'yes')) {
 3251:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3252:                     }
 3253:                 }
 3254:             }
 3255:         }
 3256:     } else {
 3257:         my ($result,$perm_reqd) = 
 3258: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3259:         if ($result eq 'ok') {
 3260:             if (!($perm_reqd eq 'yes')) {
 3261:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3262:             }
 3263:         }
 3264:     }
 3265:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3266: }
 3267: 
 3268: sub retrievestudentphoto {
 3269:     my ($udom,$unam,$ext,$type) = @_;
 3270:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3271:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3272:     if ($ret eq 'ok') {
 3273:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3274:         if ($type eq 'thumbnail') {
 3275:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3276:         }
 3277:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3278:         return $tokenurl;
 3279:     } else {
 3280:         if ($type eq 'thumbnail') {
 3281:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3282:         } else { 
 3283:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3284:         }
 3285:     }
 3286: }
 3287: 
 3288: # -------------------------------------------------------------------- New chat
 3289: 
 3290: sub chatsend {
 3291:     my ($newentry,$anon,$group)=@_;
 3292:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3293:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3294:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3295:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3296: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3297: 		   &escape($newentry)).':'.$group,$chome);
 3298: }
 3299: 
 3300: # ------------------------------------------ Find current version of a resource
 3301: 
 3302: sub getversion {
 3303:     my $fname=&clutter(shift);
 3304:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3305:     return &currentversion(&filelocation('',$fname));
 3306: }
 3307: 
 3308: sub currentversion {
 3309:     my $fname=shift;
 3310:     my $author=$fname;
 3311:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3312:     my ($udom,$uname)=split(/\//,$author);
 3313:     my $home=&homeserver($uname,$udom);
 3314:     if ($home eq 'no_host') { 
 3315:         return -1; 
 3316:     }
 3317:     my $answer=&reply("currentversion:$fname",$home);
 3318:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3319: 	return -1;
 3320:     }
 3321:     return $answer;
 3322: }
 3323: 
 3324: #
 3325: # Return special version number of resource if set by override, empty otherwise
 3326: #
 3327: sub usedversion {
 3328:     my $fname=shift;
 3329:     unless ($fname) { $fname=$env{'request.uri'}; }
 3330:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3331:     if ($urlversion) { return $urlversion; }
 3332:     return '';
 3333: }
 3334: 
 3335: # ----------------------------- Subscribe to a resource, return URL if possible
 3336: 
 3337: sub subscribe {
 3338:     my $fname=shift;
 3339:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3340:     $fname=~s/[\n\r]//g;
 3341:     my $author=$fname;
 3342:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3343:     my ($udom,$uname)=split(/\//,$author);
 3344:     my $home=homeserver($uname,$udom);
 3345:     if ($home eq 'no_host') {
 3346:         return 'not_found';
 3347:     }
 3348:     my $answer=reply("sub:$fname",$home);
 3349:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3350: 	$answer.=' by '.$home;
 3351:     }
 3352:     return $answer;
 3353: }
 3354:     
 3355: # -------------------------------------------------------------- Replicate file
 3356: 
 3357: sub repcopy {
 3358:     my $filename=shift;
 3359:     $filename=~s/\/+/\//g;
 3360:     my $londocroot = $perlvar{'lonDocRoot'};
 3361:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3362:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3363:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3364: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3365: 	return &repcopy_userfile($filename);
 3366:     }
 3367:     $filename=~s/[\n\r]//g;
 3368:     my $transname="$filename.in.transfer";
 3369: # FIXME: this should flock
 3370:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3371:     my $remoteurl=subscribe($filename);
 3372:     if ($remoteurl =~ /^con_lost by/) {
 3373: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3374:            return 'unavailable';
 3375:     } elsif ($remoteurl eq 'not_found') {
 3376: 	   #&logthis("Subscribe returned not_found: $filename");
 3377: 	   return 'not_found';
 3378:     } elsif ($remoteurl =~ /^rejected by/) {
 3379: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3380:            return 'forbidden';
 3381:     } elsif ($remoteurl eq 'directory') {
 3382:            return 'ok';
 3383:     } else {
 3384:         my $author=$filename;
 3385:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3386:         my ($udom,$uname)=split(/\//,$author);
 3387:         my $home=homeserver($uname,$udom);
 3388:         unless ($home eq $perlvar{'lonHostID'}) {
 3389:            my @parts=split(/\//,$filename);
 3390:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3391:            if ($path ne "$londocroot/res") {
 3392:                &logthis("Malconfiguration for replication: $filename");
 3393: 	       return 'bad_request';
 3394:            }
 3395:            my $count;
 3396:            for ($count=5;$count<$#parts;$count++) {
 3397:                $path.="/$parts[$count]";
 3398:                if ((-e $path)!=1) {
 3399: 		   mkdir($path,0777);
 3400:                }
 3401:            }
 3402:            my $request=new HTTP::Request('GET',"$remoteurl");
 3403:            my $response;
 3404:            if ($remoteurl =~ m{/raw/}) {
 3405:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3406:            } else {
 3407:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3408:            }
 3409:            if ($response->is_error()) {
 3410: 	       unlink($transname);
 3411:                my $message=$response->status_line;
 3412:                &logthis("<font color=\"blue\">WARNING:"
 3413:                        ." LWP get: $message: $filename</font>");
 3414:                return 'unavailable';
 3415:            } else {
 3416: 	       if ($remoteurl!~/\.meta$/) {
 3417:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3418:                   my $mresponse;
 3419:                   if ($remoteurl =~ m{/raw/}) {
 3420:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3421:                   } else {
 3422:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3423:                   }
 3424:                   if ($mresponse->is_error()) {
 3425: 		      unlink($filename.'.meta');
 3426:                       &logthis(
 3427:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3428:                   }
 3429: 	       }
 3430:                rename($transname,$filename);
 3431:                return 'ok';
 3432:            }
 3433:        }
 3434:     }
 3435: }
 3436: 
 3437: # ------------------------------------------------- Unsubscribe from a resource
 3438: 
 3439: sub unsubscribe {
 3440:     my ($fname) = @_;
 3441:     my $answer;
 3442:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3443:     $fname=~s/[\n\r]//g;
 3444:     my $author=$fname;
 3445:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3446:     my ($udom,$uname)=split(/\//,$author);
 3447:     my $home=homeserver($uname,$udom);
 3448:     if ($home eq 'no_host') {
 3449:         $answer = 'no_host';
 3450:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3451:         $answer = 'home';
 3452:     } else {
 3453:         my $defdom = $perlvar{'lonDefDomain'};
 3454:         if (&will_trust('content',$defdom,$udom)) {
 3455:             $answer = reply("unsub:$fname",$home);
 3456:         } else {
 3457:             $answer = 'untrusted';
 3458:         }
 3459:     }
 3460:     return $answer;
 3461: }
 3462: 
 3463: # ------------------------------------------------ Get server side include body
 3464: sub ssi_body {
 3465:     my ($filelink,%form)=@_;
 3466:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3467:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3468:     }
 3469:     my $output='';
 3470:     my $response;
 3471:     if ($filelink=~/^https?\:/) {
 3472:        ($output,$response)=&externalssi($filelink);
 3473:     } else {
 3474:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3475:        $filelink .= 'inhibitmenu=yes';
 3476:        ($output,$response)=&ssi($filelink,%form);
 3477:     }
 3478:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3479:     $output=~s/^.*?\<body[^\>]*\>//si;
 3480:     $output=~s/\<\/body\s*\>.*?$//si;
 3481:     if (wantarray) {
 3482:         return ($output, $response);
 3483:     } else {
 3484:         return $output;
 3485:     }
 3486: }
 3487: 
 3488: # --------------------------------------------------------- Server Side Include
 3489: 
 3490: sub absolute_url {
 3491:     my ($host_name,$unalias,$keep_proto) = @_;
 3492:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3493:     if ($host_name eq '') {
 3494: 	$host_name = $ENV{'SERVER_NAME'};
 3495:     }
 3496:     if ($unalias) {
 3497:         my $alias = &get_proxy_alias();
 3498:         if ($alias eq $host_name) {
 3499:             my $lonhost = $perlvar{'lonHostID'};
 3500:             my $hostname = &hostname($lonhost);
 3501:             my $lcproto; 
 3502:             if (($keep_proto) || ($hostname eq '')) {
 3503:                 $lcproto = $protocol;
 3504:             } else {
 3505:                 $lcproto = $protocol{$lonhost};
 3506:                 $lcproto = 'http' if ($lcproto ne 'https');
 3507:                 $lcproto .= '://';
 3508:             }
 3509:             unless ($hostname eq '') {
 3510:                 return $lcproto.$hostname;
 3511:             }
 3512:         }
 3513:     }
 3514:     return $protocol.$host_name;
 3515: }
 3516: 
 3517: #
 3518: #   Server side include.
 3519: # Parameters:
 3520: #  fn     Possibly encrypted resource name/id.
 3521: #  form   Hash that describes how the rendering should be done
 3522: #         and other things.
 3523: # Returns:
 3524: #   Scalar context: The content of the response.
 3525: #   Array context:  2 element list of the content and the full response object.
 3526: #     
 3527: sub ssi {
 3528: 
 3529:     my ($fn,%form)=@_;
 3530:     my ($host,$request,$response);
 3531:     $host = &absolute_url('',1);
 3532: 
 3533:     $form{'no_update_last_known'}=1;
 3534:     &Apache::lonenc::check_encrypt(\$fn);
 3535:     if (%form) {
 3536:       $request=new HTTP::Request('POST',$host.$fn);
 3537:       $request->content(join('&',map { 
 3538:             my $name = escape($_);
 3539:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3540:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3541:             : &escape($form{$_}) );    
 3542:         } keys(%form)));
 3543:     } else {
 3544:       $request=new HTTP::Request('GET',$host.$fn);
 3545:     }
 3546: 
 3547:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3548:     my $lonhost = $perlvar{'lonHostID'};
 3549:     my $islocal;
 3550:     if (($env{'request.course.id'}) &&
 3551:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3552:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3553:         ($form{'grade_symb'} ne '') &&
 3554:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3555:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3556:         $islocal = 1;
 3557:     }
 3558:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3559:                                              '','','',$islocal);
 3560: 
 3561:     if (wantarray) {
 3562: 	return ($response->content, $response);
 3563:     } else {
 3564: 	return $response->content;
 3565:     }
 3566: }
 3567: 
 3568: sub externalssi {
 3569:     my ($url)=@_;
 3570:     my $request=new HTTP::Request('GET',$url);
 3571:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3572:     if (wantarray) {
 3573:         return ($response->content, $response);
 3574:     } else {
 3575:         return $response->content;
 3576:     }
 3577: }
 3578: 
 3579: 
 3580: # If the local copy of a replicated resource is outdated, trigger a  
 3581: # connection from the homeserver to flush the delayed queue. If no update 
 3582: # happens, remove local copies of outdated resource (and corresponding
 3583: # metadata file).
 3584: 
 3585: sub remove_stale_resfile {
 3586:     my ($url) = @_;
 3587:     my $removed;
 3588:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3589:         my $audom = $1;
 3590:         my $auname = $2;
 3591:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3592:             my $homeserver = &homeserver($auname,$audom);
 3593:             unless (($homeserver eq 'no_host') ||
 3594:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3595:                 my $fname = &filelocation('',$url);
 3596:                 if (-e $fname) {
 3597:                     my $hostname = &hostname($homeserver);
 3598:                     if ($hostname) {
 3599:                         my $protocol = $protocol{$homeserver};
 3600:                         $protocol = 'http' if ($protocol ne 'https');
 3601:                         my $uri = &declutter($url);
 3602:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3603:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3604:                         if ($response->is_success()) {
 3605:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3606:                             my $locmodtime = (stat($fname))[9];
 3607:                             if ($locmodtime < $remmodtime) {
 3608:                                 my $stale;
 3609:                                 my $answer = &reply('pong',$homeserver);
 3610:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3611:                                     sleep(0.2);
 3612:                                     $locmodtime = (stat($fname))[9];
 3613:                                     if ($locmodtime < $remmodtime) {
 3614:                                         my $posstransfer = $fname.'.in.transfer';
 3615:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3616:                                             $removed = 1;
 3617:                                         } else {
 3618:                                             $stale = 1;
 3619:                                         }
 3620:                                     } else {
 3621:                                         $removed = 1;
 3622:                                     }
 3623:                                 } else {
 3624:                                     $stale = 1;
 3625:                                 }
 3626:                                 if ($stale) {
 3627:                                     if (unlink($fname)) {
 3628:                                         if ($uri!~/\.meta$/) {
 3629:                                             if (-e $fname.'.meta') {
 3630:                                                 unlink($fname.'.meta');
 3631:                                             }
 3632:                                         }
 3633:                                         my $unsubresult = &unsubscribe($fname);
 3634:                                         unless ($unsubresult eq 'ok') {
 3635:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3636:                                         }
 3637:                                         $removed = 1;
 3638:                                     }
 3639:                                 }
 3640:                             }
 3641:                         }
 3642:                     }
 3643:                 }
 3644:             }
 3645:         }
 3646:     }
 3647:     return $removed;
 3648: }
 3649: 
 3650: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3651: 
 3652: sub allowuploaded {
 3653:     my ($srcurl,$url)=@_;
 3654:     $url=&clutter(&declutter($url));
 3655:     my $dir=$url;
 3656:     $dir=~s/\/[^\/]+$//;
 3657:     my %httpref=();
 3658:     my $httpurl=&hreflocation('',$url);
 3659:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3660:     &Apache::lonnet::appenv(\%httpref);
 3661: }
 3662: 
 3663: #
 3664: # Determine if the current user should be able to edit a particular resource,
 3665: # when viewing in course context.
 3666: # (a) When viewing resource used to determine if "Edit" item is included in 
 3667: #     Functions.
 3668: # (b) When displaying folder contents in course editor, used to determine if
 3669: #     "Edit" link will be displayed alongside resource.
 3670: #
 3671: #  input: six args -- filename (decluttered), course number, course domain,
 3672: #                   url, symb (if registered) and group (if this is a group
 3673: #                   item -- e.g., bulletin board, group page etc.).
 3674: #  output: array of five scalars -- 
 3675: #          $cfile -- url for file editing if editable on current server
 3676: #          $home -- homeserver of resource (i.e., for author if published,
 3677: #                                           or course if uploaded.).
 3678: #          $switchserver --  1 if server switch will be needed.
 3679: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3680: #          $forceview -- 1 if icon/link should be to go to view mode
 3681: #
 3682: 
 3683: sub can_edit_resource {
 3684:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3685:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3686: #
 3687: # For aboutme pages user can only edit his/her own.
 3688: #
 3689:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3690:         my ($sdom,$sname) = ($1,$2);
 3691:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3692:             $home = $env{'user.home'};
 3693:             $cfile = $resurl;
 3694:             if ($env{'form.forceedit'}) {
 3695:                 $forceview = 1;
 3696:             } else {
 3697:                 $forceedit = 1;
 3698:             }
 3699:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3700:         } else {
 3701:             return;
 3702:         }
 3703:     }
 3704: 
 3705:     if ($env{'request.course.id'}) {
 3706:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3707:         if ($group ne '') {
 3708: # if this is a group homepage or group bulletin board, check group privs
 3709:             my $allowed = 0;
 3710:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3711:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3712:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3713:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3714:                     $allowed = 1;
 3715:                 }
 3716:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3717:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3718:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3719:                     $allowed = 1;
 3720:                 }
 3721:             }
 3722:             if ($allowed) {
 3723:                 $home=&homeserver($cnum,$cdom);
 3724:                 if ($env{'form.forceedit'}) {
 3725:                     $forceview = 1;
 3726:                 } else {
 3727:                     $forceedit = 1;
 3728:                 }
 3729:                 $cfile = $resurl;
 3730:             } else {
 3731:                 return;
 3732:             }
 3733:         } else {
 3734:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3735:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3736:                     return;
 3737:                 }
 3738:             } elsif (!$crsedit) {
 3739: #
 3740: # No edit allowed where CC has switched to student role.
 3741: #
 3742:                 return;
 3743:             }
 3744:         }
 3745:     }
 3746: 
 3747:     if ($file ne '') {
 3748:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3749:             if (&is_course_upload($file,$cnum,$cdom)) {
 3750:                 $uploaded = 1;
 3751:                 $incourse = 1;
 3752:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3753:                     $cfile = &hreflocation('',$file);
 3754:                     if ($env{'form.forceedit'}) {
 3755:                         $forceview = 1;
 3756:                     } else {
 3757:                         $forceedit = 1;
 3758:                     }
 3759:                 }
 3760:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3761:                 $incourse = 1;
 3762:                 if ($env{'form.forceedit'}) {
 3763:                     $forceview = 1;
 3764:                 } else {
 3765:                     $forceedit = 1;
 3766:                 }
 3767:                 $cfile = $resurl;
 3768:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3769:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3770:                     $incourse = 1;
 3771:                     if ($env{'form.forceedit'}) {
 3772:                         $forceview = 1;
 3773:                     } else {
 3774:                         $forceedit = 1;
 3775:                     }
 3776:                     $cfile = $resurl;
 3777:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3778:                     $incourse = 1;
 3779:                     $cfile = $resurl.'/smpedit';
 3780:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3781:                     $incourse = 1;
 3782:                     if ($env{'form.forceedit'}) {
 3783:                         $forceview = 1;
 3784:                     } else {
 3785:                         $forceedit = 1;
 3786:                     }
 3787:                     $cfile = $resurl;
 3788:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3789:                     my ($map,$id,$res) = &decode_symb($symb);
 3790:                     if ($map =~ /\.page$/) {
 3791:                         $incourse = 1;
 3792:                         if ($env{'form.forceedit'}) {
 3793:                             $forceview = 1;
 3794:                             $cfile = $map;
 3795:                         } else {
 3796:                             $forceedit = 1;
 3797:                             $cfile =  '/adm/wrapper'.$resurl;
 3798:                         }
 3799:                     }
 3800:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3801:                     $incourse = 1;
 3802:                     if ($env{'form.forceedit'}) {
 3803:                         $forceview = 1;
 3804:                     } else {
 3805:                         $forceedit = 1;
 3806:                     }
 3807:                     $cfile = $resurl;
 3808:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3809:                     $incourse = 1;
 3810:                     if ($env{'form.forceedit'}) {
 3811:                         $forceview = 1;
 3812:                     } else {
 3813:                         $forceedit = 1;
 3814:                     }
 3815:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3816:                 }
 3817:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3818:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3819:                 if (&is_on_map($template)) { 
 3820:                     $incourse = 1;
 3821:                     $forceview = 1;
 3822:                     $cfile = $template;
 3823:                 }
 3824:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3825:                 $incourse = 1;
 3826:                 if ($env{'form.forceedit'}) {
 3827:                     $forceview = 1;
 3828:                 } else {
 3829:                     $forceedit = 1;
 3830:                 }
 3831:                 $cfile = $resurl;
 3832:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3833:                 $incourse = 1;
 3834:                 if ($env{'form.forceedit'}) {
 3835:                     $forceview = 1;
 3836:                 } else {
 3837:                     $forceedit = 1;
 3838:                 }
 3839:                 $cfile = $resurl;
 3840:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3841:                 $incourse = 1;
 3842:                 $forceview = 1;
 3843:                 if ($symb) {
 3844:                     my ($map,$id,$res)=&decode_symb($symb);
 3845:                     $env{'request.symb'} = $symb;
 3846:                     $cfile = &clutter($res);
 3847:                 } else {
 3848:                     $cfile = $env{'form.suppurl'};
 3849:                     my $escfile = &unescape($cfile);
 3850:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3851:                         $cfile = '/adm/wrapper'.$escfile;
 3852:                     } else {
 3853:                         $escfile =~ s{^http://}{};
 3854:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3855:                     }
 3856:                 }
 3857:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3858:                 if ($env{'form.forceedit'}) {
 3859:                     $forceview = 1;
 3860:                 } else {
 3861:                     $forceedit = 1;
 3862:                 }
 3863:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3864:             }
 3865:         }
 3866:         if ($uploaded || $incourse) {
 3867:             $home=&homeserver($cnum,$cdom);
 3868:         } elsif ($file !~ m{/$}) {
 3869:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3870:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3871:             # Check that the user has permission to edit this resource
 3872:             my $setpriv = 1;
 3873:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3874:             if (defined($cfudom)) {
 3875:                 $home=&homeserver($cfuname,$cfudom);
 3876:                 $cfile=$file;
 3877:             }
 3878:         }
 3879:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3880:             (($home ne '') && ($home ne 'no_host'))) {
 3881:             my @ids=&current_machine_ids();
 3882:             unless (grep(/^\Q$home\E$/,@ids)) {
 3883:                 $switchserver=1;
 3884:             }
 3885:         }
 3886:     }
 3887:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3888: }
 3889: 
 3890: sub is_course_upload {
 3891:     my ($file,$cnum,$cdom) = @_;
 3892:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3893:     $uploadpath =~ s{^\/}{};
 3894:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3895:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3896:         return 1;
 3897:     }
 3898:     return;
 3899: }
 3900: 
 3901: sub in_course {
 3902:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3903:     if ($hideprivileged) {
 3904:         my $skipuser;
 3905:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3906:         my @possdoms = ($cdom);  
 3907:         if ($coursehash{'checkforpriv'}) { 
 3908:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3909:         }
 3910:         if (&privileged($uname,$udom,\@possdoms)) {
 3911:             $skipuser = 1;
 3912:             if ($coursehash{'nothideprivileged'}) {
 3913:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3914:                     my $user;
 3915:                     if ($item =~ /:/) {
 3916:                         $user = $item;
 3917:                     } else {
 3918:                         $user = join(':',split(/[\@]/,$item));
 3919:                     }
 3920:                     if ($user eq $uname.':'.$udom) {
 3921:                         undef($skipuser);
 3922:                         last;
 3923:                     }
 3924:                 }
 3925:             }
 3926:             if ($skipuser) {
 3927:                 return 0;
 3928:             }
 3929:         }
 3930:     }
 3931:     $type ||= 'any';
 3932:     if (!defined($cdom) || !defined($cnum)) {
 3933:         my $cid  = $env{'request.course.id'};
 3934:         $cdom = $env{'course.'.$cid.'.domain'};
 3935:         $cnum = $env{'course.'.$cid.'.num'};
 3936:     }
 3937:     my $typesref;
 3938:     if (($type eq 'any') || ($type eq 'all')) {
 3939:         $typesref = ['active','previous','future'];
 3940:     } elsif ($type eq 'previous' || $type eq 'future') {
 3941:         $typesref = [$type];
 3942:     }
 3943:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3944:                               $typesref,undef,[$cdom]);
 3945:     my ($tmp) = keys(%roles);
 3946:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3947:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3948:     if (@course_roles > 0) {
 3949:         return 1;
 3950:     }
 3951:     return 0;
 3952: }
 3953: 
 3954: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3955: # input: action, courseID, current domain, intended
 3956: #        path to file, source of file, instruction to parse file for objects,
 3957: #        ref to hash for embedded objects,
 3958: #        ref to hash for codebase of java objects.
 3959: #        reference to scalar to accommodate mime type determined
 3960: #          from File::MMagic if $parser = parse.
 3961: #
 3962: # output: url to file (if action was uploaddoc), 
 3963: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3964: #
 3965: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3966: # course.
 3967: #
 3968: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3969: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3970: #          course's home server.
 3971: #
 3972: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3973: #          be copied from $source (current location) to 
 3974: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3975: #         and will then be copied to
 3976: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3977: #         course's home server.
 3978: #
 3979: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3980: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3981: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3982: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3983: #         in course's home server.
 3984: #
 3985: 
 3986: sub process_coursefile {
 3987:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3988:         $mimetype)=@_;
 3989:     my $fetchresult;
 3990:     my $home=&homeserver($docuname,$docudom);
 3991:     if ($action eq 'propagate') {
 3992:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3993: 			     $home);
 3994:     } else {
 3995:         my $fpath = '';
 3996:         my $fname = $file;
 3997:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3998:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3999:         my $filepath = &build_filepath($fpath);
 4000:         if ($action eq 'copy') {
 4001:             if ($source eq '') {
 4002:                 $fetchresult = 'no source file';
 4003:                 return $fetchresult;
 4004:             } else {
 4005:                 my $destination = $filepath.'/'.$fname;
 4006:                 rename($source,$destination);
 4007:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4008:                                  $home);
 4009:             }
 4010:         } elsif ($action eq 'uploaddoc') {
 4011:             open(my $fh,'>',$filepath.'/'.$fname);
 4012:             print $fh $env{'form.'.$source};
 4013:             close($fh);
 4014:             if ($parser eq 'parse') {
 4015:                 my $mm = new File::MMagic;
 4016:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4017:                 if ($type eq 'text/html') {
 4018:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4019:                     unless ($parse_result eq 'ok') {
 4020:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4021:                     }
 4022:                 }
 4023:                 if (ref($mimetype)) {
 4024:                     $$mimetype = $type;
 4025:                 } 
 4026:             }
 4027:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4028:                                  $home);
 4029:             if ($fetchresult eq 'ok') {
 4030:                 return '/uploaded/'.$fpath.'/'.$fname;
 4031:             } else {
 4032:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4033:                         ' to host '.$home.': '.$fetchresult);
 4034:                 return '/adm/notfound.html';
 4035:             }
 4036:         }
 4037:     }
 4038:     unless ( $fetchresult eq 'ok') {
 4039:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4040:              ' to host '.$home.': '.$fetchresult);
 4041:     }
 4042:     return $fetchresult;
 4043: }
 4044: 
 4045: sub build_filepath {
 4046:     my ($fpath) = @_;
 4047:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4048:     unless ($fpath eq '') {
 4049:         my @parts=split('/',$fpath);
 4050:         foreach my $part (@parts) {
 4051:             $filepath.= '/'.$part;
 4052:             if ((-e $filepath)!=1) {
 4053:                 mkdir($filepath,0777);
 4054:             }
 4055:         }
 4056:     }
 4057:     return $filepath;
 4058: }
 4059: 
 4060: sub store_edited_file {
 4061:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4062:     my $file = $primary_url;
 4063:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4064:     my $fpath = '';
 4065:     my $fname = $file;
 4066:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4067:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4068:     my $filepath = &build_filepath($fpath);
 4069:     open(my $fh,'>',$filepath.'/'.$fname);
 4070:     print $fh $content;
 4071:     close($fh);
 4072:     my $home=&homeserver($docuname,$docudom);
 4073:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4074: 			  $home);
 4075:     if ($$fetchresult eq 'ok') {
 4076:         return '/uploaded/'.$fpath.'/'.$fname;
 4077:     } else {
 4078:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4079: 		 ' to host '.$home.': '.$$fetchresult);
 4080:         return '/adm/notfound.html';
 4081:     }
 4082: }
 4083: 
 4084: sub clean_filename {
 4085:     my ($fname,$args)=@_;
 4086: # Replace Windows backslashes by forward slashes
 4087:     $fname=~s/\\/\//g;
 4088:     if (!$args->{'keep_path'}) {
 4089:         # Get rid of everything but the actual filename
 4090: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4091:     }
 4092: # Replace spaces by underscores
 4093:     $fname=~s/\s+/\_/g;
 4094: # Transliterate non-ascii text to ascii
 4095:     my $lang = &Apache::lonlocal::current_language();
 4096:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4097: # Replace all other weird characters by nothing
 4098:     $fname=~s{[^/\w\.\-]}{}g;
 4099: # Replace all .\d. sequences with _\d. so they no longer look like version
 4100: # numbers
 4101:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4102: # Replace three or more adjacent underscores with one for consistency 
 4103: # with loncfile::filename_check() so complete url can be extracted by
 4104: # lonnet::decode_symb()
 4105:     $fname=~s/_{3,}/_/g;
 4106:     return $fname;
 4107: }
 4108: 
 4109: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4110: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4111: # image with the same aspect ratio as the original, but with dimensions which do 
 4112: # not exceed $resizewidth and $resizeheight.
 4113:  
 4114: sub resizeImage {
 4115:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4116:     my $ima = Image::Magick->new;
 4117:     my $resized;
 4118:     if (-e $img_path) {
 4119:         $ima->Read($img_path);
 4120:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4121:             my $width = $ima->Get('width');
 4122:             my $height = $ima->Get('height');
 4123:             if ($width > $resizewidth) {
 4124: 	        my $factor = $width/$resizewidth;
 4125:                 my $newheight = $height/$factor;
 4126:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4127:                 $resized = 1;
 4128:             }
 4129:         }
 4130:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4131:             my $width = $ima->Get('width');
 4132:             my $height = $ima->Get('height');
 4133:             if ($height > $resizeheight) {
 4134:                 my $factor = $height/$resizeheight;
 4135:                 my $newwidth = $width/$factor;
 4136:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4137:                 $resized = 1;
 4138:             }
 4139:         }
 4140:         if ($resized) {
 4141:             $ima->Write($img_path);
 4142:         }
 4143:     }
 4144:     return;
 4145: }
 4146: 
 4147: # --------------- Take an uploaded file and put it into the userfiles directory
 4148: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4149: #                    the desired filename is in $env{"form.$formname.filename"}
 4150: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4151: #                                    canceloverwrite, scantron or ''.
 4152: #                   if 'coursedoc': upload to the current course
 4153: #                   if 'existingfile': write file to tmp/overwrites directory 
 4154: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4155: #                   $context is passed as argument to &finishuserfileupload
 4156: #        $subdir - directory in userfile to store the file into
 4157: #        $parser - instruction to parse file for objects ($parser = parse) or
 4158: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4159: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4160: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4161: #        $allfiles - reference to hash for embedded objects
 4162: #        $codebase - reference to hash for codebase of java objects
 4163: #        $desuname - username for permanent storage of uploaded file
 4164: #        $dsetudom - domain for permanaent storage of uploaded file
 4165: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4166: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4167: #        $resizewidth - width (pixels) to which to resize uploaded image
 4168: #        $resizeheight - height (pixels) to which to resize uploaded image
 4169: #        $mimetype - reference to scalar to accommodate mime type determined
 4170: #                    from File::MMagic.
 4171: # 
 4172: # output: url of file in userspace, or error: <message> 
 4173: #             or /adm/notfound.html if failure to upload occurse
 4174: 
 4175: sub userfileupload {
 4176:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4177:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4178:     if (!defined($subdir)) { $subdir='unknown'; }
 4179:     my $fname=$env{'form.'.$formname.'.filename'};
 4180:     $fname=&clean_filename($fname);
 4181:     # See if there is anything left
 4182:     unless ($fname) { return 'error: no uploaded file'; }
 4183:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4184:     if ($fname =~ /^\./) {
 4185:         my ($s,$usec) = &gettimeofday();
 4186:         while (length($usec) < 6) {
 4187:             $usec = '0'.$usec;
 4188:         }
 4189:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4190:     }
 4191:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4192:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4193:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4194:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4195:         my $now = time;
 4196:         my $filepath;
 4197:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4198:              $filepath = 'tmp/helprequests/'.$now;
 4199:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4200:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4201:                          '_'.$env{'user.domain'}.'/pending';
 4202:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4203:             my ($docuname,$docudom);
 4204:             if ($destudom =~ /^$match_domain$/) {
 4205:                 $docudom = $destudom;
 4206:             } else {
 4207:                 $docudom = $env{'user.domain'};
 4208:             }
 4209:             if ($destuname =~ /^$match_username$/) {
 4210:                 $docuname = $destuname;
 4211:             } else {
 4212:                 $docuname = $env{'user.name'};
 4213:             }
 4214:             if (exists($env{'form.group'})) {
 4215:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4216:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4217:             }
 4218:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4219:             if ($context eq 'canceloverwrite') {
 4220:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4221:                 if (-e  $tempfile) {
 4222:                     my @info = stat($tempfile);
 4223:                     if ($info[9] eq $env{'form.timestamp'}) {
 4224:                         unlink($tempfile);
 4225:                     }
 4226:                 }
 4227:                 return;
 4228:             }
 4229:         }
 4230:         # Create the directory if not present
 4231:         my @parts=split(/\//,$filepath);
 4232:         my $fullpath = $perlvar{'lonDaemons'};
 4233:         for (my $i=0;$i<@parts;$i++) {
 4234:             $fullpath .= '/'.$parts[$i];
 4235:             if ((-e $fullpath)!=1) {
 4236:                 mkdir($fullpath,0777);
 4237:             }
 4238:         }
 4239:         open(my $fh,'>',$fullpath.'/'.$fname);
 4240:         print $fh $env{'form.'.$formname};
 4241:         close($fh);
 4242:         if ($context eq 'existingfile') {
 4243:             my @info = stat($fullpath.'/'.$fname);
 4244:             return ($fullpath.'/'.$fname,$info[9]);
 4245:         } else {
 4246:             return $fullpath.'/'.$fname;
 4247:         }
 4248:     }
 4249:     if ($subdir eq 'scantron') {
 4250:         $fname = 'scantron_orig_'.$fname;
 4251:     } else {
 4252:         $fname="$subdir/$fname";
 4253:     }
 4254:     if ($context eq 'coursedoc') {
 4255: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4256: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4257:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4258:             return &finishuserfileupload($docuname,$docudom,
 4259: 					 $formname,$fname,$parser,$allfiles,
 4260: 					 $codebase,$thumbwidth,$thumbheight,
 4261:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4262:         } else {
 4263:             if ($env{'form.folder'}) {
 4264:                 $fname=$env{'form.folder'}.'/'.$fname;
 4265:             }
 4266:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4267: 				       $fname,$formname,$parser,
 4268: 				       $allfiles,$codebase,$mimetype);
 4269:         }
 4270:     } elsif (defined($destuname)) {
 4271:         my $docuname=$destuname;
 4272:         my $docudom=$destudom;
 4273: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4274: 				     $parser,$allfiles,$codebase,
 4275:                                      $thumbwidth,$thumbheight,
 4276:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4277:     } else {
 4278:         my $docuname=$env{'user.name'};
 4279:         my $docudom=$env{'user.domain'};
 4280:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4281:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4282:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4283:         }
 4284: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4285: 				     $parser,$allfiles,$codebase,
 4286:                                      $thumbwidth,$thumbheight,
 4287:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4288:     }
 4289: }
 4290: 
 4291: sub finishuserfileupload {
 4292:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4293:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4294:     my $path=$docudom.'/'.$docuname.'/';
 4295:     my $filepath=$perlvar{'lonDocRoot'};
 4296:   
 4297:     my ($fnamepath,$file,$fetchthumb);
 4298:     $file=$fname;
 4299:     if ($fname=~m|/|) {
 4300:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4301: 	$path.=$fnamepath.'/';
 4302:     }
 4303:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4304:     my $count;
 4305:     for ($count=4;$count<=$#parts;$count++) {
 4306:         $filepath.="/$parts[$count]";
 4307:         if ((-e $filepath)!=1) {
 4308: 	    mkdir($filepath,0777);
 4309:         }
 4310:     }
 4311: 
 4312: # Save the file
 4313:     {
 4314: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4315: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4316: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4317: 	    return '/adm/notfound.html';
 4318: 	}
 4319:         if ($context eq 'overwrite') {
 4320:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4321:             my $target = $filepath.'/'.$file;
 4322:             if (-e $source) {
 4323:                 my @info = stat($source);
 4324:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4325:                     unless (&File::Copy::move($source,$target)) {
 4326:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4327:                         return "Moving from $source failed";
 4328:                     }
 4329:                 } else {
 4330:                     return "Temporary file: $source had unexpected date/time for last modification";
 4331:                 }
 4332:             } else {
 4333:                 return "Temporary file: $source missing";
 4334:             }
 4335:         } elsif (!print FH ($env{'form.'.$formname})) {
 4336: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4337: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4338: 	    return '/adm/notfound.html';
 4339: 	}
 4340: 	close(FH);
 4341:         if ($resizewidth && $resizeheight) {
 4342:             my $mm = new File::MMagic;
 4343:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4344:             if ($mime_type =~ m{^image/}) {
 4345: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4346:             }  
 4347: 	}
 4348:     }
 4349:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4350:         if (ref($mimetype)) {
 4351:             if ($$mimetype eq '') {
 4352:                 my $mm = new File::MMagic;
 4353:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4354:                 $$mimetype = $type;
 4355:             }
 4356:         }
 4357:     }
 4358:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4359:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4360:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4361:                                                        $allfiles,$codebase);
 4362:             unless ($parse_result eq 'ok') {
 4363:                 &logthis('Failed to parse '.$filepath.$file.
 4364: 	   	         ' for embedded media: '.$parse_result); 
 4365:             }
 4366:         }
 4367:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4368:         my $format = $env{'form.scantron_format'};
 4369:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4370:     }
 4371:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4372:         my $input = $filepath.'/'.$file;
 4373:         my $output = $filepath.'/'.'tn-'.$file;
 4374:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4375:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4376:         system({$args[0]} @args);
 4377:         if (-e $filepath.'/'.'tn-'.$file) {
 4378:             $fetchthumb  = 1; 
 4379:         }
 4380:     }
 4381:  
 4382: # Notify homeserver to grep it
 4383: #
 4384:     my $docuhome=&homeserver($docuname,$docudom);	
 4385:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4386:     if ($fetchresult eq 'ok') {
 4387:         if ($fetchthumb) {
 4388:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4389:             if ($thumbresult ne 'ok') {
 4390:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4391:                          $docuhome.': '.$thumbresult);
 4392:             }
 4393:         }
 4394: #
 4395: # Return the URL to it
 4396:         return '/uploaded/'.$path.$file;
 4397:     } else {
 4398:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4399: 		 ': '.$fetchresult);
 4400:         return '/adm/notfound.html';
 4401:     }
 4402: }
 4403: 
 4404: sub extract_embedded_items {
 4405:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4406:     my @state = ();
 4407:     my (%lastids,%related,%shockwave,%flashvars);
 4408:     my %javafiles = (
 4409:                       codebase => '',
 4410:                       code => '',
 4411:                       archive => ''
 4412:                     );
 4413:     my %mediafiles = (
 4414:                       src => '',
 4415:                       movie => '',
 4416:                      );
 4417:     my $p;
 4418:     if ($content) {
 4419:         $p = HTML::LCParser->new($content);
 4420:     } else {
 4421:         $p = HTML::LCParser->new($fullpath);
 4422:     }
 4423:     while (my $t=$p->get_token()) {
 4424: 	if ($t->[0] eq 'S') {
 4425: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4426: 	    push(@state, $tagname);
 4427:             if (lc($tagname) eq 'allow') {
 4428:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4429:             }
 4430: 	    if (lc($tagname) eq 'img') {
 4431: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4432: 	    }
 4433: 	    if (lc($tagname) eq 'a') {
 4434:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4435:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4436:                 }
 4437: 	    }
 4438:             if (lc($tagname) eq 'script') {
 4439:                 my $src;
 4440:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4441:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4442:                 } else {
 4443:                     if ($attr->{'src'} ne '') {
 4444:                         $src = $attr->{'src'};
 4445:                         &add_filetype($allfiles,$src,'src');
 4446:                     }
 4447:                 }
 4448:                 my $text = $p->get_trimmed_text();
 4449:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4450:                     my @swfargs = split(/,/,$1);
 4451:                     foreach my $item (@swfargs) {
 4452:                         $item =~ s/["']//g;
 4453:                         $item =~ s/^\s+//;
 4454:                         $item =~ s/\s+$//;
 4455:                     }
 4456:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4457:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4458:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4459:                         } else {
 4460:                             $related{$swfargs[0]} = [$swfargs[2]];
 4461:                         }
 4462:                     }
 4463:                 }
 4464:             }
 4465:             if (lc($tagname) eq 'link') {
 4466:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4467:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4468:                 }
 4469:             }
 4470: 	    if (lc($tagname) eq 'object' ||
 4471: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4472: 		foreach my $item (keys(%javafiles)) {
 4473: 		    $javafiles{$item} = '';
 4474: 		}
 4475:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4476:                     $lastids{lc($tagname)} = $attr->{'id'};
 4477:                 }
 4478: 	    }
 4479: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4480: 		my $name = lc($attr->{'name'});
 4481: 		foreach my $item (keys(%javafiles)) {
 4482: 		    if ($name eq $item) {
 4483: 			$javafiles{$item} = $attr->{'value'};
 4484: 			last;
 4485: 		    }
 4486: 		}
 4487:                 my $pathfrom;
 4488: 		foreach my $item (keys(%mediafiles)) {
 4489: 		    if ($name eq $item) {
 4490:                         $pathfrom = $attr->{'value'};
 4491:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4492: 			&add_filetype($allfiles,$pathfrom,$name);
 4493: 			last;
 4494: 		    }
 4495: 		}
 4496:                 if ($name eq 'flashvars') {
 4497:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4498:                 }
 4499:                 if ($pathfrom ne '') {
 4500:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4501:                                          $pathfrom);
 4502:                 }
 4503: 	    }
 4504: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4505: 		foreach my $item (keys(%javafiles)) {
 4506: 		    if ($attr->{$item}) {
 4507: 			$javafiles{$item} = $attr->{$item};
 4508: 			last;
 4509: 		    }
 4510: 		}
 4511: 		foreach my $item (keys(%mediafiles)) {
 4512: 		    if ($attr->{$item}) {
 4513: 			&add_filetype($allfiles,$attr->{$item},$item);
 4514: 			last;
 4515: 		    }
 4516: 		}
 4517:                 if (lc($tagname) eq 'embed') {
 4518:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4519:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4520:                                              $attr->{'src'});
 4521:                     }
 4522:                 }
 4523: 	    }
 4524:             if (lc($tagname) eq 'iframe') {
 4525:                 my $src = $attr->{'src'} ;
 4526:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4527:                     &add_filetype($allfiles,$src,'src');
 4528:                 } elsif ($src =~ m{^/}) {
 4529:                     if ($env{'request.course.id'}) {
 4530:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4531:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4532:                         my $url = &hreflocation('',$fullpath);
 4533:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4534:                             my $relpath = $1;
 4535:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4536:                                 &add_filetype($allfiles,$1,'src');
 4537:                             }
 4538:                         }
 4539:                     }
 4540:                 }
 4541:             }
 4542:             if ($t->[4] =~ m{/>$}) {
 4543:                 pop(@state);
 4544:             }
 4545: 	} elsif ($t->[0] eq 'E') {
 4546: 	    my ($tagname) = ($t->[1]);
 4547: 	    if ($javafiles{'codebase'} ne '') {
 4548: 		$javafiles{'codebase'} .= '/';
 4549: 	    }  
 4550: 	    if (lc($tagname) eq 'applet' ||
 4551: 		lc($tagname) eq 'object' ||
 4552: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4553: 		) {
 4554: 		foreach my $item (keys(%javafiles)) {
 4555: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4556: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4557: 			&add_filetype($allfiles,$file,$item);
 4558: 		    }
 4559: 		}
 4560: 	    } 
 4561: 	    pop @state;
 4562: 	}
 4563:     }
 4564:     foreach my $id (sort(keys(%flashvars))) {
 4565:         if ($shockwave{$id} ne '') {
 4566:             my @pairs = split(/\&/,$flashvars{$id});
 4567:             foreach my $pair (@pairs) {
 4568:                 my ($key,$value) = split(/\=/,$pair);
 4569:                 if ($key eq 'thumb') {
 4570:                     &add_filetype($allfiles,$value,$key);
 4571:                 } elsif ($key eq 'content') {
 4572:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4573:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4574:                     if ($ext ne '') {
 4575:                         &add_filetype($allfiles,$path.$value,$ext);
 4576:                     }
 4577:                 }
 4578:             }
 4579:         }
 4580:     }
 4581:     return 'ok';
 4582: }
 4583: 
 4584: sub add_filetype {
 4585:     my ($allfiles,$file,$type)=@_;
 4586:     if (exists($allfiles->{$file})) {
 4587: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4588: 	    push(@{$allfiles->{$file}}, &escape($type));
 4589: 	}
 4590:     } else {
 4591: 	@{$allfiles->{$file}} = (&escape($type));
 4592:     }
 4593: }
 4594: 
 4595: sub embedded_dependency {
 4596:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4597:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4598:         if (($identifier ne '') &&
 4599:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4600:             ($pathfrom ne '')) {
 4601:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4602:             foreach my $dep (@{$related->{$identifier}}) {
 4603:                 &add_filetype($allfiles,$path.$dep,'object');
 4604:             }
 4605:         }
 4606:     }
 4607:     return;
 4608: }
 4609: 
 4610: sub bubblesheet_converter {
 4611:     my ($cdom,$fullpath,$config,$format) = @_;
 4612:     if ((&domain($cdom) ne '') &&
 4613:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4614:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4615:         my (%csvcols,%csvoptions);
 4616:         if (ref($config->{'fields'}) eq 'HASH') {  
 4617:             %csvcols = %{$config->{'fields'}};
 4618:         }
 4619:         if (ref($config->{'options'}) eq 'HASH') {
 4620:             %csvoptions = %{$config->{'options'}};
 4621:         }
 4622:         my %csvbynum = reverse(%csvcols);
 4623:         my %scantronconf = &get_scantron_config($format,$cdom);
 4624:         if (keys(%scantronconf)) {
 4625:             my %bynum = (
 4626:                           $scantronconf{CODEstart} => 'CODEstart',
 4627:                           $scantronconf{IDstart}   => 'IDstart',
 4628:                           $scantronconf{PaperID}   => 'PaperID',
 4629:                           $scantronconf{FirstName} => 'FirstName',
 4630:                           $scantronconf{LastName}  => 'LastName',
 4631:                           $scantronconf{Qstart}    => 'Qstart',
 4632:                         );
 4633:             my @ordered;
 4634:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4635:                 push(@ordered,$bynum{$item});
 4636:             }
 4637:             my %mapstart = (
 4638:                               CODEstart => 'CODE',
 4639:                               IDstart   => 'ID',
 4640:                               PaperID   => 'PaperID',
 4641:                               FirstName => 'FirstName',
 4642:                               LastName  => 'LastName',
 4643:                               Qstart    => 'FirstQuestion',
 4644:                            );
 4645:             my %maplength = (
 4646:                               CODEstart => 'CODElength',
 4647:                               IDstart   => 'IDlength',
 4648:                               PaperID   => 'PaperIDlength',
 4649:                               FirstName => 'FirstNamelength',
 4650:                               LastName  => 'LastNamelength',
 4651:             );
 4652:             if (open(my $fh,'<',$fullpath)) {
 4653:                 my $output;
 4654:                 my %lettdig = &letter_to_digits();
 4655:                 my %diglett = reverse(%lettdig);
 4656:                 my $numletts = scalar(keys(%lettdig));
 4657:                 my $num = 0;
 4658:                 while (my $line=<$fh>) {
 4659:                     $num ++;
 4660:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4661:                     $line =~ s{[\r\n]+$}{};
 4662:                     my %found;
 4663:                     my @values = split(/,/,$line,-1);
 4664:                     my ($qstart,$record);
 4665:                     for (my $i=0; $i<@values; $i++) {
 4666:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4667:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4668:                             if ($values[$i] eq '') {
 4669:                                 $values[$i] = $scantronconf{'Qoff'};
 4670:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4671:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4672:                                     $values[$i] = $lettdig{uc($values[$i])};
 4673:                                 }
 4674:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4675:                                 if ($values[$i] =~ /^[0-9]$/) {
 4676:                                     $values[$i] = $diglett{$values[$i]};
 4677:                                 }
 4678:                             } else {
 4679:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4680:                                     my $digit;
 4681:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4682:                                         $digit = $lettdig{uc($values[$i])}-1;
 4683:                                         if ($values[$i] eq 'J') {
 4684:                                             $digit += $numletts;
 4685:                                         }
 4686:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4687:                                         $digit = $values[$i]-1;
 4688:                                         if ($values[$i] eq '0') {
 4689:                                             $digit += $numletts;
 4690:                                         }
 4691:                                     }
 4692:                                     my $qval='';
 4693:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4694:                                         if ($j == $digit) {
 4695:                                             $qval .= $scantronconf{'Qon'};
 4696:                                         } else {
 4697:                                             $qval .= $scantronconf{'Qoff'};
 4698:                                         }
 4699:                                     }
 4700:                                     $values[$i] = $qval;
 4701:                                 }
 4702:                             }
 4703:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4704:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4705:                             }
 4706:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4707:                             if ($numblank > 0) {
 4708:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4709:                             }
 4710:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4711:                                 $qstart = $i;
 4712:                                 $found{$csvbynum{$i}} = $values[$i];
 4713:                             } else {
 4714:                                 $found{'FirstQuestion'} .= $values[$i];
 4715:                             }
 4716:                         } elsif (exists($csvbynum{$i})) {
 4717:                             if ($csvoptions{'rem'}) {
 4718:                                 $values[$i] =~ s/^\s+//;
 4719:                             }
 4720:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4721:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4722:                                     $values[$i] = '0'.$values[$i];
 4723:                                 }
 4724:                             }
 4725:                             $found{$csvbynum{$i}} = $values[$i];
 4726:                         }
 4727:                     }
 4728:                     foreach my $item (@ordered) {
 4729:                         my $currlength = 1+length($record);
 4730:                         my $numspaces = $scantronconf{$item} - $currlength;
 4731:                         if ($numspaces > 0) {
 4732:                             $record .= (' ' x $numspaces);
 4733:                         }
 4734:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4735:                             unless ($item eq 'Qstart') {
 4736:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4737:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4738:                                 }
 4739:                             }
 4740:                             $record .= $found{$mapstart{$item}};
 4741:                         }
 4742:                     }
 4743:                     $output .= "$record\n";
 4744:                 }
 4745:                 close($fh);
 4746:                 if ($output) {
 4747:                     if (open(my $fh,'>',$fullpath)) {
 4748:                         print $fh $output;
 4749:                         close($fh);
 4750:                     }
 4751:                 }
 4752:             }
 4753:         }
 4754:         return;
 4755:     }
 4756: }
 4757: 
 4758: sub letter_to_digits {
 4759:     my %lettdig = (
 4760:                     A => 1,
 4761:                     B => 2,
 4762:                     C => 3,
 4763:                     D => 4,
 4764:                     E => 5,
 4765:                     F => 6,
 4766:                     G => 7,
 4767:                     H => 8,
 4768:                     I => 9,
 4769:                     J => 0,
 4770:                   );
 4771:     return %lettdig;
 4772: }
 4773: 
 4774: sub get_scantron_config {
 4775:     my ($which,$cdom) = @_;
 4776:     my @lines = &get_scantronformat_file($cdom);
 4777:     my %config;
 4778:     #FIXME probably should move to XML it has already gotten a bit much now
 4779:     foreach my $line (@lines) {
 4780:         my ($name,$descrip)=split(/:/,$line);
 4781:         if ($name ne $which ) { next; }
 4782:         chomp($line);
 4783:         my @config=split(/:/,$line);
 4784:         $config{'name'}=$config[0];
 4785:         $config{'description'}=$config[1];
 4786:         $config{'CODElocation'}=$config[2];
 4787:         $config{'CODEstart'}=$config[3];
 4788:         $config{'CODElength'}=$config[4];
 4789:         $config{'IDstart'}=$config[5];
 4790:         $config{'IDlength'}=$config[6];
 4791:         $config{'Qstart'}=$config[7];
 4792:         $config{'Qlength'}=$config[8];
 4793:         $config{'Qoff'}=$config[9];
 4794:         $config{'Qon'}=$config[10];
 4795:         $config{'PaperID'}=$config[11];
 4796:         $config{'PaperIDlength'}=$config[12];
 4797:         $config{'FirstName'}=$config[13];
 4798:         $config{'FirstNamelength'}=$config[14];
 4799:         $config{'LastName'}=$config[15];
 4800:         $config{'LastNamelength'}=$config[16];
 4801:         $config{'BubblesPerRow'}=$config[17];
 4802:         last;
 4803:     }
 4804:     return %config;
 4805: }
 4806: 
 4807: sub get_scantronformat_file {
 4808:     my ($cdom) = @_;
 4809:     if ($cdom eq '') {
 4810:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4811:     }
 4812:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4813:     my $gottab = 0;
 4814:     my @lines;
 4815:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4816:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4817:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4818:             if ($formatfile ne '-1') {
 4819:                 @lines = split("\n",$formatfile,-1);
 4820:                 $gottab = 1;
 4821:             }
 4822:         }
 4823:     }
 4824:     if (!$gottab) {
 4825:         my $confname = $cdom.'-domainconfig';
 4826:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4827:         my $formatfile = &getfile($default);
 4828:         if ($formatfile ne '-1') {
 4829:             @lines = split("\n",$formatfile,-1);
 4830:             $gottab = 1;
 4831:         }
 4832:     }
 4833:     if (!$gottab) {
 4834:         my @domains = &current_machine_domains();
 4835:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4836:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4837:                 @lines = <$fh>;
 4838:                 close($fh);
 4839:             }
 4840:         } else {
 4841:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4842:                 @lines = <$fh>;
 4843:                 close($fh);
 4844:             }
 4845:         }
 4846:     }
 4847:     return @lines;
 4848: }
 4849: 
 4850: sub removeuploadedurl {
 4851:     my ($url)=@_;	
 4852:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4853:     return &removeuserfile($uname,$udom,$fname);
 4854: }
 4855: 
 4856: sub removeuserfile {
 4857:     my ($docuname,$docudom,$fname)=@_;
 4858:     my $home=&homeserver($docuname,$docudom);    
 4859:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4860:     if ($result eq 'ok') {	
 4861:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4862:             my $metafile = $fname.'.meta';
 4863:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4864: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4865:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4866:             my $sqlresult = 
 4867:                 &update_portfolio_table($docuname,$docudom,$file,
 4868:                                         'portfolio_metadata',$group,
 4869:                                         'delete');
 4870:         }
 4871:     }
 4872:     return $result;
 4873: }
 4874: 
 4875: sub mkdiruserfile {
 4876:     my ($docuname,$docudom,$dir)=@_;
 4877:     my $home=&homeserver($docuname,$docudom);
 4878:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4879: }
 4880: 
 4881: sub renameuserfile {
 4882:     my ($docuname,$docudom,$old,$new)=@_;
 4883:     my $home=&homeserver($docuname,$docudom);
 4884:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4885:                         &escape("$old").':'.&escape("$new"),$home);
 4886:     if ($result eq 'ok') {
 4887:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4888:             my $oldmeta = $old.'.meta';
 4889:             my $newmeta = $new.'.meta';
 4890:             my $metaresult = 
 4891:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4892: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4893:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4894:             my $sqlresult = 
 4895:                 &update_portfolio_table($docuname,$docudom,$file,
 4896:                                         'portfolio_metadata',$group,
 4897:                                         'delete');
 4898:         }
 4899:     }
 4900:     return $result;
 4901: }
 4902: 
 4903: # ------------------------------------------------------------------------- Log
 4904: 
 4905: sub log {
 4906:     my ($dom,$nam,$hom,$what)=@_;
 4907:     return critical("log:$dom:$nam:$what",$hom);
 4908: }
 4909: 
 4910: # ------------------------------------------------------------------ Course Log
 4911: #
 4912: # This routine flushes several buffers of non-mission-critical nature
 4913: #
 4914: 
 4915: sub flushcourselogs {
 4916:     &logthis('Flushing log buffers');
 4917: #
 4918: # course logs
 4919: # This is a log of all transactions in a course, which can be used
 4920: # for data mining purposes
 4921: #
 4922: # It also collects the courseid database, which lists last transaction
 4923: # times and course titles for all courseids
 4924: #
 4925:     my %courseidbuffer=();
 4926:     foreach my $crsid (keys(%courselogs)) {
 4927:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4928: 		          &escape($courselogs{$crsid}),
 4929: 		          $coursehombuf{$crsid}) eq 'ok') {
 4930: 	    delete $courselogs{$crsid};
 4931:         } else {
 4932:             &logthis('Failed to flush log buffer for '.$crsid);
 4933:             if (length($courselogs{$crsid})>40000) {
 4934:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4935:                         " exceeded maximum size, deleting.</font>");
 4936:                delete $courselogs{$crsid};
 4937:             }
 4938:         }
 4939:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4940:             'description' => $coursedescrbuf{$crsid},
 4941:             'inst_code'    => $courseinstcodebuf{$crsid},
 4942:             'type'        => $coursetypebuf{$crsid},
 4943:             'owner'       => $courseownerbuf{$crsid},
 4944:         };
 4945:     }
 4946: #
 4947: # Write course id database (reverse lookup) to homeserver of courses 
 4948: # Is used in pickcourse
 4949: #
 4950:     foreach my $crs_home (keys(%courseidbuffer)) {
 4951:         my $response = &courseidput(&host_domain($crs_home),
 4952:                                     $courseidbuffer{$crs_home},
 4953:                                     $crs_home,'timeonly');
 4954:     }
 4955: #
 4956: # File accesses
 4957: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4958: #
 4959:     foreach my $entry (keys(%accesshash)) {
 4960:         if ($entry =~ /___count$/) {
 4961:             my ($dom,$name);
 4962:             ($dom,$name,undef)=
 4963: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4964:             if (! defined($dom) || $dom eq '' || 
 4965:                 ! defined($name) || $name eq '') {
 4966:                 my $cid = $env{'request.course.id'};
 4967: #
 4968: # FIXME 11/29/2021
 4969: # Typo in rev. 1.458 (2003/12/09)??
 4970: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
 4971: #
 4972: # While these ramain as  $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
 4973: # $dom and $name will always be null, so the &inc() call will default to storing this data
 4974: # in a nohist_accesscount.db file for the user rather than the course.
 4975: #
 4976: # That said there is a lot of noise in the data being stored.
 4977: # So counts for prtspool/  and adm/ etc. are recorded.
 4978: #
 4979: # A review of which items ending '___count' are written to %accesshash should likely be 
 4980: # made before deciding whether to set these to 'course.' instead of 'request.'
 4981: #
 4982: # Under the current scheme each user receives a nohist_accesscount.db file listing 
 4983: # accesses for things which are not published resources, regardless of course, and
 4984: # there is not a nohist_accesscount.db file in a course, which might log accesses from
 4985: # anyone in the course for things which are not published resources.
 4986: #
 4987: # For an author, nohist_accesscount.db ends up having records for other items
 4988: # mixed up with the legitimate access counts for the author's published resources.
 4989: #
 4990:                 $dom  = $env{'request.'.$cid.'.domain'};
 4991:                 $name = $env{'request.'.$cid.'.num'};
 4992:             }
 4993:             my $value = $accesshash{$entry};
 4994:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4995:             my %temphash=($url => $value);
 4996:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4997:             if ($result eq 'ok') {
 4998:                 delete $accesshash{$entry};
 4999:             }
 5000:         } else {
 5001:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 5002:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 5003:             my %temphash=($entry => $accesshash{$entry});
 5004:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 5005:                 delete $accesshash{$entry};
 5006:             }
 5007:         }
 5008:     }
 5009: #
 5010: # Roles
 5011: # Reverse lookup of user roles for course faculty/staff and co-authorship
 5012: #
 5013:     foreach my $entry (keys(%userrolehash)) {
 5014:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 5015: 	    split(/\:/,$entry);
 5016:         if (&Apache::lonnet::put('nohist_userroles',
 5017:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 5018:                 $rudom,$runame) eq 'ok') {
 5019: 	    delete $userrolehash{$entry};
 5020:         }
 5021:     }
 5022: #
 5023: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 5024: #
 5025:     my %domrolebuffer = ();
 5026:     foreach my $entry (keys(%domainrolehash)) {
 5027:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 5028:         if ($domrolebuffer{$rudom}) {
 5029:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5030:                       '='.&escape($domainrolehash{$entry});
 5031:         } else {
 5032:             $domrolebuffer{$rudom}.=&escape($entry).
 5033:                       '='.&escape($domainrolehash{$entry});
 5034:         }
 5035:         delete $domainrolehash{$entry};
 5036:     }
 5037:     foreach my $dom (keys(%domrolebuffer)) {
 5038: 	my %servers;
 5039: 	if (defined(&domain($dom,'primary'))) {
 5040: 	    my $primary=&domain($dom,'primary');
 5041: 	    my $hostname=&hostname($primary);
 5042: 	    $servers{$primary} = $hostname;
 5043: 	} else { 
 5044: 	    %servers = &get_servers($dom,'library');
 5045: 	}
 5046: 	foreach my $tryserver (keys(%servers)) {
 5047: 	    if (&reply('domroleput:'.$dom.':'.
 5048: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5049: 		last;
 5050: 	    } else {  
 5051: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5052: 	    }
 5053:         }
 5054:     }
 5055:     $dumpcount++;
 5056: }
 5057: 
 5058: sub courselog {
 5059:     my $what=shift;
 5060:     $what=time.':'.$what;
 5061:     unless ($env{'request.course.id'}) { return ''; }
 5062:     $coursedombuf{$env{'request.course.id'}}=
 5063:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5064:     $coursenumbuf{$env{'request.course.id'}}=
 5065:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5066:     $coursehombuf{$env{'request.course.id'}}=
 5067:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5068:     $coursedescrbuf{$env{'request.course.id'}}=
 5069:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5070:     $courseinstcodebuf{$env{'request.course.id'}}=
 5071:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5072:     $courseownerbuf{$env{'request.course.id'}}=
 5073:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5074:     $coursetypebuf{$env{'request.course.id'}}=
 5075:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5076:     if (defined $courselogs{$env{'request.course.id'}}) {
 5077: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5078:     } else {
 5079: 	$courselogs{$env{'request.course.id'}}.=$what;
 5080:     }
 5081:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5082: 	&flushcourselogs();
 5083:     }
 5084: }
 5085: 
 5086: sub courseacclog {
 5087:     my $fnsymb=shift;
 5088:     unless ($env{'request.course.id'}) { return ''; }
 5089:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5090:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5091:         $what.=':POST';
 5092:         # FIXME: Probably ought to escape things....
 5093: 	foreach my $key (keys(%env)) {
 5094:             if ($key=~/^form\.(.*)/) {
 5095:                 my $formitem = $1;
 5096:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5097:                     $what.=':'.$formitem.'='.$env{$key};
 5098:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5099:                     if ($formitem eq 'proctorpassword') {
 5100:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5101:                     } else {
 5102:                         $what.=':'.$formitem.'='.$env{$key};
 5103:                     }
 5104:                 }
 5105:             }
 5106:         }
 5107:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5108:         # FIXME: We should not be depending on a form parameter that someone
 5109:         # editing lonsearchcat.pm might change in the future.
 5110:         if ($env{'form.phase'} eq 'course_search') {
 5111:             $what.= ':POST';
 5112:             # FIXME: Probably ought to escape things....
 5113:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5114:                                  'crsdiscuss') {
 5115:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5116:             }
 5117:         }
 5118:     }
 5119:     &courselog($what);
 5120: }
 5121: 
 5122: sub countacc {
 5123:     my $url=&declutter(shift);
 5124:     return if (! defined($url) || $url eq '');
 5125:     unless ($env{'request.course.id'}) { return ''; }
 5126: #
 5127: # Mark that this url was used in this course
 5128: #
 5129:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5130: #
 5131: # Increase the access count for this resource in this child process
 5132: #
 5133:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5134:     $accesshash{$key}++;
 5135: }
 5136: 
 5137: sub linklog {
 5138:     my ($from,$to)=@_;
 5139:     $from=&declutter($from);
 5140:     $to=&declutter($to);
 5141:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5142:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5143: }
 5144: 
 5145: sub statslog {
 5146:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5147:     if ($users<2) { return; }
 5148:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5149:             'course'       => $env{'request.course.id'},
 5150:             'sections'     => '"all"',
 5151:             'num_students' => $users,
 5152:             'part'         => $part,
 5153:             'symb'         => $symb,
 5154:             'mean_tries'   => $av_attempts,
 5155:             'deg_of_diff'  => $degdiff});
 5156:     foreach my $key (keys(%dynstore)) {
 5157:         $accesshash{$key}=$dynstore{$key};
 5158:     }
 5159: }
 5160:   
 5161: sub userrolelog {
 5162:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5163:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5164:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5165:        $userrolehash
 5166:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5167:                     =$tend.':'.$tstart;
 5168:     }
 5169:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5170:        $userrolehash
 5171:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5172:                     =$tend.':'.$tstart;
 5173:     }
 5174:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5175:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5176:        $domainrolehash
 5177:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5178:                     = $tend.':'.$tstart;
 5179:     }
 5180: }
 5181: 
 5182: sub courserolelog {
 5183:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5184:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5185:         my $cdom = $1;
 5186:         my $cnum = $2;
 5187:         my $sec = $3;
 5188:         my $namespace = 'rolelog';
 5189:         my %storehash = (
 5190:                            role    => $trole,
 5191:                            start   => $tstart,
 5192:                            end     => $tend,
 5193:                            selfenroll => $selfenroll,
 5194:                            context    => $context,
 5195:                         );
 5196:         if ($trole eq 'gr') {
 5197:             $namespace = 'groupslog';
 5198:             $storehash{'group'} = $sec;
 5199:         } else {
 5200:             $storehash{'section'} = $sec;
 5201:         }
 5202:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5203:                    $domain,$cnum,$cdom);
 5204:         if (($trole ne 'st') || ($sec ne '')) {
 5205:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5206:         }
 5207:     }
 5208:     return;
 5209: }
 5210: 
 5211: sub domainrolelog {
 5212:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5213:     if ($area =~ m{^/($match_domain)/$}) {
 5214:         my $cdom = $1;
 5215:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 5216:         my $namespace = 'rolelog';
 5217:         my %storehash = (
 5218:                            role    => $trole,
 5219:                            start   => $tstart,
 5220:                            end     => $tend,
 5221:                            context => $context,
 5222:                         );
 5223:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5224:                    $domain,$domconfiguser,$cdom);
 5225:     }
 5226:     return;
 5227: 
 5228: }
 5229: 
 5230: sub coauthorrolelog {
 5231:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5232:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5233:         my $audom = $1;
 5234:         my $auname = $2;
 5235:         my $namespace = 'rolelog';
 5236:         my %storehash = (
 5237:                            role    => $trole,
 5238:                            start   => $tstart,
 5239:                            end     => $tend,
 5240:                            context => $context,
 5241:                         );
 5242:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5243:                    $domain,$auname,$audom);
 5244:     }
 5245:     return;
 5246: }
 5247: 
 5248: sub get_course_adv_roles {
 5249:     my ($cid,$codes) = @_;
 5250:     $cid=$env{'request.course.id'} unless (defined($cid));
 5251:     my %coursehash=&coursedescription($cid);
 5252:     my $crstype = &Apache::loncommon::course_type($cid);
 5253:     my %nothide=();
 5254:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5255:         if ($user !~ /:/) {
 5256: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5257:         } else {
 5258:             $nothide{$user}=1;
 5259:         }
 5260:     }
 5261:     my @possdoms = ($coursehash{'domain'});
 5262:     if ($coursehash{'checkforpriv'}) {
 5263:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5264:     }
 5265:     my %returnhash=();
 5266:     my %dumphash=
 5267:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5268:     my $now=time;
 5269:     my %privileged;
 5270:     foreach my $entry (keys(%dumphash)) {
 5271: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5272:         if (($tstart) && ($tstart<0)) { next; }
 5273:         if (($tend) && ($tend<$now)) { next; }
 5274:         if (($tstart) && ($now<$tstart)) { next; }
 5275:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5276: 	if ($username eq '' || $domain eq '') { next; }
 5277:         if ((&privileged($username,$domain,\@possdoms)) &&
 5278:             (!$nothide{$username.':'.$domain})) { next; }
 5279: 	if ($role eq 'cr') { next; }
 5280:         if ($codes) {
 5281:             if ($section) { $role .= ':'.$section; }
 5282:             if ($returnhash{$role}) {
 5283:                 $returnhash{$role}.=','.$username.':'.$domain;
 5284:             } else {
 5285:                 $returnhash{$role}=$username.':'.$domain;
 5286:             }
 5287:         } else {
 5288:             my $key=&plaintext($role,$crstype);
 5289:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5290:             if ($returnhash{$key}) {
 5291: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5292:             } else {
 5293:                 $returnhash{$key}=$username.':'.$domain;
 5294:             }
 5295:         }
 5296:     }
 5297:     return %returnhash;
 5298: }
 5299: 
 5300: sub get_my_roles {
 5301:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5302:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5303:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5304:     my (%dumphash,%nothide);
 5305:     if ($context eq 'userroles') {
 5306:         %dumphash = &dump('roles',$udom,$uname);
 5307:     } else {
 5308:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5309:         if ($hidepriv) {
 5310:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5311:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5312:                 if ($user !~ /:/) {
 5313:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5314:                 } else {
 5315:                     $nothide{$user} = 1;
 5316:                 }
 5317:             }
 5318:         }
 5319:     }
 5320:     my %returnhash=();
 5321:     my $now=time;
 5322:     my %privileged;
 5323:     foreach my $entry (keys(%dumphash)) {
 5324:         my ($role,$tend,$tstart);
 5325:         if ($context eq 'userroles') {
 5326:             next if ($entry =~ /^rolesdef/);
 5327: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5328:         } else {
 5329:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5330:         }
 5331:         if (($tstart) && ($tstart<0)) { next; }
 5332:         my $status = 'active';
 5333:         if (($tend) && ($tend<=$now)) {
 5334:             $status = 'previous';
 5335:         } 
 5336:         if (($tstart) && ($now<$tstart)) {
 5337:             $status = 'future';
 5338:         }
 5339:         if (ref($types) eq 'ARRAY') {
 5340:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5341:                 next;
 5342:             } 
 5343:         } else {
 5344:             if ($status ne 'active') {
 5345:                 next;
 5346:             }
 5347:         }
 5348:         my ($rolecode,$username,$domain,$section,$area);
 5349:         if ($context eq 'userroles') {
 5350:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5351:             (undef,$domain,$username,$section) = split(/\//,$area);
 5352:         } else {
 5353:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5354:         }
 5355:         if (ref($roledoms) eq 'ARRAY') {
 5356:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5357:                 next;
 5358:             }
 5359:         }
 5360:         if (ref($roles) eq 'ARRAY') {
 5361:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5362:                 if ($role =~ /^cr\//) {
 5363:                     if (!grep(/^cr$/,@{$roles})) {
 5364:                         next;
 5365:                     }
 5366:                 } elsif ($role =~ /^gr\//) {
 5367:                     if (!grep(/^gr$/,@{$roles})) {
 5368:                         next;
 5369:                     }
 5370:                 } else {
 5371:                     next;
 5372:                 }
 5373:             }
 5374:         }
 5375:         if ($hidepriv) {
 5376:             my @privroles = ('dc','su');
 5377:             if ($context eq 'userroles') {
 5378:                 next if (grep(/^\Q$role\E$/,@privroles));
 5379:             } else {
 5380:                 my $possdoms = [$domain];
 5381:                 if (ref($roledoms) eq 'ARRAY') {
 5382:                    push(@{$possdoms},@{$roledoms}); 
 5383:                 }
 5384:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5385:                     if (!$nothide{$username.':'.$domain}) {
 5386:                         next;
 5387:                     }
 5388:                 }
 5389:             }
 5390:         }
 5391:         if ($withsec) {
 5392:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5393:                 $tstart.':'.$tend;
 5394:         } else {
 5395:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5396:         }
 5397:     }
 5398:     return %returnhash;
 5399: }
 5400: 
 5401: sub get_all_adhocroles {
 5402:     my ($dom) = @_;
 5403:     my @roles_by_num = ();
 5404:     my %domdefaults = &get_domain_defaults($dom);
 5405:     my (%description,%access_in_dom,%access_info);
 5406:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5407:         my $count = 0;
 5408:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5409:         my %ordered;
 5410:         foreach my $role (sort(keys(%domcurrent))) {
 5411:             my ($order,$desc,$access_in_dom);
 5412:             if (ref($domcurrent{$role}) eq 'HASH') {
 5413:                 $order = $domcurrent{$role}{'order'};
 5414:                 $desc = $domcurrent{$role}{'desc'};
 5415:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5416:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5417:             }
 5418:             if ($order eq '') {
 5419:                 $order = $count;
 5420:             }
 5421:             $ordered{$order} = $role;
 5422:             if ($desc ne '') {
 5423:                 $description{$role} = $desc;
 5424:             } else {
 5425:                 $description{$role}= $role;
 5426:             }
 5427:             $count++;
 5428:         }
 5429:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5430:             push(@roles_by_num,$ordered{$item});
 5431:         }
 5432:     }
 5433:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5434: }
 5435: 
 5436: sub get_my_adhocroles {
 5437:     my ($cid,$checkreg) = @_;
 5438:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5439:     if ($env{'request.course.id'} eq $cid) {
 5440:         $cdom = $env{'course.'.$cid.'.domain'};
 5441:         $cnum = $env{'course.'.$cid.'.num'};
 5442:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5443:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5444:         $cdom = $1;
 5445:         $cnum = $2;
 5446:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5447:                                      $cdom,$cnum);
 5448:     }
 5449:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5450:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5451:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5452:         if ($rosterhash{$user} ne '') {
 5453:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5454:             return ([],{}) if ($type eq 'auto');
 5455:         }
 5456:     }
 5457:     if (($cdom ne '') && ($cnum ne ''))  {
 5458:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5459:             my $then=$env{'user.login.time'};
 5460:             my $update=$env{'user.update.time'};
 5461:             if (!$update) {
 5462:                 $update = $then;
 5463:             }
 5464:             my @liveroles;
 5465:             foreach my $role ('dh','da') {
 5466:                 if ($env{"user.role.$role./$cdom/"}) {
 5467:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5468:                     my $limit = $update;
 5469:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5470:                         $limit = $then;
 5471:                     }
 5472:                     my $activerole = 1;
 5473:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5474:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5475:                     if ($activerole) {
 5476:                         push(@liveroles,$role);
 5477:                     }
 5478:                 }
 5479:             }
 5480:             if (@liveroles) {
 5481:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5482:                     my ($accessref,$accessinfo,%access_in_dom);
 5483:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5484:                     if (ref($roles_by_num) eq 'ARRAY') {
 5485:                         if (@{$roles_by_num}) {
 5486:                             my %settings;
 5487:                             if ($env{'request.course.id'} eq $cid) {
 5488:                                 foreach my $envkey (keys(%env)) {
 5489:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5490:                                         $settings{$1} = $env{$envkey};
 5491:                                     }
 5492:                                 }
 5493:                             } else {
 5494:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5495:                             }
 5496:                             my %setincrs;
 5497:                             if ($settings{'internal.adhocaccess'}) {
 5498:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5499:                             }
 5500:                             my @statuses;
 5501:                             if ($env{'environment.inststatus'}) {
 5502:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5503:                             }
 5504:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5505:                             if (ref($accessref) eq 'HASH') {
 5506:                                 %access_in_dom = %{$accessref};
 5507:                             }
 5508:                             foreach my $role (@{$roles_by_num}) {
 5509:                                 my ($curraccess,@okstatus,@personnel);
 5510:                                 if ($setincrs{$role}) {
 5511:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5512:                                     if ($curraccess eq 'status') {
 5513:                                         @okstatus = split(/\&/,$rest);
 5514:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5515:                                         @personnel = split(/\&/,$rest);
 5516:                                     }
 5517:                                 } else {
 5518:                                     $curraccess = $access_in_dom{$role};
 5519:                                     if (ref($accessinfo) eq 'HASH') {
 5520:                                         if ($curraccess eq 'status') {
 5521:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5522:                                                 @okstatus = @{$accessinfo->{$role}};
 5523:                                             }
 5524:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5525:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5526:                                                 @personnel = @{$accessinfo->{$role}};
 5527:                                             }
 5528:                                         }
 5529:                                     }
 5530:                                 }
 5531:                                 if ($curraccess eq 'none') {
 5532:                                     next;
 5533:                                 } elsif ($curraccess eq 'all') {
 5534:                                     push(@possroles,$role);
 5535:                                 } elsif ($curraccess eq 'dh') {
 5536:                                     if (grep(/^dh$/,@liveroles)) {
 5537:                                         push(@possroles,$role);
 5538:                                     } else {
 5539:                                         next;
 5540:                                     }
 5541:                                 } elsif ($curraccess eq 'da') {
 5542:                                     if (grep(/^da$/,@liveroles)) {
 5543:                                         push(@possroles,$role);
 5544:                                     } else {
 5545:                                         next;
 5546:                                     }
 5547:                                 } elsif ($curraccess eq 'status') {
 5548:                                     if (@okstatus) {
 5549:                                         if (!@statuses) {
 5550:                                             if (grep(/^default$/,@okstatus)) {
 5551:                                                 push(@possroles,$role);
 5552:                                             }
 5553:                                         } else {
 5554:                                             foreach my $status (@okstatus) {
 5555:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5556:                                                     push(@possroles,$role);
 5557:                                                     last;
 5558:                                                 }
 5559:                                             }
 5560:                                         }
 5561:                                     }
 5562:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5563:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5564:                                         if ($curraccess eq 'exc') {
 5565:                                             push(@possroles,$role);
 5566:                                         }
 5567:                                     } elsif ($curraccess eq 'inc') {
 5568:                                         push(@possroles,$role);
 5569:                                     }
 5570:                                 }
 5571:                             }
 5572:                         }
 5573:                     }
 5574:                 }
 5575:             }
 5576:         }
 5577:     }
 5578:     unless (ref($description) eq 'HASH') {
 5579:         if (ref($roles_by_num) eq 'ARRAY') {
 5580:             my %desc;
 5581:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5582:             $description = \%desc;
 5583:         } else {
 5584:             $description = {};
 5585:         }
 5586:     }
 5587:     return (\@possroles,$description);
 5588: }
 5589: 
 5590: # ----------------------------------------------------- Frontpage Announcements
 5591: #
 5592: #
 5593: 
 5594: sub postannounce {
 5595:     my ($server,$text)=@_;
 5596:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5597:     unless ($text=~/\w/) { $text=''; }
 5598:     return &reply('setannounce:'.&escape($text),$server);
 5599: }
 5600: 
 5601: sub getannounce {
 5602: 
 5603:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5604: 	my $announcement='';
 5605: 	while (my $line = <$fh>) { $announcement .= $line; }
 5606: 	close($fh);
 5607: 	if ($announcement=~/\w/) { 
 5608: 	    return 
 5609:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5610:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5611: 	} else {
 5612: 	    return '';
 5613: 	}
 5614:     } else {
 5615: 	return '';
 5616:     }
 5617: }
 5618: 
 5619: # ---------------------------------------------------------- Course ID routines
 5620: # Deal with domain's nohist_courseid.db files
 5621: #
 5622: 
 5623: sub courseidput {
 5624:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5625:     return unless (ref($storehash) eq 'HASH');
 5626:     my $outcome;
 5627:     if ($caller eq 'timeonly') {
 5628:         my $cids = '';
 5629:         foreach my $item (keys(%$storehash)) {
 5630:             $cids.=&escape($item).'&';
 5631:         }
 5632:         $cids=~s/\&$//;
 5633:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5634:                           $coursehome);       
 5635:     } else {
 5636:         my $items = '';
 5637:         foreach my $item (keys(%$storehash)) {
 5638:             $items.= &escape($item).'='.
 5639:                      &freeze_escape($$storehash{$item}).'&';
 5640:         }
 5641:         $items=~s/\&$//;
 5642:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5643:                           $coursehome);
 5644:     }
 5645:     if ($outcome eq 'unknown_cmd') {
 5646:         my $what;
 5647:         foreach my $cid (keys(%$storehash)) {
 5648:             $what .= &escape($cid).'=';
 5649:             foreach my $item ('description','inst_code','owner','type') {
 5650:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5651:             }
 5652:             $what =~ s/\:$/&/;
 5653:         }
 5654:         $what =~ s/\&$//;  
 5655:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5656:     } else {
 5657:         return $outcome;
 5658:     }
 5659: }
 5660: 
 5661: sub courseiddump {
 5662:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5663:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5664:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5665:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5666:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5667:     my $as_hash = 1;
 5668:     my %returnhash;
 5669:     if (!$domfilter) { $domfilter=''; }
 5670:     my %libserv = &all_library();
 5671:     foreach my $tryserver (keys(%libserv)) {
 5672:         if ( (  $hostidflag == 1 
 5673: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5674: 	     || (!defined($hostidflag)) ) {
 5675: 
 5676: 	    if (($domfilter eq '') ||
 5677: 		(&host_domain($tryserver) eq $domfilter)) {
 5678:                 my $rep;
 5679:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5680:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5681:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5682:                                 &escape($descfilter), &escape($instcodefilter), 
 5683:                                 &escape($ownerfilter), &escape($coursefilter),
 5684:                                 &escape($typefilter), &escape($regexp_ok), 
 5685:                                 $as_hash, &escape($selfenrollonly), 
 5686:                                 &escape($catfilter), $showhidden, $caller, 
 5687:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5688:                                 &escape($createdbefore), &escape($createdafter), 
 5689:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5690:                                 $reqcrsdom,&escape($reqinstcode))));
 5691:                 } else {
 5692:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5693:                              $sincefilter.':'.&escape($descfilter).':'.
 5694:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5695:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5696:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5697:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5698:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5699:                              &escape($cc_clone).':'.$cloneonly.':'.
 5700:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5701:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5702:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5703:                 }
 5704:                      
 5705:                 my @pairs=split(/\&/,$rep);
 5706:                 foreach my $item (@pairs) {
 5707:                     my ($key,$value)=split(/\=/,$item,2);
 5708:                     $key = &unescape($key);
 5709:                     next if ($key =~ /^error: 2 /);
 5710:                     my $result = &thaw_unescape($value);
 5711:                     if (ref($result) eq 'HASH') {
 5712:                         $returnhash{$key}=$result;
 5713:                     } else {
 5714:                         my @responses = split(/:/,$value);
 5715:                         my @items = ('description','inst_code','owner','type');
 5716:                         for (my $i=0; $i<@responses; $i++) {
 5717:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5718:                         }
 5719:                     }
 5720:                 }
 5721:             }
 5722:         }
 5723:     }
 5724:     return %returnhash;
 5725: }
 5726: 
 5727: sub courselastaccess {
 5728:     my ($cdom,$cnum,$hostidref) = @_;
 5729:     my %returnhash;
 5730:     if ($cdom && $cnum) {
 5731:         my $chome = &homeserver($cnum,$cdom);
 5732:         if ($chome ne 'no_host') {
 5733:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5734:             &extract_lastaccess(\%returnhash,$rep);
 5735:         }
 5736:     } else {
 5737:         if (!$cdom) { $cdom=''; }
 5738:         my %libserv = &all_library();
 5739:         foreach my $tryserver (keys(%libserv)) {
 5740:             if (ref($hostidref) eq 'ARRAY') {
 5741:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5742:             } 
 5743:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5744:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5745:                 &extract_lastaccess(\%returnhash,$rep);
 5746:             }
 5747:         }
 5748:     }
 5749:     return %returnhash;
 5750: }
 5751: 
 5752: sub extract_lastaccess {
 5753:     my ($returnhash,$rep) = @_;
 5754:     if (ref($returnhash) eq 'HASH') {
 5755:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5756:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5757:                  $rep eq '') {
 5758:             my @pairs=split(/\&/,$rep);
 5759:             foreach my $item (@pairs) {
 5760:                 my ($key,$value)=split(/\=/,$item,2);
 5761:                 $key = &unescape($key);
 5762:                 next if ($key =~ /^error: 2 /);
 5763:                 $returnhash->{$key} = &thaw_unescape($value);
 5764:             }
 5765:         }
 5766:     }
 5767:     return;
 5768: }
 5769: 
 5770: # ---------------------------------------------------------- DC e-mail
 5771: 
 5772: sub dcmailput {
 5773:     my ($domain,$msgid,$message,$server)=@_;
 5774:     my $status = &Apache::lonnet::critical(
 5775:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5776:        &escape($message),$server);
 5777:     return $status;
 5778: }
 5779: 
 5780: sub dcmaildump {
 5781:     my ($dom,$startdate,$enddate,$senders) = @_;
 5782:     my %returnhash=();
 5783: 
 5784:     if (defined(&domain($dom,'primary'))) {
 5785:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5786:                                                          &escape($enddate).':';
 5787: 	my @esc_senders=map { &escape($_)} @$senders;
 5788: 	$cmd.=&escape(join('&',@esc_senders));
 5789: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5790:             my ($key,$value) = split(/\=/,$line,2);
 5791:             if (($key) && ($value)) {
 5792:                 $returnhash{&unescape($key)} = &unescape($value);
 5793:             }
 5794:         }
 5795:     }
 5796:     return %returnhash;
 5797: }
 5798: # ---------------------------------------------------------- Domain roles
 5799: 
 5800: sub get_domain_roles {
 5801:     my ($dom,$roles,$startdate,$enddate)=@_;
 5802:     if ((!defined($startdate)) || ($startdate eq '')) {
 5803:         $startdate = '.';
 5804:     }
 5805:     if ((!defined($enddate)) || ($enddate eq '')) {
 5806:         $enddate = '.';
 5807:     }
 5808:     my $rolelist;
 5809:     if (ref($roles) eq 'ARRAY') {
 5810:         $rolelist = join('&',@{$roles});
 5811:     }
 5812:     my %personnel = ();
 5813: 
 5814:     my %servers = &get_servers($dom,'library');
 5815:     foreach my $tryserver (keys(%servers)) {
 5816: 	%{$personnel{$tryserver}}=();
 5817: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5818: 					    &escape($startdate).':'.
 5819: 					    &escape($enddate).':'.
 5820: 					    &escape($rolelist), $tryserver))) {
 5821: 	    my ($key,$value) = split(/\=/,$line,2);
 5822: 	    if (($key) && ($value)) {
 5823: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5824: 	    }
 5825: 	}
 5826:     }
 5827:     return %personnel;
 5828: }
 5829: 
 5830: sub get_active_domroles {
 5831:     my ($dom,$roles) = @_;
 5832:     return () unless (ref($roles) eq 'ARRAY');
 5833:     my $now = time;
 5834:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5835:     my %domroles;
 5836:     foreach my $server (keys(%dompersonnel)) {
 5837:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5838:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5839:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5840:         }
 5841:     }
 5842:     return %domroles;
 5843: }
 5844: 
 5845: # ----------------------------------------------------------- Interval timing 
 5846: 
 5847: {
 5848: # Caches needed for speedup of navmaps
 5849: # We don't want to cache this for very long at all (5 seconds at most)
 5850: # 
 5851: # The user for whom we cache
 5852: my $cachedkey='';
 5853: # The cached times for this user
 5854: my %cachedtimes=();
 5855: # When this was last done
 5856: my $cachedtime='';
 5857: 
 5858: sub load_all_first_access {
 5859:     my ($uname,$udom,$ignorecache)=@_;
 5860:     if (($cachedkey eq $uname.':'.$udom) &&
 5861:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5862:         (!$ignorecache)) {
 5863:         return;
 5864:     }
 5865:     $cachedtime=time;
 5866:     $cachedkey=$uname.':'.$udom;
 5867:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5868: }
 5869: 
 5870: sub get_first_access {
 5871:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5872:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5873:     if ($argsymb) { $symb=$argsymb; }
 5874:     my ($map,$id,$res)=&decode_symb($symb);
 5875:     if ($argmap) { $map = $argmap; }
 5876:     if ($type eq 'course') {
 5877: 	$res='course';
 5878:     } elsif ($type eq 'map') {
 5879: 	$res=&symbread($map);
 5880:     } else {
 5881: 	$res=$symb;
 5882:     }
 5883:     &load_all_first_access($uname,$udom,$ignorecache);
 5884:     return $cachedtimes{"$courseid\0$res"};
 5885: }
 5886: 
 5887: sub set_first_access {
 5888:     my ($type,$interval)=@_;
 5889:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5890:     my ($map,$id,$res)=&decode_symb($symb);
 5891:     if ($type eq 'course') {
 5892: 	$res='course';
 5893:     } elsif ($type eq 'map') {
 5894: 	$res=&symbread($map);
 5895:     } else {
 5896: 	$res=$symb;
 5897:     }
 5898:     $cachedkey='';
 5899:     my $firstaccess=&get_first_access($type,$symb,$map);
 5900:     if ($firstaccess) {
 5901:         &logthis("First access time already set ($firstaccess) when attempting ".
 5902:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5903:                  "in $courseid");
 5904:         return 'already_set';
 5905:     } else {
 5906:         my $start = time;
 5907: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5908:                           $udom,$uname);
 5909:         if ($putres eq 'ok') {
 5910:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5911:                  $udom,$uname); 
 5912:             &appenv(
 5913:                      {
 5914:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5915:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5916:                      }
 5917:                   );
 5918:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5919:                 $cachedtimes{"$courseid\0$res"} = $start;
 5920:             }
 5921:         } elsif ($putres ne 'refused') {
 5922:             &logthis("Result: $putres when attempting to set first access time ".
 5923:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5924:         }
 5925:         return $putres;
 5926:     }
 5927:     return 'already_set';
 5928: }
 5929: }
 5930: 
 5931: # --------------------------------------------- Set Expire Date for Spreadsheet
 5932: 
 5933: sub expirespread {
 5934:     my ($uname,$udom,$stype,$usymb)=@_;
 5935:     my $cid=$env{'request.course.id'}; 
 5936:     if ($cid) {
 5937:        my $now=time;
 5938:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5939:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5940:                             $env{'course.'.$cid.'.num'}.
 5941: 	        	    ':nohist_expirationdates:'.
 5942:                             &escape($key).'='.$now,
 5943:                             $env{'course.'.$cid.'.home'})
 5944:     }
 5945:     return 'ok';
 5946: }
 5947: 
 5948: # ----------------------------------------------------- Devalidate Spreadsheets
 5949: 
 5950: sub devalidate {
 5951:     my ($symb,$uname,$udom)=@_;
 5952:     my $cid=$env{'request.course.id'}; 
 5953:     if ($cid) {
 5954:         # delete the stored spreadsheets for
 5955:         # - the student level sheet of this user in course's homespace
 5956:         # - the assessment level sheet for this resource 
 5957:         #   for this user in user's homespace
 5958: 	# - current conditional state info
 5959: 	my $key=$uname.':'.$udom.':';
 5960:         my $status=
 5961: 	    &del('nohist_calculatedsheets',
 5962: 		 [$key.'studentcalc:'],
 5963: 		 $env{'course.'.$cid.'.domain'},
 5964: 		 $env{'course.'.$cid.'.num'})
 5965: 		.' '.
 5966: 	    &del('nohist_calculatedsheets_'.$cid,
 5967: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5968:         unless ($status eq 'ok ok') {
 5969:            &logthis('Could not devalidate spreadsheet '.
 5970:                     $uname.' at '.$udom.' for '.
 5971: 		    $symb.': '.$status);
 5972:         }
 5973: 	&delenv('user.state.'.$cid);
 5974:     }
 5975: }
 5976: 
 5977: sub get_scalar {
 5978:     my ($string,$end) = @_;
 5979:     my $value;
 5980:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5981: 	$value = $1;
 5982:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5983: 	$value = $1;
 5984:     }
 5985:     return &unescape($value);
 5986: }
 5987: 
 5988: sub array2str {
 5989:   my (@array) = @_;
 5990:   my $result=&arrayref2str(\@array);
 5991:   $result=~s/^__ARRAY_REF__//;
 5992:   $result=~s/__END_ARRAY_REF__$//;
 5993:   return $result;
 5994: }
 5995: 
 5996: sub arrayref2str {
 5997:   my ($arrayref) = @_;
 5998:   my $result='__ARRAY_REF__';
 5999:   foreach my $elem (@$arrayref) {
 6000:     if(ref($elem) eq 'ARRAY') {
 6001:       $result.=&arrayref2str($elem).'&';
 6002:     } elsif(ref($elem) eq 'HASH') {
 6003:       $result.=&hashref2str($elem).'&';
 6004:     } elsif(ref($elem)) {
 6005:       #print("Got a ref of ".(ref($elem))." skipping.");
 6006:     } else {
 6007:       $result.=&escape($elem).'&';
 6008:     }
 6009:   }
 6010:   $result=~s/\&$//;
 6011:   $result .= '__END_ARRAY_REF__';
 6012:   return $result;
 6013: }
 6014: 
 6015: sub hash2str {
 6016:   my (%hash) = @_;
 6017:   my $result=&hashref2str(\%hash);
 6018:   $result=~s/^__HASH_REF__//;
 6019:   $result=~s/__END_HASH_REF__$//;
 6020:   return $result;
 6021: }
 6022: 
 6023: sub hashref2str {
 6024:   my ($hashref)=@_;
 6025:   my $result='__HASH_REF__';
 6026:   foreach my $key (sort(keys(%$hashref))) {
 6027:     if (ref($key) eq 'ARRAY') {
 6028:       $result.=&arrayref2str($key).'=';
 6029:     } elsif (ref($key) eq 'HASH') {
 6030:       $result.=&hashref2str($key).'=';
 6031:     } elsif (ref($key)) {
 6032:       $result.='=';
 6033:       #print("Got a ref of ".(ref($key))." skipping.");
 6034:     } else {
 6035: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6036:     }
 6037: 
 6038:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6039:       $result.=&arrayref2str($hashref->{$key}).'&';
 6040:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6041:       $result.=&hashref2str($hashref->{$key}).'&';
 6042:     } elsif(ref($hashref->{$key})) {
 6043:        $result.='&';
 6044:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6045:     } else {
 6046:       $result.=&escape($hashref->{$key}).'&';
 6047:     }
 6048:   }
 6049:   $result=~s/\&$//;
 6050:   $result .= '__END_HASH_REF__';
 6051:   return $result;
 6052: }
 6053: 
 6054: sub str2hash {
 6055:     my ($string)=@_;
 6056:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6057:     return %$hash;
 6058: }
 6059: 
 6060: sub str2hashref {
 6061:   my ($string) = @_;
 6062: 
 6063:   my %hash;
 6064: 
 6065:   if($string !~ /^__HASH_REF__/) {
 6066:       if (! ($string eq '' || !defined($string))) {
 6067: 	  $hash{'error'}='Not hash reference';
 6068:       }
 6069:       return (\%hash, $string);
 6070:   }
 6071: 
 6072:   $string =~ s/^__HASH_REF__//;
 6073: 
 6074:   while($string !~ /^__END_HASH_REF__/) {
 6075:       #key
 6076:       my $key='';
 6077:       if($string =~ /^__HASH_REF__/) {
 6078:           ($key, $string)=&str2hashref($string);
 6079:           if(defined($key->{'error'})) {
 6080:               $hash{'error'}='Bad data';
 6081:               return (\%hash, $string);
 6082:           }
 6083:       } elsif($string =~ /^__ARRAY_REF__/) {
 6084:           ($key, $string)=&str2arrayref($string);
 6085:           if($key->[0] eq 'Array reference error') {
 6086:               $hash{'error'}='Bad data';
 6087:               return (\%hash, $string);
 6088:           }
 6089:       } else {
 6090:           $string =~ s/^(.*?)=//;
 6091: 	  $key=&unescape($1);
 6092:       }
 6093:       $string =~ s/^=//;
 6094: 
 6095:       #value
 6096:       my $value='';
 6097:       if($string =~ /^__HASH_REF__/) {
 6098:           ($value, $string)=&str2hashref($string);
 6099:           if(defined($value->{'error'})) {
 6100:               $hash{'error'}='Bad data';
 6101:               return (\%hash, $string);
 6102:           }
 6103:       } elsif($string =~ /^__ARRAY_REF__/) {
 6104:           ($value, $string)=&str2arrayref($string);
 6105:           if($value->[0] eq 'Array reference error') {
 6106:               $hash{'error'}='Bad data';
 6107:               return (\%hash, $string);
 6108:           }
 6109:       } else {
 6110: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6111:       }
 6112:       $string =~ s/^&//;
 6113: 
 6114:       $hash{$key}=$value;
 6115:   }
 6116: 
 6117:   $string =~ s/^__END_HASH_REF__//;
 6118: 
 6119:   return (\%hash, $string);
 6120: }
 6121: 
 6122: sub str2array {
 6123:     my ($string)=@_;
 6124:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6125:     return @$array;
 6126: }
 6127: 
 6128: sub str2arrayref {
 6129:   my ($string) = @_;
 6130:   my @array;
 6131: 
 6132:   if($string !~ /^__ARRAY_REF__/) {
 6133:       if (! ($string eq '' || !defined($string))) {
 6134: 	  $array[0]='Array reference error';
 6135:       }
 6136:       return (\@array, $string);
 6137:   }
 6138: 
 6139:   $string =~ s/^__ARRAY_REF__//;
 6140: 
 6141:   while($string !~ /^__END_ARRAY_REF__/) {
 6142:       my $value='';
 6143:       if($string =~ /^__HASH_REF__/) {
 6144:           ($value, $string)=&str2hashref($string);
 6145:           if(defined($value->{'error'})) {
 6146:               $array[0] ='Array reference error';
 6147:               return (\@array, $string);
 6148:           }
 6149:       } elsif($string =~ /^__ARRAY_REF__/) {
 6150:           ($value, $string)=&str2arrayref($string);
 6151:           if($value->[0] eq 'Array reference error') {
 6152:               $array[0] ='Array reference error';
 6153:               return (\@array, $string);
 6154:           }
 6155:       } else {
 6156: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6157:       }
 6158:       $string =~ s/^&//;
 6159: 
 6160:       push(@array, $value);
 6161:   }
 6162: 
 6163:   $string =~ s/^__END_ARRAY_REF__//;
 6164: 
 6165:   return (\@array, $string);
 6166: }
 6167: 
 6168: # -------------------------------------------------------------------Temp Store
 6169: 
 6170: sub tmpreset {
 6171:   my ($symb,$namespace,$domain,$stuname) = @_;
 6172:   if (!$symb) {
 6173:     $symb=&symbread();
 6174:     if (!$symb) { $symb= $env{'request.url'}; }
 6175:   }
 6176:   $symb=escape($symb);
 6177: 
 6178:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6179:   $namespace=~s/\//\_/g;
 6180:   $namespace=~s/\W//g;
 6181: 
 6182:   if (!$domain) { $domain=$env{'user.domain'}; }
 6183:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6184:   if ($domain eq 'public' && $stuname eq 'public') {
 6185:       $stuname=&get_requestor_ip();
 6186:   }
 6187:   my $path=LONCAPA::tempdir();
 6188:   my %hash;
 6189:   if (tie(%hash,'GDBM_File',
 6190: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6191: 	  &GDBM_WRCREAT(),0640)) {
 6192:     foreach my $key (keys(%hash)) {
 6193:       if ($key=~ /:$symb/) {
 6194: 	delete($hash{$key});
 6195:       }
 6196:     }
 6197:   }
 6198: }
 6199: 
 6200: sub tmpstore {
 6201:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6202: 
 6203:   if (!$symb) {
 6204:     $symb=&symbread();
 6205:     if (!$symb) { $symb= $env{'request.url'}; }
 6206:   }
 6207:   $symb=escape($symb);
 6208: 
 6209:   if (!$namespace) {
 6210:     # I don't think we would ever want to store this for a course.
 6211:     # it seems this will only be used if we don't have a course.
 6212:     #$namespace=$env{'request.course.id'};
 6213:     #if (!$namespace) {
 6214:       $namespace=$env{'request.state'};
 6215:     #}
 6216:   }
 6217:   $namespace=~s/\//\_/g;
 6218:   $namespace=~s/\W//g;
 6219:   if (!$domain) { $domain=$env{'user.domain'}; }
 6220:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6221:   if ($domain eq 'public' && $stuname eq 'public') {
 6222:       $stuname=&get_requestor_ip();
 6223:   }
 6224:   my $now=time;
 6225:   my %hash;
 6226:   my $path=LONCAPA::tempdir();
 6227:   if (tie(%hash,'GDBM_File',
 6228: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6229: 	  &GDBM_WRCREAT(),0640)) {
 6230:     $hash{"version:$symb"}++;
 6231:     my $version=$hash{"version:$symb"};
 6232:     my $allkeys=''; 
 6233:     foreach my $key (keys(%$storehash)) {
 6234:       $allkeys.=$key.':';
 6235:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6236:     }
 6237:     $hash{"$version:$symb:timestamp"}=$now;
 6238:     $allkeys.='timestamp';
 6239:     $hash{"$version:keys:$symb"}=$allkeys;
 6240:     if (untie(%hash)) {
 6241:       return 'ok';
 6242:     } else {
 6243:       return "error:$!";
 6244:     }
 6245:   } else {
 6246:     return "error:$!";
 6247:   }
 6248: }
 6249: 
 6250: # -----------------------------------------------------------------Temp Restore
 6251: 
 6252: sub tmprestore {
 6253:   my ($symb,$namespace,$domain,$stuname) = @_;
 6254: 
 6255:   if (!$symb) {
 6256:     $symb=&symbread();
 6257:     if (!$symb) { $symb= $env{'request.url'}; }
 6258:   }
 6259:   $symb=escape($symb);
 6260: 
 6261:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6262: 
 6263:   if (!$domain) { $domain=$env{'user.domain'}; }
 6264:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6265:   if ($domain eq 'public' && $stuname eq 'public') {
 6266:       $stuname=&get_requestor_ip();
 6267:   }
 6268:   my %returnhash;
 6269:   $namespace=~s/\//\_/g;
 6270:   $namespace=~s/\W//g;
 6271:   my %hash;
 6272:   my $path=LONCAPA::tempdir();
 6273:   if (tie(%hash,'GDBM_File',
 6274: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6275: 	  &GDBM_READER(),0640)) {
 6276:     my $version=$hash{"version:$symb"};
 6277:     $returnhash{'version'}=$version;
 6278:     my $scope;
 6279:     for ($scope=1;$scope<=$version;$scope++) {
 6280:       my $vkeys=$hash{"$scope:keys:$symb"};
 6281:       my @keys=split(/:/,$vkeys);
 6282:       my $key;
 6283:       $returnhash{"$scope:keys"}=$vkeys;
 6284:       foreach $key (@keys) {
 6285: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6286: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6287:       }
 6288:     }
 6289:     if (!(untie(%hash))) {
 6290:       return "error:$!";
 6291:     }
 6292:   } else {
 6293:     return "error:$!";
 6294:   }
 6295:   return %returnhash;
 6296: }
 6297: 
 6298: # ----------------------------------------------------------------------- Store
 6299: 
 6300: sub store {
 6301:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6302:     my $home='';
 6303: 
 6304:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6305: 
 6306:     $symb=&symbclean($symb);
 6307:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6308: 
 6309:     if (!$domain) { $domain=$env{'user.domain'}; }
 6310:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6311: 
 6312:     &devalidate($symb,$stuname,$domain);
 6313: 
 6314:     $symb=escape($symb);
 6315:     if (!$namespace) { 
 6316:        unless ($namespace=$env{'request.course.id'}) { 
 6317:           return ''; 
 6318:        } 
 6319:     }
 6320:     if (!$home) { $home=$env{'user.home'}; }
 6321: 
 6322:     $$storehash{'ip'}=&get_requestor_ip();
 6323:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6324: 
 6325:     my $namevalue='';
 6326:     foreach my $key (keys(%$storehash)) {
 6327:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6328:     }
 6329:     $namevalue=~s/\&$//;
 6330:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6331:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6332: }
 6333: 
 6334: # -------------------------------------------------------------- Critical Store
 6335: 
 6336: sub cstore {
 6337:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6338:     my $home='';
 6339: 
 6340:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6341: 
 6342:     $symb=&symbclean($symb);
 6343:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6344: 
 6345:     if (!$domain) { $domain=$env{'user.domain'}; }
 6346:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6347: 
 6348:     &devalidate($symb,$stuname,$domain);
 6349: 
 6350:     $symb=escape($symb);
 6351:     if (!$namespace) { 
 6352:        unless ($namespace=$env{'request.course.id'}) { 
 6353:           return ''; 
 6354:        } 
 6355:     }
 6356:     if (!$home) { $home=$env{'user.home'}; }
 6357: 
 6358:     $$storehash{'ip'}=&get_requestor_ip();
 6359:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6360: 
 6361:     my $namevalue='';
 6362:     foreach my $key (keys(%$storehash)) {
 6363:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6364:     }
 6365:     $namevalue=~s/\&$//;
 6366:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6367:     return critical
 6368:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6369: }
 6370: 
 6371: # --------------------------------------------------------------------- Restore
 6372: 
 6373: sub restore {
 6374:     my ($symb,$namespace,$domain,$stuname) = @_;
 6375:     my $home='';
 6376: 
 6377:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6378: 
 6379:     if (!$symb) {
 6380:         return if ($namespace eq 'courserequests');
 6381:         unless ($symb=escape(&symbread())) { return ''; }
 6382:     } else {
 6383:         unless ($namespace eq 'courserequests') {
 6384:             $symb=&escape(&symbclean($symb));
 6385:         }
 6386:     }
 6387:     if (!$namespace) { 
 6388:        unless ($namespace=$env{'request.course.id'}) { 
 6389:           return ''; 
 6390:        } 
 6391:     }
 6392:     if (!$domain) { $domain=$env{'user.domain'}; }
 6393:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6394:     if (!$home) { $home=$env{'user.home'}; }
 6395:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6396: 
 6397:     my %returnhash=();
 6398:     foreach my $line (split(/\&/,$answer)) {
 6399: 	my ($name,$value)=split(/\=/,$line);
 6400:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6401:     }
 6402:     my $version;
 6403:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6404:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6405:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6406:        }
 6407:     }
 6408:     return %returnhash;
 6409: }
 6410: 
 6411: # ---------------------------------------------------------- Course Description
 6412: #
 6413: #  
 6414: 
 6415: sub coursedescription {
 6416:     my ($courseid,$args)=@_;
 6417:     $courseid=~s/^\///;
 6418:     $courseid=~s/\_/\//g;
 6419:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6420:     my $chome=&homeserver($cnum,$cdomain);
 6421:     my $normalid=$cdomain.'_'.$cnum;
 6422:     # need to always cache even if we get errors otherwise we keep 
 6423:     # trying and trying and trying to get the course description.
 6424:     my %envhash=();
 6425:     my %returnhash=();
 6426:     
 6427:     my $expiretime=600;
 6428:     if ($env{'request.course.id'} eq $normalid) {
 6429: 	$expiretime=120;
 6430:     }
 6431: 
 6432:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6433:     if (!$args->{'freshen_cache'}
 6434: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6435: 	foreach my $key (keys(%env)) {
 6436: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6437: 	    my ($setting) = $1;
 6438: 	    $returnhash{$setting} = $env{$key};
 6439: 	}
 6440: 	return %returnhash;
 6441:     }
 6442: 
 6443:     # get the data again
 6444: 
 6445:     if (!$args->{'one_time'}) {
 6446: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6447:     }
 6448: 
 6449:     if ($chome ne 'no_host') {
 6450:        %returnhash=&dump('environment',$cdomain,$cnum);
 6451:        if (!exists($returnhash{'con_lost'})) {
 6452: 	   my $username = $env{'user.name'}; # Defult username
 6453: 	   if(defined $args->{'user'}) {
 6454: 	       $username = $args->{'user'};
 6455: 	   }
 6456:            $returnhash{'home'}= $chome;
 6457: 	   $returnhash{'domain'} = $cdomain;
 6458: 	   $returnhash{'num'} = $cnum;
 6459:            if (!defined($returnhash{'type'})) {
 6460:                $returnhash{'type'} = 'Course';
 6461:            }
 6462:            while (my ($name,$value) = each %returnhash) {
 6463:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6464:            }
 6465:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6466:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6467: 	       $username.'_'.$cdomain.'_'.$cnum;
 6468:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6469:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6470:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6471:        }
 6472:     }
 6473:     if (!$args->{'one_time'}) {
 6474: 	&appenv(\%envhash);
 6475:     }
 6476:     return %returnhash;
 6477: }
 6478: 
 6479: sub update_released_required {
 6480:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6481:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6482:         $cid = $env{'request.course.id'};
 6483:         $cdom = $env{'course.'.$cid.'.domain'};
 6484:         $cnum = $env{'course.'.$cid.'.num'};
 6485:         $chome = $env{'course.'.$cid.'.home'};
 6486:     }
 6487:     if ($needsrelease) {
 6488:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6489:         my $needsupdate;
 6490:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6491:             $needsupdate = 1;
 6492:         } else {
 6493:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6494:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6495:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6496:                 $needsupdate = 1;
 6497:             }
 6498:         }
 6499:         if ($needsupdate) {
 6500:             my %needshash = (
 6501:                              'internal.releaserequired' => $needsrelease,
 6502:                             );
 6503:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6504:             if ($putresult eq 'ok') {
 6505:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6506:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6507:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6508:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6509:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6510:                 }
 6511:             }
 6512:         }
 6513:     }
 6514:     return;
 6515: }
 6516: 
 6517: # -------------------------------------------------See if a user is privileged
 6518: 
 6519: sub privileged {
 6520:     my ($username,$domain,$possdomains,$possroles)=@_;
 6521:     my $now = time;
 6522:     my $roles;
 6523:     if (ref($possroles) eq 'ARRAY') {
 6524:         $roles = $possroles; 
 6525:     } else {
 6526:         $roles = ['dc','su'];
 6527:     }
 6528:     if (ref($possdomains) eq 'ARRAY') {
 6529:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6530:         foreach my $dom (@{$possdomains}) {
 6531:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6532:                 (ref($privileged{$dom}) eq 'HASH')) {
 6533:                 foreach my $role (@{$roles}) {
 6534:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6535:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6536:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6537:                             return 1 unless (($end && $end < $now) ||
 6538:                                              ($start && $start > $now));
 6539:                         }
 6540:                     }
 6541:                 }
 6542:             }
 6543:         }
 6544:     } else {
 6545:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6546:         my $now = time;
 6547: 
 6548:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6549:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6550:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6551:                 return 1 unless ($tend && $tend < $now) 
 6552:                         or ($tstart && $tstart > $now);
 6553:             }
 6554:         }
 6555:     }
 6556:     return 0;
 6557: }
 6558: 
 6559: sub privileged_by_domain {
 6560:     my ($domains,$roles) = @_;
 6561:     my %privileged = ();
 6562:     my $cachetime = 60*60*24;
 6563:     my $now = time;
 6564:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6565:         return %privileged;
 6566:     }
 6567:     foreach my $dom (@{$domains}) {
 6568:         next if (ref($privileged{$dom}) eq 'HASH');
 6569:         my $needroles;
 6570:         foreach my $role (@{$roles}) {
 6571:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6572:             if (defined($cached)) {
 6573:                 if (ref($result) eq 'HASH') {
 6574:                     $privileged{$dom}{$role} = $result;
 6575:                 }
 6576:             } else {
 6577:                 $needroles = 1;
 6578:             }
 6579:         }
 6580:         if ($needroles) {
 6581:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6582:             $privileged{$dom} = {};
 6583:             foreach my $server (keys(%dompersonnel)) {
 6584:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6585:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6586:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6587:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6588:                         next if ($end && $end < $now);
 6589:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6590:                             $dompersonnel{$server}{$item};
 6591:                     }
 6592:                 }
 6593:             }
 6594:             if (ref($privileged{$dom}) eq 'HASH') {
 6595:                 foreach my $role (@{$roles}) {
 6596:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6597:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6598:                     } else {
 6599:                         my %hash = ();
 6600:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6601:                     }
 6602:                 }
 6603:             }
 6604:         }
 6605:     }
 6606:     return %privileged;
 6607: }
 6608: 
 6609: # -------------------------------------------------------- Get user privileges
 6610: 
 6611: sub rolesinit {
 6612:     my ($domain, $username) = @_;
 6613:     my %userroles = ('user.login.time' => time);
 6614:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6615: 
 6616:     # firstaccess and timerinterval are related to timed maps/resources. 
 6617:     # also, blocking can be triggered by an activating timer
 6618:     # it's saved in the user's %env.
 6619:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6620:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6621:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6622:         %timerintchk, %timerintenv);
 6623: 
 6624:     foreach my $key (keys(%firstaccess)) {
 6625:         my ($cid, $rest) = split(/\0/, $key);
 6626:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6627:     }
 6628: 
 6629:     foreach my $key (keys(%timerinterval)) {
 6630:         my ($cid,$rest) = split(/\0/,$key);
 6631:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6632:     }
 6633: 
 6634:     my %allroles=();
 6635:     my %allgroups=();
 6636: 
 6637:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6638:         my $role = $rolesdump{$area};
 6639:         $area =~ s/\_\w\w$//;
 6640: 
 6641:         my ($trole, $tend, $tstart, $group_privs);
 6642: 
 6643:         if ($role =~ /^cr/) {
 6644:         # Custom role, defined by a user 
 6645:         # e.g., user.role.cr/msu/smith/mynewrole
 6646:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6647:                 $trole = $1;
 6648:                 ($tend, $tstart) = split('_', $2);
 6649:             } else {
 6650:                 $trole = $role;
 6651:             }
 6652:         } elsif ($role =~ m|^gr/|) {
 6653:         # Role of member in a group, defined within a course/community
 6654:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6655:             ($trole, $tend, $tstart) = split(/_/, $role);
 6656:             next if $tstart eq '-1';
 6657:             ($trole, $group_privs) = split(/\//, $trole);
 6658:             $group_privs = &unescape($group_privs);
 6659:         } else {
 6660:         # Just a normal role, defined in roles.tab
 6661:             ($trole, $tend, $tstart) = split(/_/,$role);
 6662:         }
 6663: 
 6664:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6665:                  $username);
 6666:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6667: 
 6668:         # role expired or not available yet?
 6669:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6670:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6671: 
 6672:         next if $area eq '' or $trole eq '';
 6673: 
 6674:         my $spec = "$trole.$area";
 6675:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6676: 
 6677:         if ($trole =~ /^cr\//) {
 6678:         # Custom role, defined by a user
 6679:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6680:         } elsif ($trole eq 'gr') {
 6681:         # Role of a member in a group, defined within a course/community
 6682:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6683:             next;
 6684:         } else {
 6685:         # Normal role, defined in roles.tab
 6686:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6687:         }
 6688: 
 6689:         my $cid = $tdomain.'_'.$trest;
 6690:         unless ($firstaccchk{$cid}) {
 6691:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6692:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6693:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6694:                         $coursetimerstarts{$cid}{$item}; 
 6695:                 }
 6696:             }
 6697:             $firstaccchk{$cid} = 1;
 6698:         }
 6699:         unless ($timerintchk{$cid}) {
 6700:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6701:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6702:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6703:                        $coursetimerintervals{$cid}{$item};
 6704:                 }
 6705:             }
 6706:             $timerintchk{$cid} = 1;
 6707:         }
 6708:     }
 6709: 
 6710:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6711:                                                           \%allroles, \%allgroups);
 6712:     $env{'user.adv'} = $userroles{'user.adv'};
 6713:     $env{'user.rar'} = $userroles{'user.rar'};
 6714: 
 6715:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6716: }
 6717: 
 6718: sub set_arearole {
 6719:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6720:     unless ($nolog) {
 6721: # log the associated role with the area
 6722:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6723:     }
 6724:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6725: }
 6726: 
 6727: sub custom_roleprivs {
 6728:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6729:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6730:     my $homsvr = &homeserver($rauthor,$rdomain);
 6731:     if (&hostname($homsvr) ne '') {
 6732:         my ($rdummy,$roledef)=
 6733:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6734:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6735:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6736:             if (defined($syspriv)) {
 6737:                 if ($trest =~ /^$match_community$/) {
 6738:                     $syspriv =~ s/bre\&S//; 
 6739:                 }
 6740:                 $$allroles{'cm./'}.=':'.$syspriv;
 6741:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6742:             }
 6743:             if ($tdomain ne '') {
 6744:                 if (defined($dompriv)) {
 6745:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6746:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6747:                 }
 6748:                 if (($trest ne '') && (defined($coursepriv))) {
 6749:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6750:                         my $rolename = $1;
 6751:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6752:                     }
 6753:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6754:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6755:                 }
 6756:             }
 6757:         }
 6758:     }
 6759: }
 6760: 
 6761: sub course_adhocrole_privs {
 6762:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6763:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6764:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6765:         my (%currprivs,%storeprivs);
 6766:         foreach my $item (split(/:/,$coursepriv)) {
 6767:             my ($priv,$restrict) = split(/\&/,$item);
 6768:             $currprivs{$priv} = $restrict;
 6769:         }
 6770:         my (%possadd,%possremove,%full);
 6771:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6772:             my ($priv,$restrict)=split(/\&/,$item);
 6773:             $full{$priv} = $restrict;
 6774:         }
 6775:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6776:              next if ($item eq '');
 6777:              my ($rule,$rest) = split(/=/,$item);
 6778:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6779:              foreach my $priv (split(/:/,$rest)) {
 6780:                  if ($priv ne '') {
 6781:                      if ($rule eq 'off') {
 6782:                          $possremove{$priv} = 1;
 6783:                      } else {
 6784:                          $possadd{$priv} = 1;
 6785:                      }
 6786:                  }
 6787:              }
 6788:          }
 6789:          foreach my $priv (sort(keys(%full))) {
 6790:              if (exists($currprivs{$priv})) {
 6791:                  unless (exists($possremove{$priv})) {
 6792:                      $storeprivs{$priv} = $currprivs{$priv};
 6793:                  }
 6794:              } elsif (exists($possadd{$priv})) {
 6795:                  $storeprivs{$priv} = $full{$priv};
 6796:              }
 6797:          }
 6798:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6799:      }
 6800:      return $coursepriv;
 6801: }
 6802: 
 6803: sub group_roleprivs {
 6804:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6805:     my $access = 1;
 6806:     my $now = time;
 6807:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6808:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6809:     if ($access) {
 6810:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6811:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6812:     }
 6813: }
 6814: 
 6815: sub standard_roleprivs {
 6816:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6817:     if (defined($pr{$trole.':s'})) {
 6818:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6819:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6820:     }
 6821:     if ($tdomain ne '') {
 6822:         if (defined($pr{$trole.':d'})) {
 6823:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6824:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6825:         }
 6826:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6827:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6828:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6829:         }
 6830:     }
 6831: }
 6832: 
 6833: sub set_userprivs {
 6834:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6835:     my $author=0;
 6836:     my $adv=0;
 6837:     my $rar=0;
 6838:     my %grouproles = ();
 6839:     if (keys(%{$allgroups}) > 0) {
 6840:         my @groupkeys; 
 6841:         foreach my $role (keys(%{$allroles})) {
 6842:             push(@groupkeys,$role);
 6843:         }
 6844:         if (ref($groups_roles) eq 'HASH') {
 6845:             foreach my $key (keys(%{$groups_roles})) {
 6846:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6847:                     push(@groupkeys,$key);
 6848:                 }
 6849:             }
 6850:         }
 6851:         if (@groupkeys > 0) {
 6852:             foreach my $role (@groupkeys) {
 6853:                 my ($trole,$area,$sec,$extendedarea);
 6854:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6855:                     $trole = $1;
 6856:                     $area = $2;
 6857:                     $sec = $3;
 6858:                     $extendedarea = $area.$sec;
 6859:                     if (exists($$allgroups{$area})) {
 6860:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6861:                             my $spec = $trole.'.'.$extendedarea;
 6862:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6863:                                                 $$allgroups{$area}{$group};
 6864:                         }
 6865:                     }
 6866:                 }
 6867:             }
 6868:         }
 6869:     }
 6870:     foreach my $group (keys(%grouproles)) {
 6871:         $$allroles{$group} = $grouproles{$group};
 6872:     }
 6873:     foreach my $role (keys(%{$allroles})) {
 6874:         my %thesepriv;
 6875:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6876:         foreach my $item (split(/:/,$$allroles{$role})) {
 6877:             if ($item ne '') {
 6878:                 my ($privilege,$restrictions)=split(/&/,$item);
 6879:                 if ($restrictions eq '') {
 6880:                     $thesepriv{$privilege}='F';
 6881:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6882:                     $thesepriv{$privilege}.=$restrictions;
 6883:                 }
 6884:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6885:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6886:             }
 6887:         }
 6888:         my $thesestr='';
 6889:         foreach my $priv (sort(keys(%thesepriv))) {
 6890: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6891: 	}
 6892:         $userroles->{'user.priv.'.$role} = $thesestr;
 6893:     }
 6894:     return ($author,$adv,$rar);
 6895: }
 6896: 
 6897: sub role_status {
 6898:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6899:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6900:         my ($one,$two) = split(m{\./},$rolekey,2);
 6901:         (undef,undef,$$role) = split(/\./,$one,3);
 6902:         unless (!defined($$role) || $$role eq '') {
 6903:             $$where = '/'.$two;
 6904:             $$trolecode=$$role.'.'.$$where;
 6905:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6906:             $$tstatus='is';
 6907:             if ($$tstart && $$tstart>$update) {
 6908:                 $$tstatus='future';
 6909:                 if ($$tstart<$now) {
 6910:                     if ($$tstart && $$tstart>$refresh) {
 6911:                         if (($$where ne '') && ($$role ne '')) {
 6912:                             my (%allroles,%allgroups,$group_privs,
 6913:                                 %groups_roles,@rolecodes);
 6914:                             my %userroles = (
 6915:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6916:                             );
 6917:                             @rolecodes = ('cm'); 
 6918:                             my $spec=$$role.'.'.$$where;
 6919:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6920:                             if ($$role =~ /^cr\//) {
 6921:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6922:                                 push(@rolecodes,'cr');
 6923:                             } elsif ($$role eq 'gr') {
 6924:                                 push(@rolecodes,$$role);
 6925:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6926:                                                     $env{'user.name'});
 6927:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6928:                                 (undef,my $group_privs) = split(/\//,$trole);
 6929:                                 $group_privs = &unescape($group_privs);
 6930:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6931:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6932:                                 &get_groups_roles($tdomain,$trest,
 6933:                                                   \%course_roles,\@rolecodes,
 6934:                                                   \%groups_roles);
 6935:                             } else {
 6936:                                 push(@rolecodes,$$role);
 6937:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6938:                             }
 6939:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6940:                                                                    \%groups_roles);
 6941:                             &appenv(\%userroles,\@rolecodes);
 6942:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6943:                         }
 6944:                     }
 6945:                     $$tstatus = 'is';
 6946:                 }
 6947:             }
 6948:             if ($$tend) {
 6949:                 if ($$tend<$update) {
 6950:                     $$tstatus='expired';
 6951:                 } elsif ($$tend<$now) {
 6952:                     $$tstatus='will_not';
 6953:                 }
 6954:             }
 6955:         }
 6956:     }
 6957: }
 6958: 
 6959: sub get_groups_roles {
 6960:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6961:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6962:                   (ref($rolecodes) eq 'ARRAY') && 
 6963:                   (ref($groups_roles) eq 'HASH')); 
 6964:     if (keys(%{$cdom_courseroles}) > 0) {
 6965:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6966:         if ($cdom ne '' && $cnum ne '') {
 6967:             foreach my $key (keys(%{$cdom_courseroles})) {
 6968:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6969:                     my $crsrole = $1;
 6970:                     my $crssec = $2;
 6971:                     if ($crsrole =~ /^cr/) {
 6972:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6973:                             push(@{$rolecodes},'cr');
 6974:                         }
 6975:                     } else {
 6976:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6977:                             push(@{$rolecodes},$crsrole);
 6978:                         }
 6979:                     }
 6980:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6981:                     if ($crssec ne '') {
 6982:                         $rolekey .= "/$crssec";
 6983:                     }
 6984:                     $rolekey .= './';
 6985:                     $groups_roles->{$rolekey} = $rolecodes;
 6986:                 }
 6987:             }
 6988:         }
 6989:     }
 6990:     return;
 6991: }
 6992: 
 6993: sub delete_env_groupprivs {
 6994:     my ($where,$courseroles,$possroles) = @_;
 6995:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6996:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6997:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6998:         %{$courseroles->{$udom}} =
 6999:             &get_my_roles('','','userroles',['active'],
 7000:                           $possroles,[$udom],1);
 7001:     }
 7002:     if (ref($courseroles->{$udom}) eq 'HASH') {
 7003:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 7004:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 7005:             my $area = '/'.$cdom.'/'.$cnum;
 7006:             my $privkey = "user.priv.$crsrole.$area";
 7007:             if ($crssec ne '') {
 7008:                 $privkey .= '/'.$crssec;
 7009:             }
 7010:             $privkey .= ".$area/$group";
 7011:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 7012:         }
 7013:     }
 7014:     return;
 7015: }
 7016: 
 7017: sub check_adhoc_privs {
 7018:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 7019:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 7020:     if ($sec) {
 7021:         $cckey .= '/'.$sec;
 7022:     } 
 7023:     my $setprivs;
 7024:     if ($env{$cckey}) {
 7025:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 7026:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 7027:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 7028:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7029:             $setprivs = 1;
 7030:         }
 7031:     } else {
 7032:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7033:         $setprivs = 1;
 7034:     }
 7035:     return $setprivs;
 7036: }
 7037: 
 7038: sub set_adhoc_privileges {
 7039: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7040:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7041:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7042:     if ($sec ne '') {
 7043:         $area .= '/'.$sec;
 7044:     }
 7045:     my $spec = $role.'.'.$area;
 7046:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7047:                                   $env{'user.name'},1);
 7048:     my %rolehash = ();
 7049:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7050:         my $rolename = $1;
 7051:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7052:         my %domdef = &get_domain_defaults($dcdom);
 7053:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7054:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7055:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7056:             }
 7057:         }
 7058:     } else {
 7059:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7060:     }
 7061:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7062:     &appenv(\%userroles,[$role,'cm']);
 7063:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7064:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7065:             ($caller eq 'tiny')) {
 7066:         &appenv( {'request.role'        => $spec,
 7067:                   'request.role.domain' => $dcdom,
 7068:                   'request.course.sec'  => $sec,
 7069:                  }
 7070:                );
 7071:         my $tadv=0;
 7072:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7073:         &appenv({'request.role.adv'    => $tadv});
 7074:     }
 7075: }
 7076: 
 7077: # --------------------------------------------------------------- get interface
 7078: 
 7079: sub get {
 7080:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7081:    my $items='';
 7082:    foreach my $item (@$storearr) {
 7083:        $items.=&escape($item).'&';
 7084:    }
 7085:    $items=~s/\&$//;
 7086:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7087:    if (!$uname) { $uname=$env{'user.name'}; }
 7088:    my $uhome=&homeserver($uname,$udomain);
 7089: 
 7090:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7091:    my @pairs=split(/\&/,$rep);
 7092:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7093:      return @pairs;
 7094:    }
 7095:    my %returnhash=();
 7096:    my $i=0;
 7097:    foreach my $item (@$storearr) {
 7098:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7099:       $i++;
 7100:    }
 7101:    return %returnhash;
 7102: }
 7103: 
 7104: # --------------------------------------------------------------- del interface
 7105: 
 7106: sub del {
 7107:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7108:    my $items='';
 7109:    foreach my $item (@$storearr) {
 7110:        $items.=&escape($item).'&';
 7111:    }
 7112: 
 7113:    $items=~s/\&$//;
 7114:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7115:    if (!$uname) { $uname=$env{'user.name'}; }
 7116:    my $uhome=&homeserver($uname,$udomain);
 7117:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7118: }
 7119: 
 7120: # -------------------------------------------------------------- dump interface
 7121: 
 7122: sub unserialize {
 7123:     my ($rep, $escapedkeys) = @_;
 7124: 
 7125:     return {} if $rep =~ /^error/;
 7126: 
 7127:     my %returnhash=();
 7128: 	foreach my $item (split(/\&/,$rep)) {
 7129: 	    my ($key, $value) = split(/=/, $item, 2);
 7130: 	    $key = unescape($key) unless $escapedkeys;
 7131: 	    next if $key =~ /^error: 2 /;
 7132: 	    $returnhash{$key} = &thaw_unescape($value);
 7133: 	}
 7134:     #return %returnhash;
 7135:     return \%returnhash;
 7136: }        
 7137: 
 7138: # see Lond::dump_with_regexp
 7139: # if $escapedkeys hash keys won't get unescaped.
 7140: sub dump {
 7141:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
 7142:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7143:     if (!$uname) { $uname=$env{'user.name'}; }
 7144:     my $uhome=&homeserver($uname,$udomain);
 7145: 
 7146:     if ($regexp) {
 7147:         $regexp=&escape($regexp);
 7148:     } else {
 7149:         $regexp='.';
 7150:     }
 7151:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7152:         # user is hosted on this machine
 7153:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7154:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7155:         return %{unserialize($reply, $escapedkeys)};
 7156:     }
 7157:     my $rep;
 7158:     if ($encrypt) {
 7159:         $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7160:     } else {
 7161:         $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7162:     }
 7163:     my @pairs=split(/\&/,$rep);
 7164:     my %returnhash=();
 7165:     if (!($rep =~ /^error/ )) {
 7166: 	foreach my $item (@pairs) {
 7167: 	    my ($key,$value)=split(/=/,$item,2);
 7168:         $key = unescape($key) unless $escapedkeys;
 7169:         #$key = &unescape($key);
 7170: 	    next if ($key =~ /^error: 2 /);
 7171: 	    $returnhash{$key}=&thaw_unescape($value);
 7172: 	}
 7173:     }
 7174:     return %returnhash;
 7175: }
 7176: 
 7177: 
 7178: # --------------------------------------------------------- dumpstore interface
 7179: 
 7180: sub dumpstore {
 7181:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7182:    # same as dump but keys must be escaped. They may contain colon separated
 7183:    # lists of values that may themself contain colons (e.g. symbs).
 7184:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7185: }
 7186: 
 7187: # -------------------------------------------------------------- keys interface
 7188: 
 7189: sub getkeys {
 7190:    my ($namespace,$udomain,$uname)=@_;
 7191:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7192:    if (!$uname) { $uname=$env{'user.name'}; }
 7193:    my $uhome=&homeserver($uname,$udomain);
 7194:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7195:    my @keyarray=();
 7196:    foreach my $key (split(/\&/,$rep)) {
 7197:       next if ($key =~ /^error: 2 /);
 7198:       push(@keyarray,&unescape($key));
 7199:    }
 7200:    return @keyarray;
 7201: }
 7202: 
 7203: # --------------------------------------------------------------- currentdump
 7204: sub currentdump {
 7205:    my ($courseid,$sdom,$sname)=@_;
 7206:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7207:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7208:    $sname    = $env{'user.name'}         if (! defined($sname));
 7209:    my $uhome = &homeserver($sname,$sdom);
 7210:    my $rep;
 7211: 
 7212:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7213:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7214:                    $courseid)));
 7215:    } else {
 7216:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7217:    }
 7218: 
 7219:    return if ($rep =~ /^(error:|no_such_host)/);
 7220:    #
 7221:    my %returnhash=();
 7222:    #
 7223:    if ($rep eq 'unknown_cmd') {
 7224:        # an old lond will not know currentdump
 7225:        # Do a dump and make it look like a currentdump
 7226:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7227:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7228:        my %hash = @tmp;
 7229:        @tmp=();
 7230:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7231:    } else {
 7232:        my @pairs=split(/\&/,$rep);
 7233:        foreach my $pair (@pairs) {
 7234:            my ($key,$value)=split(/=/,$pair,2);
 7235:            my ($symb,$param) = split(/:/,$key);
 7236:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7237:                                                         &thaw_unescape($value);
 7238:        }
 7239:    }
 7240:    return %returnhash;
 7241: }
 7242: 
 7243: sub convert_dump_to_currentdump{
 7244:     my %hash = %{shift()};
 7245:     my %returnhash;
 7246:     # Code ripped from lond, essentially.  The only difference
 7247:     # here is the unescaping done by lonnet::dump().  Conceivably
 7248:     # we might run in to problems with parameter names =~ /^v\./
 7249:     while (my ($key,$value) = each(%hash)) {
 7250:         my ($v,$symb,$param) = split(/:/,$key);
 7251: 	$symb  = &unescape($symb);
 7252: 	$param = &unescape($param);
 7253:         next if ($v eq 'version' || $symb eq 'keys');
 7254:         next if (exists($returnhash{$symb}) &&
 7255:                  exists($returnhash{$symb}->{$param}) &&
 7256:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7257:         $returnhash{$symb}->{$param}=$value;
 7258:         $returnhash{$symb}->{'v.'.$param}=$v;
 7259:     }
 7260:     #
 7261:     # Remove all of the keys in the hashes which keep track of
 7262:     # the version of the parameter.
 7263:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7264:         # use a foreach because we are going to delete from the hash.
 7265:         foreach my $key (keys(%$param_hash)) {
 7266:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7267:         }
 7268:     }
 7269:     return \%returnhash;
 7270: }
 7271: 
 7272: # ------------------------------------------------------ critical inc interface
 7273: 
 7274: sub cinc {
 7275:     return &inc(@_,'critical');
 7276: }
 7277: 
 7278: # --------------------------------------------------------------- inc interface
 7279: 
 7280: sub inc {
 7281:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7282:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7283:     if (!$uname) { $uname=$env{'user.name'}; }
 7284:     my $uhome=&homeserver($uname,$udomain);
 7285:     my $items='';
 7286:     if (! ref($store)) {
 7287:         # got a single value, so use that instead
 7288:         $items = &escape($store).'=&';
 7289:     } elsif (ref($store) eq 'SCALAR') {
 7290:         $items = &escape($$store).'=&';        
 7291:     } elsif (ref($store) eq 'ARRAY') {
 7292:         $items = join('=&',map {&escape($_);} @{$store});
 7293:     } elsif (ref($store) eq 'HASH') {
 7294:         while (my($key,$value) = each(%{$store})) {
 7295:             $items.= &escape($key).'='.&escape($value).'&';
 7296:         }
 7297:     }
 7298:     $items=~s/\&$//;
 7299:     if ($critical) {
 7300: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7301:     } else {
 7302: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7303:     }
 7304: }
 7305: 
 7306: # --------------------------------------------------------------- put interface
 7307: 
 7308: sub put {
 7309:    my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
 7310:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7311:    if (!$uname) { $uname=$env{'user.name'}; }
 7312:    my $uhome=&homeserver($uname,$udomain);
 7313:    my $items='';
 7314:    foreach my $item (keys(%$storehash)) {
 7315:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7316:    }
 7317:    $items=~s/\&$//;
 7318:    if ($encrypt) {
 7319:        return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
 7320:    } else {
 7321:        return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7322:    }
 7323: }
 7324: 
 7325: # ------------------------------------------------------------ newput interface
 7326: 
 7327: sub newput {
 7328:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7329:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7330:    if (!$uname) { $uname=$env{'user.name'}; }
 7331:    my $uhome=&homeserver($uname,$udomain);
 7332:    my $items='';
 7333:    foreach my $key (keys(%$storehash)) {
 7334:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7335:    }
 7336:    $items=~s/\&$//;
 7337:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7338: }
 7339: 
 7340: # ---------------------------------------------------------  putstore interface
 7341: 
 7342: sub putstore {
 7343:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7344:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7345:    if (!$uname) { $uname=$env{'user.name'}; }
 7346:    my $uhome=&homeserver($uname,$udomain);
 7347:    my $items='';
 7348:    foreach my $key (keys(%$storehash)) {
 7349:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7350:    }
 7351:    $items=~s/\&$//;
 7352:    my $esc_symb=&escape($symb);
 7353:    my $esc_v=&escape($version);
 7354:    my $reply =
 7355:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7356: 	      $uhome);
 7357:    if (($tolog) && ($reply eq 'ok')) {
 7358:        my $namevalue='';
 7359:        foreach my $key (keys(%{$storehash})) {
 7360:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7361:        }
 7362:        my $ip = &get_requestor_ip();
 7363:        $namevalue .= 'ip='.&escape($ip).
 7364:                      '&host='.&escape($perlvar{'lonHostID'}).
 7365:                      '&version='.$esc_v.
 7366:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7367:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7368:    }
 7369:    if ($reply eq 'unknown_cmd') {
 7370:        # gfall back to way things use to be done
 7371:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7372: 			    $uname);
 7373:    }
 7374:    return $reply;
 7375: }
 7376: 
 7377: sub old_putstore {
 7378:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7379:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7380:     if (!$uname) { $uname=$env{'user.name'}; }
 7381:     my $uhome=&homeserver($uname,$udomain);
 7382:     my %newstorehash;
 7383:     foreach my $item (keys(%$storehash)) {
 7384: 	my $key = $version.':'.&escape($symb).':'.$item;
 7385: 	$newstorehash{$key} = $storehash->{$item};
 7386:     }
 7387:     my $items='';
 7388:     my %allitems = ();
 7389:     foreach my $item (keys(%newstorehash)) {
 7390: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7391: 	    my $key = $1.':keys:'.$2;
 7392: 	    $allitems{$key} .= $3.':';
 7393: 	}
 7394: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7395:     }
 7396:     foreach my $item (keys(%allitems)) {
 7397: 	$allitems{$item} =~ s/\:$//;
 7398: 	$items.= $item.'='.$allitems{$item}.'&';
 7399:     }
 7400:     $items=~s/\&$//;
 7401:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7402: }
 7403: 
 7404: # ------------------------------------------------------ critical put interface
 7405: 
 7406: sub cput {
 7407:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7408:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7409:    if (!$uname) { $uname=$env{'user.name'}; }
 7410:    my $uhome=&homeserver($uname,$udomain);
 7411:    my $items='';
 7412:    foreach my $item (keys(%$storehash)) {
 7413:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7414:    }
 7415:    $items=~s/\&$//;
 7416:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7417: }
 7418: 
 7419: # -------------------------------------------------------------- eget interface
 7420: 
 7421: sub eget {
 7422:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7423:    my $items='';
 7424:    foreach my $item (@$storearr) {
 7425:        $items.=&escape($item).'&';
 7426:    }
 7427:    $items=~s/\&$//;
 7428:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7429:    if (!$uname) { $uname=$env{'user.name'}; }
 7430:    my $uhome=&homeserver($uname,$udomain);
 7431:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7432:    my @pairs=split(/\&/,$rep);
 7433:    my %returnhash=();
 7434:    my $i=0;
 7435:    foreach my $item (@$storearr) {
 7436:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7437:       $i++;
 7438:    }
 7439:    return %returnhash;
 7440: }
 7441: 
 7442: # ------------------------------------------------------------ tmpput interface
 7443: sub tmpput {
 7444:     my ($storehash,$server,$context)=@_;
 7445:     my $items='';
 7446:     foreach my $item (keys(%$storehash)) {
 7447: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7448:     }
 7449:     $items=~s/\&$//;
 7450:     if (defined($context)) {
 7451:         $items .= ':'.&escape($context);
 7452:     }
 7453:     return &reply("tmpput:$items",$server);
 7454: }
 7455: 
 7456: # ------------------------------------------------------------ tmpget interface
 7457: sub tmpget {
 7458:     my ($token,$server)=@_;
 7459:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7460:     my $rep=&reply("tmpget:$token",$server);
 7461:     my %returnhash;
 7462:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7463:         return %returnhash;
 7464:     }
 7465:     foreach my $item (split(/\&/,$rep)) {
 7466: 	my ($key,$value)=split(/=/,$item);
 7467: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7468:     }
 7469:     return %returnhash;
 7470: }
 7471: 
 7472: # ------------------------------------------------------------ tmpdel interface
 7473: sub tmpdel {
 7474:     my ($token,$server)=@_;
 7475:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7476:     return &reply("tmpdel:$token",$server);
 7477: }
 7478: 
 7479: # ------------------------------------------------------------ get_timebased_id 
 7480: 
 7481: sub get_timebased_id {
 7482:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7483:         $maxtries) = @_;
 7484:     my ($newid,$error,$dellock);
 7485:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7486:         return ('','ok','invalid call to get suffix');
 7487:     }
 7488: 
 7489: # set defaults for any optional args for which values were not supplied
 7490:     if ($who eq '') {
 7491:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7492:     }
 7493:     if (!$locktries) {
 7494:         $locktries = 3;
 7495:     }
 7496:     if (!$maxtries) {
 7497:         $maxtries = 10;
 7498:     }
 7499:     
 7500:     if (($cdom eq '') || ($cnum eq '')) {
 7501:         if ($env{'request.course.id'}) {
 7502:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7503:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7504:         }
 7505:         if (($cdom eq '') || ($cnum eq '')) {
 7506:             return ('','ok','call to get suffix not in course context');
 7507:         }
 7508:     }
 7509: 
 7510: # construct locking item
 7511:     my $lockhash = {
 7512:                       $prefix."\0".'locked_'.$keyid => $who,
 7513:                    };
 7514:     my $tries = 0;
 7515: 
 7516: # attempt to get lock on nohist_$namespace file
 7517:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7518:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7519:         $tries ++;
 7520:         sleep 1;
 7521:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7522:     }
 7523: 
 7524: # attempt to get unique identifier, based on current timestamp
 7525:     if ($gotlock eq 'ok') {
 7526:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7527:         my $id = time;
 7528:         $newid = $id;
 7529:         if ($idtype eq 'addcode') {
 7530:             $newid .= &sixnum_code();
 7531:         }
 7532:         my $idtries = 0;
 7533:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7534:             if ($idtype eq 'concat') {
 7535:                 $newid = $id.$idtries;
 7536:             } elsif ($idtype eq 'addcode') {
 7537:                 $newid = $newid.&sixnum_code();
 7538:             } else {
 7539:                 $newid ++;
 7540:             }
 7541:             $idtries ++;
 7542:         }
 7543:         if (!exists($inuse{$prefix."\0".$newid})) {
 7544:             my %new_item =  (
 7545:                               $prefix."\0".$newid => $who,
 7546:                             );
 7547:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7548:                                                  $cdom,$cnum);
 7549:             if ($putresult ne 'ok') {
 7550:                 undef($newid);
 7551:                 $error = 'error saving new item: '.$putresult;
 7552:             }
 7553:         } else {
 7554:              undef($newid);
 7555:              $error = ('error: no unique suffix available for the new item ');
 7556:         }
 7557: #  remove lock
 7558:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7559:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7560:     } else {
 7561:         $error = "error: could not obtain lockfile\n";
 7562:         $dellock = 'ok';
 7563:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7564:             $dellock = 'nolock';
 7565:         }
 7566:     }
 7567:     return ($newid,$dellock,$error);
 7568: }
 7569: 
 7570: sub sixnum_code {
 7571:     my $code;
 7572:     for (0..6) {
 7573:         $code .= int( rand(9) );
 7574:     }
 7575:     return $code;
 7576: }
 7577: 
 7578: # -------------------------------------------------- portfolio access checking
 7579: 
 7580: sub portfolio_access {
 7581:     my ($requrl,$clientip) = @_;
 7582:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7583:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7584:     if ($result) {
 7585:         my %setters;
 7586:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7587:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 7588:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
 7589:             if (($startblock && $endblock) || ($by_ip)) {
 7590:                 return 'B';
 7591:             }
 7592:         } else {
 7593:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 7594:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 7595:             if (($startblock && $endblock) || ($by_ip)) {
 7596:                 return 'B';
 7597:             }
 7598:         }
 7599:     }
 7600:     if ($result eq 'ok') {
 7601:        return 'F';
 7602:     } elsif ($result =~ /^[^:]+:guest_/) {
 7603:        return 'A';
 7604:     }
 7605:     return '';
 7606: }
 7607: 
 7608: sub get_portfolio_access {
 7609:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7610: 
 7611:     if (!ref($access_hash)) {
 7612: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7613: 	my %access_controls = &get_access_controls($current_perms,$group,
 7614: 						   $file_name);
 7615: 	$access_hash = $access_controls{$file_name};
 7616:     }
 7617: 
 7618:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7619:     my $now = time;
 7620:     if (ref($access_hash) eq 'HASH') {
 7621:         foreach my $key (keys(%{$access_hash})) {
 7622:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7623:             if ($start > $now) {
 7624:                 next;
 7625:             }
 7626:             if ($end && $end<$now) {
 7627:                 next;
 7628:             }
 7629:             if ($scope eq 'public') {
 7630:                 $public = $key;
 7631:                 last;
 7632:             } elsif ($scope eq 'guest') {
 7633:                 $guest = $key;
 7634:             } elsif ($scope eq 'domains') {
 7635:                 push(@domains,$key);
 7636:             } elsif ($scope eq 'users') {
 7637:                 push(@users,$key);
 7638:             } elsif ($scope eq 'course') {
 7639:                 push(@courses,$key);
 7640:             } elsif ($scope eq 'group') {
 7641:                 push(@groups,$key);
 7642:             } elsif ($scope eq 'ip') {
 7643:                 push(@ips,$key);
 7644:             }
 7645:         }
 7646:         if ($public) {
 7647:             return 'ok';
 7648:         } elsif (@ips > 0) {
 7649:             my $allowed;
 7650:             foreach my $ipkey (@ips) {
 7651:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7652:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7653:                         $allowed = 1;
 7654:                         last; 
 7655:                     }
 7656:                 }
 7657:             }
 7658:             if ($allowed) {
 7659:                 return 'ok';
 7660:             }
 7661:         }
 7662:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7663:             if ($guest) {
 7664:                 return $guest;
 7665:             }
 7666:         } else {
 7667:             if (@domains > 0) {
 7668:                 foreach my $domkey (@domains) {
 7669:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7670:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7671:                             return 'ok';
 7672:                         }
 7673:                     }
 7674:                 }
 7675:             }
 7676:             if (@users > 0) {
 7677:                 foreach my $userkey (@users) {
 7678:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7679:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7680:                             if (ref($item) eq 'HASH') {
 7681:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7682:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7683:                                     return 'ok';
 7684:                                 }
 7685:                             }
 7686:                         }
 7687:                     } 
 7688:                 }
 7689:             }
 7690:             my %roleshash;
 7691:             my @courses_and_groups = @courses;
 7692:             push(@courses_and_groups,@groups); 
 7693:             if (@courses_and_groups > 0) {
 7694:                 my (%allgroups,%allroles); 
 7695:                 my ($start,$end,$role,$sec,$group);
 7696:                 foreach my $envkey (%env) {
 7697:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7698:                         my $cid = $2.'_'.$3; 
 7699:                         if ($1 eq 'gr') {
 7700:                             $group = $4;
 7701:                             $allgroups{$cid}{$group} = $env{$envkey};
 7702:                         } else {
 7703:                             if ($4 eq '') {
 7704:                                 $sec = 'none';
 7705:                             } else {
 7706:                                 $sec = $4;
 7707:                             }
 7708:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7709:                         }
 7710:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7711:                         my $cid = $2.'_'.$3;
 7712:                         if ($4 eq '') {
 7713:                             $sec = 'none';
 7714:                         } else {
 7715:                             $sec = $4;
 7716:                         }
 7717:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7718:                     }
 7719:                 }
 7720:                 if (keys(%allroles) == 0) {
 7721:                     return;
 7722:                 }
 7723:                 foreach my $key (@courses_and_groups) {
 7724:                     my %content = %{$$access_hash{$key}};
 7725:                     my $cnum = $content{'number'};
 7726:                     my $cdom = $content{'domain'};
 7727:                     my $cid = $cdom.'_'.$cnum;
 7728:                     if (!exists($allroles{$cid})) {
 7729:                         next;
 7730:                     }    
 7731:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7732:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7733:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7734:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7735:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7736:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7737:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7738:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7739:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7740:                                         if (grep/^all$/,@sections) {
 7741:                                             return 'ok';
 7742:                                         } else {
 7743:                                             if (grep/^$sec$/,@sections) {
 7744:                                                 return 'ok';
 7745:                                             }
 7746:                                         }
 7747:                                     }
 7748:                                 }
 7749:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7750:                                     if (grep/^none$/,@groups) {
 7751:                                         return 'ok';
 7752:                                     }
 7753:                                 } else {
 7754:                                     if (grep/^all$/,@groups) {
 7755:                                         return 'ok';
 7756:                                     } 
 7757:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7758:                                         if (grep/^$group$/,@groups) {
 7759:                                             return 'ok';
 7760:                                         }
 7761:                                     }
 7762:                                 } 
 7763:                             }
 7764:                         }
 7765:                     }
 7766:                 }
 7767:             }
 7768:             if ($guest) {
 7769:                 return $guest;
 7770:             }
 7771:         }
 7772:     }
 7773:     return;
 7774: }
 7775: 
 7776: sub course_group_datechecker {
 7777:     my ($dates,$now,$status) = @_;
 7778:     my ($start,$end) = split(/\./,$dates);
 7779:     if (!$start && !$end) {
 7780:         return 'ok';
 7781:     }
 7782:     if (grep/^active$/,@{$status}) {
 7783:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7784:             return 'ok';
 7785:         }
 7786:     }
 7787:     if (grep/^previous$/,@{$status}) {
 7788:         if ($end > $now ) {
 7789:             return 'ok';
 7790:         }
 7791:     }
 7792:     if (grep/^future$/,@{$status}) {
 7793:         if ($start > $now) {
 7794:             return 'ok';
 7795:         }
 7796:     }
 7797:     return; 
 7798: }
 7799: 
 7800: sub parse_portfolio_url {
 7801:     my ($url) = @_;
 7802: 
 7803:     my ($type,$udom,$unum,$group,$file_name);
 7804:     
 7805:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7806: 	$type = 1;
 7807:         $udom = $1;
 7808:         $unum = $2;
 7809:         $file_name = $3;
 7810:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7811: 	$type = 2;
 7812:         $udom = $1;
 7813:         $unum = $2;
 7814:         $group = $3;
 7815:         $file_name = $3.'/'.$4;
 7816:     }
 7817:     if (wantarray) {
 7818: 	return ($type,$udom,$unum,$file_name,$group);
 7819:     }
 7820:     return $type;
 7821: }
 7822: 
 7823: sub is_portfolio_url {
 7824:     my ($url) = @_;
 7825:     return scalar(&parse_portfolio_url($url));
 7826: }
 7827: 
 7828: sub is_portfolio_file {
 7829:     my ($file) = @_;
 7830:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7831:         return 1;
 7832:     }
 7833:     return;
 7834: }
 7835: 
 7836: sub usertools_access {
 7837:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7838:     my ($access,%tools);
 7839:     if ($context eq '') {
 7840:         $context = 'tools';
 7841:     }
 7842:     if ($context eq 'requestcourses') {
 7843:         %tools = (
 7844:                       official   => 1,
 7845:                       unofficial => 1,
 7846:                       community  => 1,
 7847:                       textbook   => 1,
 7848:                       placement  => 1,
 7849:                       lti        => 1,
 7850:                  );
 7851:     } elsif ($context eq 'requestauthor') {
 7852:         %tools = (
 7853:                       requestauthor => 1,
 7854:                  );
 7855:     } else {
 7856:         %tools = (
 7857:                       aboutme   => 1,
 7858:                       blog      => 1,
 7859:                       webdav    => 1,
 7860:                       portfolio => 1,
 7861:                  );
 7862:     }
 7863:     return if (!defined($tools{$tool}));
 7864: 
 7865:     if (($udom eq '') || ($uname eq '')) {
 7866:         $udom = $env{'user.domain'};
 7867:         $uname = $env{'user.name'};
 7868:     }
 7869: 
 7870:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7871:         if ($action ne 'reload') {
 7872:             if ($context eq 'requestcourses') {
 7873:                 return $env{'environment.canrequest.'.$tool};
 7874:             } elsif ($context eq 'requestauthor') {
 7875:                 return $env{'environment.canrequest.author'};
 7876:             } else {
 7877:                 return $env{'environment.availabletools.'.$tool};
 7878:             }
 7879:         }
 7880:     }
 7881: 
 7882:     my ($toolstatus,$inststatus,$envkey);
 7883:     if ($context eq 'requestauthor') {
 7884:         $envkey = $context; 
 7885:     } else {
 7886:         $envkey = $context.'.'.$tool;
 7887:     }
 7888: 
 7889:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7890:          ($action ne 'reload')) {
 7891:         $toolstatus = $env{'environment.'.$envkey};
 7892:         $inststatus = $env{'environment.inststatus'};
 7893:     } else {
 7894:         if (ref($userenvref) eq 'HASH') {
 7895:             $toolstatus = $userenvref->{$envkey};
 7896:             $inststatus = $userenvref->{'inststatus'};
 7897:         } else {
 7898:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7899:             $toolstatus = $userenv{$envkey};
 7900:             $inststatus = $userenv{'inststatus'};
 7901:         }
 7902:     }
 7903: 
 7904:     if ($toolstatus ne '') {
 7905:         if ($toolstatus) {
 7906:             $access = 1;
 7907:         } else {
 7908:             $access = 0;
 7909:         }
 7910:         return $access;
 7911:     }
 7912: 
 7913:     my ($is_adv,%domdef);
 7914:     if (ref($is_advref) eq 'HASH') {
 7915:         $is_adv = $is_advref->{'is_adv'};
 7916:     } else {
 7917:         $is_adv = &is_advanced_user($udom,$uname);
 7918:     }
 7919:     if (ref($domdefref) eq 'HASH') {
 7920:         %domdef = %{$domdefref};
 7921:     } else {
 7922:         %domdef = &get_domain_defaults($udom);
 7923:     }
 7924:     if (ref($domdef{$tool}) eq 'HASH') {
 7925:         if ($is_adv) {
 7926:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7927:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7928:                     $access = 1;
 7929:                 } else {
 7930:                     $access = 0;
 7931:                 }
 7932:                 return $access;
 7933:             }
 7934:         }
 7935:         if ($inststatus ne '') {
 7936:             my ($hasaccess,$hasnoaccess);
 7937:             foreach my $affiliation (split(/:/,$inststatus)) {
 7938:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7939:                     if ($domdef{$tool}{$affiliation}) {
 7940:                         $hasaccess = 1;
 7941:                     } else {
 7942:                         $hasnoaccess = 1;
 7943:                     }
 7944:                 }
 7945:             }
 7946:             if ($hasaccess || $hasnoaccess) {
 7947:                 if ($hasaccess) {
 7948:                     $access = 1;
 7949:                 } elsif ($hasnoaccess) {
 7950:                     $access = 0; 
 7951:                 }
 7952:                 return $access;
 7953:             }
 7954:         } else {
 7955:             if ($domdef{$tool}{'default'} ne '') {
 7956:                 if ($domdef{$tool}{'default'}) {
 7957:                     $access = 1;
 7958:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7959:                     $access = 0;
 7960:                 }
 7961:                 return $access;
 7962:             }
 7963:         }
 7964:     } else {
 7965:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7966:             $access = 1;
 7967:         } else {
 7968:             $access = 0;
 7969:         }
 7970:         return $access;
 7971:     }
 7972: }
 7973: 
 7974: sub is_course_owner {
 7975:     my ($cdom,$cnum,$udom,$uname) = @_;
 7976:     if (($udom eq '') || ($uname eq '')) {
 7977:         $udom = $env{'user.domain'};
 7978:         $uname = $env{'user.name'};
 7979:     }
 7980:     unless (($udom eq '') || ($uname eq '')) {
 7981:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7982:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7983:                 return 1;
 7984:             } else {
 7985:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7986:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7987:                     return 1;
 7988:                 }
 7989:             }
 7990:         }
 7991:     }
 7992:     return;
 7993: }
 7994: 
 7995: sub is_advanced_user {
 7996:     my ($udom,$uname) = @_;
 7997:     if ($udom ne '' && $uname ne '') {
 7998:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7999:             if (wantarray) {
 8000:                 return ($env{'user.adv'},$env{'user.author'});
 8001:             } else {
 8002:                 return $env{'user.adv'};
 8003:             }
 8004:         }
 8005:     }
 8006:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 8007:     my %allroles;
 8008:     my ($is_adv,$is_author);
 8009:     foreach my $role (keys(%roleshash)) {
 8010:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 8011:         my $area = '/'.$tdomain.'/'.$trest;
 8012:         if ($sec ne '') {
 8013:             $area .= '/'.$sec;
 8014:         }
 8015:         if (($area ne '') && ($trole ne '')) {
 8016:             my $spec=$trole.'.'.$area;
 8017:             if ($trole =~ /^cr\//) {
 8018:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 8019:             } elsif ($trole ne 'gr') {
 8020:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 8021:             }
 8022:             if ($trole eq 'au') {
 8023:                 $is_author = 1;
 8024:             }
 8025:         }
 8026:     }
 8027:     foreach my $role (keys(%allroles)) {
 8028:         last if ($is_adv);
 8029:         foreach my $item (split(/:/,$allroles{$role})) {
 8030:             if ($item ne '') {
 8031:                 my ($privilege,$restrictions)=split(/&/,$item);
 8032:                 if ($privilege eq 'adv') {
 8033:                     $is_adv = 1;
 8034:                     last;
 8035:                 }
 8036:             }
 8037:         }
 8038:     }
 8039:     if (wantarray) {
 8040:         return ($is_adv,$is_author);
 8041:     }
 8042:     return $is_adv;
 8043: }
 8044: 
 8045: sub check_can_request {
 8046:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8047:     my $canreq = 0;
 8048:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8049:         $uname = $env{'user.name'};
 8050:         $udom = $env{'user.domain'};
 8051:     }
 8052:     my ($types,$typename) = &Apache::loncommon::course_types();
 8053:     my @options = ('approval','validate','autolimit');
 8054:     my $optregex = join('|',@options);
 8055:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8056:         foreach my $type (@{$types}) {
 8057:             if (&usertools_access($uname,$udom,$type,undef,
 8058:                                   'requestcourses')) {
 8059:                 $canreq ++;
 8060:                 if (ref($request_domains) eq 'HASH') {
 8061:                     push(@{$request_domains->{$type}},$udom);
 8062:                 }
 8063:                 if ($dom eq $udom) {
 8064:                     $can_request->{$type} = 1;
 8065:                 }
 8066:             }
 8067:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8068:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8069:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8070:                 if (@curr > 0) {
 8071:                     foreach my $item (@curr) {
 8072:                         if (ref($request_domains) eq 'HASH') {
 8073:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8074:                             if ($otherdom ne '') {
 8075:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 8076:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8077:                                         push(@{$request_domains->{$type}},$otherdom);
 8078:                                     }
 8079:                                 } else {
 8080:                                     push(@{$request_domains->{$type}},$otherdom);
 8081:                                 }
 8082:                             }
 8083:                         }
 8084:                     }
 8085:                     unless ($dom eq $env{'user.domain'}) {
 8086:                         $canreq ++;
 8087:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8088:                             $can_request->{$type} = 1;
 8089:                         }
 8090:                     }
 8091:                 }
 8092:             }
 8093:         }
 8094:     }
 8095:     return $canreq;
 8096: }
 8097: 
 8098: # ---------------------------------------------- Custom access rule evaluation
 8099: 
 8100: sub customaccess {
 8101:     my ($priv,$uri)=@_;
 8102:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8103:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8104:     $udom = &LONCAPA::clean_domain($udom);
 8105:     $ucrs = &LONCAPA::clean_username($ucrs);
 8106:     my $access=0;
 8107:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8108: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8109: 	if ($type eq 'user') {
 8110: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8111: 		my ($tdom,$tuname)=split(m{/},$scope);
 8112: 		if ($tdom) {
 8113: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8114: 		}
 8115: 		if ($tuname) {
 8116: 		    if ($tuname ne $env{'user.name'}) { next; }
 8117: 		}
 8118: 		$access=($effect eq 'allow');
 8119: 		last;
 8120: 	    }
 8121: 	} else {
 8122: 	    if ($role) {
 8123: 		if ($role ne $urole) { next; }
 8124: 	    }
 8125: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8126: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8127: 		if ($tdom) {
 8128: 		    if ($tdom ne $udom) { next; }
 8129: 		}
 8130: 		if ($tcrs) {
 8131: 		    if ($tcrs ne $ucrs) { next; }
 8132: 		}
 8133: 		if ($tsec) {
 8134: 		    if ($tsec ne $usec) { next; }
 8135: 		}
 8136: 		$access=($effect eq 'allow');
 8137: 		last;
 8138: 	    }
 8139: 	    if ($realm eq '' && $role eq '') {
 8140: 		$access=($effect eq 'allow');
 8141: 	    }
 8142: 	}
 8143:     }
 8144:     return $access;
 8145: }
 8146: 
 8147: # ------------------------------------------------- Check for a user privilege
 8148: 
 8149: sub allowed {
 8150:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
 8151:     my $ver_orguri=$uri;
 8152:     $uri=&deversion($uri);
 8153:     my $orguri=$uri;
 8154:     $uri=&declutter($uri);
 8155: 
 8156:     if ($priv eq 'evb') {
 8157: # Evade communication block restrictions for specified role in a course
 8158:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8159:             return $1;
 8160:         } else {
 8161:             return;
 8162:         }
 8163:     }
 8164: 
 8165:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8166: # Free bre access to adm and meta resources
 8167:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8168: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8169: 	&& ($priv eq 'bre')) {
 8170: 	return 'F';
 8171:     }
 8172: 
 8173: # Free bre access to user's own portfolio contents
 8174:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8175:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8176: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8177:         my %setters;
 8178:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
 8179:             &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 8180:         if (($startblock && $endblock) || ($by_ip)) {
 8181:             return 'B';
 8182:         } else {
 8183:             return 'F';
 8184:         }
 8185:     }
 8186: 
 8187: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8188:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8189:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8190:         if (exists($env{'request.course.id'})) {
 8191:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8192:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8193:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8194:                 my $courseprivid=$env{'request.course.id'};
 8195:                 $courseprivid=~s/\_/\//;
 8196:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8197:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8198:                     return $1; 
 8199:                 } else {
 8200:                     if ($env{'request.course.sec'}) {
 8201:                         $courseprivid.='/'.$env{'request.course.sec'};
 8202:                     }
 8203:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8204:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8205:                         return $2;
 8206:                     }
 8207:                 }
 8208:             }
 8209:         }
 8210:     }
 8211: 
 8212: # Free bre to public access
 8213: 
 8214:     if ($priv eq 'bre') {
 8215:         my $copyright;
 8216:         unless ($uri =~ /ext\.tool/) {
 8217:             $copyright=&metadata($uri,'copyright');
 8218:         }
 8219: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8220:            return 'F'; 
 8221:         }
 8222:         if ($copyright eq 'priv') {
 8223:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8224: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8225: 		return '';
 8226:             }
 8227:         }
 8228:         if ($copyright eq 'domain') {
 8229:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8230: 	    unless (($env{'user.domain'} eq $1) ||
 8231:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8232: 		return '';
 8233:             }
 8234:         }
 8235:         if ($env{'request.role'}=~ /li\.\//) {
 8236:             # Library role, so allow browsing of resources in this domain.
 8237:             return 'F';
 8238:         }
 8239:         if ($copyright eq 'custom') {
 8240: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8241:         }
 8242:     }
 8243:     # Domain coordinator is trying to create a course
 8244:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8245:         # uri is the requested domain in this case.
 8246:         # comparison to 'request.role.domain' shows if the user has selected
 8247:         # a role of dc for the domain in question.
 8248:         return 'F' if ($uri eq $env{'request.role.domain'});
 8249:     }
 8250: 
 8251:     my $thisallowed='';
 8252:     my $statecond=0;
 8253:     my $courseprivid='';
 8254: 
 8255:     my $ownaccess;
 8256:     # Community Coordinator or Assistant Co-author browsing resource space.
 8257:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8258:         if ($uri eq '') {
 8259:             $ownaccess = 1;
 8260:         } else {
 8261:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8262:                 my $udom = $env{'user.domain'};
 8263:                 my $uname = $env{'user.name'};
 8264:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8265:                     $ownaccess = 1;
 8266:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8267:                     unless ($uri =~ m{\.\./}) {
 8268:                         $ownaccess = 1;
 8269:                     }
 8270:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8271:                     my $now = time;
 8272:                     if ($uri =~ m{^([^/]+)/?$}) {
 8273:                         my $adom = $1;
 8274:                         foreach my $key (keys(%env)) {
 8275:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8276:                                 my ($start,$end) = split(/\./,$env{$key});
 8277:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8278:                                     $ownaccess = 1;
 8279:                                     last;
 8280:                                 }
 8281:                             }
 8282:                         }
 8283:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8284:                         my $adom = $1;
 8285:                         my $aname = $2;
 8286:                         foreach my $role ('ca','aa') { 
 8287:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8288:                                 my ($start,$end) =
 8289:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
 8290:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8291:                                     $ownaccess = 1;
 8292:                                     last;
 8293:                                 }
 8294:                             }
 8295:                         }
 8296:                     }
 8297:                 }
 8298:             }
 8299:         }
 8300:     }
 8301: 
 8302: # Course
 8303: 
 8304:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8305:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8306:             $thisallowed.=$1;
 8307:         }
 8308:     }
 8309: 
 8310: # Domain
 8311: 
 8312:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8313:        =~/\Q$priv\E\&([^\:]*)/) {
 8314:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8315:             $thisallowed.=$1;
 8316:         }
 8317:     }
 8318: 
 8319: # User who is not author or co-author might still be able to edit
 8320: # resource of an author in the domain (e.g., if Domain Coordinator).
 8321:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8322:         (&allowed('mdc',$env{'request.course.id'}))) {
 8323:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8324:             $thisallowed.=$1;
 8325:         }
 8326:     }
 8327: 
 8328: # Course: uri itself is a course
 8329:     my $courseuri=$uri;
 8330:     $courseuri=~s/\_(\d)/\/$1/;
 8331:     $courseuri=~s/^([^\/])/\/$1/;
 8332: 
 8333:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8334:        =~/\Q$priv\E\&([^\:]*)/) {
 8335:         if ($priv eq 'mip') {
 8336:             my $rem = $1;
 8337:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8338:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8339:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8340:                 if ($cdom ne '') {
 8341:                     my %passwdconf = &get_passwdconf($cdom);
 8342:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8343:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8344:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8345:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8346:                                 unless (@inststatuses) {
 8347:                                     @inststatuses = ('default');
 8348:                                 }
 8349:                                 foreach my $status (@inststatuses) {
 8350:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8351:                                         $thisallowed.=$rem;
 8352:                                     }
 8353:                                 }
 8354:                             }
 8355:                         }
 8356:                     }
 8357:                 }
 8358:             }
 8359:         } else {
 8360:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8361:                 $thisallowed.=$1;
 8362:             }
 8363:         }
 8364:     }
 8365: 
 8366: # URI is an uploaded document for this course, default permissions don't matter
 8367: # not allowing 'edit' access (editupload) to uploaded course docs
 8368:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8369: 	$thisallowed='';
 8370:         my ($match)=&is_on_map($uri);
 8371:         if ($match) {
 8372:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8373:                   =~/\Q$priv\E\&([^\:]*)/) {
 8374:                 my $value = $1;
 8375:                 my $deeplinkblock;
 8376:                 unless ($nodeeplinkcheck) {
 8377:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8378:                 }
 8379:                 if ($deeplinkblock) {
 8380:                     $thisallowed='D';
 8381:                 } elsif ($noblockcheck) {
 8382:                     $thisallowed.=$value;
 8383:                 } else {
 8384:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8385:                     if (@blockers > 0) {
 8386:                         $thisallowed = 'B';
 8387:                     } else {
 8388:                         $thisallowed.=$value;
 8389:                     }
 8390:                 }
 8391:             }
 8392:         } else {
 8393:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8394:             if ($refuri) {
 8395:                 if ($refuri =~ m|^/adm/|) {
 8396:                     $thisallowed='F';
 8397:                 } else {
 8398:                     $refuri=&declutter($refuri);
 8399:                     my ($match) = &is_on_map($refuri);
 8400:                     if ($match) {
 8401:                         my $deeplinkblock;
 8402:                         unless ($nodeeplinkcheck) {
 8403:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8404:                         }
 8405:                         if ($deeplinkblock) {
 8406:                             $thisallowed='D';
 8407:                         } elsif ($noblockcheck) {
 8408:                             $thisallowed='F';
 8409:                         } else {
 8410:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8411:                             if (@blockers > 0) {
 8412:                                 $thisallowed = 'B';
 8413:                             } else {
 8414:                                 $thisallowed='F';
 8415:                             }
 8416:                         }
 8417:                     }
 8418:                 }
 8419:             }
 8420:         }
 8421:     }
 8422: 
 8423:     if ($priv eq 'bre'
 8424: 	&& $thisallowed ne 'F' 
 8425: 	&& $thisallowed ne '2'
 8426: 	&& &is_portfolio_url($uri)) {
 8427: 	$thisallowed = &portfolio_access($uri,$clientip);
 8428:     }
 8429: 
 8430: # Full access at system, domain or course-wide level? Exit.
 8431:     if ($thisallowed=~/F/) {
 8432: 	return 'F';
 8433:     }
 8434: 
 8435: # If this is generating or modifying users, exit with special codes
 8436: 
 8437:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8438: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8439: 	    my ($audom,$auname)=split('/',$uri);
 8440: # no author name given, so this just checks on the general right to make a co-author in this domain
 8441: 	    unless ($auname) { return $thisallowed; }
 8442: # an author name is given, so we are about to actually make a co-author for a certain account
 8443: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8444: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8445: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8446: 	}
 8447: 	return $thisallowed;
 8448:     }
 8449: #
 8450: # Gathered so far: system, domain and course wide privileges
 8451: #
 8452: # Course: See if uri or referer is an individual resource that is part of 
 8453: # the course
 8454: 
 8455:     if ($env{'request.course.id'}) {
 8456: 
 8457: # If this is modifying password (internal auth) domains must match for user and user's role.
 8458: 
 8459:         if ($priv eq 'mip') {
 8460:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8461:                 return $thisallowed;
 8462:             } else {
 8463:                 return '';
 8464:             }
 8465:         }
 8466: 
 8467:        $courseprivid=$env{'request.course.id'};
 8468:        if ($env{'request.course.sec'}) {
 8469:           $courseprivid.='/'.$env{'request.course.sec'};
 8470:        }
 8471:        $courseprivid=~s/\_/\//;
 8472:        my $checkreferer=1;
 8473:        my ($match,$cond)=&is_on_map($uri);
 8474:        if ($match) {
 8475:            $statecond=$cond;
 8476:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8477:                =~/\Q$priv\E\&([^\:]*)/) {
 8478:                my $value = $1;
 8479:                if ($priv eq 'bre') {
 8480:                    my $deeplinkblock;
 8481:                    unless ($nodeeplinkcheck) {
 8482:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8483:                    }
 8484:                    if ($deeplinkblock) {
 8485:                        $thisallowed = 'D';
 8486:                    } elsif ($noblockcheck) {
 8487:                        $thisallowed.=$value;
 8488:                    } else {
 8489:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8490:                        if (@blockers > 0) {
 8491:                            $thisallowed = 'B';
 8492:                        } else {
 8493:                            $thisallowed.=$value;
 8494:                        }
 8495:                    }
 8496:                } else {
 8497:                    $thisallowed.=$value;
 8498:                }
 8499:                $checkreferer=0;
 8500:            }
 8501:        }
 8502: 
 8503:        if ($checkreferer) {
 8504: 	  my $refuri=$env{'httpref.'.$orguri};
 8505:             unless ($refuri) {
 8506:                 foreach my $key (keys(%env)) {
 8507: 		    if ($key=~/^httpref\..*\*/) {
 8508: 			my $pattern=$key;
 8509:                         $pattern=~s/^httpref\.\/res\///;
 8510:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8511:                         $pattern=~s/\//\\\//g;
 8512:                         if ($orguri=~/$pattern/) {
 8513: 			    $refuri=$env{$key};
 8514:                         }
 8515:                     }
 8516:                 }
 8517:             }
 8518: 
 8519:          if ($refuri) { 
 8520: 	  $refuri=&declutter($refuri);
 8521:           my ($match,$cond)=&is_on_map($refuri);
 8522:             if ($match) {
 8523:               my $refstatecond=$cond;
 8524:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8525:                   =~/\Q$priv\E\&([^\:]*)/) {
 8526:                   my $value = $1;
 8527:                   if ($priv eq 'bre') {
 8528:                       my $deeplinkblock;
 8529:                       unless ($nodeeplinkcheck) {
 8530:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8531:                       }
 8532:                       if ($deeplinkblock) {
 8533:                           $thisallowed = 'D';
 8534:                       } elsif ($noblockcheck) {
 8535:                           $thisallowed.=$value;
 8536:                       } else {
 8537:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8538:                           if (@blockers > 0) {
 8539:                               $thisallowed = 'B';
 8540:                           } else {
 8541:                               $thisallowed.=$value;
 8542:                           }
 8543:                       }
 8544:                   } else {
 8545:                       $thisallowed.=$value;
 8546:                   }
 8547:                   $uri=$refuri;
 8548:                   $statecond=$refstatecond;
 8549:               }
 8550:           }
 8551:         }
 8552:        }
 8553:    }
 8554: 
 8555: #
 8556: # Gathered now: all privileges that could apply, and condition number
 8557: # 
 8558: #
 8559: # Full or no access?
 8560: #
 8561: 
 8562:     if ($thisallowed=~/F/) {
 8563: 	return 'F';
 8564:     }
 8565: 
 8566:     unless ($thisallowed) {
 8567:         return '';
 8568:     }
 8569: 
 8570: # Restrictions exist, deal with them
 8571: #
 8572: #   C:according to course preferences
 8573: #   R:according to resource settings
 8574: #   L:unless locked
 8575: #   X:according to user session state
 8576: #
 8577: 
 8578: # Possibly locked functionality, check all courses
 8579: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
 8580: # Locks might take effect only after 10 minutes cache expiration for other
 8581: # courses, and 2 minutes for current course, in which user has st or ta role
 8582: # which is neither expired nor a future role (unless current course).
 8583: 
 8584:     my ($needlockcheck,$now,$crsonly);
 8585:     if ($thisallowed=~/L/) {
 8586:         $now = time;
 8587:         if ($priv eq 'bre') {
 8588:             if ($uri ne '') {
 8589:                 if ($orguri =~ m{^/+res/}) {
 8590:                     if ($uri =~ m{^lib/templates/}) {
 8591:                         if ($env{'request.course.id'}) {
 8592:                             $crsonly = 1;
 8593:                             $needlockcheck = 1;
 8594:                         }
 8595:                     } else {
 8596:                         $needlockcheck = 1;
 8597:                     }
 8598:                 } elsif ($env{'request.course.id'}) {
 8599:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
 8600:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
 8601:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
 8602:                         $crsonly = 1;
 8603:                     }
 8604:                     $needlockcheck = 1;
 8605:                 }
 8606:             }
 8607:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
 8608:             $needlockcheck = 1;
 8609:         }
 8610:     }
 8611:     if ($needlockcheck) {
 8612:         foreach my $envkey (keys(%env)) {
 8613:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8614:                my $courseid=$2;
 8615:                my $roleid=$1.'.'.$2;
 8616:                $courseid=~s/^\///;
 8617:                unless ($env{'request.role'} eq $roleid) {
 8618:                    my ($start,$end) = split(/\./,$env{$envkey});
 8619:                    next unless (($now >= $start) && (!$end || $end > $now));
 8620:                }
 8621:                my $expiretime=600;
 8622:                if ($env{'request.role'} eq $roleid) {
 8623: 		  $expiretime=120;
 8624:                }
 8625: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8626:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8627:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8628: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8629:                }
 8630:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8631:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8632: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8633:                        &log($env{'user.domain'},$env{'user.name'},
 8634:                             $env{'user.home'},
 8635:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8636:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8637:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8638: 		       return '';
 8639:                    }
 8640:                }
 8641:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8642:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8643: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
 8644:                        &log($env{'user.domain'},$env{'user.name'},
 8645:                             $env{'user.home'},
 8646:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8647:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8648:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8649: 		       return '';
 8650:                    }
 8651:                }
 8652: 	   }
 8653:        }
 8654:     }
 8655: 
 8656: #
 8657: # Rest of the restrictions depend on selected course
 8658: #
 8659: 
 8660:     unless ($env{'request.course.id'}) {
 8661: 	if ($thisallowed eq 'A') {
 8662: 	    return 'A';
 8663:         } elsif ($thisallowed eq 'B') {
 8664:             return 'B';
 8665: 	} else {
 8666: 	    return '1';
 8667: 	}
 8668:     }
 8669: 
 8670: #
 8671: # Now user is definitely in a course
 8672: #
 8673: 
 8674: 
 8675: # Course preferences
 8676: 
 8677:    if ($thisallowed=~/C/) {
 8678:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8679:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8680:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8681: 	   =~/\Q$rolecode\E/) {
 8682: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8683: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8684: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8685: 			$env{'request.course.id'});
 8686: 	   }
 8687:            return '';
 8688:        }
 8689: 
 8690:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8691: 	   =~/\Q$unamedom\E/) {
 8692: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8693: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8694: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8695: 			$env{'request.course.id'});
 8696: 	   }
 8697:            return '';
 8698:        }
 8699:    }
 8700: 
 8701: # Resource preferences
 8702: 
 8703:    if ($thisallowed=~/R/) {
 8704:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8705:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8706: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8707: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8708: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8709: 	   }
 8710: 	   return '';
 8711:        }
 8712:    }
 8713: 
 8714: # Restricted for deeplinked session?
 8715: 
 8716:     if ($env{'request.deeplink.login'}) {
 8717:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
 8718:             if (!$symb) { $symb=&symbread($uri,1); }
 8719:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
 8720:                 return '';
 8721:             }
 8722:         }
 8723:     }
 8724: 
 8725: # Restricted by state or randomout?
 8726: 
 8727:    if ($thisallowed=~/X/) {
 8728:       if ($env{'acc.randomout'}) {
 8729: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8730:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8731:             return ''; 
 8732:          }
 8733:       }
 8734:       if (&condval($statecond)) {
 8735: 	 return '2';
 8736:       } else {
 8737:          return '';
 8738:       }
 8739:    }
 8740: 
 8741:     if ($thisallowed eq 'A') {
 8742: 	return 'A';
 8743:     } elsif ($thisallowed eq 'B') {
 8744:         return 'B';
 8745:     } elsif ($thisallowed eq 'D') {
 8746:         return 'D';
 8747:     }
 8748:    return 'F';
 8749: }
 8750: 
 8751: # ------------------------------------------- Check construction space access
 8752: 
 8753: sub constructaccess {
 8754:     my ($url,$setpriv)=@_;
 8755: 
 8756: # We do not allow editing of previous versions of files
 8757:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8758: 
 8759: # Get username and domain from URL
 8760:     my ($ownername,$ownerdomain,$ownerhome);
 8761: 
 8762:     ($ownerdomain,$ownername) =
 8763:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8764: 
 8765: # The URL does not really point to any authorspace, forget it
 8766:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8767: 
 8768: # Now we need to see if the user has access to the authorspace of
 8769: # $ownername at $ownerdomain
 8770: 
 8771:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8772: # Real author for this?
 8773:        $ownerhome = $env{'user.home'};
 8774:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8775:           return ($ownername,$ownerdomain,$ownerhome);
 8776:        }
 8777:     } else {
 8778: # Co-author for this?
 8779:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8780:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8781:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8782:             return ($ownername,$ownerdomain,$ownerhome);
 8783:         }
 8784:         if ($env{'request.course.id'}) {
 8785:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8786:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8787:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8788:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8789:                     return ($ownername,$ownerdomain,$ownerhome);
 8790:                 }
 8791:             }
 8792:         }
 8793:     }
 8794: 
 8795: # We don't have any access right now. If we are not possibly going to do anything about this,
 8796: # we might as well leave
 8797:    unless ($setpriv) { return ''; }
 8798: 
 8799: # Backdoor access?
 8800:     my $allowed=&allowed('eco',$ownerdomain);
 8801: # Nope
 8802:     unless ($allowed) { return ''; }
 8803: # Looks like we may have access, but could be locked by the owner of the construction space
 8804:     if ($allowed eq 'U') {
 8805:         my %blocked=&get('environment',['domcoord.author'],
 8806:                          $ownerdomain,$ownername);
 8807: # Is blocked by owner
 8808:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8809:     }
 8810:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8811: # Grant temporary access
 8812:         my $then=$env{'user.login.time'};
 8813:         my $update=$env{'user.update.time'};
 8814:         if (!$update) { $update = $then; }
 8815:         my $refresh=$env{'user.refresh.time'};
 8816:         if (!$refresh) { $refresh = $update; }
 8817:         my $now = time;
 8818:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8819:                            $now,'ca','constructaccess');
 8820:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8821:         return($ownername,$ownerdomain,$ownerhome);
 8822:     }
 8823: # No business here
 8824:     return '';
 8825: }
 8826: 
 8827: # ----------------------------------------------------------- Content Blocking
 8828: 
 8829: {
 8830: # Caches for faster Course Contents display where content blocking
 8831: # is in operation (i.e., interval param set) for timed quiz.
 8832: #
 8833: # User for whom data are being temporarily cached.
 8834: my $cacheduser='';
 8835: # Course for which data are being temporarily cached.
 8836: my $cachedcid='';
 8837: # Cached blockers for this user (a hash of blocking items). 
 8838: my %cachedblockers=();
 8839: # When the data were last cached.
 8840: my $cachedlast='';
 8841: 
 8842: sub load_all_blockers {
 8843:     my ($uname,$udom)=@_;
 8844:     if (($uname ne '') && ($udom ne '')) { 
 8845:         if (($cacheduser eq $uname.':'.$udom) &&
 8846:             ($cachedcid eq $env{'request.course.id'}) &&
 8847:             (abs($cachedlast-time)<5)) {
 8848:             return;
 8849:         }
 8850:     }
 8851:     $cachedlast=time;
 8852:     $cacheduser=$uname.':'.$udom;
 8853:     $cachedcid=$env{'request.course.id'};
 8854:     %cachedblockers = &get_commblock_resources();
 8855:     return;
 8856: }
 8857: 
 8858: sub get_comm_blocks {
 8859:     my ($cdom,$cnum) = @_;
 8860:     if ($cdom eq '' || $cnum eq '') {
 8861:         return unless ($env{'request.course.id'});
 8862:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8863:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8864:     }
 8865:     my %commblocks;
 8866:     my $hashid=$cdom.'_'.$cnum;
 8867:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8868:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8869:         %commblocks = %{$blocksref};
 8870:     } else {
 8871:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8872:         my $cachetime = 600;
 8873:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8874:     }
 8875:     return %commblocks;
 8876: }
 8877: 
 8878: sub get_commblock_resources {
 8879:     my ($blocks) = @_;
 8880:     my %blockers = ();
 8881:     return %blockers unless ($env{'request.course.id'});
 8882:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 8883:     if ($env{'request.course.sec'}) {
 8884:         $courseurl .= '/'.$env{'request.course.sec'};
 8885:     }
 8886:     return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 8887:     my %commblocks;
 8888:     if (ref($blocks) eq 'HASH') {
 8889:         %commblocks = %{$blocks};
 8890:     } else {
 8891:         %commblocks = &get_comm_blocks();
 8892:     }
 8893:     return %blockers unless (keys(%commblocks) > 0); 
 8894:     my $navmap = Apache::lonnavmaps::navmap->new();
 8895:     return %blockers unless (ref($navmap));
 8896:     my $now = time;
 8897:     foreach my $block (keys(%commblocks)) {
 8898:         if ($block =~ /^(\d+)____(\d+)$/) {
 8899:             my ($start,$end) = ($1,$2);
 8900:             if ($start <= $now && $end >= $now) {
 8901:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8902:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8903:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8904:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8905:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8906:                             }
 8907:                         }
 8908:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8909:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8910:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8911:                             }
 8912:                         }
 8913:                     }
 8914:                 }
 8915:             }
 8916:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8917:             my $item = $1;
 8918:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8919:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8920:                     my (@interval,$mapname);
 8921:                     my $type = 'map';
 8922:                     if ($item eq 'course') {
 8923:                         $type = 'course';
 8924:                         @interval=&EXT("resource.0.interval");
 8925:                     } else {
 8926:                         if ($item =~ /___\d+___/) {
 8927:                             $type = 'resource';
 8928:                             @interval=&EXT("resource.0.interval",$item);
 8929:                         } else {
 8930:                             $mapname = &deversion($item);
 8931:                             if (ref($navmap)) {
 8932:                                 my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
 8933:                                 @interval = ($timelimit,'map');
 8934:                             }
 8935:                         }
 8936:                     }
 8937:                     if ($interval[0] =~ /^(\d+)/) {
 8938:                         my $timelimit = $1; 
 8939:                         my $first_access;
 8940:                         if ($type eq 'resource') {
 8941:                             $first_access=&get_first_access($interval[1],$item);
 8942:                         } elsif ($type eq 'map') {
 8943:                             $first_access=&get_first_access($interval[1],undef,$item);
 8944:                         } else {
 8945:                             $first_access=&get_first_access($interval[1]);
 8946:                         }
 8947:                         if ($first_access) {
 8948:                             my $timesup = $first_access+$timelimit;
 8949:                             if ($timesup > $now) {
 8950:                                 my $activeblock;
 8951:                                 if ($type eq 'resource') {
 8952:                                     if (ref($navmap)) {
 8953:                                         my $res = $navmap->getBySymb($item);
 8954:                                         if ($res->answerable()) {
 8955:                                             $activeblock = 1;
 8956:                                         }
 8957:                                     }
 8958:                                 } elsif ($type eq 'map') {
 8959:                                     my $mapsymb = &symbread($mapname,1);
 8960:                                     if (($mapsymb) && (ref($navmap))) {
 8961:                                         my $mapres = $navmap->getBySymb($mapsymb);
 8962:                                         if (ref($mapres)) {
 8963:                                             my $first = $mapres->map_start();
 8964:                                             my $finish = $mapres->map_finish();
 8965:                                             my $it = $navmap->getIterator($first,$finish,undef,0,0);
 8966:                                             if (ref($it)) {
 8967:                                                 my $res;
 8968:                                                 while ($res = $it->next(undef,1)) {
 8969:                                                     next unless (ref($res));
 8970:                                                     my $symb = $res->symb();
 8971:                                                     next if (($symb eq $mapsymb) || ($symb eq ''));
 8972:                                                     @interval=&EXT("resource.0.interval",$symb);
 8973:                                                     if ($interval[1] eq 'map') {
 8974:                                                         if ($res->answerable()) {
 8975:                                                             $activeblock = 1;
 8976:                                                             last;
 8977:                                                         }
 8978:                                                     }
 8979:                                                 }
 8980:                                             }
 8981:                                         }
 8982:                                     }
 8983:                                 }
 8984:                                 if ($activeblock) {
 8985:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8986:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8987:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8988:                                          }
 8989:                                     }
 8990:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8991:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8992:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8993:                                         }
 8994:                                     }
 8995:                                 }
 8996:                             }
 8997:                         }
 8998:                     }
 8999:                 }
 9000:             }
 9001:         }
 9002:     }
 9003:     return %blockers;
 9004: }
 9005: 
 9006: sub has_comm_blocking {
 9007:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 9008:     my @blockers;
 9009:     return unless ($env{'request.course.id'});
 9010:     return unless ($priv eq 'bre');
 9011:     return if ($env{'request.state'} eq 'construct');
 9012:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9013:     if ($env{'request.course.sec'}) {
 9014:         $courseurl .= '/'.$env{'request.course.sec'};
 9015:     }
 9016:     return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9017:     my %blockinfo;
 9018:     if (ref($blocks) eq 'HASH') {
 9019:         %blockinfo = &get_commblock_resources($blocks);
 9020:     } else {
 9021:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 9022:         %blockinfo = %cachedblockers;
 9023:     }
 9024:     return unless (keys(%blockinfo) > 0);
 9025:     my (%possibles,@symbs);
 9026:     if (!$symb) {
 9027:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 9028:     }
 9029:     if ($symb) {
 9030:         @symbs = ($symb);
 9031:     } elsif (keys(%possibles)) { 
 9032:         @symbs = keys(%possibles);
 9033:     }
 9034:     my $noblock;
 9035:     foreach my $symb (@symbs) {
 9036:         last if ($noblock);
 9037:         my ($map,$resid,$resurl)=&decode_symb($symb);
 9038:         foreach my $block (keys(%blockinfo)) {
 9039:             if ($block =~ /^firstaccess____(.+)$/) {
 9040:                 my $item = $1;
 9041:                 unless ($blocked) {
 9042:                     if (($item eq $map) || ($item eq $symb)) {
 9043:                         $noblock = 1;
 9044:                         last;
 9045:                     }
 9046:                 }
 9047:             }
 9048:             if (ref($blockinfo{$block}) eq 'HASH') {
 9049:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 9050:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 9051:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9052:                             push(@blockers,$block);
 9053:                         }
 9054:                     }
 9055:                 }
 9056:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 9057:                     if ($blockinfo{$block}{'maps'}{$map}) {
 9058:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9059:                             push(@blockers,$block);
 9060:                         }
 9061:                     }
 9062:                 }
 9063:             }
 9064:         }
 9065:     }
 9066:     unless ($noblock) { 
 9067:         return @blockers;
 9068:     }
 9069:     return;
 9070: }
 9071: }
 9072: 
 9073: sub deeplink_check {
 9074:     my ($priv,$symb,$uri) = @_;
 9075:     return unless ($env{'request.course.id'});
 9076:     return unless ($priv eq 'bre');
 9077:     return if ($env{'request.state'} eq 'construct');
 9078:     return if ($env{'request.role.adv'});
 9079:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9080:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9081:     my (%possibles,@symbs);
 9082:     if (!$symb) {
 9083:         $symb = &symbread($uri,1,1,1,\%possibles);
 9084:     }
 9085:     if ($symb) {
 9086:         @symbs = ($symb);
 9087:     } elsif (keys(%possibles)) {
 9088:         @symbs = keys(%possibles);
 9089:     }
 9090: 
 9091:     my ($deeplink_symb,$allow);
 9092:     if ($env{'request.deeplink.login'}) {
 9093:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
 9094:     }
 9095:     foreach my $symb (@symbs) {
 9096:         last if ($allow);
 9097:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9098:         if ($deeplink eq '') {
 9099:             $allow = 1;
 9100:         } else {
 9101:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
 9102:             if ($state ne 'only') {
 9103:                 $allow = 1;
 9104:             } else {
 9105:                 my $check_deeplink_entry;
 9106:                 if ($protect ne 'none') {
 9107:                     my ($acctype,$item) = split(/:/,$protect);
 9108:                     if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
 9109:                         if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
 9110:                             $check_deeplink_entry = 1
 9111:                         }
 9112:                     } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
 9113:                         if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
 9114:                             $check_deeplink_entry = 1;
 9115:                         }
 9116:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9117:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9118:                             $check_deeplink_entry = 1;
 9119:                         }
 9120:                     }
 9121:                 }
 9122:                 if (($protect eq 'none') || ($check_deeplink_entry)) {
 9123:                     if ($scope eq 'res') {
 9124:                         if ($symb eq $deeplink_symb) {
 9125:                             $allow = 1;
 9126:                         }
 9127:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
 9128:                         my ($map_from_symb,$map_from_login);
 9129:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
 9130:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
 9131:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
 9132:                         } else {
 9133:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
 9134:                         }
 9135:                         if (($map_from_symb) && ($map_from_login)) {
 9136:                             if ($map_from_symb eq $map_from_login) {
 9137:                                 $allow = 1;
 9138:                             } elsif ($scope eq 'rec') {
 9139:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
 9140:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
 9141:                                     $allow = 1;
 9142:                                 }
 9143:                             }
 9144:                         }
 9145:                     }
 9146:                 }
 9147:             }
 9148:         }
 9149:     }
 9150:     return if ($allow);
 9151:     return 1;
 9152: }
 9153: 
 9154: # -------------------------------- Deversion and split uri into path an filename   
 9155: 
 9156: #
 9157: #   Removes the version from a URI and
 9158: #   splits it in to its filename and path to the filename.
 9159: #   Seems like File::Basename could have done this more clearly.
 9160: #   Parameters:
 9161: #      $uri   - input URI
 9162: #   Returns:
 9163: #     Two element list consisting of 
 9164: #     $pathname  - the URI up to and excluding the trailing /
 9165: #     $filename  - The part of the URI following the last /
 9166: #  NOTE:
 9167: #    Another realization of this is simply:
 9168: #    use File::Basename;
 9169: #    ...
 9170: #    $uri = shift;
 9171: #    $filename = basename($uri);
 9172: #    $path     = dirname($uri);
 9173: #    return ($filename, $path);
 9174: #
 9175: #     The implementation below is probably faster however.
 9176: #
 9177: sub split_uri_for_cond {
 9178:     my $uri=&deversion(&declutter(shift));
 9179:     my @uriparts=split(/\//,$uri);
 9180:     my $filename=pop(@uriparts);
 9181:     my $pathname=join('/',@uriparts);
 9182:     return ($pathname,$filename);
 9183: }
 9184: # --------------------------------------------------- Is a resource on the map?
 9185: 
 9186: sub is_on_map {
 9187:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9188:     #Trying to find the conditional for the file
 9189:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9190: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9191:     if ($match) {
 9192: 	return (1,$1);
 9193:     } else {
 9194: 	return (0,0);
 9195:     }
 9196: }
 9197: 
 9198: # --------------------------------------------------------- Get symb from alias
 9199: 
 9200: sub get_symb_from_alias {
 9201:     my $symb=shift;
 9202:     my ($map,$resid,$url)=&decode_symb($symb);
 9203: # Already is a symb
 9204:     if ($url) { return $symb; }
 9205: # Must be an alias
 9206:     my $aliassymb='';
 9207:     my %bighash;
 9208:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9209:                             &GDBM_READER(),0640)) {
 9210:         my $rid=$bighash{'mapalias_'.$symb};
 9211: 	if ($rid) {
 9212: 	    my ($mapid,$resid)=split(/\./,$rid);
 9213: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9214: 				    $resid,$bighash{'src_'.$rid});
 9215: 	}
 9216:         untie %bighash;
 9217:     }
 9218:     return $aliassymb;
 9219: }
 9220: 
 9221: # ----------------------------------------------------------------- Define Role
 9222: 
 9223: sub definerole {
 9224:   if (allowed('mcr','/')) {
 9225:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9226:     foreach my $role (split(':',$sysrole)) {
 9227: 	my ($crole,$cqual)=split(/\&/,$role);
 9228:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9229:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9230: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9231:                return "refused:s:$crole&$cqual"; 
 9232:             }
 9233:         }
 9234:     }
 9235:     foreach my $role (split(':',$domrole)) {
 9236: 	my ($crole,$cqual)=split(/\&/,$role);
 9237:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9238:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9239: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9240:                return "refused:d:$crole&$cqual"; 
 9241:             }
 9242:         }
 9243:     }
 9244:     foreach my $role (split(':',$courole)) {
 9245: 	my ($crole,$cqual)=split(/\&/,$role);
 9246:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9247:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9248: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9249:                return "refused:c:$crole&$cqual"; 
 9250:             }
 9251:         }
 9252:     }
 9253:     my $uhome;
 9254:     if (($uname ne '') && ($udom ne '')) {
 9255:         $uhome = &homeserver($uname,$udom);
 9256:         return $uhome if ($uhome eq 'no_host');
 9257:     } else {
 9258:         $uname = $env{'user.name'};
 9259:         $udom = $env{'user.domain'};
 9260:         $uhome = $env{'user.home'};
 9261:     }
 9262:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9263:                 "$udom:$uname:rolesdef_$rolename=".
 9264:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9265:     return reply($command,$uhome);
 9266:   } else {
 9267:     return 'refused';
 9268:   }
 9269: }
 9270: 
 9271: # ---------------- Make a metadata query against the network of library servers
 9272: 
 9273: sub metadata_query {
 9274:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9275:     my %rhash;
 9276:     my %libserv = &all_library();
 9277:     my @server_list = (defined($server_array) ? @$server_array
 9278:                                               : keys(%libserv) );
 9279:     for my $server (@server_list) {
 9280:         my $domains = ''; 
 9281:         if (ref($domains_hash) eq 'HASH') {
 9282:             $domains = $domains_hash->{$server}; 
 9283:         }
 9284: 	unless ($custom or $customshow) {
 9285: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9286: 	    $rhash{$server}=$reply;
 9287: 	}
 9288: 	else {
 9289: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9290: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9291: 			     $server);
 9292: 	    $rhash{$server}=$reply;
 9293: 	}
 9294:     }
 9295:     return \%rhash;
 9296: }
 9297: 
 9298: # ----------------------------------------- Send log queries and wait for reply
 9299: 
 9300: sub log_query {
 9301:     my ($uname,$udom,$query,%filters)=@_;
 9302:     my $uhome=&homeserver($uname,$udom);
 9303:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9304:     my $uhost=&hostname($uhome);
 9305:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9306:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9307:                        $uhome);
 9308:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9309:     return get_query_reply($queryid);
 9310: }
 9311: 
 9312: # -------------------------- Update MySQL table for portfolio file
 9313: 
 9314: sub update_portfolio_table {
 9315:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9316:     if ($group ne '') {
 9317:         $file_name =~s /^\Q$group\E//;
 9318:     }
 9319:     my $homeserver = &homeserver($uname,$udom);
 9320:     my $queryid=
 9321:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9322:                ':'.&escape($file_name).':'.$action,$homeserver);
 9323:     my $reply = &get_query_reply($queryid);
 9324:     return $reply;
 9325: }
 9326: 
 9327: # -------------------------- Update MySQL allusers table
 9328: 
 9329: sub update_allusers_table {
 9330:     my ($uname,$udom,$names) = @_;
 9331:     my $homeserver = &homeserver($uname,$udom);
 9332:     my $queryid=
 9333:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9334:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9335:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9336:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9337:                'generation='.&escape($names->{'generation'}).'%%'.
 9338:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9339:                'id='.&escape($names->{'id'}),$homeserver);
 9340:     return;
 9341: }
 9342: 
 9343: # ------- Request retrieval of institutional classlists for course(s)
 9344: 
 9345: sub fetch_enrollment_query {
 9346:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9347:     my ($homeserver,$sleep,$loopmax);
 9348:     my $maxtries = 1;
 9349:     if ($context eq 'automated') {
 9350:         $homeserver = $perlvar{'lonHostID'};
 9351:         $sleep = 2;
 9352:         $loopmax = 100;
 9353:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9354:     } else {
 9355:         $homeserver = &homeserver($cnum,$dom);
 9356:     }
 9357:     my $host=&hostname($homeserver);
 9358:     my $cmd = '';
 9359:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9360:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9361:     }
 9362:     $cmd =~ s/%%$//;
 9363:     $cmd = &escape($cmd);
 9364:     my $query = 'fetchenrollment';
 9365:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9366:     unless ($queryid=~/^\Q$host\E\_/) { 
 9367:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9368:         return 'error: '.$queryid;
 9369:     }
 9370:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9371:     my $tries = 1;
 9372:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9373:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9374:         $tries ++;
 9375:     }
 9376:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9377:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9378:     } else {
 9379:         my @responses = split(/:/,$reply);
 9380:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9381:             foreach my $line (@responses) {
 9382:                 my ($key,$value) = split(/=/,$line,2);
 9383:                 $$replyref{$key} = $value;
 9384:             }
 9385:         } else {
 9386:             my $pathname = LONCAPA::tempdir();
 9387:             foreach my $line (@responses) {
 9388:                 my ($key,$value) = split(/=/,$line);
 9389:                 $$replyref{$key} = $value;
 9390:                 if ($value > 0) {
 9391:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9392:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9393:                         my $destname = $pathname.'/'.$filename;
 9394:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9395:                         if ($xml_classlist =~ /^error/) {
 9396:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9397:                         } else {
 9398:                             if ( open(FILE,">",$destname) ) {
 9399:                                 print FILE &unescape($xml_classlist);
 9400:                                 close(FILE);
 9401:                             } else {
 9402:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9403:                             }
 9404:                         }
 9405:                     }
 9406:                 }
 9407:             }
 9408:         }
 9409:         return 'ok';
 9410:     }
 9411:     return 'error';
 9412: }
 9413: 
 9414: sub get_query_reply {
 9415:     my ($queryid,$sleep,$loopmax) = @_;
 9416:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9417:         $sleep = 0.2;
 9418:     }
 9419:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9420:         $loopmax = 100;
 9421:     }
 9422:     my $replyfile=LONCAPA::tempdir().$queryid;
 9423:     my $reply='';
 9424:     for (1..$loopmax) {
 9425: 	sleep($sleep);
 9426:         if (-e $replyfile.'.end') {
 9427: 	    if (open(my $fh,"<",$replyfile)) {
 9428: 		$reply = join('',<$fh>);
 9429: 		close($fh);
 9430: 	   } else { return 'error: reply_file_error'; }
 9431:            return &unescape($reply);
 9432: 	}
 9433:     }
 9434:     return 'timeout:'.$queryid;
 9435: }
 9436: 
 9437: sub courselog_query {
 9438: #
 9439: # possible filters:
 9440: # url: url or symb
 9441: # username
 9442: # domain
 9443: # action: view, submit, grade
 9444: # start: timestamp
 9445: # end: timestamp
 9446: #
 9447:     my (%filters)=@_;
 9448:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9449:     if ($filters{'url'}) {
 9450: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9451:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9452:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9453:     }
 9454:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9455:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9456:     return &log_query($cname,$cdom,'courselog',%filters);
 9457: }
 9458: 
 9459: sub userlog_query {
 9460: #
 9461: # possible filters:
 9462: # action: log check role
 9463: # start: timestamp
 9464: # end: timestamp
 9465: #
 9466:     my ($uname,$udom,%filters)=@_;
 9467:     return &log_query($uname,$udom,'userlog',%filters);
 9468: }
 9469: 
 9470: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9471: 
 9472: sub auto_run {
 9473:     my ($cnum,$cdom) = @_;
 9474:     my $response = 0;
 9475:     my $settings;
 9476:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9477:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9478:         $settings = $domconfig{'autoenroll'};
 9479:         if ($settings->{'run'} eq '1') {
 9480:             $response = 1;
 9481:         }
 9482:     } else {
 9483:         my $homeserver;
 9484:         if (&is_course($cdom,$cnum)) {
 9485:             $homeserver = &homeserver($cnum,$cdom);
 9486:         } else {
 9487:             $homeserver = &domain($cdom,'primary');
 9488:         }
 9489:         if ($homeserver ne 'no_host') {
 9490:             $response = &reply('autorun:'.$cdom,$homeserver);
 9491:         }
 9492:     }
 9493:     return $response;
 9494: }
 9495: 
 9496: sub auto_get_sections {
 9497:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9498:     my $homeserver;
 9499:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9500:         $homeserver = &homeserver($cnum,$cdom);
 9501:     }
 9502:     if (!defined($homeserver)) { 
 9503:         if ($cdom =~ /^$match_domain$/) {
 9504:             $homeserver = &domain($cdom,'primary');
 9505:         }
 9506:     }
 9507:     my @secs;
 9508:     if (defined($homeserver)) {
 9509:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9510:         unless ($response eq 'refused') {
 9511:             @secs = split(/:/,$response);
 9512:         }
 9513:     }
 9514:     return @secs;
 9515: }
 9516: 
 9517: sub auto_new_course {
 9518:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9519:     my $homeserver = &homeserver($cnum,$cdom);
 9520:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9521:     return $response;
 9522: }
 9523: 
 9524: sub auto_validate_courseID {
 9525:     my ($cnum,$cdom,$inst_course_id) = @_;
 9526:     my $homeserver = &homeserver($cnum,$cdom);
 9527:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9528:     return $response;
 9529: }
 9530: 
 9531: sub auto_validate_instcode {
 9532:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9533:     my ($homeserver,$response);
 9534:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9535:         $homeserver = &homeserver($cnum,$cdom);
 9536:     }
 9537:     if (!defined($homeserver)) {
 9538:         if ($cdom =~ /^$match_domain$/) {
 9539:             $homeserver = &domain($cdom,'primary');
 9540:         }
 9541:     }
 9542:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9543:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9544:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9545:     return ($outcome,$description,$defaultcredits);
 9546: }
 9547: 
 9548: sub auto_validate_inst_crosslist {
 9549:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
 9550:     my ($homeserver,$response);
 9551:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9552:         $homeserver = &homeserver($cnum,$cdom);
 9553:     }
 9554:     if (!defined($homeserver)) {
 9555:         if ($cdom =~ /^$match_domain$/) {
 9556:             $homeserver = &domain($cdom,'primary');
 9557:         }
 9558:     }
 9559:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
 9560:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
 9561:                          &escape($instcode).':'.&escape($inst_xlist).':'.
 9562:                          &escape($coowner),$homeserver);
 9563:     }
 9564:     return $response;
 9565: }
 9566: 
 9567: sub auto_create_password {
 9568:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9569:     my ($homeserver,$response);
 9570:     my $create_passwd = 0;
 9571:     my $authchk = '';
 9572:     if ($udom =~ /^$match_domain$/) {
 9573:         $homeserver = &domain($udom,'primary');
 9574:     }
 9575:     if ($homeserver eq '') {
 9576:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9577:             $homeserver = &homeserver($cnum,$cdom);
 9578:         }
 9579:     }
 9580:     if ($homeserver eq '') {
 9581:         $authchk = 'nodomain';
 9582:     } else {
 9583:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9584:         if ($response eq 'refused') {
 9585:             $authchk = 'refused';
 9586:         } else {
 9587:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9588:         }
 9589:     }
 9590:     return ($authparam,$create_passwd,$authchk);
 9591: }
 9592: 
 9593: sub auto_photo_permission {
 9594:     my ($cnum,$cdom,$students) = @_;
 9595:     my $homeserver = &homeserver($cnum,$cdom);
 9596:     my ($outcome,$perm_reqd,$conditions) = 
 9597: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9598:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9599: 	return (undef,undef);
 9600:     }
 9601:     return ($outcome,$perm_reqd,$conditions);
 9602: }
 9603: 
 9604: sub auto_checkphotos {
 9605:     my ($uname,$udom,$pid) = @_;
 9606:     my $homeserver = &homeserver($uname,$udom);
 9607:     my ($result,$resulttype);
 9608:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9609: 				   &escape($uname).':'.&escape($pid),
 9610: 				   $homeserver));
 9611:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9612: 	return (undef,undef);
 9613:     }
 9614:     if ($outcome) {
 9615:         ($result,$resulttype) = split(/:/,$outcome);
 9616:     } 
 9617:     return ($result,$resulttype);
 9618: }
 9619: 
 9620: sub auto_photochoice {
 9621:     my ($cnum,$cdom) = @_;
 9622:     my $homeserver = &homeserver($cnum,$cdom);
 9623:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9624: 						       &escape($cdom),
 9625: 						       $homeserver)));
 9626:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9627: 	return (undef,undef);
 9628:     }
 9629:     return ($update,$comment);
 9630: }
 9631: 
 9632: sub auto_photoupdate {
 9633:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9634:     my $homeserver = &homeserver($cnum,$dom);
 9635:     my $host=&hostname($homeserver);
 9636:     my $cmd = '';
 9637:     my $maxtries = 1;
 9638:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9639:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9640:     }
 9641:     $cmd =~ s/%%$//;
 9642:     $cmd = &escape($cmd);
 9643:     my $query = 'institutionalphotos';
 9644:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9645:     unless ($queryid=~/^\Q$host\E\_/) {
 9646:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9647:         return 'error: '.$queryid;
 9648:     }
 9649:     my $reply = &get_query_reply($queryid);
 9650:     my $tries = 1;
 9651:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9652:         $reply = &get_query_reply($queryid);
 9653:         $tries ++;
 9654:     }
 9655:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9656:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9657:     } else {
 9658:         my @responses = split(/:/,$reply);
 9659:         my $outcome = shift(@responses); 
 9660:         foreach my $item (@responses) {
 9661:             my ($key,$value) = split(/=/,$item);
 9662:             $$photo{$key} = $value;
 9663:         }
 9664:         return $outcome;
 9665:     }
 9666:     return 'error';
 9667: }
 9668: 
 9669: sub auto_instcode_format {
 9670:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9671: 	$cat_order) = @_;
 9672:     my $courses = '';
 9673:     my @homeservers;
 9674:     if ($caller eq 'global') {
 9675: 	my %servers = &get_servers($codedom,'library');
 9676: 	foreach my $tryserver (keys(%servers)) {
 9677: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9678: 		push(@homeservers,$tryserver);
 9679: 	    }
 9680:         }
 9681:     } elsif ($caller eq 'requests') {
 9682:         if ($codedom =~ /^$match_domain$/) {
 9683:             my $chome = &domain($codedom,'primary');
 9684:             unless ($chome eq 'no_host') {
 9685:                 push(@homeservers,$chome);
 9686:             }
 9687:         }
 9688:     } else {
 9689:         push(@homeservers,&homeserver($caller,$codedom));
 9690:     }
 9691:     foreach my $code (keys(%{$instcodes})) {
 9692:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9693:     }
 9694:     chop($courses);
 9695:     my $ok_response = 0;
 9696:     my $response;
 9697:     while (@homeservers > 0 && $ok_response == 0) {
 9698:         my $server = shift(@homeservers); 
 9699:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9700:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9701:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9702: 		split(/:/,$response);
 9703:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9704:             push(@{$codetitles},&str2array($codetitles_str));
 9705:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9706:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9707:             $ok_response = 1;
 9708:         }
 9709:     }
 9710:     if ($ok_response) {
 9711:         return 'ok';
 9712:     } else {
 9713:         return $response;
 9714:     }
 9715: }
 9716: 
 9717: sub auto_instcode_defaults {
 9718:     my ($domain,$returnhash,$code_order) = @_;
 9719:     my @homeservers;
 9720: 
 9721:     my %servers = &get_servers($domain,'library');
 9722:     foreach my $tryserver (keys(%servers)) {
 9723: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9724: 	    push(@homeservers,$tryserver);
 9725: 	}
 9726:     }
 9727: 
 9728:     my $response;
 9729:     foreach my $server (@homeservers) {
 9730:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9731:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9732: 	
 9733: 	foreach my $pair (split(/\&/,$response)) {
 9734: 	    my ($name,$value)=split(/\=/,$pair);
 9735: 	    if ($name eq 'code_order') {
 9736: 		@{$code_order} = split(/\&/,&unescape($value));
 9737: 	    } else {
 9738: 		$returnhash->{&unescape($name)}=&unescape($value);
 9739: 	    }
 9740: 	}
 9741: 	return 'ok';
 9742:     }
 9743: 
 9744:     return $response;
 9745: }
 9746: 
 9747: sub auto_possible_instcodes {
 9748:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9749:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9750:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9751:         return;
 9752:     }
 9753:     my (@homeservers,$uhome);
 9754:     if (defined(&domain($domain,'primary'))) {
 9755:         $uhome=&domain($domain,'primary');
 9756:         push(@homeservers,&domain($domain,'primary'));
 9757:     } else {
 9758:         my %servers = &get_servers($domain,'library');
 9759:         foreach my $tryserver (keys(%servers)) {
 9760:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9761:                 push(@homeservers,$tryserver);
 9762:             }
 9763:         }
 9764:     }
 9765:     my $response;
 9766:     foreach my $server (@homeservers) {
 9767:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9768:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9769:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9770:             split(':',$response);
 9771:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9772:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9773:         foreach my $item (split('&',$cat_title)) {   
 9774:             my ($name,$value)=split('=',$item);
 9775:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9776:         }
 9777:         foreach my $item (split('&',$cat_order)) {
 9778:             my ($name,$value)=split('=',$item);
 9779:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9780:         }
 9781:         return 'ok';
 9782:     }
 9783:     return $response;
 9784: }
 9785: 
 9786: sub auto_courserequest_checks {
 9787:     my ($dom) = @_;
 9788:     my ($homeserver,%validations);
 9789:     if ($dom =~ /^$match_domain$/) {
 9790:         $homeserver = &domain($dom,'primary');
 9791:     }
 9792:     unless ($homeserver eq 'no_host') {
 9793:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9794:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9795:             my @items = split(/&/,$response);
 9796:             foreach my $item (@items) {
 9797:                 my ($key,$value) = split('=',$item);
 9798:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9799:             }
 9800:         }
 9801:     }
 9802:     return %validations; 
 9803: }
 9804: 
 9805: sub auto_courserequest_validation {
 9806:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9807:     my ($homeserver,$response);
 9808:     if ($dom =~ /^$match_domain$/) {
 9809:         $homeserver = &domain($dom,'primary');
 9810:     }
 9811:     unless ($homeserver eq 'no_host') {
 9812:         my $customdata;
 9813:         if (ref($custominfo) eq 'HASH') {
 9814:             $customdata = &freeze_escape($custominfo);
 9815:         }
 9816:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9817:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9818:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9819:                                     $customdata,$homeserver));
 9820:     }
 9821:     return $response;
 9822: }
 9823: 
 9824: sub auto_validate_class_sec {
 9825:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9826:     my $homeserver = &homeserver($cnum,$cdom);
 9827:     my $ownerlist;
 9828:     if (ref($owners) eq 'ARRAY') {
 9829:         $ownerlist = join(',',@{$owners});
 9830:     } else {
 9831:         $ownerlist = $owners;
 9832:     }
 9833:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9834:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9835:     return $response;
 9836: }
 9837: 
 9838: sub auto_instsec_reformat {
 9839:     my ($cdom,$action,$instsecref) = @_;
 9840:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
 9841:     my @homeservers;
 9842:     if (defined(&domain($cdom,'primary'))) {
 9843:         push(@homeservers,&domain($cdom,'primary'));
 9844:     } else {
 9845:         my %servers = &get_servers($cdom,'library');
 9846:         foreach my $tryserver (keys(%servers)) {
 9847:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9848:                 push(@homeservers,$tryserver);
 9849:             }
 9850:         }
 9851:     }
 9852:     my $response;
 9853:     my %reformatted = %{$instsecref};
 9854:     foreach my $server (@homeservers) {
 9855:         if (ref($instsecref) eq 'HASH') {
 9856:             my $info = &freeze_escape($instsecref);
 9857:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
 9858:                                 $action.':'.$info,$server);
 9859:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
 9860:             my @items = split(/&/,$response);
 9861:             foreach my $item (@items) {
 9862:                 my ($key,$value) = split(/=/,$item);
 9863:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
 9864:             }
 9865:         }
 9866:     }
 9867:     return %reformatted;
 9868: }
 9869: 
 9870: sub auto_validate_instclasses {
 9871:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9872:     my ($homeserver,%validations);
 9873:     $homeserver = &homeserver($cnum,$cdom);
 9874:     unless ($homeserver eq 'no_host') {
 9875:         my $ownerlist;
 9876:         if (ref($owners) eq 'ARRAY') {
 9877:             $ownerlist = join(',',@{$owners});
 9878:         } else {
 9879:             $ownerlist = $owners;
 9880:         }
 9881:         if (ref($classesref) eq 'HASH') {
 9882:             my $classes = &freeze_escape($classesref);
 9883:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9884:                                 ':'.$cdom.':'.$classes,$homeserver);
 9885:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9886:                 my @items = split(/&/,$response);
 9887:                 foreach my $item (@items) {
 9888:                     my ($key,$value) = split('=',$item);
 9889:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9890:                 }
 9891:             }
 9892:         }
 9893:     }
 9894:     return %validations;
 9895: }
 9896: 
 9897: sub auto_crsreq_update {
 9898:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9899:         $code,$accessstart,$accessend,$inbound) = @_;
 9900:     my ($homeserver,%crsreqresponse);
 9901:     if ($cdom =~ /^$match_domain$/) {
 9902:         $homeserver = &domain($cdom,'primary');
 9903:     }
 9904:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9905:         my $info;
 9906:         if (ref($inbound) eq 'HASH') {
 9907:             $info = &freeze_escape($inbound);
 9908:         }
 9909:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9910:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9911:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9912:                             &escape($title).':'.&escape($code).':'.
 9913:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9914:                             $homeserver);
 9915:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9916:             my @items = split(/&/,$response);
 9917:             foreach my $item (@items) {
 9918:                 my ($key,$value) = split('=',$item);
 9919:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9920:             }
 9921:         }
 9922:     }
 9923:     return \%crsreqresponse;
 9924: }
 9925: 
 9926: sub auto_export_grades {
 9927:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9928:     my ($homeserver,%exportresponse);
 9929:     if ($cdom =~ /^$match_domain$/) {
 9930:         $homeserver = &domain($cdom,'primary');
 9931:     }
 9932:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9933:         my $info;
 9934:         if (ref($inforef) eq 'HASH') {
 9935:             $info = &freeze_escape($inforef);
 9936:         }
 9937:         if (ref($gradesref) eq 'HASH') {
 9938:             my $grades = &freeze_escape($gradesref);
 9939:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9940:                                 $info.':'.$grades,$homeserver);
 9941:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9942:                 my @items = split(/&/,$response);
 9943:                 foreach my $item (@items) {
 9944:                     my ($key,$value) = split('=',$item);
 9945:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9946:                 }
 9947:             }
 9948:         }
 9949:     }
 9950:     return \%exportresponse;
 9951: }
 9952: 
 9953: sub check_instcode_cloning {
 9954:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9955:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9956:         return;
 9957:     }
 9958:     my $canclone;
 9959:     if (@{$code_order} > 0) {
 9960:         my $instcoderegexp ='^';
 9961:         my @clonecodes = split(/\&/,$cloner);
 9962:         foreach my $item (@{$code_order}) {
 9963:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9964:                 foreach my $pair (@clonecodes) {
 9965:                     my ($key,$val) = split(/\=/,$pair,2);
 9966:                     $val = &unescape($val);
 9967:                     if ($key eq $item) {
 9968:                         $instcoderegexp .= '('.$val.')';
 9969:                         last;
 9970:                     }
 9971:                 }
 9972:             } else {
 9973:                 $instcoderegexp .= $codedefaults->{$item};
 9974:             }
 9975:         }
 9976:         $instcoderegexp .= '$';
 9977:         my (@from,@to);
 9978:         eval {
 9979:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9980:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9981:         };
 9982:         if ((@from > 0) && (@to > 0)) {
 9983:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9984:             if (!@diffs) {
 9985:                 $canclone = 1;
 9986:             }
 9987:         }
 9988:     }
 9989:     return $canclone;
 9990: }
 9991: 
 9992: sub default_instcode_cloning {
 9993:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9994:     my (%codedefaults,@code_order,$canclone);
 9995:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9996:         %codedefaults = %{$codedefaultsref};
 9997:         @code_order = @{$codeorderref};
 9998:     } elsif ($clonedom) {
 9999:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10000:     }
10001:     if (($domdefclone) && (@code_order)) {
10002:         my @clonecodes = split(/\+/,$domdefclone);
10003:         my $instcoderegexp ='^';
10004:         foreach my $item (@code_order) {
10005:             if (grep(/^\Q$item\E$/,@clonecodes)) {
10006:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
10007:             } else {
10008:                 $instcoderegexp .= $codedefaults{$item};
10009:             }
10010:         }
10011:         $instcoderegexp .= '$';
10012:         my (@from,@to);
10013:         eval {
10014:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
10015:             (@to) = ($clonetocode =~ /$instcoderegexp/);
10016:         };
10017:         if ((@from > 0) && (@to > 0)) {
10018:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10019:             if (!@diffs) {
10020:                 $canclone = 1;
10021:             }
10022:         }
10023:     }
10024:     return $canclone;
10025: }
10026: 
10027: # ------------------------------------------------------- Course Group routines
10028: 
10029: sub get_coursegroups {
10030:     my ($cdom,$cnum,$group,$namespace) = @_;
10031:     return(&dump($namespace,$cdom,$cnum,$group));
10032: }
10033: 
10034: sub modify_coursegroup {
10035:     my ($cdom,$cnum,$groupsettings) = @_;
10036:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10037: }
10038: 
10039: sub toggle_coursegroup_status {
10040:     my ($cdom,$cnum,$group,$action) = @_;
10041:     my ($from_namespace,$to_namespace);
10042:     if ($action eq 'delete') {
10043:         $from_namespace = 'coursegroups';
10044:         $to_namespace = 'deleted_groups';
10045:     } else {
10046:         $from_namespace = 'deleted_groups';
10047:         $to_namespace = 'coursegroups';
10048:     }
10049:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
10050:     if (my $tmp = &error(%curr_group)) {
10051:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10052:         return ('read error',$tmp);
10053:     } else {
10054:         my %savedsettings = %curr_group; 
10055:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
10056:         my $deloutcome;
10057:         if ($result eq 'ok') {
10058:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
10059:         } else {
10060:             return ('write error',$result);
10061:         }
10062:         if ($deloutcome eq 'ok') {
10063:             return 'ok';
10064:         } else {
10065:             return ('delete error',$deloutcome);
10066:         }
10067:     }
10068: }
10069: 
10070: sub modify_group_roles {
10071:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
10072:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10073:     my $role = 'gr/'.&escape($userprivs);
10074:     my ($uname,$udom) = split(/:/,$user);
10075:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
10076:     if ($result eq 'ok') {
10077:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10078:     }
10079:     return $result;
10080: }
10081: 
10082: sub modify_coursegroup_membership {
10083:     my ($cdom,$cnum,$membership) = @_;
10084:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
10085:     return $result;
10086: }
10087: 
10088: sub get_active_groups {
10089:     my ($udom,$uname,$cdom,$cnum) = @_;
10090:     my $now = time;
10091:     my %groups = ();
10092:     foreach my $key (keys(%env)) {
10093:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
10094:             my ($start,$end) = split(/\./,$env{$key});
10095:             if (($end!=0) && ($end<$now)) { next; }
10096:             if (($start!=0) && ($start>$now)) { next; }
10097:             if ($1 eq $cdom && $2 eq $cnum) {
10098:                 $groups{$3} = $env{$key} ;
10099:             }
10100:         }
10101:     }
10102:     return %groups;
10103: }
10104: 
10105: sub get_group_membership {
10106:     my ($cdom,$cnum,$group) = @_;
10107:     return(&dump('groupmembership',$cdom,$cnum,$group));
10108: }
10109: 
10110: sub get_users_groups {
10111:     my ($udom,$uname,$courseid) = @_;
10112:     my @usersgroups;
10113:     my $cachetime=1800;
10114: 
10115:     my $hashid="$udom:$uname:$courseid";
10116:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10117:     if (defined($cached)) {
10118:         @usersgroups = split(/:/,$grouplist);
10119:     } else {  
10120:         $grouplist = '';
10121:         my $courseurl = &courseid_to_courseurl($courseid);
10122:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
10123:         my $access_end = $env{'course.'.$courseid.
10124:                               '.default_enrollment_end_date'};
10125:         my $now = time;
10126:         foreach my $key (keys(%roleshash)) {
10127:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10128:                 my $group = $1;
10129:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10130:                     my $start = $2;
10131:                     my $end = $1;
10132:                     if ($start == -1) { next; } # deleted from group
10133:                     if (($start!=0) && ($start>$now)) { next; }
10134:                     if (($end!=0) && ($end<$now)) {
10135:                         if ($access_end && $access_end < $now) {
10136:                             if ($access_end - $end < 86400) {
10137:                                 push(@usersgroups,$group);
10138:                             }
10139:                         }
10140:                         next;
10141:                     }
10142:                     push(@usersgroups,$group);
10143:                 }
10144:             }
10145:         }
10146:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10147:         $grouplist = join(':',@usersgroups);
10148:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10149:     }
10150:     return @usersgroups;
10151: }
10152: 
10153: sub devalidate_getgroups_cache {
10154:     my ($udom,$uname,$cdom,$cnum)=@_;
10155:     my $courseid = $cdom.'_'.$cnum;
10156: 
10157:     my $hashid="$udom:$uname:$courseid";
10158:     &devalidate_cache_new('getgroups',$hashid);
10159: }
10160: 
10161: # ------------------------------------------------------------------ Plain Text
10162: 
10163: sub plaintext {
10164:     my ($short,$type,$cid,$forcedefault) = @_;
10165:     if ($short =~ m{^cr/}) {
10166: 	return (split('/',$short))[-1];
10167:     }
10168:     if (!defined($cid)) {
10169:         $cid = $env{'request.course.id'};
10170:     }
10171:     my %rolenames = (
10172:                       Course    => 'std',
10173:                       Community => 'alt1',
10174:                       Placement => 'std',
10175:                     );
10176:     if ($cid ne '') {
10177:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10178:             unless ($forcedefault) {
10179:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10180:                 &Apache::lonlocal::mt_escape(\$roletext);
10181:                 return &Apache::lonlocal::mt($roletext);
10182:             }
10183:         }
10184:     }
10185:     if ((defined($type)) && (defined($rolenames{$type})) &&
10186:         (defined($rolenames{$type})) && 
10187:         (defined($prp{$short}{$rolenames{$type}}))) {
10188:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10189:     } elsif ($cid ne '') {
10190:         my $crstype = $env{'course.'.$cid.'.type'};
10191:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10192:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10193:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10194:         }
10195:     }
10196:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10197: }
10198: 
10199: # ----------------------------------------------------------------- Assign Role
10200: 
10201: sub assignrole {
10202:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10203:         $context)=@_;
10204:     my $mrole;
10205:     if ($role =~ /^cr\//) {
10206:         my $cwosec=$url;
10207:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10208: 	unless (&allowed('ccr',$cwosec)) {
10209:            my $refused = 1;
10210:            if ($context eq 'requestcourses') {
10211:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10212:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10213:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10214:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10215:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10216:                            if ($crsenv{'internal.courseowner'} eq
10217:                                $env{'user.name'}.':'.$env{'user.domain'}) {
10218:                                $refused = '';
10219:                            }
10220:                        }
10221:                    }
10222:                }
10223:            }
10224:            if ($refused) {
10225:                &logthis('Refused custom assignrole: '.
10226:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10227:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10228:                return 'refused';
10229:            }
10230:         }
10231:         $mrole='cr';
10232:     } elsif ($role =~ /^gr\//) {
10233:         my $cwogrp=$url;
10234:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10235:         unless (&allowed('mdg',$cwogrp)) {
10236:             &logthis('Refused group assignrole: '.
10237:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10238:                     $env{'user.name'}.' at '.$env{'user.domain'});
10239:             return 'refused';
10240:         }
10241:         $mrole='gr';
10242:     } else {
10243:         my $cwosec=$url;
10244:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10245:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10246:             my $refused;
10247:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10248:                 if (!(&allowed('c'.$role,$url))) {
10249:                     $refused = 1;
10250:                 }
10251:             } else {
10252:                 $refused = 1;
10253:             }
10254:             if ($refused) {
10255:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10256:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10257:                     my %crsenv;
10258:                     if ($role eq 'cc' || $role eq 'co') {
10259:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10260:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10261:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10262:                                 if ($crsenv{'internal.courseowner'} eq 
10263:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10264:                                     $refused = '';
10265:                                 }
10266:                             }
10267:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10268:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10269:                                 if ($crsenv{'internal.courseowner'} eq 
10270:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10271:                                     $refused = '';
10272:                                 }
10273:                             }
10274:                         }
10275:                     }
10276:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10277:                     if ($role eq 'st') {
10278:                         $refused = '';
10279:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10280:                         $refused = '';
10281:                     }
10282:                 } elsif ($context eq 'requestcourses') {
10283:                     my @possroles = ('st','ta','ep','in','cc','co');
10284:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10285:                         my $wrongcc;
10286:                         if ($cnum =~ /^$match_community$/) {
10287:                             $wrongcc = 1 if ($role eq 'cc');
10288:                         } else {
10289:                             $wrongcc = 1 if ($role eq 'co');
10290:                         }
10291:                         unless ($wrongcc) {
10292:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10293:                             if ($crsenv{'internal.courseowner'} eq 
10294:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10295:                                 $refused = '';
10296:                             }
10297:                         }
10298:                     }
10299:                 } elsif ($context eq 'requestauthor') {
10300:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10301:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10302:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10303:                             $refused = '';
10304:                         } else {
10305:                             my %domdefaults = &get_domain_defaults($udom);
10306:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10307:                                 my $checkbystatus;
10308:                                 if ($env{'user.adv'}) { 
10309:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10310:                                     if ($disposition eq 'automatic') {
10311:                                         $refused = '';
10312:                                     } elsif ($disposition eq '') {
10313:                                         $checkbystatus = 1;
10314:                                     } 
10315:                                 } else {
10316:                                     $checkbystatus = 1;
10317:                                 }
10318:                                 if ($checkbystatus) {
10319:                                     if ($env{'environment.inststatus'}) {
10320:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10321:                                         foreach my $type (@inststatuses) {
10322:                                             if (($type ne '') &&
10323:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10324:                                                 $refused = '';
10325:                                             }
10326:                                         }
10327:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10328:                                         $refused = '';
10329:                                     }
10330:                                 }
10331:                             }
10332:                         }
10333:                     }
10334:                 }
10335:                 if ($refused) {
10336:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10337:                              ' '.$role.' '.$end.' '.$start.' by '.
10338: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10339:                     return 'refused';
10340:                 }
10341:             }
10342:         } elsif ($role eq 'au') {
10343:             if ($url ne '/'.$udom.'/') {
10344:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10345:                          ' to assign author role for '.$uname.':'.$udom.
10346:                          ' in domain: '.$url.' refused (wrong domain).');
10347:                 return 'refused';
10348:             }
10349:         }
10350:         $mrole=$role;
10351:     }
10352:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10353:                 "$udom:$uname:$url".'_'."$mrole=$role";
10354:     if ($end) { $command.='_'.$end; }
10355:     if ($start) {
10356: 	if ($end) { 
10357:            $command.='_'.$start; 
10358:         } else {
10359:            $command.='_0_'.$start;
10360:         }
10361:     }
10362:     my $origstart = $start;
10363:     my $origend = $end;
10364:     my $delflag;
10365: # actually delete
10366:     if ($deleteflag) {
10367: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10368: # modify command to delete the role
10369:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10370:                 "$udom:$uname:$url".'_'."$mrole";
10371: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10372: # set start and finish to negative values for userrolelog
10373:            $start=-1;
10374:            $end=-1;
10375:            $delflag = 1;
10376:         }
10377:     }
10378: # send command
10379:     my $answer=&reply($command,&homeserver($uname,$udom));
10380: # log new user role if status is ok
10381:     if ($answer eq 'ok') {
10382: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10383:         if (($role eq 'cc') || ($role eq 'in') ||
10384:             ($role eq 'ep') || ($role eq 'ad') ||
10385:             ($role eq 'ta') || ($role eq 'st') ||
10386:             ($role=~/^cr/) || ($role eq 'gr') ||
10387:             ($role eq 'co')) {
10388: # for course roles, perform group memberships changes triggered by role change.
10389:             unless ($role =~ /^gr/) {
10390:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10391:                                                  $origstart,$selfenroll,$context);
10392:             }
10393:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10394:                            $selfenroll,$context);
10395:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10396:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10397:                  ($role eq 'da')) {
10398:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10399:                            $context);
10400:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10401:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10402:                              $context); 
10403:         }
10404:         if ($role eq 'cc') {
10405:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10406:         }
10407:     }
10408:     return $answer;
10409: }
10410: 
10411: sub autoupdate_coowners {
10412:     my ($url,$end,$start,$uname,$udom) = @_;
10413:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10414:     if (($cdom ne '') && ($cnum ne '')) {
10415:         my $now = time;
10416:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10417:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10418:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10419:             my $instcode = $coursehash{'internal.coursecode'};
10420:             my $xlists = $coursehash{'internal.crosslistings'};
10421:             if ($instcode ne '') {
10422:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10423:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10424:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10425:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10426:                         unless ($result eq 'valid') {
10427:                             if ($xlists ne '') {
10428:                                 foreach my $xlist (split(',',$xlists)) {
10429:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
10430:                                     $result =
10431:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10432:                                                                       $inst_crosslist,$uname.':'.$udom);
10433:                                     last if ($result eq 'valid');
10434:                                 }
10435:                             }
10436:                         }
10437:                         if ($result eq 'valid') {
10438:                             if ($coursehash{'internal.co-owners'}) {
10439:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10440:                                     push(@newcoowners,$coowner);
10441:                                 }
10442:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10443:                                     push(@newcoowners,$uname.':'.$udom);
10444:                                 }
10445:                                 @newcoowners = sort(@newcoowners);
10446:                             } else {
10447:                                 push(@newcoowners,$uname.':'.$udom);
10448:                             }
10449:                         } elsif ($coursehash{'internal.co-owners'}) {
10450:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10451:                                 unless ($coowner eq $uname.':'.$udom) {
10452:                                     push(@newcoowners,$coowner);
10453:                                 }
10454:                             }
10455:                             unless (@newcoowners > 0) {
10456:                                 $delcoowners = 1;
10457:                                 $coowners = '';
10458:                             }
10459:                         }
10460:                         if (@newcoowners || $delcoowners) {
10461:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10462:                                             $delcoowners,@newcoowners);
10463:                         }
10464:                     }
10465:                 }
10466:             }
10467:         }
10468:     }
10469: }
10470: 
10471: sub store_coowners {
10472:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10473:     my $cid = $cdom.'_'.$cnum;
10474:     my ($coowners,$delresult,$putresult);
10475:     if (@newcoowners) {
10476:         $coowners = join(',',@newcoowners);
10477:         my %coownershash = (
10478:                             'internal.co-owners' => $coowners,
10479:                            );
10480:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10481:         if ($putresult eq 'ok') {
10482:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10483:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10484:             }
10485:         }
10486:     }
10487:     if ($delcoowners) {
10488:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10489:         if ($delresult eq 'ok') {
10490:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10491:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10492:             }
10493:         }
10494:     }
10495:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10496:         my %crsinfo =
10497:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10498:         if (ref($crsinfo{$cid}) eq 'HASH') {
10499:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10500:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10501:         }
10502:     }
10503: }
10504: 
10505: # -------------------------------------------------- Modify user authentication
10506: # Overrides without validation
10507: 
10508: sub modifyuserauth {
10509:     my ($udom,$uname,$umode,$upass)=@_;
10510:     my $uhome=&homeserver($uname,$udom);
10511:     my $allowed;
10512:     if (&allowed('mau',$udom)) {
10513:         $allowed = 1;
10514:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10515:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10516:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10517:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10518:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10519:         if (($cdom ne '') && ($cnum ne '')) {
10520:             my $is_owner = &is_course_owner($cdom,$cnum);
10521:             if ($is_owner) {
10522:                 $allowed = 1;
10523:             }
10524:         }
10525:     }
10526:     unless ($allowed) { return 'refused'; }
10527:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10528:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10529:              ' in domain '.$env{'request.role.domain'});  
10530:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10531: 		     &escape($upass),$uhome);
10532:     my $ip = &get_requestor_ip();
10533:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10534:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10535:          '(Remote '.$ip.'): '.$reply);
10536:     &log($udom,,$uname,$uhome,
10537:         'Authentication changed by '.$env{'user.domain'}.', '.
10538:                                      $env{'user.name'}.', '.$umode.
10539:          '(Remote '.$ip.'): '.$reply);
10540:     unless ($reply eq 'ok') {
10541:         &logthis('Authentication mode error: '.$reply);
10542: 	return 'error: '.$reply;
10543:     }   
10544:     return 'ok';
10545: }
10546: 
10547: # --------------------------------------------------------------- Modify a user
10548: 
10549: sub modifyuser {
10550:     my ($udom,    $uname, $uid,
10551:         $umode,   $upass, $first,
10552:         $middle,  $last,  $gene,
10553:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10554:     $udom= &LONCAPA::clean_domain($udom);
10555:     $uname=&LONCAPA::clean_username($uname);
10556:     my $showcandelete = 'none';
10557:     if (ref($candelete) eq 'ARRAY') {
10558:         if (@{$candelete} > 0) {
10559:             $showcandelete = join(', ',@{$candelete});
10560:         }
10561:     }
10562:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10563:              $umode.', '.$first.', '.$middle.', '.
10564: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10565:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10566:                                      ' desiredhome not specified'). 
10567:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10568:              ' in domain '.$env{'request.role.domain'});
10569:     my $uhome=&homeserver($uname,$udom,'true');
10570:     my $newuser;
10571:     if ($uhome eq 'no_host') {
10572:         $newuser = 1;
10573:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10574:                 ($umode eq 'lti')) {
10575:             return 'error: more information needed to create new user';
10576:         }
10577:     }
10578: # ----------------------------------------------------------------- Create User
10579:     if (($uhome eq 'no_host') && 
10580: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10581:         my $unhome='';
10582:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10583:             $unhome = $desiredhome;
10584: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10585: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10586:         } else { # load balancing routine for determining $unhome
10587:             my $loadm=10000000;
10588: 	    my %servers = &get_servers($udom,'library');
10589: 	    foreach my $tryserver (keys(%servers)) {
10590: 		my $answer=reply('load',$tryserver);
10591: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10592: 		    $loadm=$answer;
10593: 		    $unhome=$tryserver;
10594: 		}
10595: 	    }
10596:         }
10597:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10598: 	    return 'error: unable to find a home server for '.$uname.
10599:                    ' in domain '.$udom;
10600:         }
10601:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10602:                          &escape($upass),$unhome);
10603: 	unless ($reply eq 'ok') {
10604:             return 'error: '.$reply;
10605:         }   
10606:         $uhome=&homeserver($uname,$udom,'true');
10607:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10608: 	    return 'error: unable verify users home machine.';
10609:         }
10610:     }   # End of creation of new user
10611: # ---------------------------------------------------------------------- Add ID
10612:     if ($uid) {
10613:        $uid=~tr/A-Z/a-z/;
10614:        my %uidhash=&idrget($udom,$uname);
10615:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10616:          && (!$forceid)) {
10617: 	  unless ($uid eq $uidhash{$uname}) {
10618: 	      return 'error: user id "'.$uid.'" does not match '.
10619:                   'current user id "'.$uidhash{$uname}.'".';
10620:           }
10621:        } else {
10622: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10623:        }
10624:     }
10625: # -------------------------------------------------------------- Add names, etc
10626:     my @tmp=&get('environment',
10627: 		   ['firstname','middlename','lastname','generation','id',
10628:                     'permanentemail','inststatus'],
10629: 		   $udom,$uname);
10630:     my (%names,%oldnames);
10631:     if ($tmp[0] =~ m/^error:.*/) { 
10632:         %names=(); 
10633:     } else {
10634:         %names = @tmp;
10635:         %oldnames = %names;
10636:     }
10637: #
10638: # If name, email and/or uid are blank (e.g., because an uploaded file
10639: # of users did not contain them), do not overwrite existing values
10640: # unless field is in $candelete array ref.  
10641: #
10642: 
10643:     my @fields = ('firstname','middlename','lastname','generation',
10644:                   'permanentemail','id');
10645:     my %newvalues;
10646:     if (ref($candelete) eq 'ARRAY') {
10647:         foreach my $field (@fields) {
10648:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10649:                 if ($field eq 'firstname') {
10650:                     $names{$field} = $first;
10651:                 } elsif ($field eq 'middlename') {
10652:                     $names{$field} = $middle;
10653:                 } elsif ($field eq 'lastname') {
10654:                     $names{$field} = $last;
10655:                 } elsif ($field eq 'generation') { 
10656:                     $names{$field} = $gene;
10657:                 } elsif ($field eq 'permanentemail') {
10658:                     $names{$field} = $email;
10659:                 } elsif ($field eq 'id') {
10660:                     $names{$field}  = $uid;
10661:                 }
10662:             }
10663:         }
10664:     }
10665:     if ($first)  { $names{'firstname'}  = $first; }
10666:     if (defined($middle)) { $names{'middlename'} = $middle; }
10667:     if ($last)   { $names{'lastname'}   = $last; }
10668:     if (defined($gene))   { $names{'generation'} = $gene; }
10669:     if ($email) {
10670:        $email=~s/[^\w\@\.\-\,]//gs;
10671:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10672:     }
10673:     if ($uid) { $names{'id'}  = $uid; }
10674:     if (defined($inststatus)) {
10675:         $names{'inststatus'} = '';
10676:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10677:         if (ref($usertypes) eq 'HASH') {
10678:             my @okstatuses; 
10679:             foreach my $item (split(/:/,$inststatus)) {
10680:                 if (defined($usertypes->{$item})) {
10681:                     push(@okstatuses,$item);  
10682:                 }
10683:             }
10684:             if (@okstatuses) {
10685:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10686:             }
10687:         }
10688:     }
10689:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10690:                  $umode.', '.$first.', '.$middle.', '.
10691:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10692:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10693:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10694:     } else {
10695:         $logmsg .= ' during self creation';
10696:     }
10697:     my $changed;
10698:     if ($newuser) {
10699:         $changed = 1;
10700:     } else {
10701:         foreach my $field (@fields) {
10702:             if ($names{$field} ne $oldnames{$field}) {
10703:                 $changed = 1;
10704:                 last;
10705:             }
10706:         }
10707:     }
10708:     unless ($changed) {
10709:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10710:         &logthis($logmsg);
10711:         return 'ok';
10712:     }
10713:     my $reply = &put('environment', \%names, $udom,$uname);
10714:     if ($reply ne 'ok') { 
10715:         return 'error: '.$reply;
10716:     }
10717:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10718:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10719:     }
10720:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10721:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10722:     $logmsg = 'Success modifying user '.$logmsg;
10723:     &logthis($logmsg);
10724:     return 'ok';
10725: }
10726: 
10727: # -------------------------------------------------------------- Modify student
10728: 
10729: sub modifystudent {
10730:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10731:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10732:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10733:     if (!$cid) {
10734: 	unless ($cid=$env{'request.course.id'}) {
10735: 	    return 'not_in_class';
10736: 	}
10737:     }
10738: # --------------------------------------------------------------- Make the user
10739:     my $reply=&modifyuser
10740: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10741:          $desiredhome,$email,$inststatus);
10742:     unless ($reply eq 'ok') { return $reply; }
10743:     # This will cause &modify_student_enrollment to get the uid from the
10744:     # student's environment
10745:     $uid = undef if (!$forceid);
10746:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10747:                                         $gene,$usec,$end,$start,$type,$locktype,
10748:                                         $cid,$selfenroll,$context,$credits,$instsec);
10749:     return $reply;
10750: }
10751: 
10752: sub modify_student_enrollment {
10753:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10754:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10755:     my ($cdom,$cnum,$chome);
10756:     if (!$cid) {
10757: 	unless ($cid=$env{'request.course.id'}) {
10758: 	    return 'not_in_class';
10759: 	}
10760: 	$cdom=$env{'course.'.$cid.'.domain'};
10761: 	$cnum=$env{'course.'.$cid.'.num'};
10762:     } else {
10763: 	($cdom,$cnum)=split(/_/,$cid);
10764:     }
10765:     $chome=$env{'course.'.$cid.'.home'};
10766:     if (!$chome) {
10767: 	$chome=&homeserver($cnum,$cdom);
10768:     }
10769:     if (!$chome) { return 'unknown_course'; }
10770:     # Make sure the user exists
10771:     my $uhome=&homeserver($uname,$udom);
10772:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10773: 	return 'error: no such user';
10774:     }
10775:     # Get student data if we were not given enough information
10776:     if (!defined($first)  || $first  eq '' || 
10777:         !defined($last)   || $last   eq '' || 
10778:         !defined($uid)    || $uid    eq '' || 
10779:         !defined($middle) || $middle eq '' || 
10780:         !defined($gene)   || $gene   eq '') {
10781:         # They did not supply us with enough data to enroll the student, so
10782:         # we need to pick up more information.
10783:         my %tmp = &get('environment',
10784:                        ['firstname','middlename','lastname', 'generation','id']
10785:                        ,$udom,$uname);
10786: 
10787:         #foreach my $key (keys(%tmp)) {
10788:         #    &logthis("key $key = ".$tmp{$key});
10789:         #}
10790:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10791:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10792:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10793:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10794:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10795:     }
10796:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10797:     my $user = "$uname:$udom";
10798:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10799:     my $reply=cput('classlist',
10800: 		   {$user => 
10801: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10802: 		   $cdom,$cnum);
10803:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10804:         &devalidate_getsection_cache($udom,$uname,$cid);
10805:     } else { 
10806: 	return 'error: '.$reply;
10807:     }
10808:     # Add student role to user
10809:     my $uurl='/'.$cid;
10810:     $uurl=~s/\_/\//g;
10811:     if ($usec) {
10812: 	$uurl.='/'.$usec;
10813:     }
10814:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10815:                              $selfenroll,$context);
10816:     if ($result ne 'ok') {
10817:         if ($old_entry{$user} ne '') {
10818:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10819:         } else {
10820:             $reply = &del('classlist',[$user],$cdom,$cnum);
10821:         }
10822:     }
10823:     return $result; 
10824: }
10825: 
10826: sub format_name {
10827:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10828:     my $name;
10829:     if ($first ne 'lastname') {
10830: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10831:     } else {
10832: 	if ($lastname=~/\S/) {
10833: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10834: 	    $name=~s/\s+,/,/;
10835: 	} else {
10836: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10837: 	}
10838:     }
10839:     $name=~s/^\s+//;
10840:     $name=~s/\s+$//;
10841:     $name=~s/\s+/ /g;
10842:     return $name;
10843: }
10844: 
10845: # ------------------------------------------------- Write to course preferences
10846: 
10847: sub writecoursepref {
10848:     my ($courseid,%prefs)=@_;
10849:     $courseid=~s/^\///;
10850:     $courseid=~s/\_/\//g;
10851:     my ($cdomain,$cnum)=split(/\//,$courseid);
10852:     my $chome=homeserver($cnum,$cdomain);
10853:     if (($chome eq '') || ($chome eq 'no_host')) { 
10854: 	return 'error: no such course';
10855:     }
10856:     my $cstring='';
10857:     foreach my $pref (keys(%prefs)) {
10858: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10859:     }
10860:     $cstring=~s/\&$//;
10861:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10862: }
10863: 
10864: # ---------------------------------------------------------- Make/modify course
10865: 
10866: sub createcourse {
10867:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10868:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
10869:     $url=&declutter($url);
10870:     my $cid='';
10871:     if ($context eq 'requestcourses') {
10872:         my $can_create = 0;
10873:         my ($ownername,$ownerdom) = split(':',$course_owner);
10874:         if ($udom eq $ownerdom) {
10875:             my $reload;
10876:             if (($callercontext eq 'auto') &&
10877:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
10878:                 $reload = 'reload';
10879:             }
10880:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
10881:                                   $context)) {
10882:                 $can_create = 1;
10883:             }
10884:         } else {
10885:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10886:                                            $category);
10887:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10888:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10889:                 if (@curr > 0) {
10890:                     my @options = qw(approval validate autolimit);
10891:                     my $optregex = join('|',@options);
10892:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10893:                         $can_create = 1;
10894:                     }
10895:                 }
10896:             }
10897:         }
10898:         if ($can_create) {
10899:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10900:                 unless (&allowed('ccc',$udom)) {
10901:                     return 'refused'; 
10902:                 }
10903:             }
10904:         } else {
10905:             return 'refused';
10906:         }
10907:     } elsif (!&allowed('ccc',$udom)) {
10908:         return 'refused';
10909:     }
10910: # --------------------------------------------------------------- Get Unique ID
10911:     my $uname;
10912:     if ($cnum =~ /^$match_courseid$/) {
10913:         my $chome=&homeserver($cnum,$udom,'true');
10914:         if (($chome eq '') || ($chome eq 'no_host')) {
10915:             $uname = $cnum;
10916:         } else {
10917:             $uname = &generate_coursenum($udom,$crstype);
10918:         }
10919:     } else {
10920:         $uname = &generate_coursenum($udom,$crstype);
10921:     }
10922:     return $uname if ($uname =~ /^error/);
10923: # -------------------------------------------------- Check supplied server name
10924:     if (!defined($course_server)) {
10925:         if (defined(&domain($udom,'primary'))) {
10926:             $course_server = &domain($udom,'primary');
10927:         } else {
10928:             $course_server = $env{'user.home'}; 
10929:         }
10930:     }
10931:     my %host_servers =
10932:         &Apache::lonnet::get_servers($udom,'library');
10933:     unless ($host_servers{$course_server}) {
10934:         return 'error: invalid home server for course: '.$course_server;
10935:     }
10936: # ------------------------------------------------------------- Make the course
10937:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10938:                       $course_server);
10939:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10940:     my $uhome=&homeserver($uname,$udom,'true');
10941:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10942: 	return 'error: no such course';
10943:     }
10944: # ----------------------------------------------------------------- Course made
10945: # log existence
10946:     my $now = time;
10947:     my $newcourse = {
10948:                     $udom.'_'.$uname => {
10949:                                      description => $description,
10950:                                      inst_code   => $inst_code,
10951:                                      owner       => $course_owner,
10952:                                      type        => $crstype,
10953:                                      creator     => $env{'user.name'}.':'.
10954:                                                     $env{'user.domain'},
10955:                                      created     => $now,
10956:                                      context     => $context,
10957:                                                 },
10958:                     };
10959:     &courseidput($udom,$newcourse,$uhome,'notime');
10960: # set toplevel url
10961:     my $topurl=$url;
10962:     unless ($nonstandard) {
10963: # ------------------------------------------ For standard courses, make top url
10964:         my $mapurl=&clutter($url);
10965:         if ($mapurl eq '/res/') { $mapurl=''; }
10966:         $env{'form.initmap'}=(<<ENDINITMAP);
10967: <map>
10968: <resource id="1" type="start"></resource>
10969: <resource id="2" src="$mapurl"></resource>
10970: <resource id="3" type="finish"></resource>
10971: <link index="1" from="1" to="2"></link>
10972: <link index="2" from="2" to="3"></link>
10973: </map>
10974: ENDINITMAP
10975:         $topurl=&declutter(
10976:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10977:                           );
10978:     }
10979: # ----------------------------------------------------------- Write preferences
10980:     &writecoursepref($udom.'_'.$uname,
10981:                      ('description'              => $description,
10982:                       'url'                      => $topurl,
10983:                       'internal.creator'         => $env{'user.name'}.':'.
10984:                                                     $env{'user.domain'},
10985:                       'internal.created'         => $now,
10986:                       'internal.creationcontext' => $context)
10987:                     );
10988:     return '/'.$udom.'/'.$uname;
10989: }
10990: 
10991: # ------------------------------------------------------------------- Create ID
10992: sub generate_coursenum {
10993:     my ($udom,$crstype) = @_;
10994:     my $domdesc = &domain($udom);
10995:     return 'error: invalid domain' if ($domdesc eq '');
10996:     my $first;
10997:     if ($crstype eq 'Community') {
10998:         $first = '0';
10999:     } else {
11000:         $first = int(1+rand(9)); 
11001:     } 
11002:     my $uname=$first.
11003:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11004:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
11005:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11006: # ----------------------------------------------- Make sure that does not exist
11007:     my $uhome=&homeserver($uname,$udom,'true');
11008:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
11009:         if ($crstype eq 'Community') {
11010:             $first = '0';
11011:         } else {
11012:             $first = int(1+rand(9));
11013:         }
11014:         $uname=$first.
11015:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11016:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
11017:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11018:         $uhome=&homeserver($uname,$udom,'true');
11019:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
11020:             return 'error: unable to generate unique course-ID';
11021:         }
11022:     }
11023:     return $uname;
11024: }
11025: 
11026: sub is_course {
11027:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
11028:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
11029: 
11030:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11031:     my $uhome=&homeserver($cnum,$cdom);
11032:     my $iscourse;
11033:     if (grep { $_ eq $uhome } current_machine_ids()) {
11034:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11035:     } else {
11036:         my $hashid = $cdom.':'.$cnum;
11037:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11038:         unless (defined($cached)) {
11039:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11040:                                         $cnum,undef,undef,'.');
11041:             $iscourse = 0;
11042:             if (exists($courses{$cdom.'_'.$cnum})) {
11043:                 $iscourse = 1;
11044:             }
11045:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
11046:         }
11047:     }
11048:     return unless ($iscourse);
11049:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
11050: }
11051: 
11052: sub store_userdata {
11053:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
11054:     my $result;
11055:     if ($datakey ne '') {
11056:         if (ref($storehash) eq 'HASH') {
11057:             if ($udom eq '' || $uname eq '') {
11058:                 $udom = $env{'user.domain'};
11059:                 $uname = $env{'user.name'};
11060:             }
11061:             my $uhome=&homeserver($uname,$udom);
11062:             if (($uhome eq '') || ($uhome eq 'no_host')) {
11063:                 $result = 'error: no_host';
11064:             } else {
11065:                 $storehash->{'ip'} = &get_requestor_ip();
11066:                 $storehash->{'host'} = $perlvar{'lonHostID'};
11067: 
11068:                 my $namevalue='';
11069:                 foreach my $key (keys(%{$storehash})) {
11070:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11071:                 }
11072:                 $namevalue=~s/\&$//;
11073:                 unless ($namespace eq 'courserequests') {
11074:                     $datakey = &escape($datakey);
11075:                 }
11076:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
11077:                                   $namevalue,$uhome);
11078:             }
11079:         } else {
11080:             $result = 'error: data to store was not a hash reference'; 
11081:         }
11082:     } else {
11083:         $result= 'error: invalid requestkey'; 
11084:     }
11085:     return $result;
11086: }
11087: 
11088: # ---------------------------------------------------------- Assign Custom Role
11089: 
11090: sub assigncustomrole {
11091:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
11092:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
11093:                        $end,$start,$deleteflag,$selfenroll,$context);
11094: }
11095: 
11096: # ----------------------------------------------------------------- Revoke Role
11097: 
11098: sub revokerole {
11099:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
11100:     my $now=time;
11101:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
11102: }
11103: 
11104: # ---------------------------------------------------------- Revoke Custom Role
11105: 
11106: sub revokecustomrole {
11107:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
11108:     my $now=time;
11109:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
11110:            $deleteflag,$selfenroll,$context);
11111: }
11112: 
11113: # ------------------------------------------------------------ Disk usage
11114: sub diskusage {
11115:     my ($udom,$uname,$directorypath,$getpropath)=@_;
11116:     $directorypath =~ s/\/$//;
11117:     my $listing=&reply('du2:'.&escape($directorypath).':'
11118:                        .&escape($getpropath).':'.&escape($uname).':'
11119:                        .&escape($udom),homeserver($uname,$udom));
11120:     if ($listing eq 'unknown_cmd') {
11121:         if ($getpropath) {
11122:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
11123:         }
11124:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11125:     }
11126:     return $listing;
11127: }
11128: 
11129: sub is_locked {
11130:     my ($file_name, $domain, $user, $which) = @_;
11131:     my @check;
11132:     my $is_locked;
11133:     push (@check,$file_name);
11134:     my %locked = &get('file_permissions',\@check,
11135: 		      $env{'user.domain'},$env{'user.name'});
11136:     my ($tmp)=keys(%locked);
11137:     if ($tmp=~/^error:/) { undef(%locked); }
11138:     
11139:     if (ref($locked{$file_name}) eq 'ARRAY') {
11140:         $is_locked = 'false';
11141:         foreach my $entry (@{$locked{$file_name}}) {
11142:            if (ref($entry) eq 'ARRAY') {
11143:                $is_locked = 'true';
11144:                if (ref($which) eq 'ARRAY') {
11145:                    push(@{$which},$entry);
11146:                } else {
11147:                    last;
11148:                }
11149:            }
11150:        }
11151:     } else {
11152:         $is_locked = 'false';
11153:     }
11154:     return $is_locked;
11155: }
11156: 
11157: sub declutter_portfile {
11158:     my ($file) = @_;
11159:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11160:     return $file;
11161: }
11162: 
11163: # ------------------------------------------------------------- Mark as Read Only
11164: 
11165: sub mark_as_readonly {
11166:     my ($domain,$user,$files,$what) = @_;
11167:     my %current_permissions = &dump('file_permissions',$domain,$user);
11168:     my ($tmp)=keys(%current_permissions);
11169:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11170:     foreach my $file (@{$files}) {
11171: 	$file = &declutter_portfile($file);
11172:         push(@{$current_permissions{$file}},$what);
11173:     }
11174:     &put('file_permissions',\%current_permissions,$domain,$user);
11175:     return;
11176: }
11177: 
11178: # ------------------------------------------------------------Save Selected Files
11179: 
11180: sub save_selected_files {
11181:     my ($user, $path, @files) = @_;
11182:     my $filename = $user."savedfiles";
11183:     my @other_files = &files_not_in_path($user, $path);
11184:     open (OUT,'>',LONCAPA::tempdir().$filename);
11185:     foreach my $file (@files) {
11186:         print (OUT $env{'form.currentpath'}.$file."\n");
11187:     }
11188:     foreach my $file (@other_files) {
11189:         print (OUT $file."\n");
11190:     }
11191:     close (OUT);
11192:     return 'ok';
11193: }
11194: 
11195: sub clear_selected_files {
11196:     my ($user) = @_;
11197:     my $filename = $user."savedfiles";
11198:     open (OUT,'>',LONCAPA::tempdir().$filename);
11199:     print (OUT undef);
11200:     close (OUT);
11201:     return ("ok");    
11202: }
11203: 
11204: sub files_in_path {
11205:     my ($user, $path) = @_;
11206:     my $filename = $user."savedfiles";
11207:     my %return_files;
11208:     open (IN,'<',LONCAPA::tempdir().$filename);
11209:     while (my $line_in = <IN>) {
11210:         chomp ($line_in);
11211:         my @paths_and_file = split (m!/!, $line_in);
11212:         my $file_part = pop (@paths_and_file);
11213:         my $path_part = join ('/', @paths_and_file);
11214:         $path_part.='/';
11215:         my $path_and_file = $path_part.$file_part;
11216:         if ($path_part eq $path) {
11217:             $return_files{$file_part}= 'selected';
11218:         }
11219:     }
11220:     close (IN);
11221:     return (\%return_files);
11222: }
11223: 
11224: # called in portfolio select mode, to show files selected NOT in current directory
11225: sub files_not_in_path {
11226:     my ($user, $path) = @_;
11227:     my $filename = $user."savedfiles";
11228:     my @return_files;
11229:     my $path_part;
11230:     open(IN, '<',LONCAPA::tempdir().$filename);
11231:     while (my $line = <IN>) {
11232:         #ok, I know it's clunky, but I want it to work
11233:         my @paths_and_file = split(m|/|, $line);
11234:         my $file_part = pop(@paths_and_file);
11235:         chomp($file_part);
11236:         my $path_part = join('/', @paths_and_file);
11237:         $path_part .= '/';
11238:         my $path_and_file = $path_part.$file_part;
11239:         if ($path_part ne $path) {
11240:             push(@return_files, ($path_and_file));
11241:         }
11242:     }
11243:     close(OUT);
11244:     return (@return_files);
11245: }
11246: 
11247: #------------------------------Submitted/Handedback Portfolio Files Versioning
11248:  
11249: sub portfiles_versioning {
11250:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11251:     my $portfolio_root = '/userfiles/portfolio';
11252:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11253:     foreach my $file (@{$portfiles}) {
11254:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11255:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11256:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11257:         my $getpropath = 1;
11258:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11259:                                              $stu_name,$getpropath);
11260:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11261:         my $new_answer = 
11262:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11263:         if ($new_answer ne 'problem getting file') {
11264:             push(@{$versioned_portfiles}, $directory.$new_answer);
11265:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11266:                               [$symb,$env{'request.course.id'},'graded']);
11267:         }
11268:     }
11269: }
11270: 
11271: sub get_next_version {
11272:     my ($answer_name, $answer_ext, $dir_list) = @_;
11273:     my $version;
11274:     if (ref($dir_list) eq 'ARRAY') {
11275:         foreach my $row (@{$dir_list}) {
11276:             my ($file) = split(/\&/,$row,2);
11277:             my ($file_name,$file_version,$file_ext) =
11278:                 &file_name_version_ext($file);
11279:             if (($file_name eq $answer_name) &&
11280:                 ($file_ext eq $answer_ext)) {
11281:                      # gets here if filename and extension match,
11282:                      # regardless of version
11283:                 if ($file_version ne '') {
11284:                     # a versioned file is found  so save it for later
11285:                     if ($file_version > $version) {
11286:                         $version = $file_version;
11287:                     }
11288:                 }
11289:             }
11290:         }
11291:     }
11292:     $version ++;
11293:     return($version);
11294: }
11295: 
11296: sub version_selected_portfile {
11297:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11298:     my ($answer_name,$answer_ver,$answer_ext) =
11299:         &file_name_version_ext($file_name);
11300:     my $new_answer;
11301:     $env{'form.copy'} =
11302:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11303:     if($env{'form.copy'} eq '-1') {
11304:         $new_answer = 'problem getting file';
11305:     } else {
11306:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11307:         my $copy_result = 
11308:             &finishuserfileupload($stu_name,$domain,'copy',
11309:                                   '/portfolio'.$directory.$new_answer);
11310:     }
11311:     undef($env{'form.copy'});
11312:     return ($new_answer);
11313: }
11314: 
11315: sub file_name_version_ext {
11316:     my ($file)=@_;
11317:     my @file_parts = split(/\./, $file);
11318:     my ($name,$version,$ext);
11319:     if (@file_parts > 1) {
11320:         $ext=pop(@file_parts);
11321:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11322:             $version=pop(@file_parts);
11323:         }
11324:         $name=join('.',@file_parts);
11325:     } else {
11326:         $name=join('.',@file_parts);
11327:     }
11328:     return($name,$version,$ext);
11329: }
11330: 
11331: #----------------------------------------------Get portfolio file permissions
11332: 
11333: sub get_portfile_permissions {
11334:     my ($domain,$user) = @_;
11335:     my %current_permissions = &dump('file_permissions',$domain,$user);
11336:     my ($tmp)=keys(%current_permissions);
11337:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11338:     return \%current_permissions;
11339: }
11340: 
11341: #---------------------------------------------Get portfolio file access controls
11342: 
11343: sub get_access_controls {
11344:     my ($current_permissions,$group,$file) = @_;
11345:     my %access;
11346:     my $real_file = $file;
11347:     $file =~ s/\.meta$//;
11348:     if (defined($file)) {
11349:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11350:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11351:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11352:             }
11353:         }
11354:     } else {
11355:         foreach my $key (keys(%{$current_permissions})) {
11356:             if ($key =~ /\0accesscontrol$/) {
11357:                 if (defined($group)) {
11358:                     if ($key !~ m-^\Q$group\E/-) {
11359:                         next;
11360:                     }
11361:                 }
11362:                 my ($fullpath) = split(/\0/,$key);
11363:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11364:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11365:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11366:                     }
11367:                 }
11368:             }
11369:         }
11370:     }
11371:     return %access;
11372: }
11373: 
11374: sub modify_access_controls {
11375:     my ($file_name,$changes,$domain,$user)=@_;
11376:     my ($outcome,$deloutcome);
11377:     my %store_permissions;
11378:     my %new_values;
11379:     my %new_control;
11380:     my %translation;
11381:     my @deletions = ();
11382:     my $now = time;
11383:     if (exists($$changes{'activate'})) {
11384:         if (ref($$changes{'activate'}) eq 'HASH') {
11385:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11386:             my $numnew = scalar(@newitems);
11387:             for (my $i=0; $i<$numnew; $i++) {
11388:                 my $newkey = $newitems[$i];
11389:                 my $newid = &Apache::loncommon::get_cgi_id();
11390:                 if ($newkey =~ /^\d+:/) { 
11391:                     $newkey =~ s/^(\d+)/$newid/;
11392:                     $translation{$1} = $newid;
11393:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11394:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11395:                     $translation{$1} = $newid;
11396:                 }
11397:                 $new_values{$file_name."\0".$newkey} = 
11398:                                           $$changes{'activate'}{$newitems[$i]};
11399:                 $new_control{$newkey} = $now;
11400:             }
11401:         }
11402:     }
11403:     my %todelete;
11404:     my %changed_items;
11405:     foreach my $action ('delete','update') {
11406:         if (exists($$changes{$action})) {
11407:             if (ref($$changes{$action}) eq 'HASH') {
11408:                 foreach my $key (keys(%{$$changes{$action}})) {
11409:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11410:                     if ($action eq 'delete') { 
11411:                         $todelete{$itemnum} = 1;
11412:                     } else {
11413:                         $changed_items{$itemnum} = $key;
11414:                     }
11415:                 }
11416:             }
11417:         }
11418:     }
11419:     # get lock on access controls for file.
11420:     my $lockhash = {
11421:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11422:                                                        ':'.$env{'user.domain'},
11423:                    }; 
11424:     my $tries = 0;
11425:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11426:    
11427:     while (($gotlock ne 'ok') && $tries < 10) {
11428:         $tries ++;
11429:         sleep(0.1);
11430:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11431:     }
11432:     if ($gotlock eq 'ok') {
11433:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11434:         my ($tmp)=keys(%curr_permissions);
11435:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11436:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11437:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11438:             if (ref($curr_controls) eq 'HASH') {
11439:                 foreach my $control_item (keys(%{$curr_controls})) {
11440:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11441:                     if (defined($todelete{$itemnum})) {
11442:                         push(@deletions,$file_name."\0".$control_item);
11443:                     } else {
11444:                         if (defined($changed_items{$itemnum})) {
11445:                             $new_control{$changed_items{$itemnum}} = $now;
11446:                             push(@deletions,$file_name."\0".$control_item);
11447:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11448:                         } else {
11449:                             $new_control{$control_item} = $$curr_controls{$control_item};
11450:                         }
11451:                     }
11452:                 }
11453:             }
11454:         }
11455:         my ($group);
11456:         if (&is_course($domain,$user)) {
11457:             ($group,my $file) = split(/\//,$file_name,2);
11458:         }
11459:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11460:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11461:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11462:         #  remove lock
11463:         my @del_lock = ($file_name."\0".'locked_access_records');
11464:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11465:         my $sqlresult =
11466:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11467:                                     $group);
11468:     } else {
11469:         $outcome = "error: could not obtain lockfile\n";  
11470:     }
11471:     return ($outcome,$deloutcome,\%new_values,\%translation);
11472: }
11473: 
11474: sub make_public_indefinitely {
11475:     my (@requrl) = @_;
11476:     return &automated_portfile_access('public',\@requrl);
11477: }
11478: 
11479: sub automated_portfile_access {
11480:     my ($accesstype,$addsref,$delsref,$info) = @_;
11481:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11482:         return 'invalid';
11483:     }
11484:     my %urls;
11485:     if (ref($addsref) eq 'ARRAY') {
11486:         foreach my $requrl (@{$addsref}) {
11487:             if (&is_portfolio_url($requrl)) {
11488:                 unless (exists($urls{$requrl})) {
11489:                     $urls{$requrl} = 'add';
11490:                 }
11491:             }
11492:         }
11493:     }
11494:     if (ref($delsref) eq 'ARRAY') {
11495:         foreach my $requrl (@{$delsref}) { 
11496:             if (&is_portfolio_url($requrl)) {
11497:                 unless (exists($urls{$requrl})) {
11498:                     $urls{$requrl} = 'delete'; 
11499:                 }
11500:             }
11501:         }
11502:     }
11503:     unless (keys(%urls)) {
11504:         return 'invalid';
11505:     }
11506:     my $ip;
11507:     if ($accesstype eq 'ip') {
11508:         if (ref($info) eq 'HASH') {
11509:             if ($info->{'ip'} ne '') {
11510:                 $ip = $info->{'ip'};
11511:             }
11512:         }
11513:         if ($ip eq '') {
11514:             return 'invalid';
11515:         }
11516:     }
11517:     my $errors;
11518:     my $now = time;
11519:     my %current_perms;
11520:     foreach my $requrl (sort(keys(%urls))) {
11521:         my $action;
11522:         if ($urls{$requrl} eq 'add') {
11523:             $action = 'activate';
11524:         } else {
11525:             $action = 'none';
11526:         }
11527:         my $aclnum = 0;
11528:         my (undef,$udom,$unum,$file_name,$group) =
11529:             &parse_portfolio_url($requrl);
11530:         unless (exists($current_perms{$unum.':'.$udom})) {
11531:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11532:         }
11533:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11534:                                                    $group,$file_name);
11535:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11536:             my ($num,$scope,$end,$start) = 
11537:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11538:             if ($scope eq $accesstype) {
11539:                 if (($start <= $now) && ($end == 0)) {
11540:                     if ($accesstype eq 'ip') {
11541:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11542:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11543:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11544:                                     if ($urls{$requrl} eq 'add') {
11545:                                         $action = 'none';
11546:                                         last;
11547:                                     } else {
11548:                                         $action = 'delete';
11549:                                         $aclnum = $num;
11550:                                         last;
11551:                                     }
11552:                                 }
11553:                             }
11554:                         }
11555:                     } elsif ($accesstype eq 'public') {
11556:                         if ($urls{$requrl} eq 'add') {
11557:                             $action = 'none';
11558:                             last;
11559:                         } else {
11560:                             $action = 'delete';
11561:                             $aclnum = $num;
11562:                             last;
11563:                         }
11564:                     }
11565:                 } elsif ($accesstype eq 'public') {
11566:                     $action = 'update';
11567:                     $aclnum = $num;
11568:                     last;
11569:                 }
11570:             }
11571:         }
11572:         if ($action eq 'none') {
11573:             next;
11574:         } else {
11575:             my %changes;
11576:             my $newend = 0;
11577:             my $newstart = $now;
11578:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11579:             $changes{$action}{$newkey} = {
11580:                 type => $accesstype,
11581:                 time => {
11582:                     start => $newstart,
11583:                     end   => $newend,
11584:                 },
11585:             };
11586:             if ($accesstype eq 'ip') {
11587:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11588:             }
11589:             my ($outcome,$deloutcome,$new_values,$translation) =
11590:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11591:             unless ($outcome eq 'ok') {
11592:                 $errors .= $outcome.' ';
11593:             }
11594:         }
11595:     }
11596:     if ($errors) {
11597:         $errors =~ s/\s$//;
11598:         return $errors;
11599:     } else {
11600:         return 'ok';
11601:     }
11602: }
11603: 
11604: #------------------------------------------------------Get Marked as Read Only
11605: 
11606: sub get_marked_as_readonly {
11607:     my ($domain,$user,$what,$group) = @_;
11608:     my $current_permissions = &get_portfile_permissions($domain,$user);
11609:     my @readonly_files;
11610:     my $cmp1=$what;
11611:     if (ref($what)) { $cmp1=join('',@{$what}) };
11612:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11613:         if (defined($group)) {
11614:             if ($file_name !~ m-^\Q$group\E/-) {
11615:                 next;
11616:             }
11617:         }
11618:         if (ref($value) eq "ARRAY"){
11619:             foreach my $stored_what (@{$value}) {
11620:                 my $cmp2=$stored_what;
11621:                 if (ref($stored_what) eq 'ARRAY') {
11622:                     $cmp2=join('',@{$stored_what});
11623:                 }
11624:                 if ($cmp1 eq $cmp2) {
11625:                     push(@readonly_files, $file_name);
11626:                     last;
11627:                 } elsif (!defined($what)) {
11628:                     push(@readonly_files, $file_name);
11629:                     last;
11630:                 }
11631:             }
11632:         }
11633:     }
11634:     return @readonly_files;
11635: }
11636: #-----------------------------------------------------------Get Marked as Read Only Hash
11637: 
11638: sub get_marked_as_readonly_hash {
11639:     my ($current_permissions,$group,$what) = @_;
11640:     my %readonly_files;
11641:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11642:         if (defined($group)) {
11643:             if ($file_name !~ m-^\Q$group\E/-) {
11644:                 next;
11645:             }
11646:         }
11647:         if (ref($value) eq "ARRAY"){
11648:             foreach my $stored_what (@{$value}) {
11649:                 if (ref($stored_what) eq 'ARRAY') {
11650:                     foreach my $lock_descriptor(@{$stored_what}) {
11651:                         if ($lock_descriptor eq 'graded') {
11652:                             $readonly_files{$file_name} = 'graded';
11653:                         } elsif ($lock_descriptor eq 'handback') {
11654:                             $readonly_files{$file_name} = 'handback';
11655:                         } else {
11656:                             if (!exists($readonly_files{$file_name})) {
11657:                                 $readonly_files{$file_name} = 'locked';
11658:                             }
11659:                         }
11660:                     }
11661:                 } 
11662:             }
11663:         } 
11664:     }
11665:     return %readonly_files;
11666: }
11667: # ------------------------------------------------------------ Unmark as Read Only
11668: 
11669: sub unmark_as_readonly {
11670:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11671:     # for portfolio submissions, $what contains [$symb,$crsid] 
11672:     my ($domain,$user,$what,$file_name,$group) = @_;
11673:     $file_name = &declutter_portfile($file_name);
11674:     my $symb_crs = $what;
11675:     if (ref($what)) { $symb_crs=join('',@$what); }
11676:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11677:     my ($tmp)=keys(%current_permissions);
11678:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11679:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11680:     foreach my $file (@readonly_files) {
11681: 	my $clean_file = &declutter_portfile($file);
11682: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11683: 	my $current_locks = $current_permissions{$file};
11684:         my @new_locks;
11685:         my @del_keys;
11686:         if (ref($current_locks) eq "ARRAY"){
11687:             foreach my $locker (@{$current_locks}) {
11688:                 my $compare=$locker;
11689:                 if (ref($locker) eq 'ARRAY') {
11690:                     $compare=join('',@{$locker});
11691:                     if ($compare ne $symb_crs) {
11692:                         push(@new_locks, $locker);
11693:                     }
11694:                 }
11695:             }
11696:             if (scalar(@new_locks) > 0) {
11697:                 $current_permissions{$file} = \@new_locks;
11698:             } else {
11699:                 push(@del_keys, $file);
11700:                 &del('file_permissions',\@del_keys, $domain, $user);
11701:                 delete($current_permissions{$file});
11702:             }
11703:         }
11704:     }
11705:     &put('file_permissions',\%current_permissions,$domain,$user);
11706:     return;
11707: }
11708: 
11709: # ------------------------------------------------------------ Directory lister
11710: 
11711: sub dirlist {
11712:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11713:     $uri=~s/^\///;
11714:     $uri=~s/\/$//;
11715:     my ($udom, $uname);
11716:     if ($getuserdir) {
11717:         $udom = $userdomain;
11718:         $uname = $username;
11719:     } else {
11720:         (undef,$udom,$uname)=split(/\//,$uri);
11721:         if(defined($userdomain)) {
11722:             $udom = $userdomain;
11723:         }
11724:         if(defined($username)) {
11725:             $uname = $username;
11726:         }
11727:     }
11728:     my ($dirRoot,$listing,@listing_results);
11729: 
11730:     $dirRoot = $perlvar{'lonDocRoot'};
11731:     if (defined($getpropath)) {
11732:         $dirRoot = &propath($udom,$uname);
11733:         $dirRoot =~ s/\/$//;
11734:     } elsif (defined($getuserdir)) {
11735:         my $subdir=$uname.'__';
11736:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11737:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11738:                    ."/$udom/$subdir/$uname";
11739:     } elsif (defined($alternateRoot)) {
11740:         $dirRoot = $alternateRoot;
11741:     }
11742: 
11743:     if($udom) {
11744:         if($uname) {
11745:             my $uhome = &homeserver($uname,$udom);
11746:             if ($uhome eq 'no_host') {
11747:                 return ([],'no_host');
11748:             }
11749:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11750:                               .$getuserdir.':'.&escape($dirRoot)
11751:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11752:             if ($listing eq 'unknown_cmd') {
11753:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11754:             } else {
11755:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11756:             }
11757:             if ($listing eq 'unknown_cmd') {
11758:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11759:                 @listing_results = split(/:/,$listing);
11760:             } else {
11761:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11762:             }
11763:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11764:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11765:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11766:                 return ([],$listing);
11767:             } else {
11768:                 return (\@listing_results);
11769:             }
11770:         } elsif(!$alternateRoot) {
11771:             my (%allusers,%listerror);
11772: 	    my %servers = &get_servers($udom,'library');
11773:  	    foreach my $tryserver (keys(%servers)) {
11774:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11775:                                   &escape($udom),$tryserver);
11776:                 if ($listing eq 'unknown_cmd') {
11777: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11778: 				      $udom, $tryserver);
11779:                 } else {
11780:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11781:                 }
11782: 		if ($listing eq 'unknown_cmd') {
11783: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11784: 				      $udom, $tryserver);
11785: 		    @listing_results = split(/:/,$listing);
11786: 		} else {
11787: 		    @listing_results =
11788: 			map { &unescape($_); } split(/:/,$listing);
11789: 		}
11790:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11791:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11792:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11793:                     $listerror{$tryserver} = $listing;
11794:                 } else {
11795: 		    foreach my $line (@listing_results) {
11796: 			my ($entry) = split(/&/,$line,2);
11797: 			$allusers{$entry} = 1;
11798: 		    }
11799: 		}
11800:             }
11801:             my @alluserslist=();
11802:             foreach my $user (sort(keys(%allusers))) {
11803:                 push(@alluserslist,$user.'&user');
11804:             }
11805: 
11806:             if (!%listerror) {
11807:                 # no errors
11808:                 return (\@alluserslist);
11809:             } elsif (scalar(keys(%servers)) == 1) {
11810:                 # one library server, one error 
11811:                 my ($key) = keys(%listerror);
11812:                 return (\@alluserslist, $listerror{$key});
11813:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11814:                 # con_lost indicates that we might miss data from at least one
11815:                 # library server
11816:                 return (\@alluserslist, 'con_lost');
11817:             } else {
11818:                 # multiple library servers and no con_lost -> data should be
11819:                 # complete. 
11820:                 return (\@alluserslist);
11821:             }
11822: 
11823:         } else {
11824:             return ([],'missing username');
11825:         }
11826:     } elsif(!defined($getpropath)) {
11827:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11828:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11829:         return (\@all_domains);
11830:     } else {
11831:         return ([],'missing domain');
11832:     }
11833: }
11834: 
11835: # --------------------------------------------- GetFileTimestamp
11836: # This function utilizes dirlist and returns the date stamp for
11837: # when it was last modified.  It will also return an error of -1
11838: # if an error occurs
11839: 
11840: sub GetFileTimestamp {
11841:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11842:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11843:     $studentName   = &LONCAPA::clean_username($studentName);
11844:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11845:                                     undef,$getuserdir);
11846:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11847:         return -1;
11848:     }
11849:     if (ref($fileref) eq 'ARRAY') {
11850:         my @stats = split('&',$fileref->[0]);
11851:         # @stats contains first the filename, then the stat output
11852:         return $stats[10]; # so this is 10 instead of 9.
11853:     } else {
11854:         return -1;
11855:     }
11856: }
11857: 
11858: sub stat_file {
11859:     my ($uri) = @_;
11860:     $uri = &clutter_with_no_wrapper($uri);
11861: 
11862:     my ($udom,$uname,$file);
11863:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11864: 	($udom,$uname,$file) =
11865: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11866: 	$file = 'userfiles/'.$file;
11867:     }
11868:     if ($uri =~ m-^/res/-) {
11869: 	($udom,$uname) = 
11870: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11871: 	$file = $uri;
11872:     }
11873: 
11874:     if (!$udom || !$uname || !$file) {
11875: 	# unable to handle the uri
11876: 	return ();
11877:     }
11878:     my $getpropath;
11879:     if ($file =~ /^userfiles\//) {
11880:         $getpropath = 1;
11881:     }
11882:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11883:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11884:         return ();
11885:     } else {
11886:         if (ref($listref) eq 'ARRAY') {
11887:             my @stats = split('&',$listref->[0]);
11888: 	    shift(@stats); #filename is first
11889: 	    return @stats;
11890:         }
11891:     }
11892:     return ();
11893: }
11894: 
11895: # --------------------------------------------------------- recursedirs
11896: # Recursive function to traverse either a specific user's Authoring Space
11897: # or corresponding Published Resource Space, and populate the hash ref:
11898: # $dirhashref with URLs of all directories, and if $filehashref hash
11899: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11900: # or .rights files in resource space, and .meta, .save, .log, and .bak
11901: # files in Authoring Space.
11902: #
11903: # Inputs:
11904: #
11905: # $is_home - true if current server is home server for user's space
11906: # $context - either: priv, or res respectively for Authoring or Resource Space.
11907: # $docroot - Document root (i.e., /home/httpd/html
11908: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11909: # $relpath - Current path (relative to top level).
11910: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11911: # $filehashref - reference to hash to populate with URLs of files (Optional)
11912: #
11913: # Returns: nothing
11914: #
11915: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11916: #
11917: # Currently used by interface/londocs.pm to create linked select boxes for
11918: # directory and filename to import a Course "Author" resource into a course, and
11919: # also to create linked select boxes for Authoring Space and Directory to choose
11920: # save location for creation of a new "standard" problem from the Course Editor.
11921: #
11922: 
11923: sub recursedirs {
11924:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11925:     return unless (ref($dirhashref) eq 'HASH');
11926:     my $currpath = $docroot.$toppath;
11927:     if ($relpath) {
11928:         $currpath .= "/$relpath";
11929:     }
11930:     my $savefile;
11931:     if (ref($filehashref)) {
11932:         $savefile = 1;
11933:     }
11934:     if ($is_home) {
11935:         if (opendir(my $dirh,$currpath)) {
11936:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11937:                 next if ($item eq '');
11938:                 if (-d "$currpath/$item") {
11939:                     my $newpath;
11940:                     if ($relpath) {
11941:                         $newpath = "$relpath/$item";
11942:                     } else {
11943:                         $newpath = $item;
11944:                     }
11945:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11946:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11947:                 } elsif ($savefile) {
11948:                     if ($context eq 'priv') {
11949:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11950:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11951:                         }
11952:                     } else {
11953:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11954:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11955:                         }
11956:                     }
11957:                 }
11958:             }
11959:             closedir($dirh);
11960:         }
11961:     } else {
11962:         my ($dirlistref,$listerror) =
11963:             &dirlist($toppath.$relpath);
11964:         my @dir_lines;
11965:         my $dirptr=16384;
11966:         if (ref($dirlistref) eq 'ARRAY') {
11967:             foreach my $dir_line (sort
11968:                               {
11969:                                   my ($afile)=split('&',$a,2);
11970:                                   my ($bfile)=split('&',$b,2);
11971:                                   return (lc($afile) cmp lc($bfile));
11972:                               } (@{$dirlistref})) {
11973:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11974:                     split(/\&/,$dir_line,16);
11975:                 $item =~ s/\s+$//;
11976:                 next if (($item =~ /^\.\.?$/) || ($obs));
11977:                 if ($dirptr&$testdir) {
11978:                     my $newpath;
11979:                     if ($relpath) {
11980:                         $newpath = "$relpath/$item";
11981:                     } else {
11982:                         $relpath = '/';
11983:                         $newpath = $item;
11984:                     }
11985:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11986:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11987:                 } elsif ($savefile) {
11988:                     if ($context eq 'priv') {
11989:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11990:                             $filehashref->{$relpath}{$item} = 1;
11991:                         }
11992:                     } else {
11993:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11994:                             $filehashref->{$relpath}{$item} = 1;
11995:                         }
11996:                     }
11997:                 }
11998:             }
11999:         }
12000:     }
12001:     return;
12002: }
12003: 
12004: # -------------------------------------------------------- Value of a Condition
12005: 
12006: # gets the value of a specific preevaluated condition
12007: #    stored in the string  $env{user.state.<cid>}
12008: # or looks up a condition reference in the bighash and if if hasn't
12009: # already been evaluated recurses into docondval to get the value of
12010: # the condition, then memoizing it to 
12011: #   $env{user.state.<cid>.<condition>}
12012: sub directcondval {
12013:     my $number=shift;
12014:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
12015: 	&Apache::lonuserstate::evalstate();
12016:     }
12017:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12018: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12019:     } elsif ($number =~ /^_/) {
12020: 	my $sub_condition;
12021: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12022: 		&GDBM_READER(),0640)) {
12023: 	    $sub_condition=$bighash{'conditions'.$number};
12024: 	    untie(%bighash);
12025: 	}
12026: 	my $value = &docondval($sub_condition);
12027: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
12028: 	return $value;
12029:     }
12030:     if ($env{'user.state.'.$env{'request.course.id'}}) {
12031:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
12032:     } else {
12033:        return 2;
12034:     }
12035: }
12036: 
12037: # get the collection of conditions for this resource
12038: sub condval {
12039:     my $condidx=shift;
12040:     my $allpathcond='';
12041:     foreach my $cond (split(/\|/,$condidx)) {
12042: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12043: 	    $allpathcond.=
12044: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12045: 	}
12046:     }
12047:     $allpathcond=~s/\|$//;
12048:     return &docondval($allpathcond);
12049: }
12050: 
12051: #evaluates an expression of conditions
12052: sub docondval {
12053:     my ($allpathcond) = @_;
12054:     my $result=0;
12055:     if ($env{'request.course.id'}
12056: 	&& defined($allpathcond)) {
12057: 	my $operand='|';
12058: 	my @stack;
12059: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12060: 	    if ($chunk eq '(') {
12061: 		push @stack,($operand,$result);
12062: 	    } elsif ($chunk eq ')') {
12063: 		my $before=pop @stack;
12064: 		if (pop @stack eq '&') {
12065: 		    $result=$result>$before?$before:$result;
12066: 		} else {
12067: 		    $result=$result>$before?$result:$before;
12068: 		}
12069: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
12070: 		$operand=$chunk;
12071: 	    } else {
12072: 		my $new=directcondval($chunk);
12073: 		if ($operand eq '&') {
12074: 		    $result=$result>$new?$new:$result;
12075: 		} else {
12076: 		    $result=$result>$new?$result:$new;
12077: 		}
12078: 	    }
12079: 	}
12080:     }
12081:     return $result;
12082: }
12083: 
12084: # ---------------------------------------------------- Devalidate courseresdata
12085: 
12086: sub devalidatecourseresdata {
12087:     my ($coursenum,$coursedomain)=@_;
12088:     my $hashid=$coursenum.':'.$coursedomain;
12089:     &devalidate_cache_new('courseres',$hashid);
12090: }
12091: 
12092: 
12093: # --------------------------------------------------- Course Resourcedata Query
12094: #
12095: #  Parameters:
12096: #      $coursenum    - Number of the course.
12097: #      $coursedomain - Domain at which the course was created.
12098: #  Returns:
12099: #     A hash of the course parameters along (I think) with timestamps
12100: #     and version info.
12101: 
12102: sub get_courseresdata {
12103:     my ($coursenum,$coursedomain)=@_;
12104:     my $coursehom=&homeserver($coursenum,$coursedomain);
12105:     my $hashid=$coursenum.':'.$coursedomain;
12106:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
12107:     my %dumpreply;
12108:     unless (defined($cached)) {
12109: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
12110: 	$result=\%dumpreply;
12111: 	my ($tmp) = keys(%dumpreply);
12112: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12113: 	    &do_cache_new('courseres',$hashid,$result,600);
12114: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12115: 	    return $tmp;
12116: 	} elsif ($tmp =~ /^(error)/) {
12117: 	    $result=undef;
12118: 	    &do_cache_new('courseres',$hashid,$result,600);
12119: 	}
12120:     }
12121:     return $result;
12122: }
12123: 
12124: sub devalidateuserresdata {
12125:     my ($uname,$udom)=@_;
12126:     my $hashid="$udom:$uname";
12127:     &devalidate_cache_new('userres',$hashid);
12128: }
12129: 
12130: sub get_userresdata {
12131:     my ($uname,$udom)=@_;
12132:     #most student don\'t have any data set, check if there is some data
12133:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12134: 
12135:     my $hashid="$udom:$uname";
12136:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12137:     if (!defined($cached)) {
12138: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12139: 	$result=\%resourcedata;
12140: 	&do_cache_new('userres',$hashid,$result,600);
12141:     }
12142:     my ($tmp)=keys(%$result);
12143:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12144: 	return $result;
12145:     }
12146:     #error 2 occurs when the .db doesn't exist
12147:     if ($tmp!~/error: 2 /) {
12148:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12149: 	    &logthis("<font color=\"blue\">WARNING:".
12150: 		     " Trying to get resource data for ".
12151: 		     $uname." at ".$udom.": ".
12152: 		     $tmp."</font>");
12153:         }
12154:     } elsif ($tmp=~/error: 2 /) {
12155: 	#&EXT_cache_set($udom,$uname);
12156: 	&do_cache_new('userres',$hashid,undef,600);
12157: 	undef($tmp); # not really an error so don't send it back
12158:     }
12159:     return $tmp;
12160: }
12161: #----------------------------------------------- resdata - return resource data
12162: #  Purpose:
12163: #    Return resource data for either users or for a course.
12164: #  Parameters:
12165: #     $name      - Course/user name.
12166: #     $domain    - Name of the domain the user/course is registered on.
12167: #     $type      - Type of thing $name is (must be 'course' or 'user')
12168: #     $mapp      - decluttered URL of enclosing map  
12169: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12170: #     $recurseup - Ref to array of map URLs, starting with map containing
12171: #                  $mapp up through hierarchy of nested maps to top level map.  
12172: #     $courseid  - CourseID (first part of param identifier).
12173: #     $modifier  - Middle part of param identifier.
12174: #     $what      - Last part of param identifier.
12175: #     @which     - Array of names of resources desired.
12176: #  Returns:
12177: #     The value of the first reasource in @which that is found in the
12178: #     resource hash.
12179: #  Exceptional Conditions:
12180: #     If the $type passed in is not valid (not the string 'course' or 
12181: #     'user', an undefined  reference is returned.
12182: #     If none of the resources are found, an undef is returned
12183: sub resdata {
12184:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12185:         $modifier,$what,@which)=@_;
12186:     my $result;
12187:     if ($type eq 'course') {
12188: 	$result=&get_courseresdata($name,$domain);
12189:     } elsif ($type eq 'user') {
12190: 	$result=&get_userresdata($name,$domain);
12191:     }
12192:     if (!ref($result)) { return $result; }    
12193:     foreach my $item (@which) {
12194:         if ($item->[1] eq 'course') {
12195:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12196:                 unless ($$recursed) {
12197:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12198:                     $$recursed = 1;
12199:                 }
12200:                 foreach my $item (@${recurseup}) {
12201:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12202:                     last if (defined($result->{$norecursechk}));
12203:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12204:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12205:                 }
12206:             }
12207:         }
12208:         if (defined($result->{$item->[0]})) {
12209: 	    return [$result->{$item->[0]},$item->[1]];
12210: 	}
12211:     }
12212:     return undef;
12213: }
12214: 
12215: sub get_domain_lti {
12216:     my ($cdom,$context) = @_;
12217:     my ($name,%lti);
12218:     if ($context eq 'consumer') {
12219:         $name = 'ltitools';
12220:     } elsif ($context eq 'provider') {
12221:         $name = 'lti';
12222:     } else {
12223:         return %lti;
12224:     }
12225:     my ($result,$cached)=&is_cached_new($name,$cdom);
12226:     if (defined($cached)) {
12227:         if (ref($result) eq 'HASH') {
12228:             %lti = %{$result};
12229:         }
12230:     } else {
12231:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12232:         if (ref($domconfig{$name}) eq 'HASH') {
12233:             %lti = %{$domconfig{$name}};
12234:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom,undef,1);
12235:             if (ref($encdomconfig{$name}) eq 'HASH') {
12236:                 foreach my $id (keys(%lti)) {
12237:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
12238:                         foreach my $item ('key','secret') {
12239:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
12240:                         }
12241:                     }
12242:                 }
12243:             }
12244:         }
12245:         my $cachetime = 24*60*60;
12246:         &do_cache_new($name,$cdom,\%lti,$cachetime);
12247:     }
12248:     return %lti;
12249: }
12250: 
12251: sub get_course_lti {
12252:     my ($cnum,$cdom) = @_;
12253:     my $hashid=$cdom.'_'.$cnum;
12254:     my %courselti;
12255:     my ($result,$cached)=&is_cached_new('courselti',$hashid);
12256:     if (defined($cached)) {
12257:         if (ref($result) eq 'HASH') {
12258:             %courselti = %{$result};
12259:         }
12260:     } else {
12261:         %courselti = &dump('lti',$cdom,$cnum,undef,undef,undef,1);
12262:         my $cachetime = 24*60*60;
12263:         &do_cache_new('courselti',$hashid,\%courselti,$cachetime);
12264:     }
12265:     return %courselti;
12266: }
12267: 
12268: sub get_numsuppfiles {
12269:     my ($cnum,$cdom,$ignorecache)=@_;
12270:     my $hashid=$cnum.':'.$cdom;
12271:     my ($suppcount,$cached);
12272:     unless ($ignorecache) {
12273:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
12274:     }
12275:     unless (defined($cached)) {
12276:         my $chome=&homeserver($cnum,$cdom);
12277:         unless ($chome eq 'no_host') {
12278:             ($suppcount,my $supptools,my $errors) = (0,0,0);
12279:             my $suppmap = 'supplemental.sequence';
12280:             ($suppcount,$supptools,$errors) =
12281:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
12282:                                                          $supptools,$errors);
12283:         }
12284:         &do_cache_new('suppcount',$hashid,$suppcount,600);
12285:     }
12286:     return $suppcount;
12287: }
12288: 
12289: #
12290: # EXT resource caching routines
12291: #
12292: 
12293: {
12294: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12295: #
12296: # The course for which we cache
12297: my $cachedmapkey='';
12298: # The cached recursive maps for this course
12299: my %cachedmaps=();
12300: # When this was last done
12301: my $cachedmaptime='';
12302: 
12303: sub clear_EXT_cache_status {
12304:     &delenv('cache.EXT.');
12305: }
12306: 
12307: sub EXT_cache_status {
12308:     my ($target_domain,$target_user) = @_;
12309:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12310:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12311:         # We know already the user has no data
12312:         return 1;
12313:     } else {
12314:         return 0;
12315:     }
12316: }
12317: 
12318: sub EXT_cache_set {
12319:     my ($target_domain,$target_user) = @_;
12320:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12321:     #&appenv({$cachename => time});
12322: }
12323: 
12324: # --------------------------------------------------------- Value of a Variable
12325: sub EXT {
12326: 
12327:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
12328:     unless ($varname) { return ''; }
12329:     #get real user name/domain, courseid and symb
12330:     my $courseid;
12331:     my $publicuser;
12332:     if ($symbparm) {
12333: 	$symbparm=&get_symb_from_alias($symbparm);
12334:     }
12335:     if (!($uname && $udom)) {
12336:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12337:       if (!$symbparm) {	$symbparm=$cursymb; }
12338:     } else {
12339: 	$courseid=$env{'request.course.id'};
12340:     }
12341:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12342:     my $rest;
12343:     if (defined($therest[0])) {
12344:        $rest=join('.',@therest);
12345:     } else {
12346:        $rest='';
12347:     }
12348: 
12349:     my $qualifierrest=$qualifier;
12350:     if ($rest) { $qualifierrest.='.'.$rest; }
12351:     my $spacequalifierrest=$space;
12352:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12353:     if ($realm eq 'user') {
12354: # --------------------------------------------------------------- user.resource
12355: 	if ($space eq 'resource') {
12356: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12357: 		  || defined($Apache::lonhomework::parsing_a_task))
12358: 		 &&
12359: 		 ($symbparm eq &symbread()) ) {
12360: 		# if we are in the middle of processing the resource the
12361: 		# get the value we are planning on committing
12362:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12363:                     return $Apache::lonhomework::results{$qualifierrest};
12364:                 } else {
12365:                     return $Apache::lonhomework::history{$qualifierrest};
12366:                 }
12367: 	    } else {
12368: 		my %restored;
12369: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12370: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12371: 		} else {
12372: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12373: 		}
12374: 		return $restored{$qualifierrest};
12375: 	    }
12376: # ----------------------------------------------------------------- user.access
12377:         } elsif ($space eq 'access') {
12378: 	    # FIXME - not supporting calls for a specific user
12379:             return &allowed($qualifier,$rest);
12380: # ------------------------------------------ user.preferences, user.environment
12381:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12382: 	    if (($uname eq $env{'user.name'}) &&
12383: 		($udom eq $env{'user.domain'})) {
12384: 		return $env{join('.',('environment',$qualifierrest))};
12385: 	    } else {
12386: 		my %returnhash;
12387: 		if (!$publicuser) {
12388: 		    %returnhash=&userenvironment($udom,$uname,
12389: 						 $qualifierrest);
12390: 		}
12391: 		return $returnhash{$qualifierrest};
12392: 	    }
12393: # ----------------------------------------------------------------- user.course
12394:         } elsif ($space eq 'course') {
12395: 	    # FIXME - not supporting calls for a specific user
12396:             return $env{join('.',('request.course',$qualifier))};
12397: # ------------------------------------------------------------------- user.role
12398:         } elsif ($space eq 'role') {
12399: 	    # FIXME - not supporting calls for a specific user
12400:             my ($role,$where)=split(/\./,$env{'request.role'});
12401:             if ($qualifier eq 'value') {
12402: 		return $role;
12403:             } elsif ($qualifier eq 'extent') {
12404:                 return $where;
12405:             }
12406: # ----------------------------------------------------------------- user.domain
12407:         } elsif ($space eq 'domain') {
12408:             return $udom;
12409: # ------------------------------------------------------------------- user.name
12410:         } elsif ($space eq 'name') {
12411:             return $uname;
12412: # ---------------------------------------------------- Any other user namespace
12413:         } else {
12414: 	    my %reply;
12415: 	    if (!$publicuser) {
12416: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12417: 	    }
12418: 	    return $reply{$qualifierrest};
12419:         }
12420:     } elsif ($realm eq 'query') {
12421: # ---------------------------------------------- pull stuff out of query string
12422:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12423: 						[$spacequalifierrest]);
12424: 	return $env{'form.'.$spacequalifierrest}; 
12425:    } elsif ($realm eq 'request') {
12426: # ------------------------------------------------------------- request.browser
12427:         if ($space eq 'browser') {
12428:             return $env{'browser.'.$qualifier};
12429: # ------------------------------------------------------------ request.filename
12430:         } else {
12431:             return $env{'request.'.$spacequalifierrest};
12432:         }
12433:     } elsif ($realm eq 'course') {
12434: # ---------------------------------------------------------- course.description
12435:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12436:     } elsif ($realm eq 'resource') {
12437: 
12438: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12439: 	    if (!$symbparm) { $symbparm=&symbread(); }
12440: 	}
12441: 
12442:         if ($qualifier eq '') {
12443: 	    if ($space eq 'title') {
12444: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12445: 	        return &gettitle($symbparm);
12446: 	    }
12447: 	
12448: 	    if ($space eq 'map') {
12449: 	        my ($map) = &decode_symb($symbparm);
12450: 	        return &symbread($map);
12451: 	    }
12452:             if ($space eq 'maptitle') {
12453:                 my ($map) = &decode_symb($symbparm);
12454:                 return &gettitle($map);
12455:             }
12456: 	    if ($space eq 'filename') {
12457: 	        if ($symbparm) {
12458: 		    return &clutter((&decode_symb($symbparm))[2]);
12459: 	        }
12460: 	        return &hreflocation('',$env{'request.filename'});
12461: 	    }
12462: 
12463:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12464:                 if ($space eq 'visibleparts') {
12465:                     my $navmap = Apache::lonnavmaps::navmap->new();
12466:                     my $item;
12467:                     if (ref($navmap)) {
12468:                         my $res = $navmap->getBySymb($symbparm);
12469:                         my $parts = $res->parts();
12470:                         if (ref($parts) eq 'ARRAY') {
12471:                             $item = join(',',@{$parts});
12472:                         }
12473:                         undef($navmap);
12474:                     }
12475:                     return $item;
12476:                 }
12477:             }
12478:         }
12479: 
12480: 	my ($section, $group, @groups, @recurseup, $recursed);
12481:         if (ref($recurseupref) eq 'ARRAY') {
12482:             @recurseup = @{$recurseupref};
12483:             $recursed = 1;
12484:         }
12485: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12486:         if (($courseid eq '') && ($cid)) {
12487:             $courseid = $cid;
12488:         }
12489: 	if (($symbparm && $courseid) && 
12490: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12491: 
12492: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12493: 
12494: # ----------------------------------------------------- Cascading lookup scheme
12495: 	    my $symbp=$symbparm;
12496: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12497: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12498:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12499: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12500: 	    if (($env{'user.name'} eq $uname) &&
12501: 		($env{'user.domain'} eq $udom)) {
12502: 		$section=$env{'request.course.sec'};
12503:                 @groups = split(/:/,$env{'request.course.groups'});  
12504:                 @groups=&sort_course_groups($courseid,@groups); 
12505: 	    } else {
12506: 		if (! defined($usection)) {
12507: 		    $section=&getsection($udom,$uname,$courseid);
12508: 		} else {
12509: 		    $section = $usection;
12510: 		}
12511:                 @groups = &get_users_groups($udom,$uname,$courseid);
12512: 	    }
12513: 
12514: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12515: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12516:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12517: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12518: 
12519: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12520: 	    my $courselevelr=$courseid.'.'.$symbparm;
12521:             $courseleveli=$courseid.'.'.$recurseparm;
12522: 	    $courselevelm=$courseid.'.'.$mapparm;
12523: 
12524: # ----------------------------------------------------------- first, check user
12525: 
12526: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12527:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12528: 				       ([$courselevelr,'resource'],
12529: 					[$courselevelm,'map'     ],
12530:                                         [$courseleveli,'map'     ],
12531: 					[$courselevel, 'course'  ]));
12532: 	    if (defined($userreply)) { return &get_reply($userreply); }
12533: 
12534: # ------------------------------------------------ second, check some of course
12535:             my $coursereply;
12536:             if (@groups > 0) {
12537:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12538:                                        $recurseparm,$mapparm,$spacequalifierrest,
12539:                                        $mapp,\$recursed,\@recurseup);
12540:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12541:             }
12542: 
12543: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12544: 				  $env{'course.'.$courseid.'.domain'},
12545: 				  'course',$mapp,\$recursed,\@recurseup,
12546:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12547: 				  ([$seclevelr,   'resource'],
12548: 				   [$seclevelm,   'map'     ],
12549:                                    [$secleveli,   'map'     ],
12550: 				   [$seclevel,    'course'  ],
12551: 				   [$courselevelr,'resource']));
12552: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12553: 
12554: # ------------------------------------------------------ third, check map parms
12555: 	    my %parmhash=();
12556: 	    my $thisparm='';
12557: 	    if (tie(%parmhash,'GDBM_File',
12558: 		    $env{'request.course.fn'}.'_parms.db',
12559: 		    &GDBM_READER(),0640)) {
12560: 		$thisparm=$parmhash{$symbparm};
12561: 		untie(%parmhash);
12562: 	    }
12563: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12564: 	}
12565: # ------------------------------------------ fourth, look in resource metadata
12566:  
12567:         my $what = $spacequalifierrest;
12568: 	$what=~s/\./\_/;
12569: 	my $filename;
12570: 	if (!$symbparm) { $symbparm=&symbread(); }
12571: 	if ($symbparm) {
12572: 	    $filename=(&decode_symb($symbparm))[2];
12573: 	} else {
12574: 	    $filename=$env{'request.filename'};
12575: 	}
12576:         my $toolsymb;
12577:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12578:             $toolsymb = $symbparm;
12579:         }
12580: 	my $metadata=&metadata($filename,$what,$toolsymb);
12581: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12582: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12583: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12584: 
12585: # ----------------------------------------------- fifth, look in rest of course
12586: 	if ($symbparm && defined($courseid) && 
12587: 	    $courseid eq $env{'request.course.id'}) {
12588: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12589: 				     $env{'course.'.$courseid.'.domain'},
12590: 				     'course',$mapp,\$recursed,\@recurseup,
12591:                                      $courseid,'.',$spacequalifierrest,
12592: 				     ([$courselevelm,'map'   ],
12593:                                       [$courseleveli,'map'   ],
12594: 				      [$courselevel, 'course']));
12595: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12596: 	}
12597: # ------------------------------------------------------------------ Cascade up
12598: 	unless ($space eq '0') {
12599: 	    my @parts=split(/_/,$space);
12600: 	    my $id=pop(@parts);
12601: 	    my $part=join('_',@parts);
12602: 	    if ($part eq '') { $part='0'; }
12603: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12604: 				 $symbparm,$udom,$uname,$section,1);
12605: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12606: 	}
12607: 	if ($recurse) { return undef; }
12608: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12609: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12610: # ---------------------------------------------------- Any other user namespace
12611:     } elsif ($realm eq 'environment') {
12612: # ----------------------------------------------------------------- environment
12613: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12614: 	    return $env{'environment.'.$spacequalifierrest};
12615: 	} else {
12616: 	    if ($uname eq 'anonymous' && $udom eq '') {
12617: 		return '';
12618: 	    }
12619: 	    my %returnhash=&userenvironment($udom,$uname,
12620: 					    $spacequalifierrest);
12621: 	    return $returnhash{$spacequalifierrest};
12622: 	}
12623:     } elsif ($realm eq 'system') {
12624: # ----------------------------------------------------------------- system.time
12625: 	if ($space eq 'time') {
12626: 	    return time;
12627:         }
12628:     } elsif ($realm eq 'server') {
12629: # ----------------------------------------------------------------- system.time
12630: 	if ($space eq 'name') {
12631: 	    return $ENV{'SERVER_NAME'};
12632:         }
12633:     } elsif ($realm eq 'client') {
12634:         if ($space eq 'remote_addr') {
12635:             return &get_requestor_ip();
12636:         }
12637:     }
12638:     return '';
12639: }
12640: 
12641: sub get_reply {
12642:     my ($reply_value) = @_;
12643:     if (ref($reply_value) eq 'ARRAY') {
12644:         if (wantarray) {
12645: 	    return @$reply_value;
12646:         }
12647:         return $reply_value->[0];
12648:     } else {
12649:         return $reply_value;
12650:     }
12651: }
12652: 
12653: sub check_group_parms {
12654:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12655:         $recursed,$recurseupref) = @_;
12656:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12657:                   [$what,'course']);
12658:     my $coursereply;
12659:     foreach my $group (@{$groups}) {
12660:         my @groupitems = ();
12661:         foreach my $level (@levels) {
12662:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12663:              push(@groupitems,[$item,$level->[1]]);
12664:         }
12665:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12666:                                    $env{'course.'.$courseid.'.domain'},
12667:                                    'course',$mapp,$recursed,$recurseupref,
12668:                                    $courseid,'.['.$group.'].',$what,
12669:                                    @groupitems);
12670:         last if (defined($coursereply));
12671:     }
12672:     return $coursereply;
12673: }
12674: 
12675: sub get_map_hierarchy {
12676:     my ($mapname,$courseid) = @_;
12677:     my @recurseup = ();
12678:     if ($mapname) {
12679:         if (($cachedmapkey eq $courseid) &&
12680:             (abs($cachedmaptime-time)<5)) {
12681:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12682:                 return @{$cachedmaps{$mapname}};
12683:             }
12684:         }
12685:         my $navmap = Apache::lonnavmaps::navmap->new();
12686:         if (ref($navmap)) {
12687:             @recurseup = $navmap->recurseup_maps($mapname);
12688:             undef($navmap);
12689:             $cachedmaps{$mapname} = \@recurseup;
12690:             $cachedmaptime=time;
12691:             $cachedmapkey=$courseid;
12692:         }
12693:     }
12694:     return @recurseup;
12695: }
12696: 
12697: }
12698: 
12699: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12700:     my ($courseid,@groups) = @_;
12701:     @groups = sort(@groups);
12702:     return @groups;
12703: }
12704: 
12705: sub packages_tab_default {
12706:     my ($uri,$varname,$toolsymb)=@_;
12707:     my (undef,$part,$name)=split(/\./,$varname);
12708: 
12709:     my (@extension,@specifics,$do_default);
12710:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12711: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12712: 	if ($pack_type eq 'default') {
12713: 	    $do_default=1;
12714: 	} elsif ($pack_type eq 'extension') {
12715: 	    push(@extension,[$package,$pack_type,$pack_part]);
12716: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12717: 	    # only look at packages defaults for packages that this id is
12718: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12719: 	}
12720:     }
12721:     # first look for a package that matches the requested part id
12722:     foreach my $package (@specifics) {
12723: 	my (undef,$pack_type,$pack_part)=@{$package};
12724: 	next if ($pack_part ne $part);
12725: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12726: 	    return $packagetab{"$pack_type&$name&default"};
12727: 	}
12728:     }
12729:     # look for any possible matching non extension_ package
12730:     foreach my $package (@specifics) {
12731: 	my (undef,$pack_type,$pack_part)=@{$package};
12732: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12733: 	    return $packagetab{"$pack_type&$name&default"};
12734: 	}
12735: 	if ($pack_type eq 'part') { $pack_part='0'; }
12736: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12737: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12738: 	}
12739:     }
12740:     # look for any posible extension_ match
12741:     foreach my $package (@extension) {
12742: 	my ($package,$pack_type)=@{$package};
12743: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12744: 	    return $packagetab{"$pack_type&$name&default"};
12745: 	}
12746: 	if (defined($packagetab{$package."&$name&default"})) {
12747: 	    return $packagetab{$package."&$name&default"};
12748: 	}
12749:     }
12750:     # look for a global default setting
12751:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12752: 	return $packagetab{"default&$name&default"};
12753:     }
12754:     return undef;
12755: }
12756: 
12757: sub add_prefix_and_part {
12758:     my ($prefix,$part)=@_;
12759:     my $keyroot;
12760:     if (defined($prefix) && $prefix !~ /^__/) {
12761: 	# prefix that has a part already
12762: 	$keyroot=$prefix;
12763:     } elsif (defined($prefix)) {
12764: 	# prefix that is missing a part
12765: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12766:     } else {
12767: 	# no prefix at all
12768: 	if (defined($part)) { $keyroot='_'.$part; }
12769:     }
12770:     return $keyroot;
12771: }
12772: 
12773: # ---------------------------------------------------------------- Get metadata
12774: 
12775: my %metaentry;
12776: my %importedpartids;
12777: my %importedrespids;
12778: sub metadata {
12779:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12780:     $uri=&declutter($uri);
12781:     # if it is a non metadata possible uri return quickly
12782:     if (($uri eq '') || 
12783: 	(($uri =~ m|^/*adm/|) && 
12784: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12785:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12786: 	return undef;
12787:     }
12788:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12789: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12790: 	return undef;
12791:     }
12792:     my $filename=$uri;
12793:     $uri=~s/\.meta$//;
12794: #
12795: # Is the metadata already cached?
12796: # Look at timestamp of caching
12797: # Everything is cached by the main uri, libraries are never directly cached
12798: #
12799:     if (!defined($liburi)) {
12800: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12801: 	if (defined($cached)) { return $result->{':'.$what}; }
12802:     }
12803: 
12804: #
12805: # If the uri is for an external tool the file from
12806: # which metadata should be retrieved depends on whether
12807: # the tool had been configured to be gradable (set in the Course
12808: # Editor or Resource Editor).
12809: #
12810: # If a valid symb has been included as the third arg in the call
12811: # to &metadata() that can be used to retrieve the value of
12812: # parameter_0_gradable set for the resource, and included in the
12813: # uploaded map containing the tool. The value is retrieved via
12814: # &EXT(), if a valid symb is available.  Otherwise the value of
12815: # gradable in the exttool_$marker.db file for the tool instance
12816: # is retrieved via &get().
12817: #
12818: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12819: # hiddenresource and encrypturl (during course initialization)
12820: # the map-level parameter for resource.0.gradable included in the 
12821: # uploaded map containing the tool will not yet have been stored
12822: # in the user_course_parms.db file for the user's session, so in 
12823: # this case fall back to retrieving gradable status from the
12824: # exttool_$marker.db file.
12825: #
12826: # In order to avoid an infinite loop, &metadata() will return
12827: # before a call to &EXT(), if the uri is for an external tool
12828: # and the $what for which metadata is being requested is
12829: # parameter_0_gradable or 0_gradable.
12830: #
12831: 
12832:     if ($uri =~ /ext\.tool$/) {
12833:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12834:             return;
12835:         } else {
12836:             my ($checked,$use_passback);
12837:             if ($toolsymb ne '') {
12838:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12839:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12840:                     $checked = 1;
12841:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12842:                         $use_passback = 1;
12843:                     }
12844:                 }
12845:             }
12846:             unless ($checked) {
12847:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12848:                 $marker=~s/\D//g;
12849:                 if ($marker) {
12850:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12851:                     $use_passback = $toolsettings{'gradable'};
12852:                 }
12853:             }
12854:             if ($use_passback) {
12855:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12856:             } else {
12857:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12858:             }
12859:         }
12860:     }
12861: 
12862:     {
12863: # Imported parts would go here
12864:         my @origfiletagids=();
12865:         my $importedparts=0;
12866: 
12867: # Imported responseids would go here
12868:         my $importedresponses=0;
12869: #
12870: # Is this a recursive call for a library?
12871: #
12872: #	if (! exists($metacache{$uri})) {
12873: #	    $metacache{$uri}={};
12874: #	}
12875: 	my $cachetime = 60*60;
12876:         if ($liburi) {
12877: 	    $liburi=&declutter($liburi);
12878:             $filename=$liburi;
12879:         } else {
12880: 	    &devalidate_cache_new('meta',$uri);
12881: 	    undef(%metaentry);
12882: 	}
12883:         my %metathesekeys=();
12884:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12885: 	my $metastring;
12886: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12887: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12888: 	    $metastring = 
12889: 		&Apache::lonnet::ssi_body($which,
12890: 					  ('grade_target' => 'meta'));
12891: 	    $cachetime = 1; # only want this cached in the child not long term
12892: 	} elsif (($uri !~ m -^(editupload)/-) && 
12893:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12894: 	    my $file=&filelocation('',&clutter($filename));
12895: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12896: 	    $metastring=&getfile($file);
12897: 	}
12898:         my $parser=HTML::LCParser->new(\$metastring);
12899:         my $token;
12900:         undef %metathesekeys;
12901:         while ($token=$parser->get_token) {
12902: 	    if ($token->[0] eq 'S') {
12903: 		if (defined($token->[2]->{'package'})) {
12904: #
12905: # This is a package - get package info
12906: #
12907: 		    my $package=$token->[2]->{'package'};
12908: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12909: 		    if (defined($token->[2]->{'id'})) { 
12910: 			$keyroot.='_'.$token->[2]->{'id'}; 
12911: 		    }
12912: 		    if ($metaentry{':packages'}) {
12913: 			$metaentry{':packages'}.=','.$package.$keyroot;
12914: 		    } else {
12915: 			$metaentry{':packages'}=$package.$keyroot;
12916: 		    }
12917: 		    foreach my $pack_entry (keys(%packagetab)) {
12918: 			my $part=$keyroot;
12919: 			$part=~s/^\_//;
12920: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12921: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12922: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12923: 			    # ignore package.tab specified default values
12924:                             # here &package_tab_default() will fetch those
12925: 			    if ($subp eq 'default') { next; }
12926: 			    my $value=$packagetab{$pack_entry};
12927: 			    my $unikey;
12928: 			    if ($pack =~ /_0$/) {
12929: 				$unikey='parameter_0_'.$name;
12930: 				$part=0;
12931: 			    } else {
12932: 				$unikey='parameter'.$keyroot.'_'.$name;
12933: 			    }
12934: 			    if ($subp eq 'display') {
12935: 				$value.=' [Part: '.$part.']';
12936: 			    }
12937: 			    $metaentry{':'.$unikey.'.part'}=$part;
12938: 			    $metathesekeys{$unikey}=1;
12939: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12940: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12941: 			    }
12942: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12943: 				$metaentry{':'.$unikey}=
12944: 				    $metaentry{':'.$unikey.'.default'};
12945: 			    }
12946: 			}
12947: 		    }
12948: 		} else {
12949: #
12950: # This is not a package - some other kind of start tag
12951: #
12952: 		    my $entry=$token->[1];
12953: 		    my $unikey='';
12954: 
12955: 		    if ($entry eq 'import') {
12956: #
12957: # Importing a library here
12958: #
12959:                         my $location=$parser->get_text('/import');
12960:                         my $dir=$filename;
12961:                         $dir=~s|[^/]*$||;
12962:                         $location=&filelocation($dir,$location);
12963: 
12964:                         my $importid=$token->[2]->{'id'};
12965:                         my $importmode=$token->[2]->{'importmode'};
12966: #
12967: # Check metadata for imported file to
12968: # see if it contained response items
12969: #
12970:                         my ($origfile,@libfilekeys);
12971:                         my %currmetaentry = %metaentry;
12972:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12973:                                                            $depthcount+1));
12974:                         if (grep(/^responseorder$/,@libfilekeys)) {
12975:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12976:                                                              undef,$depthcount+1);
12977:                             if ($libresponseorder ne '') {
12978:                                 if ($#origfiletagids<0) {
12979:                                     undef(%importedrespids);
12980:                                     undef(%importedpartids);
12981:                                 }
12982:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12983:                                 if (@respids) {
12984:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12985:                                 }
12986:                                 if ($importedrespids{$importid} ne '') {
12987:                                     $importedresponses = 1;
12988: # We need to get the original file and the imported file to get the response order correct
12989: # Load and inspect original file
12990:                                     if ($#origfiletagids<0) {
12991:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12992:                                         $origfile=&getfile($origfilelocation);
12993:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12994:                                     }
12995:                                 }
12996:                             }
12997:                         }
12998: # Do not overwrite contents of %metaentry hash for resource itself with 
12999: # hash populated for imported library file
13000:                         %metaentry = %currmetaentry;
13001:                         undef(%currmetaentry);
13002:                         if ($importmode eq 'part') {
13003: # Import as part(s)
13004:                            $importedparts=1;
13005: # We need to get the original file and the imported file to get the part order correct
13006: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
13007: # Load and inspect original file if we didn't do that already
13008:                            if ($#origfiletagids<0) {
13009:                                undef(%importedrespids);
13010:                                undef(%importedpartids);
13011:                                if ($origfile eq '') {
13012:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13013:                                    $origfile=&getfile($origfilelocation);
13014:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13015:                                }
13016:                            }
13017:                            my @impfilepartids;
13018: # If <partorder> tag is included in metadata for the imported file
13019: # get the parts in the imported file from that.
13020:                            if (grep(/^partorder$/,@libfilekeys)) {
13021:                                %currmetaentry = %metaentry;
13022:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13023:                                                             $depthcount+1);
13024:                                %metaentry = %currmetaentry;
13025:                                undef(%currmetaentry);
13026:                                if ($libpartorder ne '') {
13027:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
13028:                                }
13029:                            } else {
13030: # If no <partorder> tag available, load and inspect imported file
13031:                                my $impfile=&getfile($location);
13032:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13033:                            }
13034:                            if ($#impfilepartids>=0) {
13035: # This problem had parts
13036:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
13037:                            } else {
13038: # Importing by turning a single problem into a problem part
13039: # It gets the import-tags ID as part-ID
13040:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
13041:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
13042:                            }
13043:                         } else {
13044: # Import as problem or as normal import
13045:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13046:                             unless ($importmode eq 'problem') {
13047: # Normal import
13048:                                 if (defined($token->[2]->{'id'})) {
13049:                                     $unikey.='_'.$token->[2]->{'id'};
13050:                                 }
13051:                             }
13052: # Check metadata for imported file to
13053: # see if it contained parts
13054:                             if (grep(/^partorder$/,@libfilekeys)) {
13055:                                 %currmetaentry = %metaentry;
13056:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13057:                                                              $depthcount+1);
13058:                                 %metaentry = %currmetaentry;
13059:                                 undef(%currmetaentry);
13060:                                 if ($libpartorder ne '') {
13061:                                     $importedparts = 1;
13062:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
13063:                                 }
13064:                             }
13065:                         }
13066: 			if ($depthcount<20) {
13067: 			    my $metadata = 
13068: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
13069: 					  $depthcount+1);
13070: 			    foreach my $meta (split(',',$metadata)) {
13071: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
13072: 				$metathesekeys{$meta}=1;
13073: 			    }
13074:                         }
13075: 		    } else {
13076: #
13077: # Not importing, some other kind of non-package, non-library start tag
13078: # 
13079:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13080:                         if (defined($token->[2]->{'id'})) {
13081:                             $unikey.='_'.$token->[2]->{'id'};
13082:                         }
13083: 			if (defined($token->[2]->{'name'})) { 
13084: 			    $unikey.='_'.$token->[2]->{'name'}; 
13085: 			}
13086: 			$metathesekeys{$unikey}=1;
13087: 			foreach my $param (@{$token->[3]}) {
13088: 			    $metaentry{':'.$unikey.'.'.$param} =
13089: 				$token->[2]->{$param};
13090: 			}
13091: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
13092: 			my $default=$metaentry{':'.$unikey.'.default'};
13093: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13094: 		 # only ws inside the tag, and not in default, so use default
13095: 		 # as value
13096: 			    $metaentry{':'.$unikey}=$default;
13097: 			} elsif ( $internaltext =~ /\S/ ) {
13098: 		  # something interesting inside the tag
13099: 			    $metaentry{':'.$unikey}=$internaltext;
13100: 			} else {
13101: 		  # no interesting values, don't set a default
13102: 			}
13103: # end of not-a-package not-a-library import
13104: 		    }
13105: # end of not-a-package start tag
13106: 		}
13107: # the next is the end of "start tag"
13108: 	    }
13109: 	}
13110: 	my ($extension) = ($uri =~ /\.(\w+)$/);
13111: 	$extension = lc($extension);
13112: 	if ($extension eq 'htm') { $extension='html'; }
13113: 
13114: 	foreach my $key (keys(%packagetab)) {
13115: 	    #no specific packages #how's our extension
13116: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
13117: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
13118: 					 \%metathesekeys);
13119: 	}
13120: 
13121: 	if (!exists($metaentry{':packages'})
13122: 	    || $packagetab{"import_defaults&extension_$extension"}) {
13123: 	    foreach my $key (keys(%packagetab)) {
13124: 		#no specific packages well let's get default then
13125: 		if ($key!~/^default&/) { next; }
13126: 		&metadata_create_package_def($uri,$key,'default',
13127: 					     \%metathesekeys);
13128: 	    }
13129: 	}
13130: # are there custom rights to evaluate
13131: 	if ($metaentry{':copyright'} eq 'custom') {
13132: 
13133:     #
13134:     # Importing a rights file here
13135:     #
13136: 	    unless ($depthcount) {
13137: 		my $location=$metaentry{':customdistributionfile'};
13138: 		my $dir=$filename;
13139: 		$dir=~s|[^/]*$||;
13140: 		$location=&filelocation($dir,$location);
13141: 		my $rights_metadata =
13142: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
13143: 			      $depthcount+1);
13144: 		foreach my $rights (split(',',$rights_metadata)) {
13145: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13146: 		    $metathesekeys{$rights}=1;
13147: 		}
13148: 	    }
13149: 	}
13150: 	# uniqifiy package listing
13151: 	my %seen;
13152: 	my @uniq_packages =
13153: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13154: 	$metaentry{':packages'} = join(',',@uniq_packages);
13155: 
13156:         if (($importedresponses) || ($importedparts)) {
13157:             if ($importedparts) {
13158: # We had imported parts and need to rebuild partorder
13159:                 $metaentry{':partorder'}='';
13160:                 $metathesekeys{'partorder'}=1;
13161:             }
13162:             if ($importedresponses) {
13163: # We had imported responses and need to rebuil responseorder
13164:                 $metaentry{':responseorder'}='';
13165:                 $metathesekeys{'responseorder'}=1;
13166:             }
13167:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
13168:                 my $origid = $origfiletagids[$index+1];
13169:                 if ($origfiletagids[$index] eq 'part') {
13170: # Original part, part of the problem
13171:                     if ($importedparts) {
13172:                         $metaentry{':partorder'}.=','.$origid;
13173:                     }
13174:                 } elsif ($origfiletagids[$index] eq 'import') {
13175:                     if ($importedparts) {
13176: # We have imported parts at this position
13177:                         if ($importedpartids{$origid} ne '') {
13178:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
13179:                         }
13180:                     }
13181:                     if ($importedresponses) {
13182: # We have imported responses at this position
13183:                         if ($importedrespids{$origid} ne '') {
13184:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
13185:                         }
13186:                     }
13187:                 } else {
13188: # Original response item, part of the problem
13189:                     if ($importedresponses) {
13190:                         $metaentry{':responseorder'}.=','.$origid;
13191:                     }
13192:                 }
13193:             }
13194:             if ($importedparts) {
13195:                 $metaentry{':partorder'}=~s/^\,//;
13196:             }
13197:             if ($importedresponses) {
13198:                 $metaentry{':responseorder'}=~s/^\,//;
13199:             }
13200:         }
13201: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
13202: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
13203: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
13204:         unless ($liburi) {
13205: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
13206:         }
13207: # this is the end of "was not already recently cached
13208:     }
13209:     return $metaentry{':'.$what};
13210: }
13211: 
13212: sub metadata_create_package_def {
13213:     my ($uri,$key,$package,$metathesekeys)=@_;
13214:     my ($pack,$name,$subp)=split(/\&/,$key);
13215:     if ($subp eq 'default') { next; }
13216:     
13217:     if (defined($metaentry{':packages'})) {
13218: 	$metaentry{':packages'}.=','.$package;
13219:     } else {
13220: 	$metaentry{':packages'}=$package;
13221:     }
13222:     my $value=$packagetab{$key};
13223:     my $unikey;
13224:     $unikey='parameter_0_'.$name;
13225:     $metaentry{':'.$unikey.'.part'}=0;
13226:     $$metathesekeys{$unikey}=1;
13227:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13228: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
13229:     }
13230:     if (defined($metaentry{':'.$unikey.'.default'})) {
13231: 	$metaentry{':'.$unikey}=
13232: 	    $metaentry{':'.$unikey.'.default'};
13233:     }
13234: }
13235: 
13236: sub metadata_generate_part0 {
13237:     my ($metadata,$metacache,$uri) = @_;
13238:     my %allnames;
13239:     foreach my $metakey (keys(%$metadata)) {
13240: 	if ($metakey=~/^parameter\_(.*)/) {
13241: 	  my $part=$$metacache{':'.$metakey.'.part'};
13242: 	  my $name=$$metacache{':'.$metakey.'.name'};
13243: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
13244: 	    $allnames{$name}=$part;
13245: 	  }
13246: 	}
13247:     }
13248:     foreach my $name (keys(%allnames)) {
13249:       $$metadata{"parameter_0_$name"}=1;
13250:       my $key=":parameter_0_$name";
13251:       $$metacache{"$key.part"}='0';
13252:       $$metacache{"$key.name"}=$name;
13253:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
13254: 					   $allnames{$name}.'_'.$name.
13255: 					   '.type'};
13256:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
13257: 			     '.display'};
13258:       my $expr='[Part: '.$allnames{$name}.']';
13259:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
13260:       $$metacache{"$key.display"}=$olddis;
13261:     }
13262: }
13263: 
13264: # ------------------------------------------------------ Devalidate title cache
13265: 
13266: sub devalidate_title_cache {
13267:     my ($url)=@_;
13268:     if (!$env{'request.course.id'}) { return; }
13269:     my $symb=&symbread($url);
13270:     if (!$symb) { return; }
13271:     my $key=$env{'request.course.id'}."\0".$symb;
13272:     &devalidate_cache_new('title',$key);
13273: }
13274: 
13275: # ------------------------------------------------- Get the title of a course
13276: 
13277: sub current_course_title {
13278:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13279: }
13280: # ------------------------------------------------- Get the title of a resource
13281: 
13282: sub gettitle {
13283:     my $urlsymb=shift;
13284:     my $symb=&symbread($urlsymb);
13285:     if ($symb) {
13286: 	my $key=$env{'request.course.id'}."\0".$symb;
13287: 	my ($result,$cached)=&is_cached_new('title',$key);
13288: 	if (defined($cached)) { 
13289: 	    return $result;
13290: 	}
13291: 	my ($map,$resid,$url)=&decode_symb($symb);
13292: 	my $title='';
13293: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13294: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13295: 	} else {
13296: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13297: 		    &GDBM_READER(),0640)) {
13298: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13299: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13300: 		untie(%bighash);
13301: 	    }
13302: 	}
13303: 	$title=~s/\&colon\;/\:/gs;
13304: 	if ($title) {
13305: # Remember both $symb and $title for dynamic metadata
13306:             $accesshash{$symb.'___crstitle'}=$title;
13307:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13308: # Cache this title and then return it
13309: 	    return &do_cache_new('title',$key,$title,600);
13310: 	}
13311: 	$urlsymb=$url;
13312:     }
13313:     my $title=&metadata($urlsymb,'title');
13314:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13315:     return $title;
13316: }
13317: 
13318: sub get_slot {
13319:     my ($which,$cnum,$cdom)=@_;
13320:     if (!$cnum || !$cdom) {
13321: 	(undef,my $courseid)=&whichuser();
13322: 	$cdom=$env{'course.'.$courseid.'.domain'};
13323: 	$cnum=$env{'course.'.$courseid.'.num'};
13324:     }
13325:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13326:     my %slotinfo;
13327:     if (exists($remembered{$key})) {
13328: 	$slotinfo{$which} = $remembered{$key};
13329:     } else {
13330: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13331: 	&Apache::lonhomework::showhash(%slotinfo);
13332: 	my ($tmp)=keys(%slotinfo);
13333: 	if ($tmp=~/^error:/) { return (); }
13334: 	$remembered{$key} = $slotinfo{$which};
13335:     }
13336:     if (ref($slotinfo{$which}) eq 'HASH') {
13337: 	return %{$slotinfo{$which}};
13338:     }
13339:     return $slotinfo{$which};
13340: }
13341: 
13342: sub get_reservable_slots {
13343:     my ($cnum,$cdom,$uname,$udom) = @_;
13344:     my $now = time;
13345:     my $reservable_info;
13346:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13347:     if (exists($remembered{$key})) {
13348:         $reservable_info = $remembered{$key};
13349:     } else {
13350:         my %resv;
13351:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13352:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13353:         $reservable_info = \%resv;
13354:         $remembered{$key} = $reservable_info;
13355:     }
13356:     return $reservable_info;
13357: }
13358: 
13359: sub get_course_slots {
13360:     my ($cnum,$cdom) = @_;
13361:     my $hashid=$cnum.':'.$cdom;
13362:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13363:     if (defined($cached)) {
13364:         if (ref($result) eq 'HASH') {
13365:             return %{$result};
13366:         }
13367:     } else {
13368:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13369:         my ($tmp) = keys(%slots);
13370:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13371:             &do_cache_new('allslots',$hashid,\%slots,600);
13372:             return %slots;
13373:         }
13374:     }
13375:     return;
13376: }
13377: 
13378: sub devalidate_slots_cache {
13379:     my ($cnum,$cdom)=@_;
13380:     my $hashid=$cnum.':'.$cdom;
13381:     &devalidate_cache_new('allslots',$hashid);
13382: }
13383: 
13384: sub get_coursechange {
13385:     my ($cdom,$cnum) = @_;
13386:     if ($cdom eq '' || $cnum eq '') {
13387:         return unless ($env{'request.course.id'});
13388:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13389:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13390:     }
13391:     my $hashid=$cdom.'_'.$cnum;
13392:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13393:     if ((defined($cached)) && ($change ne '')) {
13394:         return $change;
13395:     } else {
13396:         my %crshash;
13397:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13398:         if ($crshash{'internal.contentchange'} eq '') {
13399:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13400:             if ($change eq '') {
13401:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13402:                 $change = $crshash{'internal.created'};
13403:             }
13404:         } else {
13405:             $change = $crshash{'internal.contentchange'};
13406:         }
13407:         my $cachetime = 600;
13408:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13409:     }
13410:     return $change;
13411: }
13412: 
13413: sub devalidate_coursechange_cache {
13414:     my ($cnum,$cdom)=@_;
13415:     my $hashid=$cnum.':'.$cdom;
13416:     &devalidate_cache_new('crschange',$hashid);
13417: }
13418: 
13419: # ------------------------------------------------- Update symbolic store links
13420: 
13421: sub symblist {
13422:     my ($mapname,%newhash)=@_;
13423:     $mapname=&deversion(&declutter($mapname));
13424:     my %hash;
13425:     if (($env{'request.course.fn'}) && (%newhash)) {
13426:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13427:                       &GDBM_WRCREAT(),0640)) {
13428: 	    foreach my $url (keys(%newhash)) {
13429: 		next if ($url eq 'last_known'
13430: 			 && $env{'form.no_update_last_known'});
13431: 		$hash{declutter($url)}=&encode_symb($mapname,
13432: 						    $newhash{$url}->[1],
13433: 						    $newhash{$url}->[0]);
13434:             }
13435:             if (untie(%hash)) {
13436: 		return 'ok';
13437:             }
13438:         }
13439:     }
13440:     return 'error';
13441: }
13442: 
13443: # --------------------------------------------------------------- Verify a symb
13444: 
13445: sub symbverify {
13446:     my ($symb,$thisurl,$encstate)=@_;
13447:     my $thisfn=$thisurl;
13448:     $thisfn=&declutter($thisfn);
13449: # direct jump to resource in page or to a sequence - will construct own symbs
13450:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13451: # check URL part
13452:     my ($map,$resid,$url)=&decode_symb($symb);
13453: 
13454:     unless ($url eq $thisfn) { return 0; }
13455: 
13456:     $symb=&symbclean($symb);
13457:     $thisurl=&deversion($thisurl);
13458:     $thisfn=&deversion($thisfn);
13459: 
13460:     my %bighash;
13461:     my $okay=0;
13462: 
13463:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13464:                             &GDBM_READER(),0640)) {
13465:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13466:             $thisurl =~ s/\?.+$//;
13467:             if ($map =~ m{^uploaded/.+\.page$}) {
13468:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13469:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13470:             }
13471:         }
13472:         my $ids;
13473:         if ($map =~ m{^uploaded/.+\.page$}) {
13474:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13475:         } else {
13476:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13477:         }
13478:         unless ($ids) {
13479:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13480:             $ids=$bighash{$idkey};
13481:         }
13482:         if ($ids) {
13483: # ------------------------------------------------------------------- Has ID(s)
13484:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13485:                 $symb =~ s/\?.+$//;
13486:             }
13487: 	    foreach my $id (split(/\,/,$ids)) {
13488: 	       my ($mapid,$resid)=split(/\./,$id);
13489:                if (
13490:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13491:    eq $symb) {
13492:                    if (ref($encstate)) {
13493:                        $$encstate = $bighash{'encrypted_'.$id};
13494:                    }
13495: 		   if (($env{'request.role.adv'}) ||
13496: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13497:                        ($thisurl eq '/adm/navmaps')) {
13498: 		       $okay=1;
13499:                        last;
13500: 		   }
13501: 	       }
13502: 	   }
13503:         }
13504: 	untie(%bighash);
13505:     }
13506:     return $okay;
13507: }
13508: 
13509: # --------------------------------------------------------------- Clean-up symb
13510: 
13511: sub symbclean {
13512:     my $symb=shift;
13513:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13514: # remove version from map
13515:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13516: 
13517: # remove version from URL
13518:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13519: 
13520: # remove wrapper
13521: 
13522:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13523:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13524:     return $symb;
13525: }
13526: 
13527: # ---------------------------------------------- Split symb to find map and url
13528: 
13529: sub encode_symb {
13530:     my ($map,$resid,$url)=@_;
13531:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13532: }
13533: 
13534: sub decode_symb {
13535:     my $symb=shift;
13536:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13537:     my ($map,$resid,$url)=split(/___/,$symb);
13538:     return (&fixversion($map),$resid,&fixversion($url));
13539: }
13540: 
13541: sub fixversion {
13542:     my $fn=shift;
13543:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13544:     my %bighash;
13545:     my $uri=&clutter($fn);
13546:     my $key=$env{'request.course.id'}.'_'.$uri;
13547: # is this cached?
13548:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13549:     if (defined($cached)) { return $result; }
13550: # unfortunately not cached, or expired
13551:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13552: 	    &GDBM_READER(),0640)) {
13553:  	if ($bighash{'version_'.$uri}) {
13554:  	    my $version=$bighash{'version_'.$uri};
13555:  	    unless (($version eq 'mostrecent') || 
13556: 		    ($version==&getversion($uri))) {
13557:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13558:  	    }
13559:  	}
13560:  	untie %bighash;
13561:     }
13562:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13563: }
13564: 
13565: sub deversion {
13566:     my $url=shift;
13567:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13568:     return $url;
13569: }
13570: 
13571: # ------------------------------------------------------ Return symb list entry
13572: 
13573: sub symbread {
13574:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
13575:         $ignoresymbdb,$noenccheck)=@_;
13576:     my $cache_str='request.symbread.cached.'.$thisfn;
13577:     if (defined($env{$cache_str})) {
13578:         unless (ref($possibles) eq 'HASH') {
13579:             if ($ignorecachednull) {
13580:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
13581:             } else {
13582:                 return $env{$cache_str};
13583:             }
13584:         }
13585:     }
13586: # no filename provided? try from environment
13587:     unless ($thisfn) {
13588:         if ($env{'request.symb'}) {
13589:             return $env{$cache_str}=&symbclean($env{'request.symb'});
13590: 	}
13591: 	$thisfn=$env{'request.filename'};
13592:     }
13593:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13594: # is that filename actually a symb? Verify, clean, and return
13595:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13596: 	if (&symbverify($thisfn,$1)) {
13597: 	    return $env{$cache_str}=&symbclean($thisfn);
13598: 	}
13599:     }
13600:     $thisfn=declutter($thisfn);
13601:     my %hash;
13602:     my %bighash;
13603:     my $syval='';
13604:     if (($env{'request.course.fn'}) && ($thisfn)) {
13605:         my $targetfn = $thisfn;
13606:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13607:             $targetfn = 'adm/wrapper/'.$thisfn;
13608:         }
13609: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13610: 	    $targetfn=$1;
13611: 	}
13612:         unless ($ignoresymbdb) {
13613:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13614:                           &GDBM_READER(),0640)) {
13615: 	        $syval=$hash{$targetfn};
13616:                 untie(%hash);
13617:             }
13618:             if ($syval && $checkforblock) {
13619:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
13620:                 if (@blockers) {
13621:                     $syval='';
13622:                 }
13623:             }
13624:         }
13625: # ---------------------------------------------------------- There was an entry
13626:         if ($syval) {
13627: 	    #unless ($syval=~/\_\d+$/) {
13628: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13629: 		    #&appenv({'request.ambiguous' => $thisfn});
13630: 		    #return $env{$cache_str}='';
13631: 		#}    
13632: 		#$syval.=$1;
13633: 	    #}
13634:         } else {
13635: # ------------------------------------------------------- Was not in symb table
13636:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13637:                             &GDBM_READER(),0640)) {
13638: # ---------------------------------------------- Get ID(s) for current resource
13639:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13640:               unless ($ids) { 
13641:                  $ids=$bighash{'ids_/'.$thisfn};
13642:               }
13643:               unless ($ids) {
13644: # alias?
13645: 		  $ids=$bighash{'mapalias_'.$thisfn};
13646:               }
13647:               if ($ids) {
13648: # ------------------------------------------------------------------- Has ID(s)
13649:                  my @possibilities=split(/\,/,$ids);
13650:                  if ($#possibilities==0) {
13651: # ----------------------------------------------- There is only one possibility
13652: 		     my ($mapid,$resid)=split(/\./,$ids);
13653: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13654: 						    $resid,$thisfn);
13655:                      if (ref($possibles) eq 'HASH') {
13656:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13657:                              $possibles->{$syval} = 1;
13658:                          }
13659:                      }
13660:                      if ($checkforblock) {
13661:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13662:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
13663:                              if (@blockers) {
13664:                                  $syval = '';
13665:                                  untie(%bighash);
13666:                                  return $env{$cache_str}='';
13667:                              }
13668:                          }
13669:                      }
13670:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13671: # ------------------------------------------ There is more than one possibility
13672:                      my $realpossible=0;
13673:                      foreach my $id (@possibilities) {
13674: 			 my $file=$bighash{'src_'.$id};
13675:                          my $canaccess;
13676:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13677:                              $canaccess = 1;
13678:                          } else { 
13679:                              $canaccess = &allowed('bre',$file);
13680:                          }
13681:                          if ($canaccess) {
13682:          		     my ($mapid,$resid)=split(/\./,$id);
13683:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13684:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13685: 						             $resid,$thisfn);
13686:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
13687:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
13688:                                  if ($checkforblock) {
13689:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
13690:                                      if (@blockers > 0) {
13691:                                          $syval = '';
13692:                                      } else {
13693:                                          $syval = $poss_syval;
13694:                                          $realpossible++;
13695:                                      }
13696:                                  } else {
13697:                                      $syval = $poss_syval;
13698:                                      $realpossible++;
13699:                                  }
13700:                                  if ($syval) {
13701:                                      if (ref($possibles) eq 'HASH') {
13702:                                          $possibles->{$syval} = 1;
13703:                                      }
13704:                                  }
13705:                              }
13706: 			 }
13707:                      }
13708: 		     if ($realpossible!=1) { $syval=''; }
13709:                  } else {
13710:                      $syval='';
13711:                  }
13712: 	      }
13713:               untie(%bighash);
13714:            }
13715:         }
13716:         if ($syval) {
13717: 	    return $env{$cache_str}=$syval;
13718:         }
13719:     }
13720:     &appenv({'request.ambiguous' => $thisfn});
13721:     return $env{$cache_str}='';
13722: }
13723: 
13724: # ---------------------------------------------------------- Return random seed
13725: 
13726: sub numval {
13727:     my $txt=shift;
13728:     $txt=~tr/A-J/0-9/;
13729:     $txt=~tr/a-j/0-9/;
13730:     $txt=~tr/K-T/0-9/;
13731:     $txt=~tr/k-t/0-9/;
13732:     $txt=~tr/U-Z/0-5/;
13733:     $txt=~tr/u-z/0-5/;
13734:     $txt=~s/\D//g;
13735:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13736:     return int($txt);
13737: }
13738: 
13739: sub numval2 {
13740:     my $txt=shift;
13741:     $txt=~tr/A-J/0-9/;
13742:     $txt=~tr/a-j/0-9/;
13743:     $txt=~tr/K-T/0-9/;
13744:     $txt=~tr/k-t/0-9/;
13745:     $txt=~tr/U-Z/0-5/;
13746:     $txt=~tr/u-z/0-5/;
13747:     $txt=~s/\D//g;
13748:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13749:     my $total;
13750:     foreach my $val (@txts) { $total+=$val; }
13751:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13752:     return int($total);
13753: }
13754: 
13755: sub numval3 {
13756:     use integer;
13757:     my $txt=shift;
13758:     $txt=~tr/A-J/0-9/;
13759:     $txt=~tr/a-j/0-9/;
13760:     $txt=~tr/K-T/0-9/;
13761:     $txt=~tr/k-t/0-9/;
13762:     $txt=~tr/U-Z/0-5/;
13763:     $txt=~tr/u-z/0-5/;
13764:     $txt=~s/\D//g;
13765:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13766:     my $total;
13767:     foreach my $val (@txts) { $total+=$val; }
13768:     if ($_64bit) { $total=(($total<<32)>>32); }
13769:     return $total;
13770: }
13771: 
13772: sub digest {
13773:     my ($data)=@_;
13774:     my $digest=&Digest::MD5::md5($data);
13775:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13776:     my ($e,$f);
13777:     {
13778:         use integer;
13779:         $e=($a+$b);
13780:         $f=($c+$d);
13781:         if ($_64bit) {
13782:             $e=(($e<<32)>>32);
13783:             $f=(($f<<32)>>32);
13784:         }
13785:     }
13786:     if (wantarray) {
13787: 	return ($e,$f);
13788:     } else {
13789: 	my $g;
13790: 	{
13791: 	    use integer;
13792: 	    $g=($e+$f);
13793: 	    if ($_64bit) {
13794: 		$g=(($g<<32)>>32);
13795: 	    }
13796: 	}
13797: 	return $g;
13798:     }
13799: }
13800: 
13801: sub latest_rnd_algorithm_id {
13802:     return '64bit5';
13803: }
13804: 
13805: sub get_rand_alg {
13806:     my ($courseid)=@_;
13807:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13808:     if ($courseid) {
13809: 	return $env{"course.$courseid.rndseed"};
13810:     }
13811:     return &latest_rnd_algorithm_id();
13812: }
13813: 
13814: sub validCODE {
13815:     my ($CODE)=@_;
13816:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13817:     return 0;
13818: }
13819: 
13820: sub getCODE {
13821:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13822:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13823: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13824: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13825: 	return $Apache::lonhomework::history{'resource.CODE'};
13826:     }
13827:     return undef;
13828: }
13829: #
13830: #  Determines the random seed for a specific context:
13831: #
13832: # parameters:
13833: #   symb      - in course context the symb for the seed.
13834: #   course_id - The course id of the form domain_coursenum.
13835: #   domain    - Domain for the user.
13836: #   course    - Course for the user.
13837: #   cenv      - environment of the course.
13838: #
13839: # NOTE:
13840: #   All parameters are picked out of the environment if missing
13841: #   or not defined.
13842: #   If a symb cannot be determined the current time is used instead.
13843: #
13844: #  For a given well defined symb, courside, domain, username,
13845: #  and course environment, the seed is reproducible.
13846: #
13847: sub rndseed {
13848:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13849:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13850:     if (!defined($symb)) {
13851: 	unless ($symb=$wsymb) { return time; }
13852:     }
13853:     if (!defined $courseid) { 
13854: 	$courseid=$wcourseid; 
13855:     }
13856:     if (!defined $domain) { $domain=$wdomain; }
13857:     if (!defined $username) { $username=$wusername }
13858: 
13859:     my $which;
13860:     if (defined($cenv->{'rndseed'})) {
13861: 	$which = $cenv->{'rndseed'};
13862:     } else {
13863: 	$which =&get_rand_alg($courseid);
13864:     }
13865:     if (defined(&getCODE())) {
13866: 
13867: 	if ($which eq '64bit5') {
13868: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13869: 	} elsif ($which eq '64bit4') {
13870: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13871: 	} else {
13872: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13873: 	}
13874:     } elsif ($which eq '64bit5') {
13875: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13876:     } elsif ($which eq '64bit4') {
13877: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13878:     } elsif ($which eq '64bit3') {
13879: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13880:     } elsif ($which eq '64bit2') {
13881: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13882:     } elsif ($which eq '64bit') {
13883: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13884:     }
13885:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13886: }
13887: 
13888: sub rndseed_32bit {
13889:     my ($symb,$courseid,$domain,$username)=@_;
13890:     {
13891: 	use integer;
13892: 	my $symbchck=unpack("%32C*",$symb) << 27;
13893: 	my $symbseed=numval($symb) << 22;
13894: 	my $namechck=unpack("%32C*",$username) << 17;
13895: 	my $nameseed=numval($username) << 12;
13896: 	my $domainseed=unpack("%32C*",$domain) << 7;
13897: 	my $courseseed=unpack("%32C*",$courseid);
13898: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13899: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13900: 	#&logthis("rndseed :$num:$symb");
13901: 	if ($_64bit) { $num=(($num<<32)>>32); }
13902: 	return $num;
13903:     }
13904: }
13905: 
13906: sub rndseed_64bit {
13907:     my ($symb,$courseid,$domain,$username)=@_;
13908:     {
13909: 	use integer;
13910: 	my $symbchck=unpack("%32S*",$symb) << 21;
13911: 	my $symbseed=numval($symb) << 10;
13912: 	my $namechck=unpack("%32S*",$username);
13913: 	
13914: 	my $nameseed=numval($username) << 21;
13915: 	my $domainseed=unpack("%32S*",$domain) << 10;
13916: 	my $courseseed=unpack("%32S*",$courseid);
13917: 	
13918: 	my $num1=$symbchck+$symbseed+$namechck;
13919: 	my $num2=$nameseed+$domainseed+$courseseed;
13920: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13921: 	#&logthis("rndseed :$num:$symb");
13922: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13923: 	return "$num1,$num2";
13924:     }
13925: }
13926: 
13927: sub rndseed_64bit2 {
13928:     my ($symb,$courseid,$domain,$username)=@_;
13929:     {
13930: 	use integer;
13931: 	# strings need to be an even # of cahracters long, it it is odd the
13932:         # last characters gets thrown away
13933: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13934: 	my $symbseed=numval($symb) << 10;
13935: 	my $namechck=unpack("%32S*",$username.' ');
13936: 	
13937: 	my $nameseed=numval($username) << 21;
13938: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13939: 	my $courseseed=unpack("%32S*",$courseid.' ');
13940: 	
13941: 	my $num1=$symbchck+$symbseed+$namechck;
13942: 	my $num2=$nameseed+$domainseed+$courseseed;
13943: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13944: 	#&logthis("rndseed :$num:$symb");
13945: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13946: 	return "$num1,$num2";
13947:     }
13948: }
13949: 
13950: sub rndseed_64bit3 {
13951:     my ($symb,$courseid,$domain,$username)=@_;
13952:     {
13953: 	use integer;
13954: 	# strings need to be an even # of cahracters long, it it is odd the
13955:         # last characters gets thrown away
13956: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13957: 	my $symbseed=numval2($symb) << 10;
13958: 	my $namechck=unpack("%32S*",$username.' ');
13959: 	
13960: 	my $nameseed=numval2($username) << 21;
13961: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13962: 	my $courseseed=unpack("%32S*",$courseid.' ');
13963: 	
13964: 	my $num1=$symbchck+$symbseed+$namechck;
13965: 	my $num2=$nameseed+$domainseed+$courseseed;
13966: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13967: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13968: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13969: 	
13970: 	return "$num1:$num2";
13971:     }
13972: }
13973: 
13974: sub rndseed_64bit4 {
13975:     my ($symb,$courseid,$domain,$username)=@_;
13976:     {
13977: 	use integer;
13978: 	# strings need to be an even # of cahracters long, it it is odd the
13979:         # last characters gets thrown away
13980: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13981: 	my $symbseed=numval3($symb) << 10;
13982: 	my $namechck=unpack("%32S*",$username.' ');
13983: 	
13984: 	my $nameseed=numval3($username) << 21;
13985: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13986: 	my $courseseed=unpack("%32S*",$courseid.' ');
13987: 	
13988: 	my $num1=$symbchck+$symbseed+$namechck;
13989: 	my $num2=$nameseed+$domainseed+$courseseed;
13990: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13991: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13992: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13993: 	
13994: 	return "$num1:$num2";
13995:     }
13996: }
13997: 
13998: sub rndseed_64bit5 {
13999:     my ($symb,$courseid,$domain,$username)=@_;
14000:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14001:     return "$num1:$num2";
14002: }
14003: 
14004: sub rndseed_CODE_64bit {
14005:     my ($symb,$courseid,$domain,$username)=@_;
14006:     {
14007: 	use integer;
14008: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
14009: 	my $symbseed=numval2($symb);
14010: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14011: 	my $CODEseed=numval(&getCODE());
14012: 	my $courseseed=unpack("%32S*",$courseid.' ');
14013: 	my $num1=$symbseed+$CODEchck;
14014: 	my $num2=$CODEseed+$courseseed+$symbchck;
14015: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14016: 	#&logthis("rndseed :$num1:$num2:$symb");
14017: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
14018: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
14019: 	return "$num1:$num2";
14020:     }
14021: }
14022: 
14023: sub rndseed_CODE_64bit4 {
14024:     my ($symb,$courseid,$domain,$username)=@_;
14025:     {
14026: 	use integer;
14027: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
14028: 	my $symbseed=numval3($symb);
14029: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14030: 	my $CODEseed=numval3(&getCODE());
14031: 	my $courseseed=unpack("%32S*",$courseid.' ');
14032: 	my $num1=$symbseed+$CODEchck;
14033: 	my $num2=$CODEseed+$courseseed+$symbchck;
14034: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14035: 	#&logthis("rndseed :$num1:$num2:$symb");
14036: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
14037: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
14038: 	return "$num1:$num2";
14039:     }
14040: }
14041: 
14042: sub rndseed_CODE_64bit5 {
14043:     my ($symb,$courseid,$domain,$username)=@_;
14044:     my $code = &getCODE();
14045:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
14046:     return "$num1:$num2";
14047: }
14048: 
14049: sub setup_random_from_rndseed {
14050:     my ($rndseed)=@_;
14051:     if ($rndseed =~/([,:])/) {
14052:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14053:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14054:             &Math::Random::random_set_seed_from_phrase($rndseed);
14055:         } else {
14056:             &Math::Random::random_set_seed($num1,$num2);
14057:         }
14058:     } else {
14059: 	&Math::Random::random_set_seed_from_phrase($rndseed);
14060:     }
14061: }
14062: 
14063: sub latest_receipt_algorithm_id {
14064:     return 'receipt3';
14065: }
14066: 
14067: sub recunique {
14068:     my $fucourseid=shift;
14069:     my $unique;
14070:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14071: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14072: 	$unique=$env{"course.$fucourseid.internal.encseed"};
14073:     } else {
14074: 	$unique=$perlvar{'lonReceipt'};
14075:     }
14076:     return unpack("%32C*",$unique);
14077: }
14078: 
14079: sub recprefix {
14080:     my $fucourseid=shift;
14081:     my $prefix;
14082:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14083: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14084: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
14085:     } else {
14086: 	$prefix=$perlvar{'lonHostID'};
14087:     }
14088:     return unpack("%32C*",$prefix);
14089: }
14090: 
14091: sub ireceipt {
14092:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
14093: 
14094:     my $return =&recprefix($fucourseid).'-';
14095: 
14096:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14097: 	$env{'request.state'} eq 'construct') {
14098: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14099: 	return $return;
14100:     }
14101: 
14102:     my $cuname=unpack("%32C*",$funame);
14103:     my $cudom=unpack("%32C*",$fudom);
14104:     my $cucourseid=unpack("%32C*",$fucourseid);
14105:     my $cusymb=unpack("%32C*",$fusymb);
14106:     my $cunique=&recunique($fucourseid);
14107:     my $cpart=unpack("%32S*",$part);
14108:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14109: 
14110: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
14111: 			       
14112: 	$return.= ($cunique%$cuname+
14113: 		   $cunique%$cudom+
14114: 		   $cusymb%$cuname+
14115: 		   $cusymb%$cudom+
14116: 		   $cucourseid%$cuname+
14117: 		   $cucourseid%$cudom+
14118: 		   $cpart%$cuname+
14119: 		   $cpart%$cudom);
14120:     } else {
14121: 	$return.= ($cunique%$cuname+
14122: 		   $cunique%$cudom+
14123: 		   $cusymb%$cuname+
14124: 		   $cusymb%$cudom+
14125: 		   $cucourseid%$cuname+
14126: 		   $cucourseid%$cudom);
14127:     }
14128:     return $return;
14129: }
14130: 
14131: sub receipt {
14132:     my ($part)=@_;
14133:     my ($symb,$courseid,$domain,$name) = &whichuser();
14134:     return &ireceipt($name,$domain,$courseid,$symb,$part);
14135: }
14136: 
14137: sub whichuser {
14138:     my ($passedsymb)=@_;
14139:     my ($symb,$courseid,$domain,$name,$publicuser);
14140:     if (defined($env{'form.grade_symb'})) {
14141: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14142: 	my $allowed=&allowed('vgr',$tmp_courseid);
14143: 	if (!$allowed &&
14144: 	    exists($env{'request.course.sec'}) &&
14145: 	    $env{'request.course.sec'} !~ /^\s*$/) {
14146: 	    $allowed=&allowed('vgr',$tmp_courseid.
14147: 			      '/'.$env{'request.course.sec'});
14148: 	}
14149: 	if ($allowed) {
14150: 	    ($symb)=&get_env_multiple('form.grade_symb');
14151: 	    $courseid=$tmp_courseid;
14152: 	    ($domain)=&get_env_multiple('form.grade_domain');
14153: 	    ($name)=&get_env_multiple('form.grade_username');
14154: 	    return ($symb,$courseid,$domain,$name,$publicuser);
14155: 	}
14156:     }
14157:     if (!$passedsymb) {
14158: 	$symb=&symbread();
14159:     } else {
14160: 	$symb=$passedsymb;
14161:     }
14162:     $courseid=$env{'request.course.id'};
14163:     $domain=$env{'user.domain'};
14164:     $name=$env{'user.name'};
14165:     if ($name eq 'public' && $domain eq 'public') {
14166: 	if (!defined($env{'form.username'})) {
14167: 	    $env{'form.username'}.=time.rand(10000000);
14168: 	}
14169: 	$name.=$env{'form.username'};
14170:     }
14171:     return ($symb,$courseid,$domain,$name,$publicuser);
14172: 
14173: }
14174: 
14175: # ------------------------------------------------------------ Serves up a file
14176: # returns either the contents of the file or 
14177: # -1 if the file doesn't exist
14178: #
14179: # if the target is a file that was uploaded via DOCS, 
14180: # a check will be made to see if a current copy exists on the local server,
14181: # if it does this will be served, otherwise a copy will be retrieved from
14182: # the home server for the course and stored in /home/httpd/html/userfiles on
14183: # the local server.   
14184: 
14185: sub getfile {
14186:     my ($file) = @_;
14187:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
14188:     &repcopy($file);
14189:     return &readfile($file);
14190: }
14191: 
14192: sub repcopy_userfile {
14193:     my ($file)=@_;
14194:     my $londocroot = $perlvar{'lonDocRoot'};
14195:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
14196:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
14197:     my ($cdom,$cnum,$filename) = 
14198: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
14199:     my $uri="/uploaded/$cdom/$cnum/$filename";
14200:     if (-e "$file") {
14201: # we already have a local copy, check it out
14202: 	my @fileinfo = stat($file);
14203: 	my $rtncode;
14204: 	my $info;
14205: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
14206: 	if ($lwpresp ne 'ok') {
14207: # there is no such file anymore, even though we had a local copy
14208: 	    if ($rtncode eq '404') {
14209: 		unlink($file);
14210: 	    }
14211: 	    return -1;
14212: 	}
14213: 	if ($info < $fileinfo[9]) {
14214: # nice, the file we have is up-to-date, just say okay
14215: 	    return 'ok';
14216: 	} else {
14217: # the file is outdated, get rid of it
14218: 	    unlink($file);
14219: 	}
14220:     }
14221: # one way or the other, at this point, we don't have the file
14222: # construct the correct path for the file
14223:     my @parts = ($cdom,$cnum); 
14224:     if ($filename =~ m|^(.+)/[^/]+$|) {
14225: 	push @parts, split(/\//,$1);
14226:     }
14227:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14228:     foreach my $part (@parts) {
14229: 	$path .= '/'.$part;
14230: 	if (!-e $path) {
14231: 	    mkdir($path,0770);
14232: 	}
14233:     }
14234: # now the path exists for sure
14235: # get a user agent
14236:     my $transferfile=$file.'.in.transfer';
14237: # FIXME: this should flock
14238:     if (-e $transferfile) { return 'ok'; }
14239:     my $request;
14240:     $uri=~s/^\///;
14241:     my $homeserver = &homeserver($cnum,$cdom);
14242:     my $hostname = &hostname($homeserver);
14243:     my $protocol = $protocol{$homeserver};
14244:     $protocol = 'http' if ($protocol ne 'https');
14245:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
14246:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
14247: # did it work?
14248:     if ($response->is_error()) {
14249: 	unlink($transferfile);
14250: 	&logthis("Userfile repcopy failed for $uri");
14251: 	return -1;
14252:     }
14253: # worked, rename the transfer file
14254:     rename($transferfile,$file);
14255:     return 'ok';
14256: }
14257: 
14258: sub tokenwrapper {
14259:     my $uri=shift;
14260:     $uri=~s|^https?\://([^/]+)||;
14261:     $uri=~s|^/||;
14262:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14263:     my $token=$1;
14264:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14265:     if ($udom && $uname && $file) {
14266: 	$file=~s|(\?\.*)*$||;
14267:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14268:         my $homeserver = &homeserver($uname,$udom);
14269:         my $hostname = &hostname($homeserver);
14270:         my $protocol = $protocol{$homeserver};
14271:         $protocol = 'http' if ($protocol ne 'https');
14272:         return $protocol.'://'.$hostname.'/'.$uri.
14273:                (($uri=~/\?/)?'&':'?').'token='.$token.
14274:                                '&tokenissued='.$perlvar{'lonHostID'};
14275:     } else {
14276:         return '/adm/notfound.html';
14277:     }
14278: }
14279: 
14280: # call with reqtype HEAD: get last modification time
14281: # call with reqtype GET: get the file contents
14282: # Do not call this with reqtype GET for large files! It loads everything into memory
14283: #
14284: sub getuploaded {
14285:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14286:     $uri=~s/^\///;
14287:     my $homeserver = &homeserver($cnum,$cdom);
14288:     my $hostname = &hostname($homeserver);
14289:     my $protocol = $protocol{$homeserver};
14290:     $protocol = 'http' if ($protocol ne 'https');
14291:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14292:     my $request=new HTTP::Request($reqtype,$uri);
14293:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14294:     $$rtncode = $response->code;
14295:     if (! $response->is_success()) {
14296: 	return 'failed';
14297:     }      
14298:     if ($reqtype eq 'HEAD') {
14299: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14300:     } elsif ($reqtype eq 'GET') {
14301: 	$$info = $response->content;
14302:     }
14303:     return 'ok';
14304: }
14305: 
14306: sub readfile {
14307:     my $file = shift;
14308:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14309:     my $fh;
14310:     open($fh,"<",$file);
14311:     my $a='';
14312:     while (my $line = <$fh>) { $a .= $line; }
14313:     return $a;
14314: }
14315: 
14316: sub filelocation {
14317:     my ($dir,$file) = @_;
14318:     my $location;
14319:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14320: 
14321:     if ($file =~ m-^/adm/-) {
14322: 	$file=~s-^/adm/wrapper/-/-;
14323: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14324:     }
14325: 
14326:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14327:         $location = $file;
14328:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14329:         my ($udom,$uname,$filename)=
14330:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14331:         my $home=&homeserver($uname,$udom);
14332:         my $is_me=0;
14333:         my @ids=&current_machine_ids();
14334:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14335:         if ($is_me) {
14336:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14337:         } else {
14338:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14339:   	      $udom.'/'.$uname.'/'.$filename;
14340:         }
14341:     } elsif ($file =~ m-^/adm/-) {
14342: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14343:     } else {
14344:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14345:         $file=~s:^/(res|priv)/:/:;
14346:         my $space=$1;
14347:         if ( !( $file =~ m:^/:) ) {
14348:             $location = $dir. '/'.$file;
14349:         } else {
14350:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14351:         }
14352:     }
14353:     $location=~s://+:/:g; # remove duplicate /
14354:     while ($location=~m{/\.\./}) {
14355: 	if ($location =~ m{/[^/]+/\.\./}) {
14356: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14357: 	} else {
14358: 	    $location=~ s{/\.\./}{/}g;
14359: 	}
14360:     } #remove dir/..
14361:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14362:     return $location;
14363: }
14364: 
14365: sub hreflocation {
14366:     my ($dir,$file)=@_;
14367:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14368: 	$file=filelocation($dir,$file);
14369:     } elsif ($file=~m-^/adm/-) {
14370: 	$file=~s-^/adm/wrapper/-/-;
14371: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14372:     }
14373:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14374: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14375:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14376: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14377: 	        {/uploaded/$1/$2/}x;
14378:     }
14379:     if ($file=~ m{^/userfiles/}) {
14380: 	$file =~ s{^/userfiles/}{/uploaded/};
14381:     }
14382:     return $file;
14383: }
14384: 
14385: 
14386: 
14387: 
14388: 
14389: sub current_machine_domains {
14390:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14391: }
14392: 
14393: sub machine_domains {
14394:     my ($hostname) = @_;
14395:     my @domains;
14396:     my %hostname = &all_hostnames();
14397:     while( my($id, $name) = each(%hostname)) {
14398: #	&logthis("-$id-$name-$hostname-");
14399: 	if ($hostname eq $name) {
14400: 	    push(@domains,&host_domain($id));
14401: 	}
14402:     }
14403:     return @domains;
14404: }
14405: 
14406: sub current_machine_ids {
14407:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14408: }
14409: 
14410: sub machine_ids {
14411:     my ($hostname) = @_;
14412:     $hostname ||= &hostname($perlvar{'lonHostID'});
14413:     my @ids;
14414:     my %name_to_host = &all_names();
14415:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14416: 	return @{ $name_to_host{$hostname} };
14417:     }
14418:     return;
14419: }
14420: 
14421: sub additional_machine_domains {
14422:     my @domains;
14423:     if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14424:         if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14425:             while (my $line = <$fh>) {
14426:                 chomp($line);           
14427:                 $line =~ s/\s//g;
14428:                 push(@domains,$line);
14429:             }
14430:             close($fh);
14431:         }
14432:     }
14433:     return @domains;
14434: }
14435: 
14436: sub default_login_domain {
14437:     my $domain = $perlvar{'lonDefDomain'};
14438:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14439:     foreach my $posdom (&current_machine_domains(),
14440:                         &additional_machine_domains()) {
14441:         if (lc($posdom) eq lc($testdomain)) {
14442:             $domain=$posdom;
14443:             last;
14444:         }
14445:     }
14446:     return $domain;
14447: }
14448: 
14449: sub shared_institution {
14450:     my ($dom,$lonhost) = @_;
14451:     if ($lonhost eq '') {
14452:         $lonhost = $perlvar{'lonHostID'};
14453:     }
14454:     my $same_intdom;
14455:     my $hostintdom = &internet_dom($lonhost);
14456:     if ($hostintdom ne '') {
14457:         my %iphost = &get_iphost();
14458:         my $primary_id = &domain($dom,'primary');
14459:         my $primary_ip = &get_host_ip($primary_id);
14460:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14461:             foreach my $id (@{$iphost{$primary_ip}}) {
14462:                 my $intdom = &internet_dom($id);
14463:                 if ($intdom eq $hostintdom) {
14464:                     $same_intdom = 1;
14465:                     last;
14466:                 }
14467:             }
14468:         }
14469:     }
14470:     return $same_intdom;
14471: }
14472: 
14473: sub uses_sts {
14474:     my ($ignore_cache) = @_;
14475:     my $lonhost = $perlvar{'lonHostID'};
14476:     my $hostname = &hostname($lonhost);
14477:     my $sts_on;
14478:     if ($protocol{$lonhost} eq 'https') {
14479:         my $cachetime = 12*3600;
14480:         if (!$ignore_cache) {
14481:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14482:             if (defined($cached)) {
14483:                 return $sts_on;
14484:             }
14485:         }
14486:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14487:         my $request=new HTTP::Request('HEAD',$url);
14488:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14489:         if ($response->is_success) {
14490:             my $has_sts = $response->header('Strict-Transport-Security');
14491:             if ($has_sts eq '') {
14492:                 $sts_on = 0;
14493:             } else {
14494:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14495:                     my $maxage = $1;
14496:                     if ($maxage) {
14497:                         $sts_on = 1;
14498:                     } else {
14499:                         $sts_on = 0;
14500:                     }
14501:                 } else {
14502:                     $sts_on = 0;
14503:                 }
14504:             }
14505:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14506:         }
14507:     }
14508:     return;
14509: }
14510: 
14511: sub waf_allssl {
14512:     my ($host_name) = @_;
14513:     my $alias = &get_proxy_alias();
14514:     if ($host_name eq '') {
14515:         $host_name = $ENV{'SERVER_NAME'};
14516:     }
14517:     if (($host_name ne '') && ($alias eq $host_name)) {
14518:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14519:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
14520:         if ($defdomdefaults{'waf_sslopt'}) {
14521:             return $defdomdefaults{'waf_sslopt'};
14522:         }
14523:     }
14524:     return;
14525: }
14526: 
14527: sub get_requestor_ip {
14528:     my ($r,$nolookup,$noproxy) = @_;
14529:     my $from_ip;
14530:     if (ref($r)) {
14531:         if ($r->can('useragent_ip')) {
14532:             if ($noproxy && $r->can('client_ip')) {
14533:                 $from_ip = $r->client_ip();
14534:             } else {
14535:                 $from_ip = $r->useragent_ip();
14536:             }
14537:         } elsif ($r->connection->can('remote_ip')) {
14538:             $from_ip = $r->connection->remote_ip();
14539:         } else {
14540:             $from_ip = $r->get_remote_host($nolookup);
14541:         }
14542:     } else {
14543:         $from_ip = $ENV{'REMOTE_ADDR'};
14544:     }
14545:     return $from_ip if ($noproxy); 
14546:     # Who controls proxy settings for server
14547:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
14548:     my $proxyinfo = &get_proxy_settings($dom_in_use);
14549:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
14550:         if ($proxyinfo->{'vpnint'}) {
14551:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
14552:                 return $from_ip;
14553:             }
14554:         }
14555:         if ($proxyinfo->{'trusted'}) {
14556:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
14557:                 my $ipheader = $proxyinfo->{'ipheader'};
14558:                 my ($ip,$xfor);
14559:                 if (ref($r)) {
14560:                     if ($ipheader) {
14561:                         $ip = $r->headers_in->{$ipheader};
14562:                     }
14563:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
14564:                 } else {
14565:                     if ($ipheader) {
14566:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
14567:                     }
14568:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
14569:                 }
14570:                 if (($ip eq '') && ($xfor ne '')) {
14571:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
14572:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
14573:                             $ip = $poss_ip;
14574:                             last;
14575:                         }
14576:                     }
14577:                 }
14578:                 if ($ip ne '') {
14579:                     return $ip;
14580:                 }
14581:             }
14582:         }
14583:     }
14584:     return $from_ip;
14585: }
14586: 
14587: sub get_proxy_settings {
14588:     my ($dom_in_use) = @_;
14589:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom_in_use);
14590:     my $proxyinfo = {
14591:                        ipheader => $domdefaults{'waf_ipheader'},
14592:                        trusted  => $domdefaults{'waf_trusted'},
14593:                        vpnint   => $domdefaults{'waf_vpnint'},
14594:                        vpnext   => $domdefaults{'waf_vpnext'},
14595:                        sslopt   => $domdefaults{'waf_sslopt'},
14596:                     };
14597:     return $proxyinfo;
14598: }
14599: 
14600: sub ip_match {
14601:     my ($ip,$pattern_str) = @_;
14602:     $ip=Net::CIDR::cidrvalidate($ip);
14603:     if ($ip) {
14604:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
14605:     }
14606:     return;
14607: }
14608: 
14609: sub get_proxy_alias {
14610:     my ($lonid) = @_;
14611:     if ($lonid eq '') {
14612:         $lonid = $perlvar{'lonHostID'};
14613:     }
14614:     if (!defined(&hostname($lonid))) {
14615:         return;
14616:     }
14617:     if ($lonid ne '') {
14618:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
14619:         if ($cached) {
14620:             return $alias;
14621:         }
14622:         my $dom = &Apache::lonnet::host_domain($lonid);
14623:         if ($dom ne '') {
14624:             my $cachetime = 60*60*24;
14625:             my %domconfig =
14626:                 &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14627:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14628:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
14629:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
14630:                 }
14631:             }
14632:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
14633:         }
14634:     }
14635:     return;
14636: }
14637: 
14638: sub use_proxy_alias {
14639:     my ($r,$lonid) = @_;
14640:     my $alias = &get_proxy_alias($lonid);
14641:     if ($alias) {
14642:         my $dom = &host_domain($lonid);
14643:         if ($dom ne '') {
14644:             my $proxyinfo = &get_proxy_settings($dom);
14645:             my ($vpnint,$remote_ip);
14646:             if (ref($proxyinfo) eq 'HASH') {
14647:                 $vpnint = $proxyinfo->{'vpnint'};
14648:                 if ($vpnint) {
14649:                     $remote_ip = &get_requestor_ip($r,1,1);
14650:                 }
14651:             }
14652:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
14653:                 return $alias;
14654:             }
14655:         }
14656:     }
14657:     return;
14658: }
14659: 
14660: sub alias_sso {
14661:     my ($lonid) = @_;
14662:     if ($lonid eq '') {
14663:         $lonid = $perlvar{'lonHostID'};
14664:     }
14665:     if (!defined(&hostname($lonid))) {
14666:         return;
14667:     }
14668:     if ($lonid ne '') {
14669:         my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
14670:         if ($cached) {
14671:             return $use_alias;
14672:         }
14673:         my $dom = &Apache::lonnet::host_domain($lonid);
14674:         if ($dom ne '') {
14675:             my $cachetime = 60*60*24;
14676:             my %domconfig =
14677:                 &Apache::lonnet::get_dom('configuration',['wafproxy'],$dom);
14678:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
14679:                 if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
14680:                     $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
14681:                 }
14682:             }
14683:             return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
14684:         }
14685:     }
14686:     return;
14687: }
14688: 
14689: sub get_saml_landing {
14690:     my ($lonid) = @_;
14691:     if ($lonid eq '') {
14692:         my $defdom = &default_login_domain();
14693:         my @hosts = &current_machine_ids();
14694:         if (@hosts > 1) {
14695:             foreach my $hostid (@hosts) {
14696:                 if (&host_domain($hostid) eq $defdom) {
14697:                     $lonid = $hostid;
14698:                     last;
14699:                 }
14700:             }
14701:         } else {
14702:             $lonid = $perlvar{'lonHostID'};
14703:         }
14704:         if ($lonid) {
14705:             unless (&Apache::lonnet::host_domain($lonid) eq $defdom) {
14706:                 return;
14707:             }
14708:         } else {
14709:             return;
14710:         }
14711:     } elsif (!defined(&hostname($lonid))) {
14712:         return;
14713:     }
14714:     my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
14715:     if ($cached) {
14716:         return $landing;
14717:     }
14718:     my $dom = &Apache::lonnet::host_domain($lonid);
14719:     if ($dom ne '') {
14720:         my $cachetime = 60*60*24;
14721:         my %domconfig =
14722:             &Apache::lonnet::get_dom('configuration',['login'],$dom);
14723:         if (ref($domconfig{'login'}) eq 'HASH') {
14724:             if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
14725:                 if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
14726:                     $landing = 1;
14727:                 }
14728:             }
14729:         }
14730:         return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
14731:     }
14732:     return;
14733: }
14734: 
14735: # ------------------------------------------------------------- Declutters URLs
14736: 
14737: sub declutter {
14738:     my $thisfn=shift;
14739:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14740:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14741:         $thisfn=~s{^/home/httpd/html}{};
14742:     }
14743:     $thisfn=~s/^\///;
14744:     $thisfn=~s|^adm/wrapper/||;
14745:     $thisfn=~s|^adm/coursedocs/showdoc/||;
14746:     $thisfn=~s/^res\///;
14747:     $thisfn=~s/^priv\///;
14748:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14749:         $thisfn=~s/\?.+$//;
14750:     }
14751:     return $thisfn;
14752: }
14753: 
14754: # ------------------------------------------------------------- Clutter up URLs
14755: 
14756: sub clutter {
14757:     my $thisfn='/'.&declutter(shift);
14758:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14759: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14760:        $thisfn='/res'.$thisfn; 
14761:     }
14762:     if ($thisfn !~m|^/adm|) {
14763: 	if ($thisfn =~ m|^/ext/|) {
14764: 	    $thisfn='/adm/wrapper'.$thisfn;
14765: 	} else {
14766: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14767: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14768: 	    if ($embstyle eq 'ssi'
14769: 		|| ($embstyle eq 'hdn')
14770: 		|| ($embstyle eq 'rat')
14771: 		|| ($embstyle eq 'prv')
14772: 		|| ($embstyle eq 'ign')) {
14773: 		#do nothing with these
14774: 	    } elsif (($embstyle eq 'img') 
14775: 		|| ($embstyle eq 'emb')
14776: 		|| ($embstyle eq 'wrp')) {
14777: 		$thisfn='/adm/wrapper'.$thisfn;
14778: 	    } elsif ($embstyle eq 'unk'
14779: 		     && $thisfn!~/\.(sequence|page)$/) {
14780: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14781: 	    } else {
14782: #		&logthis("Got a blank emb style");
14783: 	    }
14784: 	}
14785:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14786:         $thisfn='/adm/wrapper'.$thisfn;
14787:     }
14788:     return $thisfn;
14789: }
14790: 
14791: sub clutter_with_no_wrapper {
14792:     my $uri = &clutter(shift);
14793:     if ($uri =~ m-^/adm/-) {
14794: 	$uri =~ s-^/adm/wrapper/-/-;
14795: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14796:     }
14797:     return $uri;
14798: }
14799: 
14800: sub freeze_escape {
14801:     my ($value)=@_;
14802:     if (ref($value)) {
14803: 	$value=&nfreeze($value);
14804: 	return '__FROZEN__'.&escape($value);
14805:     }
14806:     return &escape($value);
14807: }
14808: 
14809: 
14810: sub thaw_unescape {
14811:     my ($value)=@_;
14812:     if ($value =~ /^__FROZEN__/) {
14813: 	substr($value,0,10,undef);
14814: 	$value=&unescape($value);
14815: 	return &thaw($value);
14816:     }
14817:     return &unescape($value);
14818: }
14819: 
14820: sub correct_line_ends {
14821:     my ($result)=@_;
14822:     $$result =~s/\r\n/\n/mg;
14823:     $$result =~s/\r/\n/mg;
14824: }
14825: # ================================================================ Main Program
14826: 
14827: sub goodbye {
14828:    &logthis("Starting Shut down");
14829: #not converted to using infrastruture and probably shouldn't be
14830:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14831: #converted
14832: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14833:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14834: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14835: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14836: #1.1 only
14837: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14838: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14839: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14840: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14841:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14842:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14843:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14844:    &flushcourselogs();
14845:    &logthis("Shutting down");
14846: }
14847: 
14848: sub get_dns {
14849:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14850:     if (!$ignore_cache) {
14851: 	my ($content,$cached)=
14852: 	    &Apache::lonnet::is_cached_new('dns',$url);
14853: 	if ($cached) {
14854: 	    &$func($content,$hashref);
14855: 	    return;
14856: 	}
14857:     }
14858: 
14859:     my %alldns;
14860:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14861:         foreach my $dns (<$config>) {
14862: 	    next if ($dns !~ /^\^(\S*)/x);
14863:             my $line = $1;
14864:             my ($host,$protocol) = split(/:/,$line);
14865:             if ($protocol ne 'https') {
14866:                 $protocol = 'http';
14867:             }
14868: 	    $alldns{$host} = $protocol;
14869:         }
14870:         close($config);
14871:     }
14872:     while (%alldns) {
14873: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14874:         my ($contents,@content);
14875:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
14876:             my $command = (split('/',$url))[3];
14877:             my ($dir,$file) = &parse_getdns_url($command,$url);
14878:             delete($alldns{$dns});
14879:             next if (($dir eq '') || ($file eq ''));
14880:             if (open(my $config,'<',"$dir/$file")) {
14881:                 @content = <$config>;
14882:                 close($config);
14883:             }
14884:             if ($url eq '/adm/dns/loncapaCRL') {
14885:                 $contents = join('',@content);
14886:             }
14887:         } else {
14888: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14889:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14890:             delete($alldns{$dns});
14891: 	    next if ($response->is_error());
14892:             if ($url eq '/adm/dns/loncapaCRL') {
14893:                 $contents = $response->content;
14894:             } else {
14895:                 @content = split("\n",$response->content);
14896:             }
14897:         }
14898:         if ($url eq '/adm/dns/loncapaCRL') {
14899:             return &$func($contents);
14900:         } else {
14901: 	    unless ($nocache) {
14902: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14903: 	    }
14904: 	    &$func(\@content,$hashref);
14905:             return;
14906:         }
14907:     }
14908:     my $which = (split('/',$url,4))[3];
14909:     if ($which eq 'loncapaCRL') {
14910:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14911:         if (-e $diskfile) {
14912:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14913:         } else {
14914:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14915:         }
14916:     } else {
14917:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14918:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14919:             my @content = <$config>;
14920:             close($config);
14921:             &$func(\@content,$hashref);
14922:         }
14923:     }
14924:     return;
14925: }
14926: 
14927: # ------------------------------------------------------Get DNS checksums file
14928: sub parse_dns_checksums_tab {
14929:     my ($lines,$hashref) = @_;
14930:     my $lonhost = $perlvar{'lonHostID'};
14931:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14932:     my $loncaparev = &get_server_loncaparev($machine_dom);
14933:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14934:     my $webconfdir = '/etc/httpd/conf';
14935:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14936:         $webconfdir = '/etc/apache2';
14937:     } elsif ($distro =~ /^sles(\d+)$/) {
14938:         if ($1 >= 10) {
14939:             $webconfdir = '/etc/apache2';
14940:         }
14941:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14942:         if ($1 >= 10.0) {
14943:             $webconfdir = '/etc/apache2';
14944:         }
14945:     }
14946:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14947:     my (%chksum,%revnum);
14948:     if (ref($lines) eq 'ARRAY') {
14949:         chomp(@{$lines});
14950:         my $version = shift(@{$lines});
14951:         if ($version eq $release) {  
14952:             foreach my $line (@{$lines}) {
14953:                 my ($file,$version,$shasum) = split(/,/,$line);
14954:                 if ($file =~ m{^/etc/httpd/conf}) {
14955:                     if ($webconfdir eq '/etc/apache2') {
14956:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14957:                     }
14958:                 }
14959:                 $chksum{$file} = $shasum;
14960:                 $revnum{$file} = $version;
14961:             }
14962:             if (ref($hashref) eq 'HASH') {
14963:                 %{$hashref} = (
14964:                                 sums     => \%chksum,
14965:                                 versions => \%revnum,
14966:                               );
14967:             }
14968:         }
14969:     }
14970:     return;
14971: }
14972: 
14973: sub fetch_dns_checksums {
14974:     my %checksums;
14975:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14976:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14977:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14978:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14979:              \%checksums);
14980:     return \%checksums;
14981: }
14982: 
14983: sub fetch_crl_pemfile {
14984:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14985: }
14986: 
14987: sub save_crl_pem {
14988:     my ($content) = @_;
14989:     my ($msg,$hadchanges);
14990:     if ($content ne '') {
14991:         my $now = time;
14992:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14993:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14994:         if (open(my $fh,'>',"$tmpcrl")) {
14995:             print $fh $content;
14996:             close($fh);
14997:             if (-e $lonca) {
14998:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14999:                     my $check = <PIPE>;
15000:                     close(PIPE);
15001:                     chomp($check);
15002:                     if ($check eq 'verify OK') {
15003:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
15004:                         my $backup;
15005:                         if (-e $dest) {
15006:                             if (&File::Copy::move($dest,"$dest.bak")) {
15007:                                 $backup = 'ok';
15008:                             }
15009:                         }
15010:                         if (&File::Copy::move($tmpcrl,$dest)) {
15011:                             $msg = 'ok';
15012:                             if ($backup) {
15013:                                 my (%oldnums,%newnums);
15014:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
15015:                                     while (<PIPE>) {
15016:                                         $oldnums{(split(/:/))[1]} = 1;
15017:                                     }
15018:                                     close(PIPE);
15019:                                 }
15020:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
15021:                                     while(<PIPE>) {
15022:                                         $newnums{(split(/:/))[1]} = 1;
15023:                                     }
15024:                                     close(PIPE);
15025:                                 }
15026:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
15027:                                     unless (exists($oldnums{$key})) {
15028:                                         $hadchanges = 1;
15029:                                         last;
15030:                                     }
15031:                                 }
15032:                                 unless ($hadchanges) {
15033:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
15034:                                         unless (exists($newnums{$key})) {
15035:                                             $hadchanges = 1;
15036:                                             last;
15037:                                         }
15038:                                     }
15039:                                 }
15040:                             }
15041:                         }
15042:                     } else {
15043:                         unlink($tmpcrl);
15044:                     }
15045:                 } else {
15046:                     unlink($tmpcrl);
15047:                 }
15048:             } else {
15049:                 unlink($tmpcrl);
15050:             }
15051:         }
15052:     }
15053:     return ($msg,$hadchanges);
15054: }
15055: 
15056: sub parse_getdns_url {
15057:     my ($command,$url) = @_;
15058:     my $dir = $perlvar{'lonTabDir'};
15059:     my $file;
15060:     if ($command eq 'hosts') {
15061:         $file = 'dns_hosts.tab';
15062:     } elsif ($command eq 'domain') {
15063:         $file = 'dns_domain.tab';
15064:     } elsif ($command eq 'checksums') {
15065:         my $version = (split('/',$url))[4];
15066:         $file = "dns_checksums/$version.tab",
15067:     } elsif ($command eq 'loncapaCRL') {
15068:         $dir = $perlvar{'lonCertificateDirectory'};
15069:         $file = $perlvar{'lonnetCertRevocationList'};
15070:     }
15071:     return ($dir,$file);
15072: }
15073: 
15074: # ------------------------------------------------------------ Read domain file
15075: {
15076:     my $loaded;
15077:     my %domain;
15078: 
15079:     sub parse_domain_tab {
15080: 	my ($lines) = @_;
15081: 	foreach my $line (@$lines) {
15082: 	    next if ($line =~ /^(\#|\s*$ )/x);
15083: 
15084: 	    chomp($line);
15085: 	    my ($name,@elements) = split(/:/,$line,9);
15086: 	    my %this_domain;
15087: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
15088: 			       'lang_def', 'city', 'longi', 'lati',
15089: 			       'primary') {
15090: 		$this_domain{$field} = shift(@elements);
15091: 	    }
15092: 	    $domain{$name} = \%this_domain;
15093: 	}
15094:     }
15095: 
15096:     sub reset_domain_info {
15097: 	undef($loaded);
15098: 	undef(%domain);
15099:     }
15100: 
15101:     sub load_domain_tab {
15102: 	my ($ignore_cache,$nocache) = @_;
15103: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
15104: 	my $fh;
15105: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
15106: 	    my @lines = <$fh>;
15107: 	    &parse_domain_tab(\@lines);
15108: 	}
15109: 	close($fh);
15110: 	$loaded = 1;
15111:     }
15112: 
15113:     sub domain {
15114: 	&load_domain_tab() if (!$loaded);
15115: 
15116: 	my ($name,$what) = @_;
15117: 	return if ( !exists($domain{$name}) );
15118: 
15119: 	if (!$what) {
15120: 	    return $domain{$name}{'description'};
15121: 	}
15122: 	return $domain{$name}{$what};
15123:     }
15124: 
15125:     sub domain_info {
15126:         &load_domain_tab() if (!$loaded);
15127:         return %domain;
15128:     }
15129: 
15130: }
15131: 
15132: 
15133: # ------------------------------------------------------------- Read hosts file
15134: {
15135:     my %hostname;
15136:     my %hostdom;
15137:     my %libserv;
15138:     my $loaded;
15139:     my %name_to_host;
15140:     my %internetdom;
15141:     my %LC_dns_serv;
15142: 
15143:     sub parse_hosts_tab {
15144: 	my ($file) = @_;
15145: 	foreach my $configline (@$file) {
15146: 	    next if ($configline =~ /^(\#|\s*$ )/x);
15147:             chomp($configline);
15148: 	    if ($configline =~ /^\^/) {
15149:                 if ($configline =~ /^\^([\w.\-]+)/) {
15150:                     $LC_dns_serv{$1} = 1;
15151:                 }
15152:                 next;
15153:             }
15154: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
15155: 	    $name=~s/\s//g;
15156: 	    if ($id && $domain && $role && $name) {
15157:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15158:                     my $curr = $hostname{$id};
15159:                     my $skip;
15160:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
15161:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15162:                             $skip = 1;
15163:                         } else {
15164:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15165:                         }
15166:                     }
15167:                     unless ($skip) {
15168:                         push(@{$name_to_host{$name}},$id);
15169:                     }
15170:                 } else {
15171:                     push(@{$name_to_host{$name}},$id);
15172:                 }
15173: 		$hostname{$id}=$name;
15174: 		$hostdom{$id}=$domain;
15175: 		if ($role eq 'library') { $libserv{$id}=$name; }
15176:                 if (defined($protocol)) {
15177:                     if ($protocol eq 'https') {
15178:                         $protocol{$id} = $protocol;
15179:                     } else {
15180:                         $protocol{$id} = 'http'; 
15181:                     }
15182:                 } else {
15183:                     $protocol{$id} = 'http';
15184:                 }
15185:                 if (defined($intdom)) {
15186:                     $internetdom{$id} = $intdom;
15187:                 }
15188: 	    }
15189: 	}
15190:     }
15191:     
15192:     sub reset_hosts_info {
15193: 	&purge_remembered();
15194: 	&reset_domain_info();
15195: 	&reset_hosts_ip_info();
15196:         undef(%internetdom);
15197: 	undef(%name_to_host);
15198: 	undef(%hostname);
15199: 	undef(%hostdom);
15200: 	undef(%libserv);
15201: 	undef($loaded);
15202:     }
15203: 
15204:     sub load_hosts_tab {
15205: 	my ($ignore_cache,$nocache) = @_;
15206: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
15207: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
15208: 	my @config = <$config>;
15209: 	&parse_hosts_tab(\@config);
15210: 	close($config);
15211: 	$loaded=1;
15212:     }
15213: 
15214:     sub hostname {
15215: 	&load_hosts_tab() if (!$loaded);
15216: 
15217: 	my ($lonid) = @_;
15218: 	return $hostname{$lonid};
15219:     }
15220: 
15221:     sub all_hostnames {
15222: 	&load_hosts_tab() if (!$loaded);
15223: 
15224: 	return %hostname;
15225:     }
15226: 
15227:     sub all_names {
15228:         my ($ignore_cache,$nocache) = @_;
15229: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
15230: 
15231: 	return %name_to_host;
15232:     }
15233: 
15234:     sub all_host_domain {
15235:         &load_hosts_tab() if (!$loaded);
15236:         return %hostdom;
15237:     }
15238: 
15239:     sub all_host_intdom {
15240:         &load_hosts_tab() if (!$loaded);
15241:         return %internetdom;
15242:     }
15243: 
15244:     sub is_library {
15245: 	&load_hosts_tab() if (!$loaded);
15246: 
15247: 	return exists($libserv{$_[0]});
15248:     }
15249: 
15250:     sub all_library {
15251: 	&load_hosts_tab() if (!$loaded);
15252: 
15253: 	return %libserv;
15254:     }
15255: 
15256:     sub unique_library {
15257: 	#2x reverse removes all hostnames that appear more than once
15258:         my %unique = reverse &all_library();
15259:         return reverse %unique;
15260:     }
15261: 
15262:     sub get_servers {
15263: 	&load_hosts_tab() if (!$loaded);
15264: 
15265: 	my ($domain,$type) = @_;
15266: 	my %possible_hosts = ($type eq 'library') ? %libserv
15267: 	                                          : %hostname;
15268: 	my %result;
15269: 	if (ref($domain) eq 'ARRAY') {
15270: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15271: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
15272: 		    $result{$host} = $hostname;
15273: 		}
15274: 	    }
15275: 	} else {
15276: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15277: 		if ($hostdom{$host} eq $domain) {
15278: 		    $result{$host} = $hostname;
15279: 		}
15280: 	    }
15281: 	}
15282: 	return %result;
15283:     }
15284: 
15285:     sub get_unique_servers {
15286:         my %unique = reverse &get_servers(@_);
15287: 	return reverse %unique;
15288:     }
15289: 
15290:     sub host_domain {
15291: 	&load_hosts_tab() if (!$loaded);
15292: 
15293: 	my ($lonid) = @_;
15294: 	return $hostdom{$lonid};
15295:     }
15296: 
15297:     sub all_domains {
15298: 	&load_hosts_tab() if (!$loaded);
15299: 
15300: 	my %seen;
15301: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
15302: 	return @uniq;
15303:     }
15304: 
15305:     sub internet_dom {
15306:         &load_hosts_tab() if (!$loaded);
15307: 
15308:         my ($lonid) = @_;
15309:         return $internetdom{$lonid};
15310:     }
15311: 
15312:     sub is_LC_dns {
15313:         &load_hosts_tab() if (!$loaded);
15314: 
15315:         my ($hostname) = @_;
15316:         return exists($LC_dns_serv{$hostname});
15317:     }
15318: 
15319: }
15320: 
15321: { 
15322:     my %iphost;
15323:     my %name_to_ip;
15324:     my %lonid_to_ip;
15325: 
15326:     sub get_hosts_from_ip {
15327: 	my ($ip) = @_;
15328: 	my %iphosts = &get_iphost();
15329: 	if (ref($iphosts{$ip})) {
15330: 	    return @{$iphosts{$ip}};
15331: 	}
15332: 	return;
15333:     }
15334:     
15335:     sub reset_hosts_ip_info {
15336: 	undef(%iphost);
15337: 	undef(%name_to_ip);
15338: 	undef(%lonid_to_ip);
15339:     }
15340: 
15341:     sub get_host_ip {
15342: 	my ($lonid) = @_;
15343: 	if (exists($lonid_to_ip{$lonid})) {
15344: 	    return $lonid_to_ip{$lonid};
15345: 	}
15346: 	my $name=&hostname($lonid);
15347:    	my $ip = gethostbyname($name);
15348: 	return if (!$ip || length($ip) ne 4);
15349: 	$ip=inet_ntoa($ip);
15350: 	$name_to_ip{$name}   = $ip;
15351: 	$lonid_to_ip{$lonid} = $ip;
15352: 	return $ip;
15353:     }
15354:     
15355:     sub get_iphost {
15356: 	my ($ignore_cache,$nocache) = @_;
15357: 
15358: 	if (!$ignore_cache) {
15359: 	    if (%iphost) {
15360: 		return %iphost;
15361: 	    }
15362: 	    my ($ip_info,$cached)=
15363: 		&Apache::lonnet::is_cached_new('iphost','iphost');
15364: 	    if ($cached) {
15365: 		%iphost      = %{$ip_info->[0]};
15366: 		%name_to_ip  = %{$ip_info->[1]};
15367: 		%lonid_to_ip = %{$ip_info->[2]};
15368: 		return %iphost;
15369: 	    }
15370: 	}
15371: 
15372: 	# get yesterday's info for fallback
15373: 	my %old_name_to_ip;
15374: 	my ($ip_info,$cached)=
15375: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
15376: 	if ($cached) {
15377: 	    %old_name_to_ip = %{$ip_info->[1]};
15378: 	}
15379: 
15380: 	my %name_to_host = &all_names($ignore_cache,$nocache);
15381: 	foreach my $name (keys(%name_to_host)) {
15382: 	    my $ip;
15383: 	    if (!exists($name_to_ip{$name})) {
15384: 		$ip = gethostbyname($name);
15385: 		if (!$ip || length($ip) ne 4) {
15386: 		    if (defined($old_name_to_ip{$name})) {
15387: 			$ip = $old_name_to_ip{$name};
15388: 			&logthis("Can't find $name defaulting to old $ip");
15389: 		    } else {
15390: 			&logthis("Name $name no IP found");
15391: 			next;
15392: 		    }
15393: 		} else {
15394: 		    $ip=inet_ntoa($ip);
15395: 		}
15396: 		$name_to_ip{$name} = $ip;
15397: 	    } else {
15398: 		$ip = $name_to_ip{$name};
15399: 	    }
15400: 	    foreach my $id (@{ $name_to_host{$name} }) {
15401: 		$lonid_to_ip{$id} = $ip;
15402: 	    }
15403: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
15404: 	}
15405:         unless ($nocache) {
15406: 	    &do_cache_new('iphost','iphost',
15407: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
15408: 		          48*60*60);
15409:         }
15410: 
15411: 	return %iphost;
15412:     }
15413: 
15414:     #
15415:     #  Given a DNS returns the loncapa host name for that DNS 
15416:     # 
15417:     sub host_from_dns {
15418:         my ($dns) = @_;
15419:         my @hosts;
15420:         my $ip;
15421: 
15422:         if (exists($name_to_ip{$dns})) {
15423:             $ip = $name_to_ip{$dns};
15424:         }
15425:         if (!$ip) {
15426:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15427:             if (length($ip) == 4) { 
15428: 	        $ip   = &IO::Socket::inet_ntoa($ip);
15429:             }
15430:         }
15431:         if ($ip) {
15432: 	    @hosts = get_hosts_from_ip($ip);
15433: 	    return $hosts[0];
15434:         }
15435:         return undef;
15436:     }
15437: 
15438:     sub get_internet_names {
15439:         my ($lonid) = @_;
15440:         return if ($lonid eq '');
15441:         my ($idnref,$cached)=
15442:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
15443:         if ($cached) {
15444:             return $idnref;
15445:         }
15446:         my $ip = &get_host_ip($lonid);
15447:         my @hosts = &get_hosts_from_ip($ip);
15448:         my %iphost = &get_iphost();
15449:         my (@idns,%seen);
15450:         foreach my $id (@hosts) {
15451:             my $dom = &host_domain($id);
15452:             my $prim_id = &domain($dom,'primary');
15453:             my $prim_ip = &get_host_ip($prim_id);
15454:             next if ($seen{$prim_ip});
15455:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15456:                 foreach my $id (@{$iphost{$prim_ip}}) {
15457:                     my $intdom = &internet_dom($id);
15458:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
15459:                         push(@idns,$intdom);
15460:                     }
15461:                 }
15462:             }
15463:             $seen{$prim_ip} = 1;
15464:         }
15465:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
15466:     }
15467: 
15468: }
15469: 
15470: sub all_loncaparevs {
15471:     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);
15472: }
15473: 
15474: # ---------------------------------------------------------- Read loncaparev table
15475: {
15476:     sub load_loncaparevs { 
15477:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
15478:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
15479:                 while (my $configline=<$config>) {
15480:                     chomp($configline);
15481:                     my ($hostid,$loncaparev)=split(/:/,$configline);
15482:                     $loncaparevs{$hostid}=$loncaparev;
15483:                 }
15484:                 close($config);
15485:             }
15486:         }
15487:     }
15488: }
15489: 
15490: # ---------------------------------------------------------- Read serverhostID table
15491: {
15492:     sub load_serverhomeIDs {
15493:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
15494:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
15495:                 while (my $configline=<$config>) {
15496:                     chomp($configline);
15497:                     my ($name,$id)=split(/:/,$configline);
15498:                     $serverhomeIDs{$name}=$id;
15499:                 }
15500:                 close($config);
15501:             }
15502:         }
15503:     }
15504: }
15505: 
15506: 
15507: BEGIN {
15508: 
15509: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15510:     unless ($readit) {
15511: {
15512:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15513:     %perlvar = (%perlvar,%{$configvars});
15514: }
15515: 
15516: 
15517: # ------------------------------------------------------ Read spare server file
15518: {
15519:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
15520: 
15521:     while (my $configline=<$config>) {
15522:        chomp($configline);
15523:        if ($configline) {
15524: 	   my ($host,$type) = split(':',$configline,2);
15525: 	   if (!defined($type) || $type eq '') { $type = 'default' };
15526: 	   push(@{ $spareid{$type} }, $host);
15527:        }
15528:     }
15529:     close($config);
15530: }
15531: # ------------------------------------------------------------ Read permissions
15532: {
15533:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
15534: 
15535:     while (my $configline=<$config>) {
15536: 	chomp($configline);
15537: 	if ($configline) {
15538: 	    my ($role,$perm)=split(/ /,$configline);
15539: 	    if ($perm ne '') { $pr{$role}=$perm; }
15540: 	}
15541:     }
15542:     close($config);
15543: }
15544: 
15545: # -------------------------------------------- Read plain texts for permissions
15546: {
15547:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
15548: 
15549:     while (my $configline=<$config>) {
15550: 	chomp($configline);
15551: 	if ($configline) {
15552: 	    my ($short,@plain)=split(/:/,$configline);
15553:             %{$prp{$short}} = ();
15554: 	    if (@plain > 0) {
15555:                 $prp{$short}{'std'} = $plain[0];
15556:                 for (my $i=1; $i<@plain; $i++) {
15557:                     $prp{$short}{'alt'.$i} = $plain[$i];  
15558:                 }
15559:             }
15560: 	}
15561:     }
15562:     close($config);
15563: }
15564: 
15565: # ---------------------------------------------------------- Read package table
15566: {
15567:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
15568: 
15569:     while (my $configline=<$config>) {
15570: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
15571: 	chomp($configline);
15572: 	my ($short,$plain)=split(/:/,$configline);
15573: 	my ($pack,$name)=split(/\&/,$short);
15574: 	if ($plain ne '') {
15575: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
15576: 	    $packagetab{$short}=$plain; 
15577: 	}
15578:     }
15579:     close($config);
15580: }
15581: 
15582: # ---------------------------------------------------------- Read loncaparev table
15583: 
15584: &load_loncaparevs();
15585: 
15586: # ---------------------------------------------------------- Read serverhostID table
15587: 
15588: &load_serverhomeIDs();
15589: 
15590: # ---------------------------------------------------------- Read releaseslist XML
15591: {
15592:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15593:     if (-e $file) {
15594:         my $parser = HTML::LCParser->new($file);
15595:         while (my $token = $parser->get_token()) {
15596:             if ($token->[0] eq 'S') {
15597:                 my $item = $token->[1];
15598:                 my $name = $token->[2]{'name'};
15599:                 my $value = $token->[2]{'value'};
15600:                 my $valuematch = $token->[2]{'valuematch'};
15601:                 my $namematch = $token->[2]{'namematch'};
15602:                 if ($item eq 'parameter') {
15603:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
15604:                         my $release = $parser->get_text();
15605:                         $release =~ s/(^\s*|\s*$ )//gx;
15606:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
15607:                     }
15608:                 } elsif ($item ne '' && $name ne '') {
15609:                     my $release = $parser->get_text();
15610:                     $release =~ s/(^\s*|\s*$ )//gx;
15611:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
15612:                 }
15613:             }
15614:         }
15615:     }
15616: }
15617: 
15618: # ---------------------------------------------------------- Read managers table
15619: {
15620:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
15621:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
15622:             while (my $configline=<$config>) {
15623:                 chomp($configline);
15624:                 next if ($configline =~ /^\#/);
15625:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15626:                     $managerstab{$configline} = 1;
15627:                 }
15628:             }
15629:             close($config);
15630:         }
15631:     }
15632: }
15633: 
15634: # ------------- set up temporary directory
15635: {
15636:     $tmpdir = LONCAPA::tempdir();
15637: 
15638: }
15639: 
15640: # ------------- set default texengine (domain default overrides this)
15641: {
15642:     $deftex = LONCAPA::texengine();
15643: }
15644: 
15645: # ------------- set default minimum length for passwords for internal auth users
15646: {
15647:     $passwdmin = LONCAPA::passwd_min();
15648: }
15649: 
15650: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
15651: 				'compress_threshold'=> 20_000,
15652:  			        });
15653: 
15654: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
15655: $dumpcount=0;
15656: $locknum=0;
15657: 
15658: &logtouch();
15659: &logthis('<font color="yellow">INFO: Read configuration</font>');
15660: $readit=1;
15661:     {
15662: 	use integer;
15663: 	my $test=(2**32)+1;
15664: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
15665: 	&logthis(" Detected 64bit platform ($_64bit)");
15666:     }
15667: }
15668: }
15669: 
15670: 1;
15671: __END__
15672: 
15673: =pod
15674: 
15675: =head1 NAME
15676: 
15677: Apache::lonnet - Subroutines to ask questions about things in the network.
15678: 
15679: =head1 SYNOPSIS
15680: 
15681: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
15682: 
15683:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15684: 
15685: Common parameters:
15686: 
15687: =over 4
15688: 
15689: =item *
15690: 
15691: $uname : an internal username (if $cname expecting a course Id specifically)
15692: 
15693: =item *
15694: 
15695: $udom : a domain (if $cdom expecting a course's domain specifically)
15696: 
15697: =item *
15698: 
15699: $symb : a resource instance identifier
15700: 
15701: =item *
15702: 
15703: $namespace : the name of a .db file that contains the data needed or
15704: being set.
15705: 
15706: =back
15707: 
15708: =head1 OVERVIEW
15709: 
15710: lonnet provides subroutines which interact with the
15711: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15712: about classes, users, and resources.
15713: 
15714: For many of these objects you can also use this to store data about
15715: them or modify them in various ways.
15716: 
15717: =head2 Symbs
15718: 
15719: To identify a specific instance of a resource, LON-CAPA uses symbols
15720: or "symbs"X<symb>. These identifiers are built from the URL of the
15721: map, the resource number of the resource in the map, and the URL of
15722: the resource itself. The latter is somewhat redundant, but might help
15723: if maps change.
15724: 
15725: An example is
15726: 
15727:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15728: 
15729: The respective map entry is
15730: 
15731:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
15732:   title="Problem 2">
15733:  </resource>
15734: 
15735: Symbs are used by the random number generator, as well as to store and
15736: restore data specific to a certain instance of for example a problem.
15737: 
15738: =head2 Storing And Retrieving Data
15739: 
15740: X<store()>X<cstore()>X<restore()>Three of the most important functions
15741: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15742: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15743: is is the non-critical message twin of cstore. These functions are for
15744: handlers to store a perl hash to a user's permanent data space in an
15745: easy manner, and to retrieve it again on another call. It is expected
15746: that a handler would use this once at the beginning to retrieve data,
15747: and then again once at the end to send only the new data back.
15748: 
15749: The data is stored in the user's data directory on the user's
15750: homeserver under the ID of the course.
15751: 
15752: The hash that is returned by restore will have all of the previous
15753: value for all of the elements of the hash.
15754: 
15755: Example:
15756: 
15757:  #creating a hash
15758:  my %hash;
15759:  $hash{'foo'}='bar';
15760: 
15761:  #storing it
15762:  &Apache::lonnet::cstore(\%hash);
15763: 
15764:  #changing a value
15765:  $hash{'foo'}='notbar';
15766: 
15767:  #adding a new value
15768:  $hash{'bar'}='foo';
15769:  &Apache::lonnet::cstore(\%hash);
15770: 
15771:  #retrieving the hash
15772:  my %history=&Apache::lonnet::restore();
15773: 
15774:  #print the hash
15775:  foreach my $key (sort(keys(%history))) {
15776:    print("\%history{$key} = $history{$key}");
15777:  }
15778: 
15779: Will print out:
15780: 
15781:  %history{1:foo} = bar
15782:  %history{1:keys} = foo:timestamp
15783:  %history{1:timestamp} = 990455579
15784:  %history{2:bar} = foo
15785:  %history{2:foo} = notbar
15786:  %history{2:keys} = foo:bar:timestamp
15787:  %history{2:timestamp} = 990455580
15788:  %history{bar} = foo
15789:  %history{foo} = notbar
15790:  %history{timestamp} = 990455580
15791:  %history{version} = 2
15792: 
15793: Note that the special hash entries C<keys>, C<version> and
15794: C<timestamp> were added to the hash. C<version> will be equal to the
15795: total number of versions of the data that have been stored. The
15796: C<timestamp> attribute will be the UNIX time the hash was
15797: stored. C<keys> is available in every historical section to list which
15798: keys were added or changed at a specific historical revision of a
15799: hash.
15800: 
15801: B<Warning>: do not store the hash that restore returns directly. This
15802: will cause a mess since it will restore the historical keys as if the
15803: were new keys. I.E. 1:foo will become 1:1:foo etc.
15804: 
15805: Calling convention:
15806: 
15807:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15808:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15809: 
15810: For more detailed information, see lonnet specific documentation.
15811: 
15812: =head1 RETURN MESSAGES
15813: 
15814: =over 4
15815: 
15816: =item * B<con_lost>: unable to contact remote host
15817: 
15818: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15819: when the connection is brought back up
15820: 
15821: =item * B<con_failed>: unable to contact remote host and unable to save message
15822: for later delivery
15823: 
15824: =item * B<error:>: an error a occurred, a description of the error follows the :
15825: 
15826: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15827: that was requested
15828: 
15829: =back
15830: 
15831: =head1 PUBLIC SUBROUTINES
15832: 
15833: =head2 Session Environment Functions
15834: 
15835: =over 4
15836: 
15837: =item * 
15838: X<appenv()>
15839: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15840: the user envirnoment file, and will be restored for each access this
15841: user makes during this session, also modifies the %env for the current
15842: process. Optional rolesarrayref - if defined contains a reference to an array
15843: of roles which are exempt from the restriction on modifying user.role entries 
15844: in the user's environment.db and in %env.    
15845: 
15846: =item *
15847: X<delenv()>
15848: B<delenv($delthis,$regexp)>: removes all items from the session
15849: environment file that begin with $delthis. If the 
15850: optional second arg - $regexp - is true, $delthis is treated as a 
15851: regular expression, otherwise \Q$delthis\E is used. 
15852: The values are also deleted from the current processes %env.
15853: 
15854: =item * get_env_multiple($name) 
15855: 
15856: gets $name from the %env hash, it seemlessly handles the cases where multiple
15857: values may be defined and end up as an array ref.
15858: 
15859: returns an array of values
15860: 
15861: =back
15862: 
15863: =head2 User Information
15864: 
15865: =over 4
15866: 
15867: =item *
15868: X<queryauthenticate()>
15869: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15870: authentication scheme
15871: 
15872: =item *
15873: X<authenticate()>
15874: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15875: authenticate user from domain's lib servers (first use the current
15876: one). C<$upass> should be the users password.
15877: $checkdefauth is optional (value is 1 if a check should be made to
15878:    authenticate user using default authentication method, and allow
15879:    account creation if username does not have account in the domain).
15880: $clientcancheckhost is optional (value is 1 if checking whether the
15881:    server can host will occur on the client side in lonauth.pm).   
15882: 
15883: =item *
15884: X<homeserver()>
15885: B<homeserver($uname,$udom)>: find the server which has
15886: the user's directory and files (there must be only one), this caches
15887: the answer, and also caches if there is a borken connection.
15888: 
15889: =item *
15890: X<idget()>
15891: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15892: a list of student/employee IDs or clicker IDs
15893: (student/employee IDs are a unique resource in a domain, there must be 
15894: only 1 ID per username, and only 1 username per ID in a specific domain).
15895: clickerIDs are not necessarily unique, as students might share clickers.
15896: (returns hash: id=>name,id=>name)
15897: 
15898: =item *
15899: X<idrget()>
15900: B<idrget($udom,@unames)>: find the IDs behind a list of
15901: usernames (returns hash: name=>id,name=>id)
15902: 
15903: =item *
15904: X<idput()>
15905: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15906: names and associated student/employee IDs or clicker IDs.
15907: 
15908: =item *
15909: X<iddel()>
15910: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15911: student/employee ID or clicker ID username look-ups from domain.
15912: The homeserver ($uhome) and namespace ($namespace) are optional.
15913: If no $uhome is provided, it will be determined usig &homeserver()
15914: for each user.  If no $namespace is provided, the default is ids.
15915: 
15916: =item *
15917: X<updateclickers()>
15918: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15919: clicker ID-to-username look-ups in clickers.db on library server.
15920: Permitted actions are add or del (i.e., add or delete). The 
15921: clickers.db contains clickerID as keys (escaped), and each corresponding
15922: value is an escaped comma-separated list of usernames (for whom the
15923: library server is the homeserver), who registered that particular ID.
15924: If $critical is true, the update will be sent via &critical, otherwise
15925: &reply() will be used.
15926: 
15927: =item *
15928: X<rolesinit()>
15929: B<rolesinit($udom,$username)>: get user privileges.
15930: returns user role, first access and timer interval hashes
15931: 
15932: =item *
15933: X<privileged()>
15934: B<privileged($username,$domain)>: returns a true if user has a
15935: privileged and active role (i.e. su or dc), false otherwise.
15936: 
15937: =item *
15938: X<getsection()>
15939: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15940: course $cname, return section name/number or '' for "not in course"
15941: and '-1' for "no section"
15942: 
15943: =item *
15944: X<userenvironment()>
15945: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15946: passed in @what from the requested user's environment, returns a hash
15947: 
15948: =item * 
15949: X<userlog_query()>
15950: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15951: activity.log file. %filters defines filters applied when parsing the
15952: log file. These can be start or end timestamps, or the type of action
15953: - log to look for Login or Logout events, check for Checkin or
15954: Checkout, role for role selection. The response is in the form
15955: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15956: escaped strings of the action recorded in the activity.log file.
15957: 
15958: =back
15959: 
15960: =head2 User Roles
15961: 
15962: =over 4
15963: 
15964: =item *
15965: 
15966: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15967: returns codes for allowed actions.
15968: 
15969: The first argument is required, all others are optional.
15970: 
15971: $priv is the privilege being checked.
15972: $uri contains additional information about what is being checked for access (e.g.,
15973: URL, course ID etc.). 
15974: $symb is the unique resource instance identifier in a course; if needed,
15975: but not provided, it will be retrieved via a call to &symbread(). 
15976: $role is the role for which a priv is being checked (only used if priv is evb). 
15977: $clientip is the user's IP address (only used when checking for access to portfolio 
15978: files).
15979: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15980: prevents recursive calls to &allowed.
15981: 
15982:  F: full access
15983:  U,I,K: authentication modes (cxx only)
15984:  '': forbidden
15985:  1: user needs to choose course
15986:  2: browse allowed
15987:  A: passphrase authentication needed
15988:  B: access temporarily blocked because of a blocking event in a course.
15989:  D: access blocked because access is required via session initiated via deep-link 
15990: 
15991: =item *
15992: 
15993: constructaccess($url,$setpriv) : check for access to construction space URL
15994: 
15995: See if the owner domain and name in the URL match those in the
15996: expected environment.  If so, return three element list
15997: ($ownername,$ownerdomain,$ownerhome).
15998: 
15999: Otherwise return the null string.
16000: 
16001: If second argument 'setpriv' is true, it assigns the privileges,
16002: and returns the same three element list, unless the owner has
16003: blocked "ad hoc" Domain Coordinator access to the Author Space,
16004: in which case the null string is returned.
16005: 
16006: =item *
16007: 
16008: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16009: define a custom role rolename set privileges in format of lonTabs/roles.tab
16010: for system, domain, and course level. $uname and $udom are optional (current
16011: user's username and domain will be used when either of $uname or $udom are absent.
16012: 
16013: =item *
16014: 
16015: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
16016: (rolesplain.tab); plain text explanation of a user role term.
16017: $type is Course (default) or Community.
16018: If $forcedefault evaluates to true, text returned will be default 
16019: text for $type. Otherwise, if this is a course, the text returned 
16020: will be a custom name for the role (if defined in the course's 
16021: environment).  If no custom name is defined the default is returned.
16022:    
16023: =item *
16024: 
16025: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
16026: All arguments are optional. Returns a hash of a roles, either for
16027: co-author/assistant author roles for a user's Construction Space
16028: (default), or if $context is 'userroles', roles for the user himself,
16029: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16030: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16031: For each key, value is set to colon-separated start and end times for
16032: the role.  If no username and domain are specified, will default to
16033: current user/domain. Types, roles, and roledoms are references to arrays
16034: of role statuses (active, future or previous), roles 
16035: (e.g., cc,in, st etc.) and domains of the roles which can be used
16036: to restrict the list of roles reported. If no array ref is 
16037: provided for types, will default to return only active roles.
16038: 
16039: =item *
16040: 
16041: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16042: user: $uname:$udom has a role in the course: $cdom_$cnum. 
16043: 
16044: Additional optional arguments are: $type (if role checking is to be restricted 
16045: to certain user status types -- previous (expired roles), active (currently
16046: available roles) or future (roles available in the future), and
16047: $hideprivileged -- if true will not report course roles for users who
16048: have active Domain Coordinator role in course's domain or in additional
16049: domains (specified in 'Domains to check for privileged users' in course
16050: environment -- set via:  Course Settings -> Classlists and staff listing).
16051: 
16052: =item *
16053: 
16054: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16055: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16056: $possdomains and $possroles are optional array refs -- to domains to check and
16057: roles to check.  If $possdomains is not specified, a dump will be done of the
16058: users' roles.db to check for a dc or su role in any domain. This can be
16059: time consuming if &privileged is called repeatedly (e.g., when displaying a
16060: classlist), so in such cases, supplying a $possdomains array is preferred, as
16061: this then allows &privileged_by_domain() to be used, which caches the identity
16062: of privileged users, eliminating the need for repeated calls to &dump().
16063: 
16064: =item *
16065: 
16066: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16067: where the outer hash keys are domains specified in the $possdomains array ref,
16068: next inner hash keys are privileged roles specified in the $roles array ref,
16069: and the innermost hash contains key = value pairs for username:domain = end:start
16070: for active or future "privileged" users with that role in that domain. To avoid
16071: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16072: innerhash are cached using priv_$role and $dom as the identifiers.
16073: 
16074: =back
16075: 
16076: =head2 User Modification
16077: 
16078: =over 4
16079: 
16080: =item *
16081: 
16082: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
16083: user for the level given by URL.  Optional start and end dates (leave empty
16084: string or zero for "no date")
16085: 
16086: =item *
16087: 
16088: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16089: change a users, password, possible return values are: ok,
16090: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16091: refused
16092: 
16093: =item *
16094: 
16095: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
16096: 
16097: =item *
16098: 
16099: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16100:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
16101: 
16102: will update user information (firstname,middlename,lastname,generation,
16103: permanentemail), and if forceid is true, student/employee ID also.
16104: A user's institutional affiliation(s) can also be updated.
16105: User information fields will not be overwritten with empty entries 
16106: unless the field is included in the $candelete array reference.
16107: This array is included when a single user is modified via "Manage Users",
16108: or when Autoupdate.pl is run by cron in a domain.
16109: 
16110: =item *
16111: 
16112: modifystudent
16113: 
16114: modify a student's enrollment and identification information.
16115: The course id is resolved based on the current user's environment.  
16116: This means the invoking user must be a course coordinator or otherwise
16117: associated with a course.
16118: 
16119: This call is essentially a wrapper for lonnet::modifyuser and
16120: lonnet::modify_student_enrollment
16121: 
16122: Inputs: 
16123: 
16124: =over 4
16125: 
16126: =item B<$udom> Student's loncapa domain
16127: 
16128: =item B<$uname> Student's loncapa login name
16129: 
16130: =item B<$uid> Student/Employee ID
16131: 
16132: =item B<$umode> Student's authentication mode
16133: 
16134: =item B<$upass> Student's password
16135: 
16136: =item B<$first> Student's first name
16137: 
16138: =item B<$middle> Student's middle name
16139: 
16140: =item B<$last> Student's last name
16141: 
16142: =item B<$gene> Student's generation
16143: 
16144: =item B<$usec> Student's section in course
16145: 
16146: =item B<$end> Unix time of the roles expiration
16147: 
16148: =item B<$start> Unix time of the roles start date
16149: 
16150: =item B<$forceid> If defined, allow $uid to be changed
16151: 
16152: =item B<$desiredhome> server to use as home server for student
16153: 
16154: =item B<$email> Student's permanent e-mail address
16155: 
16156: =item B<$type> Type of enrollment (auto or manual)
16157: 
16158: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
16159: 
16160: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
16161: 
16162: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
16163: 
16164: =item B<$context> role change context (shown in User Management Logs display in a course)
16165: 
16166: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16167: 
16168: =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.
16169: 
16170: =back
16171: 
16172: =item *
16173: 
16174: modify_student_enrollment
16175: 
16176: Change a student's enrollment status in a class.  The environment variable
16177: 'role.request.course' must be defined for this function to proceed.
16178: 
16179: Inputs:
16180: 
16181: =over 4
16182: 
16183: =item $udom, student's domain
16184: 
16185: =item $uname, student's name
16186: 
16187: =item $uid, student's user id
16188: 
16189: =item $first, student's first name
16190: 
16191: =item $middle
16192: 
16193: =item $last
16194: 
16195: =item $gene
16196: 
16197: =item $usec
16198: 
16199: =item $end
16200: 
16201: =item $start
16202: 
16203: =item $type
16204: 
16205: =item $locktype
16206: 
16207: =item $cid
16208: 
16209: =item $selfenroll
16210: 
16211: =item $context
16212: 
16213: =item $credits, number of credits student will earn from this class
16214: 
16215: =item $instsec, institutional course section code for student
16216: 
16217: =back
16218: 
16219: 
16220: =item *
16221: 
16222: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16223: custom role; give a custom role to a user for the level given by URL.  Specify
16224: name and domain of role author, and role name
16225: 
16226: =item *
16227: 
16228: revokerole($udom,$uname,$url,$role) : revoke a role for url
16229: 
16230: =item *
16231: 
16232: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16233: 
16234: =back
16235: 
16236: =head2 Course Infomation
16237: 
16238: =over 4
16239: 
16240: =item *
16241: 
16242: coursedescription($courseid,$options) : returns a hash of information about the
16243: specified course id, including all environment settings for the
16244: course, the description of the course will be in the hash under the
16245: key 'description'
16246: 
16247: $options is an optional parameter that if supplied is a hash reference that controls
16248: what how this function works.  It has the following key/values:
16249: 
16250: =over 4
16251: 
16252: =item freshen_cache
16253: 
16254: If defined, and the environment cache for the course is valid, it is 
16255: returned in the returned hash.
16256: 
16257: =item one_time
16258: 
16259: If defined, the last cache time is set to _now_
16260: 
16261: =item user
16262: 
16263: If defined, the supplied username is used instead of the current user.
16264: 
16265: 
16266: =back
16267: 
16268: =item *
16269: 
16270: resdata($name,$domain,$type,@which) : request for current parameter
16271: setting for a specific $type, where $type is either 'course' or 'user',
16272: @what should be a list of parameters to ask about. This routine caches
16273: answers for 10 minutes.
16274: 
16275: =item *
16276: 
16277: get_courseresdata($courseid, $domain) : dump the entire course resource
16278: data base, returning a hash that is keyed by the resource name and has
16279: values that are the resource value.  I believe that the timestamps and
16280: versions are also returned.
16281: 
16282: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
16283: supplemental content area. This routine caches the number of files for 
16284: 10 minutes.
16285: 
16286: =back
16287: 
16288: =head2 Course Modification
16289: 
16290: =over 4
16291: 
16292: =item *
16293: 
16294: writecoursepref($courseid,%prefs) : write preferences (environment
16295: database) for a course
16296: 
16297: =item *
16298: 
16299: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16300: 
16301: =item *
16302: 
16303: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
16304: 
16305: =item *
16306: 
16307: is_course($courseid), is_course($cdom, $cnum)
16308: 
16309: Accepts either a combined $courseid (in the form of domain_courseid) or the
16310: two component version $cdom, $cnum. It checks if the specified course exists.
16311: 
16312: Returns:
16313:     undef if the course doesn't exist, otherwise
16314:     in scalar context the combined courseid.
16315:     in list context the two components of the course identifier, domain and 
16316:     courseid.    
16317: 
16318: =back
16319: 
16320: =head2 Bubblesheet Configuration
16321: 
16322: =over 4
16323: 
16324: =item *
16325: 
16326: get_scantron_config($which)
16327: 
16328: $which - the name of the configuration to parse from the file.
16329: 
16330: Parses and returns the bubblesheet configuration line selected as a
16331: hash of configuration file fields.
16332: 
16333: 
16334: Returns:
16335:     If the named configuration is not in the file, an empty
16336:     hash is returned.
16337: 
16338:     a hash with the fields
16339:       name         - internal name for the this configuration setup
16340:       description  - text to display to operator that describes this config
16341:       CODElocation - if 0 or the string 'none'
16342:                           - no CODE exists for this config
16343:                      if -1 || the string 'letter'
16344:                           - a CODE exists for this config and is
16345:                             a string of letters
16346:                      Unsupported value (but planned for future support)
16347:                           if a positive integer
16348:                                - The CODE exists as the first n items from
16349:                                  the question section of the form
16350:                           if the string 'number'
16351:                                - The CODE exists for this config and is
16352:                                  a string of numbers
16353:       CODEstart   - (only matter if a CODE exists) column in the line where
16354:                      the CODE starts
16355:       CODElength  - length of the CODE
16356:       IDstart     - column where the student/employee ID starts
16357:       IDlength    - length of the student/employee ID info
16358:       Qstart      - column where the information from the bubbled
16359:                     'questions' start
16360:       Qlength     - number of columns comprising a single bubble line from
16361:                     the sheet. (usually either 1 or 10)
16362:       Qon         - either a single character representing the character used
16363:                     to signal a bubble was chosen in the positional setup, or
16364:                     the string 'letter' if the letter of the chosen bubble is
16365:                     in the final, or 'number' if a number representing the
16366:                     chosen bubble is in the file (1->A 0->J)
16367:       Qoff        - the character used to represent that a bubble was
16368:                     left blank
16369:       PaperID     - if the scanning process generates a unique number for each
16370:                     sheet scanned the column that this ID number starts in
16371:       PaperIDlength - number of columns that comprise the unique ID number
16372:                       for the sheet of paper
16373:       FirstName   - column that the first name starts in
16374:       FirstNameLength - number of columns that the first name spans
16375:       LastName    - column that the last name starts in
16376:       LastNameLength - number of columns that the last name spans
16377:       BubblesPerRow - number of bubbles available in each row used to
16378:                       bubble an answer. (If not specified, 10 assumed).
16379: 
16380: 
16381: =item *
16382: 
16383: get_scantronformat_file($cdom)
16384: 
16385: $cdom - the course's domain (optional); if not supplied, uses
16386: domain for current $env{'request.course.id'}.
16387: 
16388: Returns an array containing lines from the scantron format file for
16389: the domain of the course.
16390: 
16391: If a url for a custom.tab file is listed in domain's configuration.db,
16392: lines are from this file.
16393: 
16394: Otherwise, if a default.tab has been published in RES space by the
16395: domainconfig user, lines are from this file.
16396: 
16397: Otherwise, fall back to getting lines from the legacy file on the
16398: local server:  /home/httpd/lonTabs/default_scantronformat.tab
16399: 
16400: =back
16401: 
16402: =head2 Resource Subroutines
16403: 
16404: =over 4
16405: 
16406: =item *
16407: 
16408: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
16409: 
16410: =item *
16411: 
16412: repcopy($filename) : subscribes to the requested file, and attempts to
16413: replicate from the owning library server, Might return
16414: 'unavailable', 'not_found', 'forbidden', 'ok', or
16415: 'bad_request', also attempts to grab the metadata for the
16416: resource. Expects the local filesystem pathname
16417: (/home/httpd/html/res/....)
16418: 
16419: =back
16420: 
16421: =head2 Resource Information
16422: 
16423: =over 4
16424: 
16425: =item *
16426: 
16427: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
16428: and returns the value of a variety of different possible values,
16429: $varname should be a request string, and the other parameters can be
16430: used to specify who and what one is asking about. Ordinarily, $cid 
16431: does not need to be specified, as it is retrived from 
16432: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16433: within lonuserstate::loadmap() when initializing a course, before
16434: $env{'request.course.id'} has been set, so it needs to be provided
16435: in that one case.
16436: 
16437: Possible values for $varname are environment.lastname (or other item
16438: from the envirnment hash), user.name (or someother aspect about the
16439: user), resource.0.maxtries (or some other part and parameter of a
16440: resource)
16441: 
16442: =item *
16443: 
16444: directcondval($number) : get current value of a condition; reads from a state
16445: string
16446: 
16447: =item *
16448: 
16449: condval($condidx) : value of condition index based on state
16450: 
16451: =item *
16452: 
16453: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
16454: resource's metadata, $what should be either a specific key, or either
16455: 'keys' (to get a list of possible keys) or 'packages' to get a list of
16456: packages that this resource currently uses, the last 3 arguments are 
16457: only used internally for recursive metadata.
16458: 
16459: the toolsymb is only used where the uri is for an external tool (for which
16460: the uri as well as the symb are guaranteed to be unique).
16461: 
16462: this function automatically caches all requests except any made recursively
16463: to retrieve a list of metadata keys for an imported library file ($liburi is 
16464: defined).
16465: 
16466: =item *
16467: 
16468: metadata_query($query,$custom,$customshow) : make a metadata query against the
16469: network of library servers; returns file handle of where SQL and regex results
16470: will be stored for query
16471: 
16472: =item *
16473: 
16474: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
16475: return symbolic list entry (all arguments optional). 
16476: 
16477: Args: filename is the filename (including path) for the file for which a symb 
16478: is required; donotrecurse, if true will prevent calls to allowed() being made 
16479: to check access status if more than one resource was found in the bighash 
16480: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
16481: a randompick); ignorecachednull, if true will prevent a symb of '' being 
16482: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16483: cause possible symbs to be checked to determine if they are subject to content
16484: blocking, if so they will not be included as possible symbs; possibles is a
16485: ref to a hash, which, as a side effect, will be populated with all possible 
16486: symbs (content blocking not tested).
16487:  
16488: returns the data handle
16489: 
16490: =item *
16491: 
16492: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
16493: and is a possible symb for the URL in $thisfn, and if is an encrypted
16494: resource that the user accessed using /enc/ returns a 1 on success, 0
16495: on failure, user must be in a course, as it assumes the existence of
16496: the course initial hash, and uses $env('request.course.id'}.  The third
16497: arg is an optional reference to a scalar.  If this arg is passed in the 
16498: call to symbverify, it will be set to 1 if the symb has been set to be 
16499: encrypted; otherwise it will be null.  
16500: 
16501: =item *
16502: 
16503: symbclean($symb) : removes versions numbers from a symb, returns the
16504: cleaned symb
16505: 
16506: =item *
16507: 
16508: is_on_map($uri) : checks if the $uri is somewhere on the current
16509: course map, user must be in a course for it to work.
16510: 
16511: =item *
16512: 
16513: numval($salt) : return random seed value (addend for rndseed)
16514: 
16515: =item *
16516: 
16517: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16518: a random seed, all arguments are optional, if they aren't sent it uses the
16519: environment to derive them. Note: if symb isn't sent and it can't get one
16520: from &symbread it will use the current time as its return value
16521: 
16522: =item *
16523: 
16524: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16525: unfakeable, receipt
16526: 
16527: =item *
16528: 
16529: receipt() : API to ireceipt working off of env values; given out to users
16530: 
16531: =item *
16532: 
16533: countacc($url) : count the number of accesses to a given URL
16534: 
16535: =item *
16536: 
16537: 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
16538: 
16539: =item *
16540: 
16541: 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)
16542: 
16543: =item *
16544: 
16545: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
16546: 
16547: =item *
16548: 
16549: devalidate($symb) : devalidate temporary spreadsheet calculations,
16550: forcing spreadsheet to reevaluate the resource scores next time.
16551: 
16552: =item * 
16553: 
16554: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16555: when viewing in course context.
16556: 
16557:  input: six args -- filename (decluttered), course number, course domain,
16558:                     url, symb (if registered) and group (if this is a 
16559:                     group item -- e.g., bulletin board, group page etc.).
16560: 
16561:  output: array of five scalars --
16562:          $cfile -- url for file editing if editable on current server
16563:          $home -- homeserver of resource (i.e., for author if published,
16564:                                           or course if uploaded.).
16565:          $switchserver --  1 if server switch will be needed.
16566:          $forceedit -- 1 if icon/link should be to go to edit mode 
16567:          $forceview -- 1 if icon/link should be to go to view mode
16568: 
16569: =item *
16570: 
16571: is_course_upload($file,$cnum,$cdom)
16572: 
16573: Used in course context to determine if current file was uploaded to 
16574: the course (i.e., would be found in /userfiles/docs on the course's 
16575: homeserver.
16576: 
16577:   input: 3 args -- filename (decluttered), course number and course domain.
16578:   output: boolean -- 1 if file was uploaded.
16579: 
16580: =back
16581: 
16582: =head2 Storing/Retreiving Data
16583: 
16584: =over 4
16585: 
16586: =item *
16587: 
16588: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16589: permanently for this url; hashref needs to be given and should be a \%hashname;
16590: the remaining args aren't required and if they aren't passed or are '' they will
16591: be derived from the env (with the exception of $laststore, which is an 
16592: optional arg used when a user's submission is stored in grading).
16593: $laststore is $version=$timestamp, where $version is the most recent version
16594: number retrieved for the corresponding $symb in the $namespace db file, and
16595: $timestamp is the timestamp for that transaction (UNIX time).
16596: $laststore is currently only passed when cstore() is called by 
16597: structuretags::finalize_storage().
16598: 
16599: =item *
16600: 
16601: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16602: but uses critical subroutine
16603: 
16604: =item *
16605: 
16606: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16607: all args are optional
16608: 
16609: =item *
16610: 
16611: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
16612: dumps the complete (or key matching regexp) namespace into a hash
16613: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16614: normally &store()ed into
16615: 
16616: $range should be either an integer '100' (give me the first 100
16617:                                            matching records)
16618:               or be  two integers sperated by a - with no spaces
16619:                  '30-50' (give me the 30th through the 50th matching
16620:                           records)
16621: 
16622: 
16623: =item *
16624: 
16625: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
16626: replaces a &store() version of data with a replacement set of data
16627: for a particular resource in a namespace passed in the $storehash hash 
16628: reference. If $tolog is true, the transaction is logged in the courselog
16629: with an action=PUTSTORE.
16630: 
16631: =item *
16632: 
16633: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16634: works very similar to store/cstore, but all data is stored in a
16635: temporary location and can be reset using tmpreset, $storehash should
16636: be a hash reference, returns nothing on success
16637: 
16638: =item *
16639: 
16640: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16641: similar to restore, but all data is stored in a temporary location and
16642: can be reset using tmpreset. Returns a hash of values on success,
16643: error string otherwise.
16644: 
16645: =item *
16646: 
16647: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16648: deltes all keys for $symb form the temporary storage hash.
16649: 
16650: =item *
16651: 
16652: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16653: reference filled in from namesp ($udom and $uname are optional)
16654: 
16655: =item *
16656: 
16657: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16658: namesp ($udom and $uname are optional)
16659: 
16660: =item *
16661: 
16662: dump($namespace,$udom,$uname,$regexp,$range) : 
16663: dumps the complete (or key matching regexp) namespace into a hash
16664: ($udom, $uname, $regexp, $range are optional)
16665: 
16666: $range should be either an integer '100' (give me the first 100
16667:                                            matching records)
16668:               or be  two integers sperated by a - with no spaces
16669:                  '30-50' (give me the 30th through the 50th matching
16670:                           records)
16671: =item *
16672: 
16673: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16674: $store can be a scalar, an array reference, or if the amount to be 
16675: incremented is > 1, a hash reference.
16676: 
16677: ($udom and $uname are optional)
16678: 
16679: =item *
16680: 
16681: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16682: ($udom and $uname are optional)
16683: 
16684: =item *
16685: 
16686: cput($namespace,$storehash,$udom,$uname) : critical put
16687: ($udom and $uname are optional)
16688: 
16689: =item *
16690: 
16691: newput($namespace,$storehash,$udom,$uname) :
16692: 
16693: Attempts to store the items in the $storehash, but only if they don't
16694: currently exist, if this succeeds you can be certain that you have 
16695: successfully created a new key value pair in the $namespace db.
16696: 
16697: 
16698: Args:
16699:  $namespace: name of database to store values to
16700:  $storehash: hashref to store to the db
16701:  $udom: (optional) domain of user containing the db
16702:  $uname: (optional) name of user caontaining the db
16703: 
16704: Returns:
16705:  'ok' -> succeeded in storing all keys of $storehash
16706:  'key_exists: <key>' -> failed to anything out of $storehash, as at
16707:                         least <key> already existed in the db (other
16708:                         requested keys may also already exist)
16709:  'error: <msg>' -> unable to tie the DB or other error occurred
16710:  'con_lost' -> unable to contact request server
16711:  'refused' -> action was not allowed by remote machine
16712: 
16713: 
16714: =item *
16715: 
16716: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16717: reference filled in from namesp (encrypts the return communication)
16718: ($udom and $uname are optional)
16719: 
16720: =item *
16721: 
16722: log($udom,$name,$home,$message) : write to permanent log for user; use
16723: critical subroutine
16724: 
16725: =item *
16726: 
16727: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16728: array reference filled in from namespace found in domain level on either
16729: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
16730: 
16731: =item *
16732: 
16733: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
16734: domain level either on specified domain server ($uhome) or primary domain 
16735: server ($udom and $uhome are optional)
16736: 
16737: =item * 
16738: 
16739: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
16740: for: authentication, language, quotas, timezone, date locale, and portal URL in
16741: the target domain.
16742: 
16743: May also include additional key => value pairs for the following groups:
16744: 
16745: =over
16746: 
16747: =item
16748: disk quotas (MB allocated by default to portfolios and authoring spaces).
16749: 
16750: =over
16751: 
16752: =item defaultquota, authorquota
16753: 
16754: =back
16755: 
16756: =item
16757: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16758: portfolio for users).
16759: 
16760: =over
16761: 
16762: =item
16763: aboutme, blog, webdav, portfolio
16764: 
16765: =back
16766: 
16767: =item
16768: requestcourses: ability to request courses, and how requests are processed.
16769: 
16770: =over
16771: 
16772: =item
16773: official, unofficial, community, textbook, placement
16774: 
16775: =back
16776: 
16777: =item
16778: inststatus: types of institutional affiliation, and order in which they are displayed.
16779: 
16780: =over
16781: 
16782: =item
16783: inststatustypes, inststatusorder, inststatusguest
16784: 
16785: =back
16786: 
16787: =item
16788: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16789: for course's uploaded content.
16790: 
16791: =over
16792: 
16793: =item
16794: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16795: communityquota, textbookquota, placementquota
16796: 
16797: =back
16798: 
16799: =item
16800: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16801: on your servers.
16802: 
16803: =over
16804: 
16805: =item 
16806: remotesessions, hostedsessions
16807: 
16808: =back
16809: 
16810: =back
16811: 
16812: In cases where a domain coordinator has never used the "Set Domain Configuration"
16813: utility to create a configuration.db file on a domain's primary library server 
16814: only the following domain defaults: auth_def, auth_arg_def, lang_def
16815: -- corresponding values are authentication type (internal, krb4, krb5,
16816: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16817: will be available. Values are retrieved from cache (if current), unless the
16818: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16819: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16820: 
16821: Typical usage:
16822: 
16823: %domdefaults = &get_domain_defaults($target_domain);
16824: 
16825: =back
16826: 
16827: =head2 Network Status Functions
16828: 
16829: =over 4
16830: 
16831: =item *
16832: 
16833: dirlist() : return directory list based on URI (first arg).
16834: 
16835: Inputs: 1 required, 5 optional.
16836: 
16837: =over
16838: 
16839: =item 
16840: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16841: 
16842: =item
16843: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16844: 
16845: =item
16846: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16847: 
16848: =item
16849: $getpropath - boolean: 1 if prepend path using &propath(). 
16850: 
16851: =item
16852: $getuserdir - boolean: 1 if prepend path for "userfiles".
16853: 
16854: =item 
16855: $alternateRoot - path to prepend in place of path from $uri.
16856: 
16857: =back
16858: 
16859: Returns: Array of up to two items.
16860: 
16861: =over
16862: 
16863: a reference to an array of files/subdirectories
16864: 
16865: =over
16866: 
16867: Each element in the array of files/subdirectories is a & separated list of
16868: item name and the result of running stat on the item.  If dirlist was requested
16869: for a file instead of a directory, the item name will be ''. For a directory 
16870: listing, if the item is a metadata file, the element will end &N&M 
16871: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16872: default copyright set (1).  
16873: 
16874: =back
16875: 
16876: a scalar containing error condition (if encountered).
16877: 
16878: =over
16879: 
16880: =item 
16881: no_host (no homeserver identified for $username:$domain).
16882: 
16883: =item 
16884: no_such_host (server contacted for listing not identified as valid host).
16885: 
16886: =item 
16887: con_lost (connection to remote server failed).
16888: 
16889: =item 
16890: refused (invalid $username:$domain received on lond side).
16891: 
16892: =item 
16893: no_such_dir (directory at specified path on lond side does not exist). 
16894: 
16895: =item 
16896: empty (directory at specified path on lond side is empty).
16897: 
16898: =over
16899: 
16900: This is currently not encountered because the &ls3, &ls2, 
16901: &ls (_handler) routines on the lond side do not filter out
16902: . and .. from a directory listing. 
16903: 
16904: =back
16905: 
16906: =back
16907: 
16908: =back
16909: 
16910: =item *
16911: 
16912: spareserver() : find server with least workload from spare.tab
16913: 
16914: 
16915: =item *
16916: 
16917: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16918: if there is no corresponding loncapa host.
16919: 
16920: =back
16921: 
16922: 
16923: =head2 Apache Request
16924: 
16925: =over 4
16926: 
16927: =item *
16928: 
16929: ssi($url,%hash) : server side include, does a complete request cycle on url to
16930: localhost, posts hash
16931: 
16932: =back
16933: 
16934: =head2 Data to String to Data
16935: 
16936: =over 4
16937: 
16938: =item *
16939: 
16940: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16941: and '&' separators, supports elements that are arrayrefs and hashrefs
16942: 
16943: =item *
16944: 
16945: hashref2str($hashref) : convert a hashref into a string complete with
16946: escaping and '=' and '&' separators, supports elements that are
16947: arrayrefs and hashrefs
16948: 
16949: =item *
16950: 
16951: arrayref2str($arrayref) : convert an arrayref into a string complete
16952: with escaping and '&' separators, supports elements that are arrayrefs
16953: and hashrefs
16954: 
16955: =item *
16956: 
16957: str2hash($string) : convert string to hash using unescaping and
16958: splitting on '=' and '&', supports elements that are arrayrefs and
16959: hashrefs
16960: 
16961: =item *
16962: 
16963: str2array($string) : convert string to hash using unescaping and
16964: splitting on '&', supports elements that are arrayrefs and hashrefs
16965: 
16966: =back
16967: 
16968: =head2 Logging Routines
16969: 
16970: 
16971: These routines allow one to make log messages in the lonnet.log and
16972: lonnet.perm logfiles.
16973: 
16974: =over 4
16975: 
16976: =item *
16977: 
16978: logtouch() : make sure the logfile, lonnet.log, exists
16979: 
16980: =item *
16981: 
16982: logthis() : append message to the normal lonnet.log file, it gets
16983: preiodically rolled over and deleted.
16984: 
16985: =item *
16986: 
16987: logperm() : append a permanent message to lonnet.perm.log, this log
16988: file never gets deleted by any automated portion of the system, only
16989: messages of critical importance should go in here.
16990: 
16991: 
16992: =back
16993: 
16994: =head2 General File Helper Routines
16995: 
16996: =over 4
16997: 
16998: =item *
16999: 
17000: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17001: (a) files in /uploaded
17002:   (i) If a local copy of the file exists - 
17003:       compares modification date of local copy with last-modified date for 
17004:       definitive version stored on home server for course. If local copy is 
17005:       stale, requests a new version from the home server and stores it. 
17006:       If the original has been removed from the home server, then local copy 
17007:       is unlinked.
17008:   (ii) If local copy does not exist -
17009:       requests the file from the home server and stores it. 
17010:   
17011:   If $caller is 'uploadrep':  
17012:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17013:     for request for files originally uploaded via DOCS. 
17014:      - returns 'ok' if fresh local copy now available, -1 otherwise.
17015:   
17016:   Otherwise:
17017:      This indicates a call from the content generation phase of the request.
17018:      -  returns the entire contents of the file or -1.
17019:      
17020: (b) files in /res
17021:    - returns the entire contents of a file or -1; 
17022:    it properly subscribes to and replicates the file if neccessary.
17023: 
17024: 
17025: =item *
17026: 
17027: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17028:                   reference
17029: 
17030: returns either a stat() list of data about the file or an empty list
17031: if the file doesn't exist or couldn't find out about it (connection
17032: problems or user unknown)
17033: 
17034: =item *
17035: 
17036: filelocation($dir,$file) : returns file system location of a file
17037: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17038: directory that relative $file lookups are to looked in ($dir of /a/dir
17039: and a file of ../bob will become /a/bob)
17040: 
17041: =item *
17042: 
17043: hreflocation($dir,$file) : returns file system location or a URL; same as
17044: filelocation except for hrefs
17045: 
17046: =item *
17047: 
17048: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17049: also removes beginning /home/httpd/html unless /priv/ follows it.
17050: 
17051: =back
17052: 
17053: =head2 Usererfile file routines (/uploaded*)
17054: 
17055: =over 4
17056: 
17057: =item *
17058: 
17059: userfileupload(): main rotine for putting a file in a user or course's
17060:                   filespace, arguments are,
17061: 
17062:  formname - required - this is the name of the element in $env where the
17063:            filename, and the contents of the file to create/modifed exist
17064:            the filename is in $env{'form.'.$formname.'.filename'} and the
17065:            contents of the file is located in $env{'form.'.$formname}
17066:  context - if coursedoc, store the file in the course of the active role
17067:              of the current user; 
17068:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17069:            if 'canceloverwrite': delete file in tmp/overwrites directory
17070:  subdir - required - subdirectory to put the file in under ../userfiles/
17071:          if undefined, it will be placed in "unknown"
17072: 
17073:  (This routine calls clean_filename() to remove any dangerous
17074:  characters from the filename, and then calls finuserfileupload() to
17075:  complete the transaction)
17076: 
17077:  returns either the url of the uploaded file (/uploaded/....) if successful
17078:  and /adm/notfound.html if unsuccessful
17079: 
17080: =item *
17081: 
17082: clean_filename(): routine for cleaing a filename up for storage in
17083:                  userfile space, argument is:
17084: 
17085:  filename - proposed filename
17086: 
17087: returns: the new clean filename
17088: 
17089: =item *
17090: 
17091: finishuserfileupload(): routine that creates and sends the file to
17092: userspace, probably shouldn't be called directly
17093: 
17094:   docuname: username or courseid of destination for the file
17095:   docudom: domain of user/course of destination for the file
17096:   formname: same as for userfileupload()
17097:   fname: filename (including subdirectories) for the file
17098:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
17099:           if hashref, and context is scantron, will convert csv format to standard format
17100:   allfiles: reference to hash used to store objects found by parser
17101:   codebase: reference to hash used for codebases of java objects found by parser
17102:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17103:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
17104:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
17105:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
17106:   context: if 'overwrite', will move the uploaded file from its temporary location to
17107:             userfiles to facilitate overwriting a previously uploaded file with same name.
17108:   mimetype: reference to scalar to accommodate mime type determined
17109:             from File::MMagic if $parser = parse.
17110: 
17111:  returns either the url of the uploaded file (/uploaded/....) if successful
17112:  and /adm/notfound.html if unsuccessful (or an error message if context 
17113:  was 'overwrite').
17114:  
17115: 
17116: =item *
17117: 
17118: renameuserfile(): renames an existing userfile to a new name
17119: 
17120:   Args:
17121:    docuname: username or courseid of destination for the file
17122:    docudom: domain of user/course of destination for the file
17123:    old: current file name (including any subdirs under userfiles)
17124:    new: desired file name (including any subdirs under userfiles)
17125: 
17126: =item *
17127: 
17128: mkdiruserfile(): creates a directory is a userfiles dir
17129: 
17130:   Args:
17131:    docuname: username or courseid of destination for the file
17132:    docudom: domain of user/course of destination for the file
17133:    dir: dir to create (including any subdirs under userfiles)
17134: 
17135: =item *
17136: 
17137: removeuserfile(): removes a file that exists in userfiles
17138: 
17139:   Args:
17140:    docuname: username or courseid of destination for the file
17141:    docudom: domain of user/course of destination for the file
17142:    fname: filname to delete (including any subdirs under userfiles)
17143: 
17144: =item *
17145: 
17146: removeuploadedurl(): convience function for removeuserfile()
17147: 
17148:   Args:
17149:    url:  a full /uploaded/... url to delete
17150: 
17151: =item * 
17152: 
17153: get_portfile_permissions():
17154:   Args:
17155:     domain: domain of user or course contain the portfolio files
17156:     user: name of user or num of course contain the portfolio files
17157:   Returns:
17158:     hashref of a dump of the proper file_permissions.db
17159:    
17160: 
17161: =item * 
17162: 
17163: get_access_controls():
17164: 
17165: Args:
17166:   current_permissions: the hash ref returned from get_portfile_permissions()
17167:   group: (optional) the group you want the files associated with
17168:   file: (optional) the file you want access info on
17169: 
17170: Returns:
17171:     a hash (keys are file names) of hashes containing
17172:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17173:         values are XML containing access control settings (see below) 
17174: 
17175: Internal notes:
17176: 
17177:  access controls are stored in file_permissions.db as key=value pairs.
17178:     key -> path to file/file_name\0uniqueID:scope_end_start
17179:         where scope -> public,guest,course,group,domains or users.
17180:               end -> UNIX time for end of access (0 -> no end date)
17181:               start -> UNIX time for start of access
17182: 
17183:     value -> XML description of access control
17184:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17185:             <start></start>
17186:             <end></end>
17187: 
17188:             <password></password>  for scope type = guest
17189: 
17190:             <domain></domain>     for scope type = course or group
17191:             <number></number>
17192:             <roles id="">
17193:              <role></role>
17194:              <access></access>
17195:              <section></section>
17196:              <group></group>
17197:             </roles>
17198: 
17199:             <dom></dom>         for scope type = domains
17200: 
17201:             <users>             for scope type = users
17202:              <user>
17203:               <uname></uname>
17204:               <udom></udom>
17205:              </user>
17206:             </users>
17207:            </scope> 
17208:               
17209:  Access data is also aggregated for each file in an additional key=value pair:
17210:  key -> path to file/file_name\0accesscontrol 
17211:  value -> reference to hash
17212:           hash contains key = value pairs
17213:           where key = uniqueID:scope_end_start
17214:                 value = UNIX time record was last updated
17215: 
17216:           Used to improve speed of look-ups of access controls for each file.  
17217:  
17218:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17219: 
17220: =item *
17221: 
17222: modify_access_controls():
17223: 
17224: Modifies access controls for a portfolio file
17225: Args
17226: 1. file name
17227: 2. reference to hash of required changes,
17228: 3. domain
17229: 4. username
17230:   where domain,username are the domain of the portfolio owner 
17231:   (either a user or a course) 
17232: 
17233: Returns:
17234: 1. result of additions or updates ('ok' or 'error', with error message). 
17235: 2. result of deletions ('ok' or 'error', with error message).
17236: 3. reference to hash of any new or updated access controls.
17237: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17238:    key = integer (inbound ID)
17239:    value = uniqueID
17240: 
17241: =item *
17242: 
17243: get_timebased_id():
17244: 
17245: Attempts to get a unique timestamp-based suffix for use with items added to a 
17246: course via the Course Editor (e.g., folders, composite pages, 
17247: group bulletin boards).
17248: 
17249: Args: (first three required; six others optional)
17250: 
17251: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17252:    docssequence, or name of group
17253: 
17254: 2. keyid (alphanumeric): name of temporary locking key in hash,
17255:    e.g., num, boardids
17256: 
17257: 3. namespace: name of gdbm file used to store suffixes already assigned;  
17258:    file will be named nohist_namespace.db
17259: 
17260: 4. cdom: domain of course; default is current course domain from %env
17261: 
17262: 5. cnum: course number; default is current course number from %env
17263: 
17264: 6. idtype: set to concat if an additional digit is to be appended to the 
17265:    unix timestamp to form the suffix, if the plain timestamp is already
17266:    in use.  Default is to not do this, but simply increment the unix 
17267:    timestamp by 1 until a unique key is obtained.
17268: 
17269: 7. who: holder of locking key; defaults to user:domain for user.
17270: 
17271: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
17272:    retrying); default is 3.
17273: 
17274: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
17275: 
17276: Returns:
17277: 
17278: 1. suffix obtained (numeric)
17279: 
17280: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17281: 
17282: 3. error: contains (localized) error message if an error occurred.
17283: 
17284: 
17285: =back
17286: 
17287: =head2 HTTP Helper Routines
17288: 
17289: =over 4
17290: 
17291: =item *
17292: 
17293: escape() : unpack non-word characters into CGI-compatible hex codes
17294: 
17295: =item *
17296: 
17297: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17298: 
17299: =back
17300: 
17301: =head1 PRIVATE SUBROUTINES
17302: 
17303: =head2 Underlying communication routines (Shouldn't call)
17304: 
17305: =over 4
17306: 
17307: =item *
17308: 
17309: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17310: 
17311: =item *
17312: 
17313: reply() : uses subreply to send a message to remote machine, logs all failures
17314: 
17315: =item *
17316: 
17317: critical() : passes a critical message to another server; if cannot
17318: get through then place message in connection buffer directory and
17319: returns con_delayed, if incapable of saving message, returns
17320: con_failed
17321: 
17322: =item *
17323: 
17324: reconlonc() : tries to reconnect lonc client processes.
17325: 
17326: =back
17327: 
17328: =head2 Resource Access Logging
17329: 
17330: =over 4
17331: 
17332: =item *
17333: 
17334: flushcourselogs() : flush (save) buffer logs and access logs
17335: 
17336: =item *
17337: 
17338: courselog($what) : save message for course in hash
17339: 
17340: =item *
17341: 
17342: courseacclog($what) : save message for course using &courselog().  Perform
17343: special processing for specific resource types (problems, exams, quizzes, etc).
17344: 
17345: =item *
17346: 
17347: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17348: as a PerlChildExitHandler
17349: 
17350: =back
17351: 
17352: =head2 Other
17353: 
17354: =over 4
17355: 
17356: =item *
17357: 
17358: symblist($mapname,%newhash) : update symbolic storage links
17359: 
17360: =back
17361: 
17362: =cut
17363: 

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