File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1514: download - view: text, annotated - select for diffs
Sat Jul 29 20:33:26 2023 UTC (11 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain configuration for default cumulative quota for all group portfolio
  spaces in course can be overridden for a specific course by a Domain
  Coordinator.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1514 2023/07/29 20:33:26 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: sub sign_lti {
  419:     my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
  420:     my $chome;
  421:     if (&domain($cdom) ne '') {
  422:         if ($crsdef) {
  423:             $chome = &homeserver($cnum,$cdom);
  424:         } else {
  425:             $chome = &domain($cdom,'primary');
  426:         }
  427:     }
  428:     if ($cdom && $chome && ($chome ne 'no_host')) {
  429:         if ((ref($paramsref) eq 'HASH') &&
  430:             (ref($inforef) eq 'HASH')) {
  431:             my $rep;
  432:             if (grep { $_ eq $chome } &current_machine_ids()) {
  433:                 # domain information is hosted on this machine
  434:                 $rep =
  435:                     &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
  436:                                                      $context,$url,$ltinum,$keynum,
  437:                                                      $perlvar{'lonVersion'},
  438:                                                      $paramsref,$inforef);
  439:                 if (ref($rep) eq 'HASH') {
  440:                     return ('ok',$rep);
  441:                 }
  442:             } else {
  443:                 my ($escurl,$params,$info);
  444:                 $escurl = &escape($url);
  445:                 if (ref($paramsref) eq 'HASH') {
  446:                     $params = &freeze_escape($paramsref);
  447:                 }
  448:                 if (ref($inforef) eq 'HASH') {
  449:                     $info = &freeze_escape($inforef);
  450:                 }
  451:                 $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
  452:             }
  453:             if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
  454:                 return ();
  455:             } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
  456:                      ($inforef->{'respfmt'} eq 'to_authorization_header')) {
  457:                 return ('ok',$rep);
  458:             } else {
  459:                 my %returnhash;
  460:                 foreach my $item (split(/\&/,$rep)) {
  461:                     my ($name,$value)=split(/\=/,$item);
  462:                     $returnhash{&unescape($name)}=&thaw_unescape($value);
  463:                 }
  464:                 return('ok',\%returnhash);
  465:             }
  466:         } else {
  467:             return ();
  468:         }
  469:     } else {
  470:         return ();
  471:         &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
  472:     }
  473: }
  474: 
  475: # -------------------------------------------------- Non-critical communication
  476: sub subreply {
  477:     my ($cmd,$server)=@_;
  478:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  479:     #
  480:     #  With loncnew process trimming, there's a timing hole between lonc server
  481:     #  process exit and the master server picking up the listen on the AF_UNIX
  482:     #  socket.  In that time interval, a lock file will exist:
  483: 
  484:     my $lockfile=$peerfile.".lock";
  485:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  486: 	sleep(0.1);
  487:     }
  488:     # At this point, either a loncnew parent is listening or an old lonc
  489:     # or loncnew child is listening so we can connect or everything's dead.
  490:     #
  491:     #   We'll give the connection a few tries before abandoning it.  If
  492:     #   connection is not possible, we'll con_lost back to the client.
  493:     #   
  494:     my $client;
  495:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  496: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  497: 				      Type    => SOCK_STREAM,
  498: 				      Timeout => 10);
  499: 	if ($client) {
  500: 	    last;		# Connected!
  501: 	} else {
  502: 	    &create_connection(&hostname($server),$server);
  503: 	}
  504:         sleep(0.1);	# Try again later if failed connection.
  505:     }
  506:     my $answer;
  507:     if ($client) {
  508: 	print $client "sethost:$server:$cmd\n";
  509: 	$answer=<$client>;
  510: 	if (!$answer) { $answer="con_lost"; }
  511: 	chomp($answer);
  512:     } else {
  513: 	$answer = 'con_lost';	# Failed connection.
  514:     }
  515:     return $answer;
  516: }
  517: 
  518: sub reply {
  519:     my ($cmd,$server)=@_;
  520:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  521:     my $answer=subreply($cmd,$server);
  522:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  523:         my $logged = $cmd;
  524:         if ($cmd =~ /^encrypt:([^:]+):/) {
  525:             my $subcmd = $1;
  526:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  527:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  528:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
  529:                 ($subcmd eq 'put')) {
  530:                 (undef,undef,my @rest) = split(/:/,$cmd);
  531:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  532:                     splice(@rest,2,1,'Hidden');
  533:                 } elsif ($subcmd eq 'passwd') {
  534:                     splice(@rest,2,2,('Hidden','Hidden'));
  535:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  536:                          ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
  537:                     splice(@rest,3,1,'Hidden');
  538:                 }
  539:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  540:             }
  541:         }
  542:         &logthis("<font color=\"blue\">WARNING:".
  543:                  " $logged to $server returned $answer</font>");
  544:     }
  545:     return $answer;
  546: }
  547: 
  548: # ----------------------------------------------------------- Send USR1 to lonc
  549: 
  550: sub reconlonc {
  551:     my ($lonid) = @_;
  552:     if ($lonid) {
  553:         my $hostname = &hostname($lonid);
  554: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  555: 	if ($hostname && -e $peerfile) {
  556: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  557: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  558: 					     Type    => SOCK_STREAM,
  559: 					     Timeout => 10);
  560: 	    if ($client) {
  561: 		print $client ("reset_retries\n");
  562: 		my $answer=<$client>;
  563: 		#reset just this one.
  564: 	    }
  565: 	}
  566: 	return;
  567:     }
  568: 
  569:     &logthis("Trying to reconnect lonc");
  570:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  571:     if (open(my $fh,"<",$loncfile)) {
  572: 	my $loncpid=<$fh>;
  573:         chomp($loncpid);
  574:         if (kill 0 => $loncpid) {
  575: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  576:             kill USR1 => $loncpid;
  577:             sleep 1;
  578:         } else {
  579: 	    &logthis(
  580:                "<font color=\"blue\">WARNING:".
  581:                " lonc at pid $loncpid not responding, giving up</font>");
  582:         }
  583:     } else {
  584: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  585:     }
  586: }
  587: 
  588: # ------------------------------------------------------ Critical communication
  589: 
  590: sub critical {
  591:     my ($cmd,$server)=@_;
  592:     unless (&hostname($server)) {
  593:         &logthis("<font color=\"blue\">WARNING:".
  594:                " Critical message to unknown server ($server)</font>");
  595:         return 'no_such_host';
  596:     }
  597:     my $answer=reply($cmd,$server);
  598:     if ($answer eq 'con_lost') {
  599: 	&reconlonc($server);
  600: 	my $answer=reply($cmd,$server);
  601:         if ($answer eq 'con_lost') {
  602:             my $now=time;
  603:             my $middlename=$cmd;
  604:             $middlename=substr($middlename,0,16);
  605:             $middlename=~s/\W//g;
  606:             my $dfilename=
  607:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  608:             $dumpcount++;
  609:             {
  610: 		my $dfh;
  611: 		if (open($dfh,">",$dfilename)) {
  612: 		    print $dfh "$cmd\n"; 
  613: 		    close($dfh);
  614: 		}
  615:             }
  616:             sleep 1;
  617:             my $wcmd='';
  618:             {
  619: 		my $dfh;
  620: 		if (open($dfh,"<",$dfilename)) {
  621: 		    $wcmd=<$dfh>; 
  622: 		    close($dfh);
  623: 		}
  624:             }
  625:             chomp($wcmd);
  626:             if ($wcmd eq $cmd) {
  627: 		&logthis("<font color=\"blue\">WARNING: ".
  628:                          "Connection buffer $dfilename: $cmd</font>");
  629:                 &logperm("D:$server:$cmd");
  630: 	        return 'con_delayed';
  631:             } else {
  632:                 &logthis("<font color=\"red\">CRITICAL:"
  633:                         ." Critical connection failed: $server $cmd</font>");
  634:                 &logperm("F:$server:$cmd");
  635:                 return 'con_failed';
  636:             }
  637:         }
  638:     }
  639:     return $answer;
  640: }
  641: 
  642: # ------------------------------------------- check if return value is an error
  643: 
  644: sub error {
  645:     my ($result) = @_;
  646:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  647: 	if ($2 == 2) { return undef; }
  648: 	return $1;
  649:     }
  650:     return undef;
  651: }
  652: 
  653: sub convert_and_load_session_env {
  654:     my ($lonidsdir,$handle)=@_;
  655:     my @profile;
  656:     {
  657: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  658: 	if (!$opened) {
  659: 	    return 0;
  660: 	}
  661: 	flock($idf,LOCK_SH);
  662: 	@profile=<$idf>;
  663: 	close($idf);
  664:     }
  665:     my %temp_env;
  666:     foreach my $line (@profile) {
  667: 	if ($line !~ m/=/) {
  668: 	    return 0;
  669: 	}
  670: 	chomp($line);
  671: 	my ($envname,$envvalue)=split(/=/,$line,2);
  672: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  673:     }
  674:     unlink("$lonidsdir/$handle.id");
  675:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  676: 	    0640)) {
  677: 	%disk_env = %temp_env;
  678: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  679: 	untie(%disk_env);
  680:     }
  681:     return 1;
  682: }
  683: 
  684: # ------------------------------------------- Transfer profile into environment
  685: my $env_loaded;
  686: sub transfer_profile_to_env {
  687:     my ($lonidsdir,$handle,$force_transfer) = @_;
  688:     if (!$force_transfer && $env_loaded) { return; } 
  689: 
  690:     if (!defined($lonidsdir)) {
  691: 	$lonidsdir = $perlvar{'lonIDsDir'};
  692:     }
  693:     if (!defined($handle)) {
  694:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  695:     }
  696: 
  697:     my $convert;
  698:     {
  699:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  700: 	if (!$opened) {
  701: 	    return;
  702: 	}
  703: 	flock($idf,LOCK_SH);
  704: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  705: 		&GDBM_READER(),0640)) {
  706: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  707: 	    untie(%disk_env);
  708: 	} else {
  709: 	    $convert = 1;
  710: 	}
  711:     }
  712:     if ($convert) {
  713: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  714: 	    &logthis("Failed to load session, or convert session.");
  715: 	}
  716:     }
  717: 
  718:     my %remove;
  719:     while ( my $envname = each(%env) ) {
  720:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  721:             if ($time < time-300) {
  722:                 $remove{$key}++;
  723:             }
  724:         }
  725:     }
  726: 
  727:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  728:     $env_loaded=1;
  729:     foreach my $expired_key (keys(%remove)) {
  730:         &delenv($expired_key);
  731:     }
  732: }
  733: 
  734: # ---------------------------------------------------- Check for valid session 
  735: sub check_for_valid_session {
  736:     my ($r,$name,$userhashref,$domref) = @_;
  737:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  738:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  739:     if ($name eq 'lonDAV') {
  740:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  741:     } else {
  742:         $lonidsdir=$r->dir_config('lonIDsDir');
  743:         if ($name eq '') {
  744:             $name = 'lonID';
  745:         }
  746:     }
  747:     if ($name eq 'lonID') {
  748:         $secure = 'lonSID';
  749:         $linkname = 'lonLinkID';
  750:         $pubname = 'lonPubID';
  751:         if (exists($cookies{$secure})) {
  752:             $lonid=$cookies{$secure};
  753:         } elsif (exists($cookies{$name})) {
  754:             $lonid=$cookies{$name};
  755:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  756:             $lonid=$cookies{$linkname};
  757:         } elsif (exists($cookies{$pubname})) {
  758:             $lonid=$cookies{$pubname};
  759:         }
  760:     } else {
  761:         $lonid=$cookies{$name};
  762:     }
  763:     return undef if (!$lonid);
  764: 
  765:     my $handle=&LONCAPA::clean_handle($lonid->value);
  766:     if (-l "$lonidsdir/$handle.id") {
  767:         my $link = readlink("$lonidsdir/$handle.id");
  768:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  769:             $handle = $1;
  770:         }
  771:     }
  772:     if (!-e "$lonidsdir/$handle.id") {
  773:         if ((ref($domref)) && ($name eq 'lonID') && 
  774:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  775:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  776:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  777:                 $$domref = $possudom;
  778:             }
  779:         }
  780:         return undef;
  781:     }
  782: 
  783:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  784:     return undef if (!$opened);
  785: 
  786:     flock($idf,LOCK_SH);
  787:     my %disk_env;
  788:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  789: 	    &GDBM_READER(),0640)) {
  790: 	return undef;	
  791:     }
  792: 
  793:     if (!defined($disk_env{'user.name'})
  794: 	|| !defined($disk_env{'user.domain'})) {
  795:         untie(%disk_env);
  796: 	return undef;
  797:     }
  798: 
  799:     if (ref($userhashref) eq 'HASH') {
  800:         $userhashref->{'name'} = $disk_env{'user.name'};
  801:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  802:         if ($disk_env{'request.role'}) {
  803:             $userhashref->{'role'} = $disk_env{'request.role'};
  804:         }
  805:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  806:         if ($userhashref->{'lti'}) {
  807:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  808:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  809:         }
  810:     }
  811:     untie(%disk_env);
  812: 
  813:     return $handle;
  814: }
  815: 
  816: sub timed_flock {
  817:     my ($file,$lock_type) = @_;
  818:     my $failed=0;
  819:     eval {
  820: 	local $SIG{__DIE__}='DEFAULT';
  821: 	local $SIG{ALRM}=sub {
  822: 	    $failed=1;
  823: 	    die("failed lock");
  824: 	};
  825: 	alarm(13);
  826: 	flock($file,$lock_type);
  827: 	alarm(0);
  828:     };
  829:     if ($failed) {
  830: 	return undef;
  831:     } else {
  832: 	return 1;
  833:     }
  834: }
  835: 
  836: sub get_sessionfile_vars {
  837:     my ($handle,$lonidsdir,$storearr) = @_;
  838:     my %returnhash;
  839:     unless (ref($storearr) eq 'ARRAY') {
  840:         return %returnhash;
  841:     }
  842:     if (-l "$lonidsdir/$handle.id") {
  843:         my $link = readlink("$lonidsdir/$handle.id");
  844:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  845:             $handle = $1;
  846:         }
  847:     }
  848:     if ((-e "$lonidsdir/$handle.id") &&
  849:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  850:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  851:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  852:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  853:                 flock($idf,LOCK_SH);
  854:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  855:                         &GDBM_READER(),0640)) {
  856:                     foreach my $item (@{$storearr}) {
  857:                         $returnhash{$item} = $disk_env{$item};
  858:                     }
  859:                     untie(%disk_env);
  860:                 }
  861:             }
  862:         }
  863:     }
  864:     return %returnhash;
  865: }
  866: 
  867: # ---------------------------------------------------------- Append Environment
  868: 
  869: sub appenv {
  870:     my ($newenv,$roles) = @_;
  871:     if (ref($newenv) eq 'HASH') {
  872:         foreach my $key (keys(%{$newenv})) {
  873:             my $refused = 0;
  874: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  875:                 $refused = 1;
  876:                 if (ref($roles) eq 'ARRAY') {
  877:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  878:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  879:                         $refused = 0;
  880:                     }
  881:                 }
  882:             }
  883:             if ($refused) {
  884:                 &logthis("<font color=\"blue\">WARNING: ".
  885:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  886:                          .'</font>');
  887: 	        delete($newenv->{$key});
  888:             } else {
  889:                 $env{$key}=$newenv->{$key};
  890:             }
  891:         }
  892:         my $lonids = $perlvar{'lonIDsDir'};
  893:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  894:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  895:             if ($opened
  896: 	        && &timed_flock($env_file,LOCK_EX)
  897: 	        &&
  898: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  899: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  900: 	        while (my ($key,$value) = each(%{$newenv})) {
  901: 	            $disk_env{$key} = $value;
  902: 	        }
  903: 	        untie(%disk_env);
  904:             }
  905:         }
  906:     }
  907:     return 'ok';
  908: }
  909: # ----------------------------------------------------- Delete from Environment
  910: 
  911: sub delenv {
  912:     my ($delthis,$regexp,$roles) = @_;
  913:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  914:         my $refused = 1;
  915:         if (ref($roles) eq 'ARRAY') {
  916:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  917:             if (grep(/^\Q$role\E$/,@{$roles})) {
  918:                 $refused = 0;
  919:             }
  920:         }
  921:         if ($refused) {
  922:             &logthis("<font color=\"blue\">WARNING: ".
  923:                      "Attempt to delete from environment ".$delthis);
  924:             return 'error';
  925:         }
  926:     }
  927:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  928:     if ($opened
  929: 	&& &timed_flock($env_file,LOCK_EX)
  930: 	&&
  931: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  932: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  933: 	foreach my $key (keys(%disk_env)) {
  934: 	    if ($regexp) {
  935:                 if ($key=~/^$delthis/) {
  936:                     delete($env{$key});
  937:                     delete($disk_env{$key});
  938:                 } 
  939:             } else {
  940:                 if ($key=~/^\Q$delthis\E/) {
  941: 		    delete($env{$key});
  942: 		    delete($disk_env{$key});
  943: 	        }
  944:             }
  945: 	}
  946: 	untie(%disk_env);
  947:     }
  948:     return 'ok';
  949: }
  950: 
  951: sub get_env_multiple {
  952:     my ($name) = @_;
  953:     my @values;
  954:     if (defined($env{$name})) {
  955:         # exists is it an array
  956:         if (ref($env{$name})) {
  957:             @values=@{ $env{$name} };
  958:         } else {
  959:             $values[0]=$env{$name};
  960:         }
  961:     }
  962:     return(@values);
  963: }
  964: 
  965: # ------------------------------------------------------------------- Locking
  966: 
  967: sub set_lock {
  968:     my ($text)=@_;
  969:     $locknum++;
  970:     my $id=$$.'-'.$locknum;
  971:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  972:              'session.lock.'.$id => $text});
  973:     return $id;
  974: }
  975: 
  976: sub get_locks {
  977:     my $num=0;
  978:     my %texts=();
  979:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  980:        if ($lock=~/\w/) {
  981:           $num++;
  982:           $texts{$lock}=$env{'session.lock.'.$lock};
  983:        }
  984:    }
  985:    return ($num,%texts);
  986: }
  987: 
  988: sub remove_lock {
  989:     my ($id)=@_;
  990:     my $newlocks='';
  991:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  992:        if (($lock=~/\w/) && ($lock ne $id)) {
  993:           $newlocks.=','.$lock;
  994:        }
  995:     }
  996:     &appenv({'session.locks' => $newlocks});
  997:     &delenv('session.lock.'.$id);
  998: }
  999: 
 1000: sub remove_all_locks {
 1001:     my $activelocks=$env{'session.locks'};
 1002:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
 1003:        if ($lock=~/\w/) {
 1004:           &remove_lock($lock);
 1005:        }
 1006:     }
 1007: }
 1008: 
 1009: 
 1010: # ------------------------------------------ Find out current server userload
 1011: sub userload {
 1012:     my $numusers=0;
 1013:     {
 1014: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
 1015: 	my $filename;
 1016: 	my $curtime=time;
 1017: 	while ($filename=readdir(LONIDS)) {
 1018: 	    next if ($filename eq '.' || $filename eq '..');
 1019: 	    next if ($filename =~ /publicuser_\d+\.id/);
 1020:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
 1021: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
 1022: 	    if ($curtime-$mtime < 1800) { $numusers++; }
 1023: 	}
 1024: 	closedir(LONIDS);
 1025:     }
 1026:     my $userloadpercent=0;
 1027:     my $maxuserload=$perlvar{'lonUserLoadLim'};
 1028:     if ($maxuserload) {
 1029: 	$userloadpercent=100*$numusers/$maxuserload;
 1030:     }
 1031:     $userloadpercent=sprintf("%.2f",$userloadpercent);
 1032:     return $userloadpercent;
 1033: }
 1034: 
 1035: # ------------------------------ Find server with least workload from spare.tab
 1036: 
 1037: sub spareserver {
 1038:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
 1039:     my $spare_server;
 1040:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
 1041:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
 1042:                                                      :  $userloadpercent;
 1043:     my ($uint_dom,$remotesessions);
 1044:     if (($udom ne '') && (&domain($udom) ne '')) {
 1045:         my $uprimary_id = &domain($udom,'primary');
 1046:         $uint_dom = &internet_dom($uprimary_id);
 1047:         my %udomdefaults = &get_domain_defaults($udom);
 1048:         $remotesessions = $udomdefaults{'remotesessions'};
 1049:     }
 1050:     my $spareshash = &this_host_spares($udom);
 1051:     if (ref($spareshash) eq 'HASH') {
 1052:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1053:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1054:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
 1055:                                              $try_server));
 1056: 	        ($spare_server, $lowest_load) =
 1057: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
 1058:             }
 1059:         }
 1060: 
 1061:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1062: 
 1063:         if (!$found_server) {
 1064:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1065: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1066:                     next unless (&spare_can_host($udom,$uint_dom,
 1067:                                                  $remotesessions,$try_server));
 1068: 	            ($spare_server, $lowest_load) =
 1069: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1070:                 }
 1071: 	    }
 1072:         }
 1073:     }
 1074: 
 1075:     if (!$want_server_name) {
 1076:         if (defined($spare_server)) {
 1077:             my $hostname = &hostname($spare_server);
 1078:             if (defined($hostname)) {
 1079:                 my $protocol = 'http';
 1080:                 if ($protocol{$spare_server} eq 'https') {
 1081:                     $protocol = $protocol{$spare_server};
 1082:                 }
 1083:                 my $alias = &use_proxy_alias($r,$spare_server);
 1084:                 $hostname = $alias if ($alias ne '');
 1085: 	        $spare_server = $protocol.'://'.$hostname;
 1086:             }
 1087:         }
 1088:     }
 1089:     return $spare_server;
 1090: }
 1091: 
 1092: sub compare_server_load {
 1093:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1094: 
 1095:     if ($required) {
 1096:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1097:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1098:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1099:         if (($major eq '' && $minor eq '') ||
 1100:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1101:             return ($spare_server,$lowest_load);
 1102:         }
 1103:     }
 1104: 
 1105:     my $loadans     = &reply('load',    $try_server);
 1106:     my $userloadans = &reply('userload',$try_server);
 1107: 
 1108:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1109: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1110:     }
 1111: 
 1112:     my $load;
 1113:     if ($loadans =~ /\d/) {
 1114: 	if ($userloadans =~ /\d/) {
 1115: 	    #both are numbers, pick the bigger one
 1116: 	    $load = ($loadans > $userloadans) ? $loadans 
 1117: 		                              : $userloadans;
 1118: 	} else {
 1119: 	    $load = $loadans;
 1120: 	}
 1121:     } else {
 1122: 	$load = $userloadans;
 1123:     }
 1124: 
 1125:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1126: 	$spare_server = $try_server;
 1127: 	$lowest_load  = $load;
 1128:     }
 1129:     return ($spare_server,$lowest_load);
 1130: }
 1131: 
 1132: # --------------------------- ask offload servers if user already has a session
 1133: sub find_existing_session {
 1134:     my ($udom,$uname) = @_;
 1135:     my $spareshash = &this_host_spares($udom);
 1136:     if (ref($spareshash) eq 'HASH') {
 1137:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1138:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1139:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1140:             }
 1141:         }
 1142:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1143:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1144:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1145:             }
 1146:         }
 1147:     }
 1148:     return;
 1149: }
 1150: 
 1151: sub delusersession {
 1152:     my ($lonid,$udom,$uname) = @_;
 1153:     my $uprimary_id = &domain($udom,'primary');
 1154:     my $uintdom = &internet_dom($uprimary_id);
 1155:     my $intdom = &internet_dom($lonid);
 1156:     my $serverhomedom = &host_domain($lonid);
 1157:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1158:         return &reply(join(':','delusersession',
 1159:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1160:     }
 1161:     return;
 1162: }
 1163: 
 1164: # check if user's browser sent load balancer cookie and server still has session
 1165: # and is not overloaded.
 1166: sub check_for_balancer_cookie {
 1167:     my ($r,$update_mtime) = @_;
 1168:     my ($otherserver,$cookie);
 1169:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1170:     if (exists($cookies{'balanceID'})) {
 1171:         my $balid = $cookies{'balanceID'};
 1172:         $cookie=&LONCAPA::clean_handle($balid->value);
 1173:         my $balancedir=$r->dir_config('lonBalanceDir');
 1174:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1175:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1176:                 my ($possudom,$possuname) = ($1,$2);
 1177:                 my $has_session = 0;
 1178:                 if ((&domain($possudom) ne '') &&
 1179:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1180:                     my $try_server;
 1181:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1182:                     if ($opened) {
 1183:                         flock($idf,LOCK_SH);
 1184:                         while (my $line = <$idf>) {
 1185:                             chomp($line);
 1186:                             if (&hostname($line) ne '') {
 1187:                                 $try_server = $line;
 1188:                                 last;
 1189:                             }
 1190:                         }
 1191:                         close($idf);
 1192:                         if (($try_server) &&
 1193:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1194:                             my $lowest_load = 30000;
 1195:                             ($otherserver,$lowest_load) =
 1196:                                 &compare_server_load($try_server,undef,$lowest_load);
 1197:                             if ($otherserver ne '' && $lowest_load < 100) {
 1198:                                 $has_session = 1;
 1199:                             } else {
 1200:                                 undef($otherserver);
 1201:                             }
 1202:                         }
 1203:                     }
 1204:                 }
 1205:                 if ($has_session) {
 1206:                     if ($update_mtime) {
 1207:                         my $atime = my $mtime = time;
 1208:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1209:                     }
 1210:                 } else {
 1211:                     unlink("$balancedir/$cookie.id");
 1212:                 }
 1213:             }
 1214:         }
 1215:     }
 1216:     return ($otherserver,$cookie);
 1217: }
 1218: 
 1219: sub updatebalcookie {
 1220:     my ($cookie,$balancer,$lastentry)=@_;
 1221:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1222:         my ($udom,$uname) = ($1,$2);
 1223:         my $uprimary_id = &domain($udom,'primary');
 1224:         my $uintdom = &internet_dom($uprimary_id);
 1225:         my $intdom = &internet_dom($balancer);
 1226:         my $serverhomedom = &host_domain($balancer);
 1227:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1228:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1229:         }
 1230:     }
 1231:     return;
 1232: }
 1233: 
 1234: sub delbalcookie {
 1235:     my ($cookie,$balancer) =@_;
 1236:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1237:         my ($udom,$uname) = ($1,$2);
 1238:         my $uprimary_id = &domain($udom,'primary');
 1239:         my $uintdom = &internet_dom($uprimary_id);
 1240:         my $intdom = &internet_dom($balancer);
 1241:         my $serverhomedom = &host_domain($balancer);
 1242:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1243:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1244:         }
 1245:     }
 1246: }
 1247: 
 1248: # -------------------------------- ask if server already has a session for user
 1249: sub has_user_session {
 1250:     my ($lonid,$udom,$uname) = @_;
 1251:     my $result = &reply(join(':','userhassession',
 1252: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1253:     return 1 if ($result eq 'ok');
 1254: 
 1255:     return 0;
 1256: }
 1257: 
 1258: # --------- determine least loaded server in a user's domain which allows login
 1259: 
 1260: sub choose_server {
 1261:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1262:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1263:     my %servers = &get_servers($udom);
 1264:     my $lowest_load = 30000;
 1265:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1266:     if ($skiploadbal) {
 1267:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1268:         unless (defined($cached)) {
 1269:             my $cachetime = 60*60*24;
 1270:             my %domconfig =
 1271:                 &get_dom('configuration',['loadbalancing'],$udom);
 1272:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1273:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1274:                                            $cachetime);
 1275:             }
 1276:         }
 1277:     }
 1278:     foreach my $lonhost (keys(%servers)) {
 1279:         if ($skiploadbal) {
 1280:             if (ref($balancers) eq 'HASH') {
 1281:                 next if (exists($balancers->{$lonhost}));
 1282:             }
 1283:         }
 1284:         my $loginvia;
 1285:         if ($checkloginvia) {
 1286:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1287:             if ($loginvia) {
 1288:                 my ($server,$path) = split(/:/,$loginvia);
 1289:                 ($login_host, $lowest_load) =
 1290:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1291:                 if ($login_host eq $server) {
 1292:                     $portal_path = $path;
 1293:                     $isredirect = 1;
 1294:                 }
 1295:             } else {
 1296:                 ($login_host, $lowest_load) =
 1297:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1298:                 if ($login_host eq $lonhost) {
 1299:                     $portal_path = '';
 1300:                     $isredirect = ''; 
 1301:                 }
 1302:             }
 1303:         } else {
 1304:             ($login_host, $lowest_load) =
 1305:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1306:         }
 1307:     }
 1308:     if ($login_host ne '') {
 1309:         $hostname = &hostname($login_host);
 1310:     }
 1311:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1312: }
 1313: 
 1314: sub get_course_sessions {
 1315:     my ($cnum,$cdom,$lastactivity) = @_;
 1316:     my %servers = &internet_dom_servers($cdom);
 1317:     my %returnhash;
 1318:     foreach my $server (sort(keys(%servers))) {
 1319:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1320:         my @pairs=split(/\&/,$rep);
 1321:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1322:             foreach my $item (@pairs) {
 1323:                 my ($key,$value)=split(/=/,$item,2);
 1324:                 $key = &unescape($key);
 1325:                 next if ($key =~ /^error: 2 /);
 1326:                 if (exists($returnhash{$key})) {
 1327:                     next if ($value < $returnhash{$key});
 1328:                 }
 1329:                 $returnhash{$key}=$value;
 1330:             }
 1331:         }
 1332:     }
 1333:     return %returnhash;
 1334: }
 1335: 
 1336: # --------------------------------------------- Try to change a user's password
 1337: 
 1338: sub changepass {
 1339:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1340:     $currentpass = &escape($currentpass);
 1341:     $newpass     = &escape($newpass);
 1342:     my $lonhost = $perlvar{'lonHostID'};
 1343:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1344: 		       $server);
 1345:     if (! $answer) {
 1346: 	&logthis("No reply on password change request to $server ".
 1347: 		 "by $uname in domain $udom.");
 1348:     } elsif ($answer =~ "^ok") {
 1349:         &logthis("$uname in $udom successfully changed their password ".
 1350: 		 "on $server.");
 1351:     } elsif ($answer =~ "^pwchange_failure") {
 1352: 	&logthis("$uname in $udom was unable to change their password ".
 1353: 		 "on $server.  The action was blocked by either lcpasswd ".
 1354: 		 "or pwchange");
 1355:     } elsif ($answer =~ "^non_authorized") {
 1356:         &logthis("$uname in $udom did not get their password correct when ".
 1357: 		 "attempting to change it on $server.");
 1358:     } elsif ($answer =~ "^auth_mode_error") {
 1359:         &logthis("$uname in $udom attempted to change their password despite ".
 1360: 		 "not being locally or internally authenticated on $server.");
 1361:     } elsif ($answer =~ "^unknown_user") {
 1362:         &logthis("$uname in $udom attempted to change their password ".
 1363: 		 "on $server but were unable to because $server is not ".
 1364: 		 "their home server.");
 1365:     } elsif ($answer =~ "^refused") {
 1366: 	&logthis("$server refused to change $uname in $udom password because ".
 1367: 		 "it was sent an unencrypted request to change the password.");
 1368:     } elsif ($answer =~ "invalid_client") {
 1369:         &logthis("$server refused to change $uname in $udom password because ".
 1370:                  "it was a reset by e-mail originating from an invalid server.");
 1371:     } elsif ($answer =~ "^prioruse") {
 1372:        &logthis("$server refused to change $uname in $udom password because ".
 1373:                 "the password had been used before");
 1374:     }
 1375:     return $answer;
 1376: }
 1377: 
 1378: # ----------------------- Try to determine user's current authentication scheme
 1379: 
 1380: sub queryauthenticate {
 1381:     my ($uname,$udom)=@_;
 1382:     my $uhome=&homeserver($uname,$udom);
 1383:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1384: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1385: 	return 'no_host';
 1386:     }
 1387:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1388:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1389: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1390:     }
 1391:     return $answer;
 1392: }
 1393: 
 1394: # --------- Try to authenticate user from domain's lib servers (first this one)
 1395: 
 1396: sub authenticate {
 1397:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1398:     $upass=&escape($upass);
 1399:     $uname= &LONCAPA::clean_username($uname);
 1400:     my $uhome=&homeserver($uname,$udom,1);
 1401:     my $newhome;
 1402:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1403: # Maybe the machine was offline and only re-appeared again recently?
 1404:         &reconlonc();
 1405: # One more
 1406: 	$uhome=&homeserver($uname,$udom,1);
 1407:         if (($uhome eq 'no_host') && $checkdefauth) {
 1408:             if (defined(&domain($udom,'primary'))) {
 1409:                 $newhome=&domain($udom,'primary');
 1410:             }
 1411:             if ($newhome ne '') {
 1412:                 $uhome = $newhome;
 1413:             }
 1414:         }
 1415: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1416: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1417: 	    return 'no_host';
 1418:         }
 1419:     }
 1420:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1421:     if ($answer eq 'authorized') {
 1422:         if ($newhome) {
 1423:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1424:             return 'no_account_on_host'; 
 1425:         } else {
 1426:             &logthis("User $uname at $udom authorized by $uhome");
 1427:             return $uhome;
 1428:         }
 1429:     }
 1430:     if ($answer eq 'non_authorized') {
 1431: 	&logthis("User $uname at $udom rejected by $uhome");
 1432: 	return 'no_host';
 1433:     }
 1434:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1435:     return 'no_host';
 1436: }
 1437: 
 1438: sub can_switchserver {
 1439:     my ($udom,$home) = @_;
 1440:     my ($canswitch,@intdoms);
 1441:     my $internet_names = &get_internet_names($home);
 1442:     if (ref($internet_names) eq 'ARRAY') {
 1443:         @intdoms = @{$internet_names};
 1444:     }
 1445:     my $uint_dom = &internet_dom(&domain($udom,'primary'));
 1446:     if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1447:         $canswitch = 1;
 1448:     } else {
 1449:          my $serverhomeID = &get_server_homeID(&hostname($home));
 1450:          my $serverhomedom = &host_domain($serverhomeID);
 1451:          my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1452:          my %udomdefaults = &get_domain_defaults($udom);
 1453:          my $remoterev = &get_server_loncaparev('',$home);
 1454:          $canswitch = &can_host_session($udom,$home,$remoterev,
 1455:                                         $udomdefaults{'remotesessions'},
 1456:                                         $defdomdefaults{'hostedsessions'});
 1457:     }
 1458:     return $canswitch;
 1459: }
 1460: 
 1461: sub can_host_session {
 1462:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1463:     my $canhost = 1;
 1464:     my $host_idn = &internet_dom($lonhost);
 1465:     if (ref($remotesessions) eq 'HASH') {
 1466:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1467:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1468:                 $canhost = 0;
 1469:             } else {
 1470:                 $canhost = 1;
 1471:             }
 1472:         }
 1473:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1474:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1475:                 $canhost = 1;
 1476:             } else {
 1477:                 $canhost = 0;
 1478:             }
 1479:         }
 1480:         if ($canhost) {
 1481:             if ($remotesessions->{'version'} ne '') {
 1482:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1483:                 if ($reqmajor ne '' && $reqminor ne '') {
 1484:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1485:                         my $major = $1;
 1486:                         my $minor = $2;
 1487:                         if (($major < $reqmajor ) ||
 1488:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1489:                             $canhost = 0;
 1490:                         }
 1491:                     } else {
 1492:                         $canhost = 0;
 1493:                     }
 1494:                 }
 1495:             }
 1496:         }
 1497:     }
 1498:     if ($canhost) {
 1499:         if (ref($hostedsessions) eq 'HASH') {
 1500:             my $uprimary_id = &domain($udom,'primary');
 1501:             my $uint_dom = &internet_dom($uprimary_id);
 1502:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1503:                 if (($uint_dom ne '') && 
 1504:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1505:                     $canhost = 0;
 1506:                 } else {
 1507:                     $canhost = 1;
 1508:                 }
 1509:             }
 1510:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1511:                 if (($uint_dom ne '') && 
 1512:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1513:                     $canhost = 1;
 1514:                 } else {
 1515:                     $canhost = 0;
 1516:                 }
 1517:             }
 1518:         }
 1519:     }
 1520:     return $canhost;
 1521: }
 1522: 
 1523: sub spare_can_host {
 1524:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1525:     my $canhost=1;
 1526:     my $try_server_hostname = &hostname($try_server);
 1527:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1528:     my $serverhomedom = &host_domain($serverhomeID);
 1529:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1530:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1531:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1532:             $canhost = 0;
 1533:         }
 1534:     }
 1535:     if ($canhost) {
 1536:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1537:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1538:                 unless (&shared_institution($udom,$try_server)) {
 1539:                     $canhost = 0;
 1540:                 }
 1541:             }
 1542:         }
 1543:     }
 1544:     if (($canhost) && ($uint_dom)) {
 1545:         my @intdoms;
 1546:         my $internet_names = &get_internet_names($try_server);
 1547:         if (ref($internet_names) eq 'ARRAY') {
 1548:             @intdoms = @{$internet_names};
 1549:         }
 1550:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1551:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1552:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1553:                                          $remotesessions,
 1554:                                          $defdomdefaults{'hostedsessions'});
 1555:         }
 1556:     }
 1557:     return $canhost;
 1558: }
 1559: 
 1560: sub this_host_spares {
 1561:     my ($dom) = @_;
 1562:     my ($dom_in_use,$lonhost_in_use,$result);
 1563:     my @hosts = &current_machine_ids();
 1564:     foreach my $lonhost (@hosts) {
 1565:         if (&host_domain($lonhost) eq $dom) {
 1566:             $dom_in_use = $dom;
 1567:             $lonhost_in_use = $lonhost;
 1568:             last;
 1569:         }
 1570:     }
 1571:     if ($dom_in_use ne '') {
 1572:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1573:     }
 1574:     if (ref($result) ne 'HASH') {
 1575:         $lonhost_in_use = $perlvar{'lonHostID'};
 1576:         $dom_in_use = &host_domain($lonhost_in_use);
 1577:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1578:         if (ref($result) ne 'HASH') {
 1579:             $result = \%spareid;
 1580:         }
 1581:     }
 1582:     return $result;
 1583: }
 1584: 
 1585: sub spares_for_offload  {
 1586:     my ($dom_in_use,$lonhost_in_use) = @_;
 1587:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1588:     if (defined($cached)) {
 1589:         return $result;
 1590:     } else {
 1591:         my $cachetime = 60*60*24;
 1592:         my %domconfig =
 1593:             &get_dom('configuration',['usersessions'],$dom_in_use);
 1594:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1595:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1596:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1597:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1598:                 }
 1599:             }
 1600:         }
 1601:     }
 1602:     return;
 1603: }
 1604: 
 1605: sub get_lonbalancer_config {
 1606:     my ($servers) = @_;
 1607:     my ($currbalancer,$currtargets);
 1608:     if (ref($servers) eq 'HASH') {
 1609:         foreach my $server (keys(%{$servers})) {
 1610:             my %what = (
 1611:                          spareid => 1,
 1612:                          perlvar => 1,
 1613:                        );
 1614:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1615:             if ($result eq 'ok') {
 1616:                 if (ref($returnhash) eq 'HASH') {
 1617:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1618:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1619:                             $currbalancer = $server;
 1620:                             $currtargets = {};
 1621:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1622:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1623:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1624:                                 }
 1625:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1626:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1627:                                 }
 1628:                             }
 1629:                             last;
 1630:                         }
 1631:                     }
 1632:                 }
 1633:             }
 1634:         }
 1635:     }
 1636:     return ($currbalancer,$currtargets);
 1637: }
 1638: 
 1639: sub check_loadbalancing {
 1640:     my ($uname,$udom,$caller) = @_;
 1641:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1642:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1643:     my $lonhost = $perlvar{'lonHostID'};
 1644:     my @hosts = &current_machine_ids();
 1645:     my $uprimary_id = &domain($udom,'primary');
 1646:     my $uintdom = &internet_dom($uprimary_id);
 1647:     my $intdom = &internet_dom($lonhost);
 1648:     my $serverhomedom = &host_domain($lonhost);
 1649:     my $domneedscache;
 1650:     my $cachetime = 60*60*24;
 1651: 
 1652:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1653:         $dom_in_use = $udom;
 1654:         $homeintdom = 1;
 1655:     } else {
 1656:         $dom_in_use = $serverhomedom;
 1657:     }
 1658:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1659:     unless (defined($cached)) {
 1660:         my %domconfig =
 1661:             &get_dom('configuration',['loadbalancing'],$dom_in_use);
 1662:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1663:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1664:         } else {
 1665:             $domneedscache = $dom_in_use;
 1666:         }
 1667:     }
 1668:     if (ref($result) eq 'HASH') {
 1669:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1670:             &check_balancer_result($result,@hosts);
 1671:         if ($is_balancer) {
 1672:             if (ref($currrules) eq 'HASH') {
 1673:                 if ($homeintdom) {
 1674:                     if ($uname ne '') {
 1675:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1676:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1677:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1678:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1679:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1680:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1681:                             }
 1682:                         }
 1683:                         if ($rule_in_effect eq '') {
 1684:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1685:                             if ($userenv{'inststatus'} ne '') {
 1686:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1687:                                 my ($othertitle,$usertypes,$types) =
 1688:                                     &Apache::loncommon::sorted_inst_types($udom);
 1689:                                 if (ref($types) eq 'ARRAY') {
 1690:                                     foreach my $type (@{$types}) {
 1691:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1692:                                             if (exists($currrules->{$type})) {
 1693:                                                 $rule_in_effect = $currrules->{$type};
 1694:                                             }
 1695:                                         }
 1696:                                     }
 1697:                                 }
 1698:                             } else {
 1699:                                 if (exists($currrules->{'default'})) {
 1700:                                     $rule_in_effect = $currrules->{'default'};
 1701:                                 }
 1702:                             }
 1703:                         }
 1704:                     } else {
 1705:                         if (exists($currrules->{'default'})) {
 1706:                             $rule_in_effect = $currrules->{'default'};
 1707:                         }
 1708:                     }
 1709:                 } else {
 1710:                     if ($currrules->{'_LC_external'} ne '') {
 1711:                         $rule_in_effect = $currrules->{'_LC_external'};
 1712:                     }
 1713:                 }
 1714:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1715:                                                        $uname,$udom);
 1716:             }
 1717:         }
 1718:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1719:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1720:         unless (defined($cached)) {
 1721:             my %domconfig =
 1722:                 &get_dom('configuration',['loadbalancing'],$serverhomedom);
 1723:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1724:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1725:             } else {
 1726:                 $domneedscache = $serverhomedom;
 1727:             }
 1728:         }
 1729:         if (ref($result) eq 'HASH') {
 1730:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1731:                 &check_balancer_result($result,@hosts);
 1732:             if ($is_balancer) {
 1733:                 if (ref($currrules) eq 'HASH') {
 1734:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1735:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1736:                     }
 1737:                 }
 1738:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1739:                                                        $uname,$udom);
 1740:             }
 1741:         } else {
 1742:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1743:                 $is_balancer = 1;
 1744:                 $offloadto = &this_host_spares($dom_in_use);
 1745:             }
 1746:             unless (defined($cached)) {
 1747:                 $domneedscache = $serverhomedom;
 1748:             }
 1749:         }
 1750:     } else {
 1751:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1752:             $is_balancer = 1;
 1753:             $offloadto = &this_host_spares($dom_in_use);
 1754:         }
 1755:         unless (defined($cached)) {
 1756:             $domneedscache = $serverhomedom;
 1757:         }
 1758:     }
 1759:     if ($domneedscache) {
 1760:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1761:     }
 1762:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1763:         my $lowest_load = 30000;
 1764:         if (ref($offloadto) eq 'HASH') {
 1765:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1766:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1767:                     ($otherserver,$lowest_load) =
 1768:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1769:                 }
 1770:             }
 1771:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1772: 
 1773:             if (!$found_server) {
 1774:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1775:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1776:                         ($otherserver,$lowest_load) =
 1777:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1778:                     }
 1779:                 }
 1780:             }
 1781:         } elsif (ref($offloadto) eq 'ARRAY') {
 1782:             if (@{$offloadto} == 1) {
 1783:                 $otherserver = $offloadto->[0];
 1784:             } elsif (@{$offloadto} > 1) {
 1785:                 foreach my $try_server (@{$offloadto}) {
 1786:                     ($otherserver,$lowest_load) =
 1787:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1788:                 }
 1789:             }
 1790:         }
 1791:         unless ($caller eq 'login') {
 1792:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1793:                 $is_balancer = 0;
 1794:                 if ($uname ne '' && $udom ne '') {
 1795:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1796:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1797:                                  'user.loadbalcheck.time' => time});
 1798:                     }
 1799:                 }
 1800:             }
 1801:         }
 1802:     }
 1803:     if (($is_balancer) && (!$homeintdom)) {
 1804:         undef($setcookie);
 1805:     }
 1806:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1807: }
 1808: 
 1809: sub check_balancer_result {
 1810:     my ($result,@hosts) = @_;
 1811:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1812:     if (ref($result) eq 'HASH') {
 1813:         if ($result->{'lonhost'} ne '') {
 1814:             my $currbalancer = $result->{'lonhost'};
 1815:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1816:                 $is_balancer = 1;
 1817:                 $currtargets = $result->{'targets'};
 1818:                 $currrules = $result->{'rules'};
 1819:             }
 1820:             $dom_balancers = $currbalancer;
 1821:         } else {
 1822:             if (keys(%{$result})) {
 1823:                 foreach my $key (keys(%{$result})) {
 1824:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1825:                         (ref($result->{$key}) eq 'HASH')) {
 1826:                         $is_balancer = 1;
 1827:                         $currrules = $result->{$key}{'rules'};
 1828:                         $currtargets = $result->{$key}{'targets'};
 1829:                         $setcookie = $result->{$key}{'cookie'};
 1830:                         last;
 1831:                     }
 1832:                 }
 1833:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1834:             }
 1835:         }
 1836:     }
 1837:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1838: }
 1839: 
 1840: sub get_loadbalancer_targets {
 1841:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1842:     my $offloadto;
 1843:     if ($rule_in_effect eq 'none') {
 1844:         return [$perlvar{'lonHostID'}];
 1845:     } elsif ($rule_in_effect eq '') {
 1846:         $offloadto = $currtargets;
 1847:     } else {
 1848:         if ($rule_in_effect eq 'homeserver') {
 1849:             my $homeserver = &homeserver($uname,$udom);
 1850:             if ($homeserver ne 'no_host') {
 1851:                 $offloadto = [$homeserver];
 1852:             }
 1853:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1854:             my %domconfig =
 1855:                 &get_dom('configuration',['loadbalancing'],$udom);
 1856:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1857:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1858:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1859:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1860:                     }
 1861:                 }
 1862:             } else {
 1863:                 my %servers = &internet_dom_servers($udom);
 1864:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1865:                 if (&hostname($remotebalancer) ne '') {
 1866:                     $offloadto = [$remotebalancer];
 1867:                 }
 1868:             }
 1869:         } elsif (&hostname($rule_in_effect) ne '') {
 1870:             $offloadto = [$rule_in_effect];
 1871:         }
 1872:     }
 1873:     return $offloadto;
 1874: }
 1875: 
 1876: sub internet_dom_servers {
 1877:     my ($dom) = @_;
 1878:     my (%uniqservers,%servers);
 1879:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1880:     my @machinedoms = &machine_domains($primaryserver);
 1881:     foreach my $mdom (@machinedoms) {
 1882:         my %currservers = %servers;
 1883:         my %server = &get_servers($mdom);
 1884:         %servers = (%currservers,%server);
 1885:     }
 1886:     my %by_hostname;
 1887:     foreach my $id (keys(%servers)) {
 1888:         push(@{$by_hostname{$servers{$id}}},$id);
 1889:     }
 1890:     foreach my $hostname (sort(keys(%by_hostname))) {
 1891:         if (@{$by_hostname{$hostname}} > 1) {
 1892:             my $match = 0;
 1893:             foreach my $id (@{$by_hostname{$hostname}}) {
 1894:                 if (&host_domain($id) eq $dom) {
 1895:                     $uniqservers{$id} = $hostname;
 1896:                     $match = 1;
 1897:                 }
 1898:             }
 1899:             unless ($match) {
 1900:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1901:             }
 1902:         } else {
 1903:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1904:         }
 1905:     }
 1906:     return %uniqservers;
 1907: }
 1908: 
 1909: sub trusted_domains {
 1910:     my ($cmdtype,$calldom) = @_;
 1911:     my ($trusted,$untrusted);
 1912:     if (&domain($calldom) eq '') {
 1913:         return ($trusted,$untrusted);
 1914:     }
 1915:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1916:         return ($trusted,$untrusted);
 1917:     }
 1918:     my $callprimary = &domain($calldom,'primary');
 1919:     my $intcalldom = &internet_dom($callprimary);
 1920:     if ($intcalldom eq '') {
 1921:         return ($trusted,$untrusted);
 1922:     }
 1923: 
 1924:     my ($trustconfig,$cached)=&is_cached_new('trust',$calldom);
 1925:     unless (defined($cached)) {
 1926:         my %domconfig = &get_dom('configuration',['trust'],$calldom);
 1927:         &do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1928:         $trustconfig = $domconfig{'trust'};
 1929:     }
 1930:     if (ref($trustconfig)) {
 1931:         my (%possexc,%possinc,@allexc,@allinc); 
 1932:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1933:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1934:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1935:             }
 1936:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1937:                 $possinc{$intcalldom} = 1;
 1938:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1939:             }
 1940:         }
 1941:         if (keys(%possexc)) {
 1942:             if (keys(%possinc)) {
 1943:                 foreach my $key (sort(keys(%possexc))) {
 1944:                     next if ($key eq $intcalldom);
 1945:                     unless ($possinc{$key}) {
 1946:                         push(@allexc,$key);
 1947:                     }
 1948:                 }
 1949:             } else {
 1950:                 @allexc = sort(keys(%possexc));
 1951:             }
 1952:         }
 1953:         if (keys(%possinc)) {
 1954:             $possinc{$intcalldom} = 1;
 1955:             @allinc = sort(keys(%possinc));
 1956:         }
 1957:         if ((@allexc > 0) || (@allinc > 0)) {
 1958:             my %doms_by_intdom;
 1959:             my %allintdoms = &all_host_intdom();
 1960:             my %alldoms = &all_host_domain();
 1961:             foreach my $key (%allintdoms) {
 1962:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1963:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1964:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1965:                     }
 1966:                 } else {
 1967:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1968:                 }
 1969:             }
 1970:             foreach my $exc (@allexc) {
 1971:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1972:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1973:                 }
 1974:             }
 1975:             foreach my $inc (@allinc) {
 1976:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1977:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1978:                 }
 1979:             }
 1980:         }
 1981:     }
 1982:     return ($trusted,$untrusted);
 1983: }
 1984: 
 1985: sub will_trust {
 1986:     my ($cmdtype,$domain,$possdom) = @_;
 1987:     return 1 if ($domain eq $possdom);
 1988:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1989:     my $willtrust; 
 1990:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1991:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1992:             $willtrust = 1;
 1993:         }
 1994:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1995:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1996:             $willtrust = 1;
 1997:         }
 1998:     } else {
 1999:         $willtrust = 1;
 2000:     }
 2001:     return $willtrust;
 2002: }
 2003: 
 2004: # ---------------------- Find the homebase for a user from domain's lib servers
 2005: 
 2006: my %homecache;
 2007: sub homeserver {
 2008:     my ($uname,$udom,$ignoreBadCache)=@_;
 2009:     my $index="$uname:$udom";
 2010: 
 2011:     if (exists($homecache{$index})) { return $homecache{$index}; }
 2012: 
 2013:     my %servers = &get_servers($udom,'library');
 2014:     foreach my $tryserver (keys(%servers)) {
 2015:         next if ($ignoreBadCache ne 'true' && 
 2016: 		 exists($badServerCache{$tryserver}));
 2017: 
 2018: 	my $answer=reply("home:$udom:$uname",$tryserver);
 2019: 	if ($answer eq 'found') {
 2020: 	    delete($badServerCache{$tryserver}); 
 2021: 	    return $homecache{$index}=$tryserver;
 2022: 	} elsif ($answer eq 'no_host') {
 2023: 	    $badServerCache{$tryserver}=1;
 2024: 	}
 2025:     }    
 2026:     return 'no_host';
 2027: }
 2028: 
 2029: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 2030: 
 2031: sub idget {
 2032:     my ($udom,$idsref,$namespace)=@_;
 2033:     my %returnhash=();
 2034:     my @ids=(); 
 2035:     if (ref($idsref) eq 'ARRAY') {
 2036:         @ids = @{$idsref};
 2037:     } else {
 2038:         return %returnhash; 
 2039:     }
 2040:     if ($namespace eq '') {
 2041:         $namespace = 'ids';
 2042:     }
 2043:     
 2044:     my %servers = &get_servers($udom,'library');
 2045:     foreach my $tryserver (keys(%servers)) {
 2046: 	my $idlist=join('&', map { &escape($_); } @ids);
 2047: 	if ($namespace eq 'ids') {
 2048: 	    $idlist=~tr/A-Z/a-z/;
 2049: 	}
 2050: 	my $reply;
 2051: 	if ($namespace eq 'ids') {
 2052: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 2053: 	} else {
 2054: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 2055: 	}
 2056: 	my @answer=();
 2057: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 2058: 	    @answer=split(/\&/,$reply);
 2059: 	}                    ;
 2060: 	my $i;
 2061: 	for ($i=0;$i<=$#ids;$i++) {
 2062: 	    if ($answer[$i]) {
 2063: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 2064: 	    }
 2065: 	}
 2066:     }
 2067:     return %returnhash;
 2068: }
 2069: 
 2070: # ------------------------------------- Find the IDs behind a list of usernames
 2071: 
 2072: sub idrget {
 2073:     my ($udom,@unames)=@_;
 2074:     my %returnhash=();
 2075:     foreach my $uname (@unames) {
 2076:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 2077:     }
 2078:     return %returnhash;
 2079: }
 2080: 
 2081: # Store away a list of names and associated student/employee IDs or clicker IDs
 2082: 
 2083: sub idput {
 2084:     my ($udom,$idsref,$uhom,$namespace)=@_;
 2085:     my %servers=();
 2086:     my %ids=();
 2087:     my %byid = ();
 2088:     if (ref($idsref) eq 'HASH') {
 2089:         %ids=%{$idsref};
 2090:     }
 2091:     if ($namespace eq '') {
 2092:         $namespace = 'ids'; 
 2093:     }
 2094:     foreach my $uname (keys(%ids)) {
 2095: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2096:         if ($uhom eq '') {
 2097:             $uhom=&homeserver($uname,$udom);
 2098:         }
 2099:         if ($uhom ne 'no_host') {
 2100:             my $esc_unam=&escape($uname);
 2101:             if ($namespace eq 'ids') {
 2102:                 my $id=&escape($ids{$uname});
 2103:                 $id=~tr/A-Z/a-z/;
 2104:                 my $esc_unam=&escape($uname);
 2105:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2106:             } else {
 2107:                 my @currids = split(/,/,$ids{$uname});
 2108:                 foreach my $id (@currids) {
 2109:                     $byid{$uhom}{$id} .= $uname.',';
 2110:                 }
 2111:             }
 2112:         }
 2113:     }
 2114:     if ($namespace eq 'clickers') {
 2115:         foreach my $server (keys(%byid)) {
 2116:             if (ref($byid{$server}) eq 'HASH') {
 2117:                 foreach my $id (keys(%{$byid{$server}})) {
 2118:                     $byid{$server} =~ s/,$//;
 2119:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2120:                 }
 2121:             }
 2122:         }
 2123:     }
 2124:     foreach my $server (keys(%servers)) {
 2125:         $servers{$server} =~ s/\&$//;
 2126:         if ($namespace eq 'ids') {     
 2127:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2128:         } else {
 2129:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2130:         }
 2131:     }
 2132: }
 2133: 
 2134: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2135: 
 2136: sub iddel {
 2137:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2138:     my %result=();
 2139:     my %ids=();
 2140:     my %byid = ();
 2141:     if (ref($idshashref) eq 'HASH') {
 2142:         %ids=%{$idshashref};
 2143:     } else {
 2144:         return %result;
 2145:     }
 2146:     if ($namespace eq '') {
 2147:         $namespace = 'ids';
 2148:     }
 2149:     my %servers=();
 2150:     while (my ($id,$unamestr) = each(%ids)) {
 2151:         if ($namespace eq 'ids') {
 2152:             my $uhom = $uhome;
 2153:             if ($uhom eq '') { 
 2154:                 $uhom=&homeserver($unamestr,$udom);
 2155:             }
 2156:             if ($uhom ne 'no_host') {
 2157:                 $servers{$uhom}.='&'.&escape($id);
 2158:             }
 2159:          } else {
 2160:             my @curritems = split(/,/,$ids{$id});
 2161:             foreach my $uname (@curritems) {
 2162:                 my $uhom = $uhome;
 2163:                 if ($uhom eq '') {
 2164:                     $uhom=&homeserver($uname,$udom);
 2165:                 }
 2166:                 if ($uhom ne 'no_host') { 
 2167:                     $byid{$uhom}{$id} .= $uname.',';
 2168:                 }
 2169:             }
 2170:         }
 2171:     }
 2172:     if ($namespace eq 'clickers') {
 2173:         foreach my $server (keys(%byid)) {
 2174:             if (ref($byid{$server}) eq 'HASH') {
 2175:                 foreach my $id (keys(%{$byid{$server}})) {
 2176:                     $byid{$server}{$id} =~ s/,$//;
 2177:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2178:                 }
 2179:             }
 2180:         }
 2181:     }
 2182:     foreach my $server (keys(%servers)) {
 2183:         $servers{$server} =~ s/\&$//;
 2184:         if ($namespace eq 'ids') {
 2185:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2186:         } elsif ($namespace eq 'clickers') {
 2187:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2188:         }
 2189:     }
 2190:     return %result;
 2191: }
 2192: 
 2193: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2194: 
 2195: sub updateclickers {
 2196:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2197:     my %clickers;
 2198:     if (ref($idshashref) eq 'HASH') {
 2199:         %clickers=%{$idshashref};
 2200:     } else {
 2201:         return;
 2202:     }
 2203:     my $items='';
 2204:     foreach my $item (keys(%clickers)) {
 2205:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2206:     }
 2207:     $items=~s/\&$//;
 2208:     my $request = "updateclickers:$udom:$action:$items";
 2209:     if ($critical) {
 2210:         return &critical($request,$uhome);
 2211:     } else {
 2212:         return &reply($request,$uhome);
 2213:     }
 2214: }
 2215: 
 2216: # ------------------------------dump from db file owned by domainconfig user
 2217: sub dump_dom {
 2218:     my ($namespace, $udom, $regexp) = @_;
 2219: 
 2220:     $udom ||= $env{'user.domain'};
 2221: 
 2222:     return () unless $udom;
 2223: 
 2224:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2225: }
 2226: 
 2227: # ------------------------------------------ get items from domain db files   
 2228: 
 2229: sub get_dom {
 2230:     my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
 2231:     return if ($udom eq 'public');
 2232:     my $items='';
 2233:     foreach my $item (@$storearr) {
 2234:         $items.=&escape($item).'&';
 2235:     }
 2236:     $items=~s/\&$//;
 2237:     if (!$udom) {
 2238:         $udom=$env{'user.domain'};
 2239:         return if ($udom eq 'public');
 2240:         if (defined(&domain($udom,'primary'))) {
 2241:             $uhome=&domain($udom,'primary');
 2242:         } else {
 2243:             undef($uhome);
 2244:         }
 2245:     } else {
 2246:         if (!$uhome) {
 2247:             if (defined(&domain($udom,'primary'))) {
 2248:                 $uhome=&domain($udom,'primary');
 2249:             }
 2250:         }
 2251:     }
 2252:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2253:         my $rep;
 2254:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2255:             # domain information is hosted on this machine
 2256:             $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
 2257:         } else {
 2258:             if ($encrypt) {
 2259:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2260:             } else {
 2261:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2262:             }
 2263:         }
 2264:         my %returnhash;
 2265:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2266:             return %returnhash;
 2267:         }
 2268:         my @pairs=split(/\&/,$rep);
 2269:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2270:             return @pairs;
 2271:         }
 2272:         my $i=0;
 2273:         foreach my $item (@$storearr) {
 2274:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2275:             $i++;
 2276:         }
 2277:         return %returnhash;
 2278:     } else {
 2279:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2280:     }
 2281: }
 2282: 
 2283: # -------------------------------------------- put items in domain db files 
 2284: 
 2285: sub put_dom {
 2286:     my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
 2287:     if (!$udom) {
 2288:         $udom=$env{'user.domain'};
 2289:         if (defined(&domain($udom,'primary'))) {
 2290:             $uhome=&domain($udom,'primary');
 2291:         } else {
 2292:             undef($uhome);
 2293:         }
 2294:     } else {
 2295:         if (!$uhome) {
 2296:             if (defined(&domain($udom,'primary'))) {
 2297:                 $uhome=&domain($udom,'primary');
 2298:             }
 2299:         }
 2300:     } 
 2301:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2302:         my $items='';
 2303:         foreach my $item (keys(%$storehash)) {
 2304:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2305:         }
 2306:         $items=~s/\&$//;
 2307:         if ($encrypt) {
 2308:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2309:         } else {
 2310:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2311:         }
 2312:     } else {
 2313:         &logthis("put_dom failed - no homeserver and/or domain");
 2314:     }
 2315: }
 2316: 
 2317: # --------------------- newput for items in db file owned by domainconfig user
 2318: sub newput_dom {
 2319:     my ($namespace,$storehash,$udom) = @_;
 2320:     my $result;
 2321:     if (!$udom) {
 2322:         $udom=$env{'user.domain'};
 2323:     }
 2324:     if ($udom) {
 2325:         my $uname = &get_domainconfiguser($udom);
 2326:         $result = &newput($namespace,$storehash,$udom,$uname);
 2327:     }
 2328:     return $result;
 2329: }
 2330: 
 2331: # --------------------- delete for items in db file owned by domainconfig user
 2332: sub del_dom {
 2333:     my ($namespace,$storearr,$udom)=@_;
 2334:     if (ref($storearr) eq 'ARRAY') {
 2335:         if (!$udom) {
 2336:             $udom=$env{'user.domain'};
 2337:         }
 2338:         if ($udom) {
 2339:             my $uname = &get_domainconfiguser($udom); 
 2340:             return &del($namespace,$storearr,$udom,$uname);
 2341:         }
 2342:     }
 2343: }
 2344: 
 2345: sub store_dom {
 2346:     my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
 2347:     $$storehash{'ip'}=&get_requestor_ip();
 2348:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2349:     my $namevalue='';
 2350:     foreach my $key (keys(%{$storehash})) {
 2351:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 2352:     }
 2353:     $namevalue=~s/\&$//;
 2354:     if (grep { $_ eq $home } current_machine_ids()) {
 2355:         return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
 2356:     } else {
 2357:         if ($namespace eq 'private') {
 2358:             return 'refused';
 2359:         } elsif ($encrypt) {
 2360:             return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
 2361:         } else {
 2362:             return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
 2363:         }
 2364:     }
 2365: }
 2366: 
 2367: sub restore_dom {
 2368:     my ($id,$namespace,$dom,$home,$encrypt) = @_;
 2369:     my $answer;
 2370:     if (grep { $_ eq $home } current_machine_ids()) {
 2371:         $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
 2372:     } elsif ($namespace ne 'private') {
 2373:         if ($encrypt) {
 2374:             $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
 2375:         } else {
 2376:             $answer=&reply("restoredom:$dom:$namespace:$id",$home);
 2377:         }
 2378:     }
 2379:     my %returnhash=();
 2380:     unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') || 
 2381:             ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
 2382:         foreach my $line (split(/\&/,$answer)) {
 2383:             my ($name,$value)=split(/\=/,$line);
 2384:             $returnhash{&unescape($name)}=&thaw_unescape($value);
 2385:         }
 2386:         my $version;
 2387:         for ($version=1;$version<=$returnhash{'version'};$version++) {
 2388:             foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 2389:                 $returnhash{$item}=$returnhash{$version.':'.$item};
 2390:             }
 2391:         }
 2392:     }
 2393:     return %returnhash;
 2394: }
 2395: 
 2396: # ----------------------------------construct domainconfig user for a domain 
 2397: sub get_domainconfiguser {
 2398:     my ($udom) = @_;
 2399:     return $udom.'-domainconfig';
 2400: }
 2401: 
 2402: sub retrieve_inst_usertypes {
 2403:     my ($udom) = @_;
 2404:     my (%returnhash,@order);
 2405:     my %domdefs = &get_domain_defaults($udom);
 2406:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2407:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2408:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2409:     } else {
 2410:         if (defined(&domain($udom,'primary'))) {
 2411:             my $uhome=&domain($udom,'primary');
 2412:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2413:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2414:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2415:                 return (\%returnhash,\@order);
 2416:             }
 2417:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2418:             my @pairs=split(/\&/,$hashitems);
 2419:             foreach my $item (@pairs) {
 2420:                 my ($key,$value)=split(/=/,$item,2);
 2421:                 $key = &unescape($key);
 2422:                 next if ($key =~ /^error: 2 /);
 2423:                 $returnhash{$key}=&thaw_unescape($value);
 2424:             }
 2425:             my @esc_order = split(/\&/,$orderitems);
 2426:             foreach my $item (@esc_order) {
 2427:                 push(@order,&unescape($item));
 2428:             }
 2429:         } else {
 2430:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2431:         }
 2432:         return (\%returnhash,\@order);
 2433:     }
 2434: }
 2435: 
 2436: sub is_domainimage {
 2437:     my ($url) = @_;
 2438:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
 2439:         if (&domain($1) ne '') {
 2440:             return '1';
 2441:         }
 2442:     }
 2443:     return;
 2444: }
 2445: 
 2446: sub inst_directory_query {
 2447:     my ($srch) = @_;
 2448:     my $udom = $srch->{'srchdomain'};
 2449:     my %results;
 2450:     my $homeserver = &domain($udom,'primary');
 2451:     my $outcome;
 2452:     if ($homeserver ne '') {
 2453:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2454:             if ($srch->{'srchby'} eq 'email') {
 2455:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2456:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2457:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2458:                     (($major == 2) && ($minor < 12))) {
 2459:                     return;
 2460:                 }
 2461:             }
 2462:         }
 2463: 	my $queryid=&reply("querysend:instdirsearch:".
 2464: 			   &escape($srch->{'srchby'}).':'.
 2465: 			   &escape($srch->{'srchterm'}).':'.
 2466: 			   &escape($srch->{'srchtype'}),$homeserver);
 2467: 	my $host=&hostname($homeserver);
 2468: 	if ($queryid !~/^\Q$host\E\_/) {
 2469: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2470: 	    return;
 2471: 	}
 2472: 	my $response = &get_query_reply($queryid);
 2473: 	my $maxtries = 5;
 2474: 	my $tries = 1;
 2475: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2476: 	    $response = &get_query_reply($queryid);
 2477: 	    $tries ++;
 2478: 	}
 2479: 
 2480:         if (!&error($response) && $response ne 'refused') {
 2481:             if ($response eq 'unavailable') {
 2482:                 $outcome = $response;
 2483:             } else {
 2484:                 $outcome = 'ok';
 2485:                 my @matches = split(/\n/,$response);
 2486:                 foreach my $match (@matches) {
 2487:                     my ($key,$value) = split(/=/,$match);
 2488:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2489:                 }
 2490:             }
 2491:         }
 2492:     }
 2493:     return ($outcome,%results);
 2494: }
 2495: 
 2496: sub usersearch {
 2497:     my ($srch) = @_;
 2498:     my $dom = $srch->{'srchdomain'};
 2499:     my %results;
 2500:     my %libserv = &all_library();
 2501:     my $query = 'usersearch';
 2502:     foreach my $tryserver (keys(%libserv)) {
 2503:         if (&host_domain($tryserver) eq $dom) {
 2504:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2505:                 if ($srch->{'srchby'} eq 'email') {
 2506:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2507:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2508:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2509:                              (($major == 2) && ($minor < 12)));
 2510:                 }
 2511:             }
 2512:             my $host=&hostname($tryserver);
 2513:             my $queryid=
 2514:                 &reply("querysend:".&escape($query).':'.
 2515:                        &escape($srch->{'srchby'}).':'.
 2516:                        &escape($srch->{'srchtype'}).':'.
 2517:                        &escape($srch->{'srchterm'}),$tryserver);
 2518:             if ($queryid !~/^\Q$host\E\_/) {
 2519:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2520:                 next;
 2521:             }
 2522:             my $reply = &get_query_reply($queryid);
 2523:             my $maxtries = 1;
 2524:             my $tries = 1;
 2525:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2526:                 $reply = &get_query_reply($queryid);
 2527:                 $tries ++;
 2528:             }
 2529:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2530:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2531:             } else {
 2532:                 my @matches;
 2533:                 if ($reply =~ /\n/) {
 2534:                     @matches = split(/\n/,$reply);
 2535:                 } else {
 2536:                     @matches = split(/\&/,$reply);
 2537:                 }
 2538:                 foreach my $match (@matches) {
 2539:                     my ($uname,$udom,%userhash);
 2540:                     foreach my $entry (split(/:/,$match)) {
 2541:                         my ($key,$value) =
 2542:                             map {&unescape($_);} split(/=/,$entry);
 2543:                         $userhash{$key} = $value;
 2544:                         if ($key eq 'username') {
 2545:                             $uname = $value;
 2546:                         } elsif ($key eq 'domain') {
 2547:                             $udom = $value;
 2548:                         }
 2549:                     }
 2550:                     $results{$uname.':'.$udom} = \%userhash;
 2551:                 }
 2552:             }
 2553:         }
 2554:     }
 2555:     return %results;
 2556: }
 2557: 
 2558: sub get_instuser {
 2559:     my ($udom,$uname,$id) = @_;
 2560:     my $homeserver = &domain($udom,'primary');
 2561:     my ($outcome,%results);
 2562:     if ($homeserver ne '') {
 2563:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2564:                            &escape($id).':'.&escape($udom),$homeserver);
 2565:         my $host=&hostname($homeserver);
 2566:         if ($queryid !~/^\Q$host\E\_/) {
 2567:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2568:             return;
 2569:         }
 2570:         my $response = &get_query_reply($queryid);
 2571:         my $maxtries = 5;
 2572:         my $tries = 1;
 2573:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2574:             $response = &get_query_reply($queryid);
 2575:             $tries ++;
 2576:         }
 2577:         if (!&error($response) && $response ne 'refused') {
 2578:             if ($response eq 'unavailable') {
 2579:                 $outcome = $response;
 2580:             } else {
 2581:                 $outcome = 'ok';
 2582:                 my @matches = split(/\n/,$response);
 2583:                 foreach my $match (@matches) {
 2584:                     my ($key,$value) = split(/=/,$match);
 2585:                     $results{&unescape($key)} = &thaw_unescape($value);
 2586:                 }
 2587:             }
 2588:         }
 2589:     }
 2590:     my %userinfo;
 2591:     if (ref($results{$uname}) eq 'HASH') {
 2592:         %userinfo = %{$results{$uname}};
 2593:     } 
 2594:     return ($outcome,%userinfo);
 2595: }
 2596: 
 2597: sub get_multiple_instusers {
 2598:     my ($udom,$users,$caller) = @_;
 2599:     my ($outcome,$results);
 2600:     if (ref($users) eq 'HASH') {
 2601:         my $count = keys(%{$users}); 
 2602:         my $requested = &freeze_escape($users);
 2603:         my $homeserver = &domain($udom,'primary');
 2604:         if ($homeserver ne '') {
 2605:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2606:             my $host=&hostname($homeserver);
 2607:             if ($queryid !~/^\Q$host\E\_/) {
 2608:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2609:                          ' for host: '.$homeserver.'in domain '.$udom);
 2610:                 return ($outcome,$results);
 2611:             }
 2612:             my $response = &get_query_reply($queryid);
 2613:             my $maxtries = 5;
 2614:             if ($count > 100) {
 2615:                 $maxtries = 1+int($count/20);
 2616:             }
 2617:             my $tries = 1;
 2618:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2619:                 $response = &get_query_reply($queryid);
 2620:                 $tries ++;
 2621:             }
 2622:             if ($response eq '') {
 2623:                 $results = {};
 2624:                 foreach my $key (keys(%{$users})) {
 2625:                     my ($uname,$id);
 2626:                     if ($caller eq 'id') {
 2627:                         $id = $key;
 2628:                     } else {
 2629:                         $uname = $key;
 2630:                     }
 2631:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2632:                     $outcome = $resp;
 2633:                     if ($resp eq 'ok') {
 2634:                         %{$results} = (%{$results}, %info);
 2635:                     } else {
 2636:                         last;
 2637:                     }
 2638:                 }
 2639:             } elsif(!&error($response) && ($response ne 'refused')) {
 2640:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2641:                     $outcome = $response;
 2642:                 } else {
 2643:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2644:                     if ($outcome eq 'ok') {
 2645:                         $results = &thaw_unescape($userdata); 
 2646:                     }
 2647:                 }
 2648:             }
 2649:         }
 2650:     }
 2651:     return ($outcome,$results);
 2652: }
 2653: 
 2654: sub inst_rulecheck {
 2655:     my ($udom,$uname,$id,$item,$rules) = @_;
 2656:     my %returnhash;
 2657:     if ($udom ne '') {
 2658:         if (ref($rules) eq 'ARRAY') {
 2659:             @{$rules} = map {&escape($_);} (@{$rules});
 2660:             my $rulestr = join(':',@{$rules});
 2661:             my $homeserver=&domain($udom,'primary');
 2662:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2663:                 my $response;
 2664:                 if ($item eq 'username') {                
 2665:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2666:                                               ':'.&escape($uname).':'.$rulestr,
 2667:                                               $homeserver));
 2668:                 } elsif ($item eq 'id') {
 2669:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2670:                                               ':'.&escape($id).':'.$rulestr,
 2671:                                               $homeserver));
 2672:                 } elsif ($item eq 'selfcreate') {
 2673:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2674:                                                &escape($udom).':'.&escape($uname).
 2675:                                               ':'.$rulestr,$homeserver));
 2676:                 } elsif ($item eq 'unamemap') {
 2677:                     $response=&unescape(&reply('instunamemapcheck:'.
 2678:                                                &escape($udom).':'.&escape($uname).
 2679:                                               ':'.$rulestr,$homeserver));
 2680:                 }
 2681:                 if ($response ne 'refused') {
 2682:                     my @pairs=split(/\&/,$response);
 2683:                     foreach my $item (@pairs) {
 2684:                         my ($key,$value)=split(/=/,$item,2);
 2685:                         $key = &unescape($key);
 2686:                         next if ($key =~ /^error: 2 /);
 2687:                         $returnhash{$key}=&thaw_unescape($value);
 2688:                     }
 2689:                 }
 2690:             }
 2691:         }
 2692:     }
 2693:     return %returnhash;
 2694: }
 2695: 
 2696: sub inst_userrules {
 2697:     my ($udom,$check) = @_;
 2698:     my (%ruleshash,@ruleorder);
 2699:     if ($udom ne '') {
 2700:         my $homeserver=&domain($udom,'primary');
 2701:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2702:             my $response;
 2703:             if ($check eq 'id') {
 2704:                 $response=&reply('instidrules:'.&escape($udom),
 2705:                                  $homeserver);
 2706:             } elsif ($check eq 'email') {
 2707:                 $response=&reply('instemailrules:'.&escape($udom),
 2708:                                  $homeserver);
 2709:             } elsif ($check eq 'unamemap') {
 2710:                 $response=&reply('unamemaprules:'.&escape($udom),
 2711:                                  $homeserver); 
 2712:             } else {
 2713:                 $response=&reply('instuserrules:'.&escape($udom),
 2714:                                  $homeserver);
 2715:             }
 2716:             if (($response ne 'refused') && ($response ne 'error') && 
 2717:                 ($response ne 'unknown_cmd') && 
 2718:                 ($response ne 'no_such_host')) {
 2719:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2720:                 my @pairs=split(/\&/,$hashitems);
 2721:                 foreach my $item (@pairs) {
 2722:                     my ($key,$value)=split(/=/,$item,2);
 2723:                     $key = &unescape($key);
 2724:                     next if ($key =~ /^error: 2 /);
 2725:                     $ruleshash{$key}=&thaw_unescape($value);
 2726:                 }
 2727:                 my @esc_order = split(/\&/,$orderitems);
 2728:                 foreach my $item (@esc_order) {
 2729:                     push(@ruleorder,&unescape($item));
 2730:                 }
 2731:             }
 2732:         }
 2733:     }
 2734:     return (\%ruleshash,\@ruleorder);
 2735: }
 2736: 
 2737: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2738: 
 2739: sub get_domain_defaults {
 2740:     my ($domain,$ignore_cache) = @_;
 2741:     return if (($domain eq '') || ($domain eq 'public'));
 2742:     my $cachetime = 60*60*24;
 2743:     unless ($ignore_cache) {
 2744:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2745:         if (defined($cached)) {
 2746:             if (ref($result) eq 'HASH') {
 2747:                 return %{$result};
 2748:             }
 2749:         }
 2750:     }
 2751:     my %domdefaults;
 2752:     my %domconfig =
 2753:          &get_dom('configuration',['defaults','quotas',
 2754:                                   'requestcourses','inststatus',
 2755:                                   'coursedefaults','usersessions',
 2756:                                   'requestauthor','selfenrollment',
 2757:                                   'coursecategories','ssl','autoenroll',
 2758:                                   'trust','helpsettings','wafproxy',
 2759:                                   'ltisec','toolsec','domexttool',
 2760:                                   'exttool','privacy'],$domain);
 2761:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2762:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2763:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2764:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2765:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2766:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2767:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2768:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2769:         $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
 2770:         $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
 2771:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2772:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2773:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2774:         $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
 2775:     } else {
 2776:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2777:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2778:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2779:     }
 2780:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2781:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2782:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2783:         } else {
 2784:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2785:         }
 2786:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2787:         foreach my $item (@usertools) {
 2788:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2789:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2790:             }
 2791:         }
 2792:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2793:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2794:         }
 2795:     }
 2796:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2797:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2798:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2799:         }
 2800:     }
 2801:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2802:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2803:     }
 2804:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2805:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2806:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2807:         }
 2808:     }
 2809:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2810:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2811:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2812:         $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
 2813:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2814:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2815:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2816:         }
 2817:         foreach my $type (@coursetypes) {
 2818:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2819:                 unless ($type eq 'community') {
 2820:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2821:                 }
 2822:             }
 2823:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2824:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2825:             }
 2826:             if (ref($domconfig{'coursedefaults'}{'coursequota'}) eq 'HASH') {
 2827:                 $domdefaults{$type.'coursequota'} = $domconfig{'coursedefaults'}{'coursequota'}{$type};
 2828:             }
 2829:             if ($domdefaults{'postsubmit'} eq 'on') {
 2830:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2831:                     $domdefaults{$type.'postsubtimeout'} = 
 2832:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2833:                 }
 2834:             }
 2835:             if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
 2836:                 $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
 2837:             } else {
 2838:                 $domdefaults{$type.'domexttool'} = 1;
 2839:             }
 2840:             if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
 2841:                 $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
 2842:             } else {
 2843:                 $domdefaults{$type.'exttool'} = 0;
 2844:             }
 2845:         }
 2846:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2847:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2848:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2849:                 if (@clonecodes) {
 2850:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2851:                 }
 2852:             }
 2853:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2854:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2855:         }
 2856:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2857:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2858:         }
 2859:         if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
 2860:             $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
 2861:         }
 2862:     }
 2863:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2864:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2865:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2866:         }
 2867:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2868:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2869:         }
 2870:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2871:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2872:         }
 2873:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2874:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2875:         }
 2876:     }
 2877:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2878:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2879:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2880:                             'approval','limit');
 2881:             foreach my $type (@coursetypes) {
 2882:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2883:                     my @mgrdc = ();
 2884:                     foreach my $item (@settings) {
 2885:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2886:                             push(@mgrdc,$item);
 2887:                         }
 2888:                     }
 2889:                     if (@mgrdc) {
 2890:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2891:                     }
 2892:                 }
 2893:             }
 2894:         }
 2895:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2896:             foreach my $type (@coursetypes) {
 2897:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2898:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2899:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2900:                     }
 2901:                 }
 2902:             }
 2903:         }
 2904:     }
 2905:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2906:         $domdefaults{'catauth'} = 'std';
 2907:         $domdefaults{'catunauth'} = 'std';
 2908:         if ($domconfig{'coursecategories'}{'auth'}) {
 2909:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2910:         }
 2911:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2912:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2913:         }
 2914:     }
 2915:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2916:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2917:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2918:         }
 2919:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2920:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2921:         }
 2922:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2923:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2924:         }
 2925:     }
 2926:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2927:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2928:         foreach my $prefix (@prefixes) {
 2929:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2930:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2931:             }
 2932:         }
 2933:     }
 2934:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2935:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2936:         $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
 2937:     }
 2938:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2939:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2940:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2941:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2942:         }
 2943:     }
 2944:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2945:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
 2946:             if ($domconfig{'wafproxy'}{$item}) {
 2947:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2948:             }
 2949:         }
 2950:     }
 2951:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
 2952:         if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
 2953:             $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
 2954:             $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
 2955:             $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
 2956:         }
 2957:         if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
 2958:             if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
 2959:                 $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
 2960:             }
 2961:         }
 2962:     }
 2963:     if (ref($domconfig{'toolsec'}) eq 'HASH') {
 2964:         if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
 2965:             $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
 2966:             $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
 2967:         }
 2968:         if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
 2969:             if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
 2970:                 $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
 2971:             }
 2972:         }
 2973:     }
 2974:     if (ref($domconfig{'privacy'}) eq 'HASH') {
 2975:         if (ref($domconfig{'privacy'}{'approval'}) eq 'HASH') {
 2976:             foreach my $domtype ('instdom','extdom') {
 2977:                 if (ref($domconfig{'privacy'}{'approval'}{$domtype}) eq 'HASH') {
 2978:                     foreach my $roletype ('domain','author','course','community') {
 2979:                         if ($domconfig{'privacy'}{'approval'}{$domtype}{$roletype} eq 'user') {
 2980:                             $domdefaults{'userapprovals'} = 1;
 2981:                             last;
 2982:                         }
 2983:                     }
 2984:                 }
 2985:                 last if ($domdefaults{'userapprovals'});
 2986:             }
 2987:         }
 2988:     }
 2989:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2990:     return %domdefaults;
 2991: }
 2992: 
 2993: sub get_dom_cats {
 2994:     my ($dom) = @_;
 2995:     return unless (&domain($dom));
 2996:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2997:     unless (defined($cached)) {
 2998:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2999:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 3000:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 3001:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 3002:             } else {
 3003:                 $cats = {};
 3004:             }
 3005:         } else {
 3006:             $cats = {};
 3007:         }
 3008:         &do_cache_new('cats',$dom,$cats,3600);
 3009:     }
 3010:     return $cats;
 3011: }
 3012: 
 3013: sub get_dom_instcats {
 3014:     my ($dom) = @_;
 3015:     return unless (&domain($dom));
 3016:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 3017:     unless (defined($cached)) {
 3018:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 3019:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 3020:         if ($totcodes > 0) {
 3021:             my $caller = 'global';
 3022:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 3023:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 3024:                 $instcats = {
 3025:                                 totcodes => $totcodes,
 3026:                                 codes => \%codes,
 3027:                                 codetitles => \@codetitles,
 3028:                                 cat_titles => \%cat_titles,
 3029:                                 cat_order => \%cat_order,
 3030:                             };
 3031:                 &do_cache_new('instcats',$dom,$instcats,3600);
 3032:             }
 3033:         }
 3034:     }
 3035:     return $instcats;
 3036: }
 3037: 
 3038: sub retrieve_instcodes {
 3039:     my ($coursecodes,$dom) = @_;
 3040:     my $totcodes;
 3041:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 3042:     foreach my $course (keys(%courses)) {
 3043:         if (ref($courses{$course}) eq 'HASH') {
 3044:             if ($courses{$course}{'inst_code'} ne '') {
 3045:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 3046:                 $totcodes ++;
 3047:             }
 3048:         }
 3049:     }
 3050:     return $totcodes;
 3051: }
 3052: 
 3053: sub course_portal_url {
 3054:     my ($cnum,$cdom,$r) = @_;
 3055:     my $chome = &homeserver($cnum,$cdom);
 3056:     my $hostname = &hostname($chome);
 3057:     my $protocol = $protocol{$chome};
 3058:     $protocol = 'http' if ($protocol ne 'https');
 3059:     my %domdefaults = &get_domain_defaults($cdom);
 3060:     my $firsturl;
 3061:     if ($domdefaults{'portal_def'}) {
 3062:         $firsturl = $domdefaults{'portal_def'};
 3063:     } else {
 3064:         my $alias = &use_proxy_alias($r,$chome);
 3065:         $hostname = $alias if ($alias ne '');
 3066:         $firsturl = $protocol.'://'.$hostname;
 3067:     }
 3068:     return $firsturl;
 3069: }
 3070: 
 3071: sub url_prefix {
 3072:     my ($r,$dom,$home,$context) = @_;
 3073:     my $prefix;
 3074:     my %domdefs = &get_domain_defaults($dom);
 3075:     if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
 3076:         if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
 3077:             $prefix = $1;
 3078:         }
 3079:     }
 3080:     if ($prefix eq '') {
 3081:         my $hostname = &hostname($home);
 3082:         my $protocol = $protocol{$home};
 3083:         $protocol = 'http' if ($protocol{$home} ne 'https');
 3084:         my $alias = &use_proxy_alias($r,$home);
 3085:         $hostname = $alias if ($alias ne '');
 3086:         $prefix = $protocol.'://'.$hostname;
 3087:     }
 3088:     return $prefix;
 3089: }
 3090: 
 3091: # --------------------------------------------- Get domain config for passwords
 3092: 
 3093: sub get_passwdconf {
 3094:     my ($dom) = @_;
 3095:     my (%passwdconf,$gotconf,$lookup);
 3096:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 3097:     if (defined($cached)) {
 3098:         if (ref($result) eq 'HASH') {
 3099:             %passwdconf = %{$result};
 3100:             $gotconf = 1;
 3101:         }
 3102:     }
 3103:     unless ($gotconf) {
 3104:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 3105:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 3106:             %passwdconf = %{$domconfig{'passwords'}};
 3107:         }
 3108:         my $cachetime = 24*60*60;
 3109:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 3110:     }
 3111:     return %passwdconf;
 3112: }
 3113: 
 3114: # --------------------------------------------------- Assign a key to a student
 3115: 
 3116: sub assign_access_key {
 3117: #
 3118: # a valid key looks like uname:udom#comments
 3119: # comments are being appended
 3120: #
 3121:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 3122:     $kdom=
 3123:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 3124:     $knum=
 3125:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 3126:     $cdom=
 3127:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3128:     $cnum=
 3129:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3130:     $udom=$env{'user.name'} unless (defined($udom));
 3131:     $uname=$env{'user.domain'} unless (defined($uname));
 3132:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 3133:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 3134:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 3135:                                                   # assigned to this person
 3136:                                                   # - this should not happen,
 3137:                                                   # unless something went wrong
 3138:                                                   # the first time around
 3139: # ready to assign
 3140:         $logentry=$1.'; '.$logentry;
 3141:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 3142:                                                  $kdom,$knum) eq 'ok') {
 3143: # key now belongs to user
 3144: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 3145:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 3146:                 &appenv({'environment.'.$envkey => $ckey});
 3147:                 return 'ok';
 3148:             } else {
 3149:                 return 
 3150:   'error: Count not permanently assign key, will need to be re-entered later.';
 3151: 	    }
 3152:         } else {
 3153:             return 'error: Could not assign key, try again later.';
 3154:         }
 3155:     } elsif (!$existing{$ckey}) {
 3156: # the key does not exist
 3157: 	return 'error: The key does not exist';
 3158:     } else {
 3159: # the key is somebody else's
 3160: 	return 'error: The key is already in use';
 3161:     }
 3162: }
 3163: 
 3164: # ------------------------------------------ put an additional comment on a key
 3165: 
 3166: sub comment_access_key {
 3167: #
 3168: # a valid key looks like uname:udom#comments
 3169: # comments are being appended
 3170: #
 3171:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 3172:     $cdom=
 3173:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3174:     $cnum=
 3175:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3176:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3177:     if ($existing{$ckey}) {
 3178:         $existing{$ckey}.='; '.$logentry;
 3179: # ready to assign
 3180:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 3181:                                                  $cdom,$cnum) eq 'ok') {
 3182: 	    return 'ok';
 3183:         } else {
 3184: 	    return 'error: Count not store comment.';
 3185:         }
 3186:     } else {
 3187: # the key does not exist
 3188: 	return 'error: The key does not exist';
 3189:     }
 3190: }
 3191: 
 3192: # ------------------------------------------------------ Generate a set of keys
 3193: 
 3194: sub generate_access_keys {
 3195:     my ($number,$cdom,$cnum,$logentry)=@_;
 3196:     $cdom=
 3197:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3198:     $cnum=
 3199:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3200:     unless (&allowed('mky',$cdom)) { return 0; }
 3201:     unless (($cdom) && ($cnum)) { return 0; }
 3202:     if ($number>10000) { return 0; }
 3203:     sleep(2); # make sure don't get same seed twice
 3204:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 3205:     my $total=0;
 3206:     for (my $i=1;$i<=$number;$i++) {
 3207:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 3208:                   sprintf("%lx",int(100000*rand)).'-'.
 3209:                   sprintf("%lx",int(100000*rand));
 3210:        $newkey=~s/1/g/g; # folks mix up 1 and l
 3211:        $newkey=~s/0/h/g; # and also 0 and O
 3212:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 3213:        if ($existing{$newkey}) {
 3214:            $i--;
 3215:        } else {
 3216: 	  if (&put('accesskeys',
 3217:               { $newkey => '# generated '.localtime().
 3218:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 3219:                            '; '.$logentry },
 3220: 		   $cdom,$cnum) eq 'ok') {
 3221:               $total++;
 3222: 	  }
 3223:        }
 3224:     }
 3225:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3226:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3227:     return $total;
 3228: }
 3229: 
 3230: # ------------------------------------------------------- Validate an accesskey
 3231: 
 3232: sub validate_access_key {
 3233:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3234:     $cdom=
 3235:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3236:     $cnum=
 3237:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3238:     $udom=$env{'user.domain'} unless (defined($udom));
 3239:     $uname=$env{'user.name'} unless (defined($uname));
 3240:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3241:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3242: }
 3243: 
 3244: # ------------------------------------- Find the section of student in a course
 3245: sub devalidate_getsection_cache {
 3246:     my ($udom,$unam,$courseid)=@_;
 3247:     my $hashid="$udom:$unam:$courseid";
 3248:     &devalidate_cache_new('getsection',$hashid);
 3249: }
 3250: 
 3251: sub courseid_to_courseurl {
 3252:     my ($courseid) = @_;
 3253:     #already url style courseid
 3254:     return $courseid if ($courseid =~ m{^/});
 3255: 
 3256:     if (exists($env{'course.'.$courseid.'.num'})) {
 3257: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3258: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3259: 	return "/$cdom/$cnum";
 3260:     }
 3261: 
 3262:     my %courseinfo=&coursedescription($courseid);
 3263:     if (exists($courseinfo{'num'})) {
 3264: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3265:     }
 3266: 
 3267:     return undef;
 3268: }
 3269: 
 3270: sub getsection {
 3271:     my ($udom,$unam,$courseid)=@_;
 3272:     my $cachetime=1800;
 3273: 
 3274:     my $hashid="$udom:$unam:$courseid";
 3275:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3276:     if (defined($cached)) { return $result; }
 3277: 
 3278:     my %Pending; 
 3279:     my %Expired;
 3280:     #
 3281:     # Each role can either have not started yet (pending), be active, 
 3282:     #    or have expired.
 3283:     #
 3284:     # If there is an active role, we are done.
 3285:     #
 3286:     # If there is more than one role which has not started yet, 
 3287:     #     choose the one which will start sooner
 3288:     # If there is one role which has not started yet, return it.
 3289:     #
 3290:     # If there is more than one expired role, choose the one which ended last.
 3291:     # If there is a role which has expired, return it.
 3292:     #
 3293:     $courseid = &courseid_to_courseurl($courseid);
 3294:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3295:     foreach my $key (keys(%roleshash)) {
 3296:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3297:         my $section=$1;
 3298:         if ($key eq $courseid.'_st') { $section=''; }
 3299:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3300:         my $now=time;
 3301:         if (defined($end) && $end && ($now > $end)) {
 3302:             $Expired{$end}=$section;
 3303:             next;
 3304:         }
 3305:         if (defined($start) && $start && ($now < $start)) {
 3306:             $Pending{$start}=$section;
 3307:             next;
 3308:         }
 3309:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3310:     }
 3311:     #
 3312:     # Presumedly there will be few matching roles from the above
 3313:     # loop and the sorting time will be negligible.
 3314:     if (scalar(keys(%Pending))) {
 3315:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3316:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3317:     } 
 3318:     if (scalar(keys(%Expired))) {
 3319:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3320:         my $time = pop(@sorted);
 3321:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3322:     }
 3323:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3324: }
 3325: 
 3326: sub save_cache {
 3327:     &purge_remembered();
 3328:     #&Apache::loncommon::validate_page();
 3329:     undef(%env);
 3330:     undef($env_loaded);
 3331: }
 3332: 
 3333: my $to_remember=-1;
 3334: my %remembered;
 3335: my %accessed;
 3336: my $kicks=0;
 3337: my $hits=0;
 3338: sub make_key {
 3339:     my ($name,$id) = @_;
 3340:     if (length($id) > 65 
 3341: 	&& length(&escape($id)) > 200) {
 3342: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3343:     }
 3344:     return &escape($name.':'.$id);
 3345: }
 3346: 
 3347: sub devalidate_cache_new {
 3348:     my ($name,$id,$debug) = @_;
 3349:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3350:     my $remembered_id=$name.':'.$id;
 3351:     $id=&make_key($name,$id);
 3352:     $memcache->delete($id);
 3353:     delete($remembered{$remembered_id});
 3354:     delete($accessed{$remembered_id});
 3355: }
 3356: 
 3357: sub is_cached_new {
 3358:     my ($name,$id,$debug) = @_;
 3359:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3360:     if (exists($remembered{$remembered_id})) {
 3361: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3362: 	$accessed{$remembered_id}=[&gettimeofday()];
 3363: 	$hits++;
 3364: 	return ($remembered{$remembered_id},1);
 3365:     }
 3366:     $id=&make_key($name,$id);
 3367:     my $value = $memcache->get($id);
 3368:     if (!(defined($value))) {
 3369: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3370: 	return (undef,undef);
 3371:     }
 3372:     if ($value eq '__undef__') {
 3373: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3374: 	$value=undef;
 3375:     }
 3376:     &make_room($remembered_id,$value,$debug);
 3377:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3378:     return ($value,1);
 3379: }
 3380: 
 3381: sub do_cache_new {
 3382:     my ($name,$id,$value,$time,$debug) = @_;
 3383:     my $remembered_id=$name.':'.$id;
 3384:     $id=&make_key($name,$id);
 3385:     my $setvalue=$value;
 3386:     if (!defined($setvalue)) {
 3387: 	$setvalue='__undef__';
 3388:     }
 3389:     if (!defined($time) ) {
 3390: 	$time=600;
 3391:     }
 3392:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3393:     my $result = $memcache->set($id,$setvalue,$time);
 3394:     if (! $result) {
 3395: 	&logthis("caching of id -> $id  failed");
 3396: 	$memcache->disconnect_all();
 3397:     }
 3398:     # need to make a copy of $value
 3399:     &make_room($remembered_id,$value,$debug);
 3400:     return $value;
 3401: }
 3402: 
 3403: sub make_room {
 3404:     my ($remembered_id,$value,$debug)=@_;
 3405: 
 3406:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3407:                                     : $value;
 3408:     if ($to_remember<0) { return; }
 3409:     $accessed{$remembered_id}=[&gettimeofday()];
 3410:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3411:     my $to_kick;
 3412:     my $max_time=0;
 3413:     foreach my $other (keys(%accessed)) {
 3414: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3415: 	    $to_kick=$other;
 3416: 	    $max_time=&tv_interval($accessed{$other});
 3417: 	}
 3418:     }
 3419:     delete($remembered{$to_kick});
 3420:     delete($accessed{$to_kick});
 3421:     $kicks++;
 3422:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3423:     return;
 3424: }
 3425: 
 3426: sub purge_remembered {
 3427:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3428:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3429:     undef(%remembered);
 3430:     undef(%accessed);
 3431: }
 3432: # ------------------------------------- Read an entry from a user's environment
 3433: 
 3434: sub userenvironment {
 3435:     my ($udom,$unam,@what)=@_;
 3436:     my $items;
 3437:     foreach my $item (@what) {
 3438:         $items.=&escape($item).'&';
 3439:     }
 3440:     $items=~s/\&$//;
 3441:     my %returnhash=();
 3442:     my $uhome = &homeserver($unam,$udom);
 3443:     unless ($uhome eq 'no_host') {
 3444:         my @answer=split(/\&/, 
 3445:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3446:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3447:             return %returnhash;
 3448:         }
 3449:         my $i;
 3450:         for ($i=0;$i<=$#what;$i++) {
 3451: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3452:         }
 3453:     }
 3454:     return %returnhash;
 3455: }
 3456: 
 3457: # ---------------------------------------------------------- Get a studentphoto
 3458: sub studentphoto {
 3459:     my ($udom,$unam,$ext) = @_;
 3460:     my $home=&homeserver($unam,$udom);
 3461:     if (defined($env{'request.course.id'})) {
 3462:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3463:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3464:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3465:             } else {
 3466:                 my ($result,$perm_reqd)=
 3467: 		    &auto_photo_permission($unam,$udom);
 3468:                 if ($result eq 'ok') {
 3469:                     if (!($perm_reqd eq 'yes')) {
 3470:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3471:                     }
 3472:                 }
 3473:             }
 3474:         }
 3475:     } else {
 3476:         my ($result,$perm_reqd) = 
 3477: 	    &auto_photo_permission($unam,$udom);
 3478:         if ($result eq 'ok') {
 3479:             if (!($perm_reqd eq 'yes')) {
 3480:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3481:             }
 3482:         }
 3483:     }
 3484:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3485: }
 3486: 
 3487: sub retrievestudentphoto {
 3488:     my ($udom,$unam,$ext,$type) = @_;
 3489:     my $home=&homeserver($unam,$udom);
 3490:     my $ret=&reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3491:     if ($ret eq 'ok') {
 3492:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3493:         if ($type eq 'thumbnail') {
 3494:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3495:         }
 3496:         my $tokenurl=&tokenwrapper($url);
 3497:         return $tokenurl;
 3498:     } else {
 3499:         if ($type eq 'thumbnail') {
 3500:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3501:         } else { 
 3502:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3503:         }
 3504:     }
 3505: }
 3506: 
 3507: # -------------------------------------------------------------------- New chat
 3508: 
 3509: sub chatsend {
 3510:     my ($newentry,$anon,$group)=@_;
 3511:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3512:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3513:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3514:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3515: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3516: 		   &escape($newentry)).':'.$group,$chome);
 3517: }
 3518: 
 3519: # ------------------------------------------ Find current version of a resource
 3520: 
 3521: sub getversion {
 3522:     my $fname=&clutter(shift);
 3523:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3524:     return &currentversion(&filelocation('',$fname));
 3525: }
 3526: 
 3527: sub currentversion {
 3528:     my $fname=shift;
 3529:     my $author=$fname;
 3530:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3531:     my ($udom,$uname)=split(/\//,$author);
 3532:     my $home=&homeserver($uname,$udom);
 3533:     if ($home eq 'no_host') { 
 3534:         return -1; 
 3535:     }
 3536:     my $answer=&reply("currentversion:$fname",$home);
 3537:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3538: 	return -1;
 3539:     }
 3540:     return $answer;
 3541: }
 3542: 
 3543: #
 3544: # Return special version number of resource if set by override, empty otherwise
 3545: #
 3546: sub usedversion {
 3547:     my $fname=shift;
 3548:     unless ($fname) { $fname=$env{'request.uri'}; }
 3549:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3550:     if ($urlversion) { return $urlversion; }
 3551:     return '';
 3552: }
 3553: 
 3554: # ----------------------------- Subscribe to a resource, return URL if possible
 3555: 
 3556: sub subscribe {
 3557:     my $fname=shift;
 3558:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3559:     $fname=~s/[\n\r]//g;
 3560:     my $author=$fname;
 3561:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3562:     my ($udom,$uname)=split(/\//,$author);
 3563:     my $home=homeserver($uname,$udom);
 3564:     if ($home eq 'no_host') {
 3565:         return 'not_found';
 3566:     }
 3567:     my $answer=reply("sub:$fname",$home);
 3568:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3569: 	$answer.=' by '.$home;
 3570:     }
 3571:     return $answer;
 3572: }
 3573:     
 3574: # -------------------------------------------------------------- Replicate file
 3575: 
 3576: sub repcopy {
 3577:     my $filename=shift;
 3578:     $filename=~s/\/+/\//g;
 3579:     my $londocroot = $perlvar{'lonDocRoot'};
 3580:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3581:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3582:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3583: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3584: 	return &repcopy_userfile($filename);
 3585:     }
 3586:     $filename=~s/[\n\r]//g;
 3587:     my $transname="$filename.in.transfer";
 3588: # FIXME: this should flock
 3589:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3590:     my $remoteurl=subscribe($filename);
 3591:     if ($remoteurl =~ /^con_lost by/) {
 3592: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3593:            return 'unavailable';
 3594:     } elsif ($remoteurl eq 'not_found') {
 3595: 	   #&logthis("Subscribe returned not_found: $filename");
 3596: 	   return 'not_found';
 3597:     } elsif ($remoteurl =~ /^rejected by/) {
 3598: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3599:            return 'forbidden';
 3600:     } elsif ($remoteurl eq 'directory') {
 3601:            return 'ok';
 3602:     } else {
 3603:         my $author=$filename;
 3604:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3605:         my ($udom,$uname)=split(/\//,$author);
 3606:         my $home=homeserver($uname,$udom);
 3607:         unless ($home eq $perlvar{'lonHostID'}) {
 3608:            my @parts=split(/\//,$filename);
 3609:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3610:            if ($path ne "$londocroot/res") {
 3611:                &logthis("Malconfiguration for replication: $filename");
 3612: 	       return 'bad_request';
 3613:            }
 3614:            my $count;
 3615:            for ($count=5;$count<$#parts;$count++) {
 3616:                $path.="/$parts[$count]";
 3617:                if ((-e $path)!=1) {
 3618: 		   mkdir($path,0777);
 3619:                }
 3620:            }
 3621:            my $request=new HTTP::Request('GET',"$remoteurl");
 3622:            my $response;
 3623:            if ($remoteurl =~ m{/raw/}) {
 3624:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3625:            } else {
 3626:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3627:            }
 3628:            if ($response->is_error()) {
 3629: 	       unlink($transname);
 3630:                my $message=$response->status_line;
 3631:                &logthis("<font color=\"blue\">WARNING:"
 3632:                        ." LWP get: $message: $filename</font>");
 3633:                return 'unavailable';
 3634:            } else {
 3635: 	       if ($remoteurl!~/\.meta$/) {
 3636:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3637:                   my $mresponse;
 3638:                   if ($remoteurl =~ m{/raw/}) {
 3639:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3640:                   } else {
 3641:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3642:                   }
 3643:                   if ($mresponse->is_error()) {
 3644: 		      unlink($filename.'.meta');
 3645:                       &logthis(
 3646:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3647:                   }
 3648: 	       }
 3649:                rename($transname,$filename);
 3650:                return 'ok';
 3651:            }
 3652:        }
 3653:     }
 3654: }
 3655: 
 3656: # ------------------------------------------------- Unsubscribe from a resource
 3657: 
 3658: sub unsubscribe {
 3659:     my ($fname) = @_;
 3660:     my $answer;
 3661:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3662:     $fname=~s/[\n\r]//g;
 3663:     my $author=$fname;
 3664:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3665:     my ($udom,$uname)=split(/\//,$author);
 3666:     my $home=homeserver($uname,$udom);
 3667:     if ($home eq 'no_host') {
 3668:         $answer = 'no_host';
 3669:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3670:         $answer = 'home';
 3671:     } else {
 3672:         my $defdom = $perlvar{'lonDefDomain'};
 3673:         if (&will_trust('content',$defdom,$udom)) {
 3674:             $answer = reply("unsub:$fname",$home);
 3675:         } else {
 3676:             $answer = 'untrusted';
 3677:         }
 3678:     }
 3679:     return $answer;
 3680: }
 3681: 
 3682: # ------------------------------------------------ Get server side include body
 3683: sub ssi_body {
 3684:     my ($filelink,%form)=@_;
 3685:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3686:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3687:     }
 3688:     my $output='';
 3689:     my $response;
 3690:     if ($filelink=~/^https?\:/) {
 3691:        ($output,$response)=&externalssi($filelink);
 3692:     } else {
 3693:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3694:        $filelink .= 'inhibitmenu=yes';
 3695:        ($output,$response)=&ssi($filelink,%form);
 3696:     }
 3697:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3698:     $output=~s/^.*?\<body[^\>]*\>//si;
 3699:     $output=~s/\<\/body\s*\>.*?$//si;
 3700:     if (wantarray) {
 3701:         return ($output, $response);
 3702:     } else {
 3703:         return $output;
 3704:     }
 3705: }
 3706: 
 3707: # --------------------------------------------------------- Server Side Include
 3708: 
 3709: sub absolute_url {
 3710:     my ($host_name,$unalias,$keep_proto) = @_;
 3711:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3712:     if ($host_name eq '') {
 3713: 	$host_name = $ENV{'SERVER_NAME'};
 3714:     }
 3715:     if ($unalias) {
 3716:         my $alias = &get_proxy_alias();
 3717:         if ($alias eq $host_name) {
 3718:             my $lonhost = $perlvar{'lonHostID'};
 3719:             my $hostname = &hostname($lonhost);
 3720:             my $lcproto; 
 3721:             if (($keep_proto) || ($hostname eq '')) {
 3722:                 $lcproto = $protocol;
 3723:             } else {
 3724:                 $lcproto = $protocol{$lonhost};
 3725:                 $lcproto = 'http' if ($lcproto ne 'https');
 3726:                 $lcproto .= '://';
 3727:             }
 3728:             unless ($hostname eq '') {
 3729:                 return $lcproto.$hostname;
 3730:             }
 3731:         }
 3732:     }
 3733:     return $protocol.$host_name;
 3734: }
 3735: 
 3736: #
 3737: #   Server side include.
 3738: # Parameters:
 3739: #  fn     Possibly encrypted resource name/id.
 3740: #  form   Hash that describes how the rendering should be done
 3741: #         and other things.
 3742: # Returns:
 3743: #   Scalar context: The content of the response.
 3744: #   Array context:  2 element list of the content and the full response object.
 3745: #     
 3746: sub ssi {
 3747: 
 3748:     my ($fn,%form)=@_;
 3749:     my ($host,$request,$response);
 3750:     $host = &absolute_url('',1);
 3751: 
 3752:     $form{'no_update_last_known'}=1;
 3753:     &Apache::lonenc::check_encrypt(\$fn);
 3754:     if (%form) {
 3755:       $request=new HTTP::Request('POST',$host.$fn);
 3756:       $request->content(join('&',map { 
 3757:             my $name = escape($_);
 3758:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3759:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3760:             : &escape($form{$_}) );    
 3761:         } keys(%form)));
 3762:     } else {
 3763:       $request=new HTTP::Request('GET',$host.$fn);
 3764:     }
 3765: 
 3766:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3767:     my $lonhost = $perlvar{'lonHostID'};
 3768:     my $islocal;
 3769:     if (($env{'request.course.id'}) &&
 3770:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3771:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3772:         ($form{'grade_symb'} ne '') &&
 3773:         (&allowed('mgr',$env{'request.course.id'}.
 3774:                         ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3775:         $islocal = 1;
 3776:     }
 3777:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3778:                                              '','','',$islocal);
 3779: 
 3780:     if (wantarray) {
 3781: 	return ($response->content, $response);
 3782:     } else {
 3783: 	return $response->content;
 3784:     }
 3785: }
 3786: 
 3787: sub externalssi {
 3788:     my ($url)=@_;
 3789:     my $request=new HTTP::Request('GET',$url);
 3790:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3791:     if (wantarray) {
 3792:         return ($response->content, $response);
 3793:     } else {
 3794:         return $response->content;
 3795:     }
 3796: }
 3797: 
 3798: 
 3799: # If the local copy of a replicated resource is outdated, trigger a  
 3800: # connection from the homeserver to flush the delayed queue. If no update 
 3801: # happens, remove local copies of outdated resource (and corresponding
 3802: # metadata file).
 3803: 
 3804: sub remove_stale_resfile {
 3805:     my ($url) = @_;
 3806:     my $removed;
 3807:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3808:         my $audom = $1;
 3809:         my $auname = $2;
 3810:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3811:             my $homeserver = &homeserver($auname,$audom);
 3812:             unless (($homeserver eq 'no_host') ||
 3813:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3814:                 my $fname = &filelocation('',$url);
 3815:                 if (-e $fname) {
 3816:                     my $hostname = &hostname($homeserver);
 3817:                     if ($hostname) {
 3818:                         my $protocol = $protocol{$homeserver};
 3819:                         $protocol = 'http' if ($protocol ne 'https');
 3820:                         my $uri = &declutter($url);
 3821:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3822:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3823:                         if ($response->is_success()) {
 3824:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3825:                             my $locmodtime = (stat($fname))[9];
 3826:                             if ($locmodtime < $remmodtime) {
 3827:                                 my $stale;
 3828:                                 my $answer = &reply('pong',$homeserver);
 3829:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3830:                                     sleep(0.2);
 3831:                                     $locmodtime = (stat($fname))[9];
 3832:                                     if ($locmodtime < $remmodtime) {
 3833:                                         my $posstransfer = $fname.'.in.transfer';
 3834:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3835:                                             $removed = 1;
 3836:                                         } else {
 3837:                                             $stale = 1;
 3838:                                         }
 3839:                                     } else {
 3840:                                         $removed = 1;
 3841:                                     }
 3842:                                 } else {
 3843:                                     $stale = 1;
 3844:                                 }
 3845:                                 if ($stale) {
 3846:                                     if (unlink($fname)) {
 3847:                                         if ($uri!~/\.meta$/) {
 3848:                                             if (-e $fname.'.meta') {
 3849:                                                 unlink($fname.'.meta');
 3850:                                             }
 3851:                                         }
 3852:                                         my $unsubresult = &unsubscribe($fname);
 3853:                                         unless ($unsubresult eq 'ok') {
 3854:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3855:                                         }
 3856:                                         $removed = 1;
 3857:                                     }
 3858:                                 }
 3859:                             }
 3860:                         }
 3861:                     }
 3862:                 }
 3863:             }
 3864:         }
 3865:     }
 3866:     return $removed;
 3867: }
 3868: 
 3869: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3870: 
 3871: sub allowuploaded {
 3872:     my ($srcurl,$url)=@_;
 3873:     $url=&clutter(&declutter($url));
 3874:     my $dir=$url;
 3875:     $dir=~s/\/[^\/]+$//;
 3876:     my %httpref=();
 3877:     my $httpurl=&hreflocation('',$url);
 3878:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3879:     &Apache::lonnet::appenv(\%httpref);
 3880: }
 3881: 
 3882: #
 3883: # Determine if the current user should be able to edit a particular resource,
 3884: # when viewing in course context.
 3885: # (a) When viewing resource used to determine if "Edit" item is included in 
 3886: #     Functions.
 3887: # (b) When displaying folder contents in course editor, used to determine if
 3888: #     "Edit" link will be displayed alongside resource.
 3889: #
 3890: #  input: six args -- filename (decluttered), course number, course domain,
 3891: #                   url, symb (if registered) and group (if this is a group
 3892: #                   item -- e.g., bulletin board, group page etc.).
 3893: #  output: array of five scalars -- 
 3894: #          $cfile -- url for file editing if editable on current server
 3895: #          $home -- homeserver of resource (i.e., for author if published,
 3896: #                                           or course if uploaded.).
 3897: #          $switchserver --  1 if server switch will be needed.
 3898: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3899: #          $forceview -- 1 if icon/link should be to go to view mode
 3900: #
 3901: 
 3902: sub can_edit_resource {
 3903:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3904:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3905: #
 3906: # For aboutme pages user can only edit his/her own.
 3907: #
 3908:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3909:         my ($sdom,$sname) = ($1,$2);
 3910:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3911:             $home = $env{'user.home'};
 3912:             $cfile = $resurl;
 3913:             if ($env{'form.forceedit'}) {
 3914:                 $forceview = 1;
 3915:             } else {
 3916:                 $forceedit = 1;
 3917:             }
 3918:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3919:         } else {
 3920:             return;
 3921:         }
 3922:     }
 3923: 
 3924:     if ($env{'request.course.id'}) {
 3925:         my $crsedit = &allowed('mdc',$env{'request.course.id'});
 3926:         if ($group ne '') {
 3927: # if this is a group homepage or group bulletin board, check group privs
 3928:             my $allowed = 0;
 3929:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3930:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3931:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3932:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3933:                     $allowed = 1;
 3934:                 }
 3935:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3936:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3937:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3938:                     $allowed = 1;
 3939:                 }
 3940:             }
 3941:             if ($allowed) {
 3942:                 $home=&homeserver($cnum,$cdom);
 3943:                 if ($env{'form.forceedit'}) {
 3944:                     $forceview = 1;
 3945:                 } else {
 3946:                     $forceedit = 1;
 3947:                 }
 3948:                 $cfile = $resurl;
 3949:             } else {
 3950:                 return;
 3951:             }
 3952:         } else {
 3953:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3954:                 unless (&allowed('opa',$env{'request.course.id'})) {
 3955:                     return;
 3956:                 }
 3957:             } elsif (!$crsedit) {
 3958:                 if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
 3959: #
 3960: # No edit allowed where CC has switched to student role.
 3961: #
 3962:                     return;
 3963:                 } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
 3964:                          ($resurl =~ m{^/res/lib/templates/})) {
 3965:                     return;
 3966:                 }
 3967:             }
 3968:         }
 3969:     }
 3970: 
 3971:     if ($file ne '') {
 3972:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3973:             if (&is_course_upload($file,$cnum,$cdom)) {
 3974:                 $uploaded = 1;
 3975:                 $incourse = 1;
 3976:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3977:                     $cfile = &hreflocation('',$file);
 3978:                     if ($env{'form.forceedit'}) {
 3979:                         $forceview = 1;
 3980:                     } else {
 3981:                         $forceedit = 1;
 3982:                     }
 3983:                 }
 3984:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3985:                 $incourse = 1;
 3986:                 if ($env{'form.forceedit'}) {
 3987:                     $forceview = 1;
 3988:                 } else {
 3989:                     $forceedit = 1;
 3990:                 }
 3991:                 $cfile = $resurl;
 3992:             } elsif (($resurl ne '') && (&is_on_map($resurl))) {
 3993:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3994:                     $incourse = 1;
 3995:                     if ($env{'form.forceedit'}) {
 3996:                         $forceview = 1;
 3997:                     } else {
 3998:                         $forceedit = 1;
 3999:                     }
 4000:                     $cfile = $resurl;
 4001:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 4002:                     $incourse = 1;
 4003:                     $cfile = $resurl.'/smpedit';
 4004:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 4005:                     $incourse = 1;
 4006:                     if ($env{'form.forceedit'}) {
 4007:                         $forceview = 1;
 4008:                     } else {
 4009:                         $forceedit = 1;
 4010:                     }
 4011:                     $cfile = $resurl;
 4012:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 4013:                     my ($map,$id,$res) = &decode_symb($symb);
 4014:                     if ($map =~ /\.page$/) {
 4015:                         $incourse = 1;
 4016:                         if ($env{'form.forceedit'}) {
 4017:                             $forceview = 1;
 4018:                             $cfile = $map;
 4019:                         } else {
 4020:                             $forceedit = 1;
 4021:                             $cfile =  '/adm/wrapper'.$resurl;
 4022:                         }
 4023:                     }
 4024:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 4025:                     $incourse = 1;
 4026:                     if ($env{'form.forceedit'}) {
 4027:                         $forceview = 1;
 4028:                     } else {
 4029:                         $forceedit = 1;
 4030:                     }
 4031:                     $cfile = $resurl;
 4032:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 4033:                     $incourse = 1;
 4034:                     if ($env{'form.forceedit'}) {
 4035:                         $forceview = 1;
 4036:                     } else {
 4037:                         $forceedit = 1;
 4038:                     }
 4039:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 4040:                 }
 4041:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 4042:                 my $template = '/res/lib/templates/simpleproblem.problem';
 4043:                 if (&is_on_map($template)) { 
 4044:                     $incourse = 1;
 4045:                     $forceview = 1;
 4046:                     $cfile = $template;
 4047:                 }
 4048:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 4049:                 $incourse = 1;
 4050:                 if ($env{'form.forceedit'}) {
 4051:                     $forceview = 1;
 4052:                 } else {
 4053:                     $forceedit = 1;
 4054:                 }
 4055:                 $cfile = $resurl;
 4056:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 4057:                 $incourse = 1;
 4058:                 if ($env{'form.forceedit'}) {
 4059:                     $forceview = 1;
 4060:                 } else {
 4061:                     $forceedit = 1;
 4062:                 }
 4063:                 $cfile = $resurl;
 4064:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 4065:                 $incourse = 1;
 4066:                 $forceview = 1;
 4067:                 if ($symb) {
 4068:                     my ($map,$id,$res)=&decode_symb($symb);
 4069:                     $env{'request.symb'} = $symb;
 4070:                     $cfile = &clutter($res);
 4071:                 } else {
 4072:                     $cfile = $env{'form.suppurl'};
 4073:                     my $escfile = &unescape($cfile);
 4074:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 4075:                         $cfile = '/adm/wrapper'.$escfile;
 4076:                     } else {
 4077:                         $escfile =~ s{^http://}{};
 4078:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 4079:                     }
 4080:                 }
 4081:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 4082:                 if ($env{'form.forceedit'}) {
 4083:                     $forceview = 1;
 4084:                 } else {
 4085:                     $forceedit = 1;
 4086:                 }
 4087:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 4088:             }
 4089:         }
 4090:         if ($uploaded || $incourse) {
 4091:             $home=&homeserver($cnum,$cdom);
 4092:         } elsif ($file !~ m{/$}) {
 4093:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 4094:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 4095:             # Check that the user has permission to edit this resource
 4096:             my $setpriv = 1;
 4097:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 4098:             if (defined($cfudom)) {
 4099:                 $home=&homeserver($cfuname,$cfudom);
 4100:                 $cfile=$file;
 4101:             }
 4102:         }
 4103:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 4104:             (($home ne '') && ($home ne 'no_host'))) {
 4105:             my @ids=&current_machine_ids();
 4106:             unless (grep(/^\Q$home\E$/,@ids)) {
 4107:                 $switchserver=1;
 4108:             }
 4109:         }
 4110:     }
 4111:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 4112: }
 4113: 
 4114: sub is_course_upload {
 4115:     my ($file,$cnum,$cdom) = @_;
 4116:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 4117:     $uploadpath =~ s{^\/}{};
 4118:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 4119:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 4120:         return 1;
 4121:     }
 4122:     return;
 4123: }
 4124: 
 4125: sub in_course {
 4126:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 4127:     if ($hideprivileged) {
 4128:         my $skipuser;
 4129:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 4130:         my @possdoms = ($cdom);  
 4131:         if ($coursehash{'checkforpriv'}) { 
 4132:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 4133:         }
 4134:         if (&privileged($uname,$udom,\@possdoms)) {
 4135:             $skipuser = 1;
 4136:             if ($coursehash{'nothideprivileged'}) {
 4137:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 4138:                     my $user;
 4139:                     if ($item =~ /:/) {
 4140:                         $user = $item;
 4141:                     } else {
 4142:                         $user = join(':',split(/[\@]/,$item));
 4143:                     }
 4144:                     if ($user eq $uname.':'.$udom) {
 4145:                         undef($skipuser);
 4146:                         last;
 4147:                     }
 4148:                 }
 4149:             }
 4150:             if ($skipuser) {
 4151:                 return 0;
 4152:             }
 4153:         }
 4154:     }
 4155:     $type ||= 'any';
 4156:     if (!defined($cdom) || !defined($cnum)) {
 4157:         my $cid  = $env{'request.course.id'};
 4158:         $cdom = $env{'course.'.$cid.'.domain'};
 4159:         $cnum = $env{'course.'.$cid.'.num'};
 4160:     }
 4161:     my $typesref;
 4162:     if (($type eq 'any') || ($type eq 'all')) {
 4163:         $typesref = ['active','previous','future'];
 4164:     } elsif ($type eq 'previous' || $type eq 'future') {
 4165:         $typesref = [$type];
 4166:     }
 4167:     my %roles = &get_my_roles($uname,$udom,'userroles',
 4168:                               $typesref,undef,[$cdom]);
 4169:     my ($tmp) = keys(%roles);
 4170:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 4171:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 4172:     if (@course_roles > 0) {
 4173:         return 1;
 4174:     }
 4175:     return 0;
 4176: }
 4177: 
 4178: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 4179: # input: action, courseID, current domain, intended
 4180: #        path to file, source of file, instruction to parse file for objects,
 4181: #        ref to hash for embedded objects,
 4182: #        ref to hash for codebase of java objects.
 4183: #        reference to scalar to accommodate mime type determined
 4184: #          from File::MMagic if $parser = parse.
 4185: #
 4186: # output: url to file (if action was uploaddoc), 
 4187: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 4188: #
 4189: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 4190: # course.
 4191: #
 4192: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4193: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 4194: #          course's home server.
 4195: #
 4196: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 4197: #          be copied from $source (current location) to 
 4198: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4199: #         and will then be copied to
 4200: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 4201: #         course's home server.
 4202: #
 4203: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4204: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 4205: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4206: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 4207: #         in course's home server.
 4208: #
 4209: 
 4210: sub process_coursefile {
 4211:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 4212:         $mimetype)=@_;
 4213:     my $fetchresult;
 4214:     my $home=&homeserver($docuname,$docudom);
 4215:     if ($action eq 'propagate') {
 4216:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4217: 			     $home);
 4218:     } else {
 4219:         my $fpath = '';
 4220:         my $fname = $file;
 4221:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4222:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4223:         my $filepath = &build_filepath($fpath);
 4224:         if ($action eq 'copy') {
 4225:             if ($source eq '') {
 4226:                 $fetchresult = 'no source file';
 4227:                 return $fetchresult;
 4228:             } else {
 4229:                 my $destination = $filepath.'/'.$fname;
 4230:                 rename($source,$destination);
 4231:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4232:                                  $home);
 4233:             }
 4234:         } elsif ($action eq 'uploaddoc') {
 4235:             open(my $fh,'>',$filepath.'/'.$fname);
 4236:             print $fh $env{'form.'.$source};
 4237:             close($fh);
 4238:             if ($parser eq 'parse') {
 4239:                 my $mm = new File::MMagic;
 4240:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4241:                 if ($type eq 'text/html') {
 4242:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4243:                     unless ($parse_result eq 'ok') {
 4244:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4245:                     }
 4246:                 }
 4247:                 if (ref($mimetype)) {
 4248:                     $$mimetype = $type;
 4249:                 } 
 4250:             }
 4251:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4252:                                  $home);
 4253:             if ($fetchresult eq 'ok') {
 4254:                 return '/uploaded/'.$fpath.'/'.$fname;
 4255:             } else {
 4256:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4257:                         ' to host '.$home.': '.$fetchresult);
 4258:                 return '/adm/notfound.html';
 4259:             }
 4260:         }
 4261:     }
 4262:     unless ( $fetchresult eq 'ok') {
 4263:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4264:              ' to host '.$home.': '.$fetchresult);
 4265:     }
 4266:     return $fetchresult;
 4267: }
 4268: 
 4269: sub build_filepath {
 4270:     my ($fpath) = @_;
 4271:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4272:     unless ($fpath eq '') {
 4273:         my @parts=split('/',$fpath);
 4274:         foreach my $part (@parts) {
 4275:             $filepath.= '/'.$part;
 4276:             if ((-e $filepath)!=1) {
 4277:                 mkdir($filepath,0777);
 4278:             }
 4279:         }
 4280:     }
 4281:     return $filepath;
 4282: }
 4283: 
 4284: sub store_edited_file {
 4285:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4286:     my $file = $primary_url;
 4287:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4288:     my $fpath = '';
 4289:     my $fname = $file;
 4290:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4291:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4292:     my $filepath = &build_filepath($fpath);
 4293:     open(my $fh,'>',$filepath.'/'.$fname);
 4294:     print $fh $content;
 4295:     close($fh);
 4296:     my $home=&homeserver($docuname,$docudom);
 4297:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4298: 			  $home);
 4299:     if ($$fetchresult eq 'ok') {
 4300:         return '/uploaded/'.$fpath.'/'.$fname;
 4301:     } else {
 4302:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4303: 		 ' to host '.$home.': '.$$fetchresult);
 4304:         return '/adm/notfound.html';
 4305:     }
 4306: }
 4307: 
 4308: sub clean_filename {
 4309:     my ($fname,$args)=@_;
 4310: # Replace Windows backslashes by forward slashes
 4311:     $fname=~s/\\/\//g;
 4312:     if (!$args->{'keep_path'}) {
 4313:         # Get rid of everything but the actual filename
 4314: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4315:     }
 4316: # Replace spaces by underscores
 4317:     $fname=~s/\s+/\_/g;
 4318: # Transliterate non-ascii text to ascii
 4319:     my $lang = &Apache::lonlocal::current_language();
 4320:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4321: # Replace all other weird characters by nothing
 4322:     $fname=~s{[^/\w\.\-]}{}g;
 4323: # Replace all .\d. sequences with _\d. so they no longer look like version
 4324: # numbers
 4325:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4326: # Replace three or more adjacent underscores with one for consistency 
 4327: # with loncfile::filename_check() so complete url can be extracted by
 4328: # lonnet::decode_symb()
 4329:     $fname=~s/_{3,}/_/g;
 4330:     return $fname;
 4331: }
 4332: 
 4333: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4334: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4335: # image with the same aspect ratio as the original, but with dimensions which do 
 4336: # not exceed $resizewidth and $resizeheight.
 4337:  
 4338: sub resizeImage {
 4339:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4340:     my $ima = Image::Magick->new;
 4341:     my $resized;
 4342:     if (-e $img_path) {
 4343:         $ima->Read($img_path);
 4344:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4345:             my $width = $ima->Get('width');
 4346:             my $height = $ima->Get('height');
 4347:             if ($width > $resizewidth) {
 4348: 	        my $factor = $width/$resizewidth;
 4349:                 my $newheight = $height/$factor;
 4350:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4351:                 $resized = 1;
 4352:             }
 4353:         }
 4354:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4355:             my $width = $ima->Get('width');
 4356:             my $height = $ima->Get('height');
 4357:             if ($height > $resizeheight) {
 4358:                 my $factor = $height/$resizeheight;
 4359:                 my $newwidth = $width/$factor;
 4360:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4361:                 $resized = 1;
 4362:             }
 4363:         }
 4364:         if ($resized) {
 4365:             $ima->Write($img_path);
 4366:         }
 4367:     }
 4368:     return;
 4369: }
 4370: 
 4371: # --------------- Take an uploaded file and put it into the userfiles directory
 4372: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4373: #                    the desired filename is in $env{"form.$formname.filename"}
 4374: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4375: #                                    canceloverwrite, scantron, toollogo  or ''.
 4376: #                   if 'coursedoc': upload to the current course
 4377: #                   if 'existingfile': write file to tmp/overwrites directory 
 4378: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4379: #                   $context is passed as argument to &finishuserfileupload
 4380: #        $subdir - directory in userfile to store the file into
 4381: #        $parser - instruction to parse file for objects ($parser = parse) or
 4382: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4383: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4384: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4385: #        $allfiles - reference to hash for embedded objects
 4386: #        $codebase - reference to hash for codebase of java objects
 4387: #        $destuname - username for permanent storage of uploaded file
 4388: #        $destudom - domain for permanaent storage of uploaded file
 4389: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4390: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4391: #        $resizewidth - width (pixels) to which to resize uploaded image
 4392: #        $resizeheight - height (pixels) to which to resize uploaded image
 4393: #        $mimetype - reference to scalar to accommodate mime type determined
 4394: #                    from File::MMagic.
 4395: # 
 4396: # output: url of file in userspace, or error: <message> 
 4397: #             or /adm/notfound.html if failure to upload occurse
 4398: 
 4399: sub userfileupload {
 4400:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4401:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4402:     if (!defined($subdir)) { $subdir='unknown'; }
 4403:     my $fname=$env{'form.'.$formname.'.filename'};
 4404:     $fname=&clean_filename($fname);
 4405:     # See if there is anything left
 4406:     unless ($fname) { return 'error: no uploaded file'; }
 4407:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4408:     if ($fname =~ /^\./) {
 4409:         my ($s,$usec) = &gettimeofday();
 4410:         while (length($usec) < 6) {
 4411:             $usec = '0'.$usec;
 4412:         }
 4413:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4414:     }
 4415:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4416:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4417:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4418:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4419:         my $now = time;
 4420:         my $filepath;
 4421:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4422:              $filepath = 'tmp/helprequests/'.$now;
 4423:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4424:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4425:                          '_'.$env{'user.domain'}.'/pending';
 4426:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4427:             my ($docuname,$docudom);
 4428:             if ($destudom =~ /^$match_domain$/) {
 4429:                 $docudom = $destudom;
 4430:             } else {
 4431:                 $docudom = $env{'user.domain'};
 4432:             }
 4433:             if ($destuname =~ /^$match_username$/) {
 4434:                 $docuname = $destuname;
 4435:             } else {
 4436:                 $docuname = $env{'user.name'};
 4437:             }
 4438:             if (exists($env{'form.group'})) {
 4439:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4440:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4441:             }
 4442:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4443:             if ($context eq 'canceloverwrite') {
 4444:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4445:                 if (-e  $tempfile) {
 4446:                     my @info = stat($tempfile);
 4447:                     if ($info[9] eq $env{'form.timestamp'}) {
 4448:                         unlink($tempfile);
 4449:                     }
 4450:                 }
 4451:                 return;
 4452:             }
 4453:         }
 4454:         # Create the directory if not present
 4455:         my @parts=split(/\//,$filepath);
 4456:         my $fullpath = $perlvar{'lonDaemons'};
 4457:         for (my $i=0;$i<@parts;$i++) {
 4458:             $fullpath .= '/'.$parts[$i];
 4459:             if ((-e $fullpath)!=1) {
 4460:                 mkdir($fullpath,0777);
 4461:             }
 4462:         }
 4463:         open(my $fh,'>',$fullpath.'/'.$fname);
 4464:         print $fh $env{'form.'.$formname};
 4465:         close($fh);
 4466:         if ($context eq 'existingfile') {
 4467:             my @info = stat($fullpath.'/'.$fname);
 4468:             return ($fullpath.'/'.$fname,$info[9]);
 4469:         } else {
 4470:             return $fullpath.'/'.$fname;
 4471:         }
 4472:     }
 4473:     if ($subdir eq 'scantron') {
 4474:         $fname = 'scantron_orig_'.$fname;
 4475:     } else {
 4476:         $fname="$subdir/$fname";
 4477:     }
 4478:     if ($context eq 'coursedoc') {
 4479: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4480: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4481:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4482:             return &finishuserfileupload($docuname,$docudom,
 4483: 					 $formname,$fname,$parser,$allfiles,
 4484: 					 $codebase,$thumbwidth,$thumbheight,
 4485:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4486:         } else {
 4487:             if ($env{'form.folder'}) {
 4488:                 $fname=$env{'form.folder'}.'/'.$fname;
 4489:             }
 4490:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4491: 				       $fname,$formname,$parser,
 4492: 				       $allfiles,$codebase,$mimetype);
 4493:         }
 4494:     } elsif (defined($destuname)) {
 4495:         my $docuname=$destuname;
 4496:         my $docudom=$destudom;
 4497: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4498: 				     $parser,$allfiles,$codebase,
 4499:                                      $thumbwidth,$thumbheight,
 4500:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4501:     } else {
 4502:         my $docuname=$env{'user.name'};
 4503:         my $docudom=$env{'user.domain'};
 4504:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4505:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4506:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4507:         }
 4508: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4509: 				     $parser,$allfiles,$codebase,
 4510:                                      $thumbwidth,$thumbheight,
 4511:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4512:     }
 4513: }
 4514: 
 4515: sub finishuserfileupload {
 4516:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4517:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4518:     my $path=$docudom.'/'.$docuname.'/';
 4519:     my $filepath=$perlvar{'lonDocRoot'};
 4520:   
 4521:     my ($fnamepath,$file,$fetchthumb);
 4522:     $file=$fname;
 4523:     if ($fname=~m|/|) {
 4524:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4525: 	$path.=$fnamepath.'/';
 4526:     }
 4527:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4528:     my $count;
 4529:     for ($count=4;$count<=$#parts;$count++) {
 4530:         $filepath.="/$parts[$count]";
 4531:         if ((-e $filepath)!=1) {
 4532: 	    mkdir($filepath,0777);
 4533:         }
 4534:     }
 4535: 
 4536: # Save the file
 4537:     {
 4538: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4539: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4540: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4541: 	    return '/adm/notfound.html';
 4542: 	}
 4543:         if ($context eq 'overwrite') {
 4544:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4545:             my $target = $filepath.'/'.$file;
 4546:             if (-e $source) {
 4547:                 my @info = stat($source);
 4548:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4549:                     unless (&File::Copy::move($source,$target)) {
 4550:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4551:                         return "Moving from $source failed";
 4552:                     }
 4553:                 } else {
 4554:                     return "Temporary file: $source had unexpected date/time for last modification";
 4555:                 }
 4556:             } else {
 4557:                 return "Temporary file: $source missing";
 4558:             }
 4559:         } elsif (!print FH ($env{'form.'.$formname})) {
 4560: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4561: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4562: 	    return '/adm/notfound.html';
 4563: 	}
 4564: 	close(FH);
 4565:         if ($resizewidth && $resizeheight) {
 4566:             my $mm = new File::MMagic;
 4567:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4568:             if ($mime_type =~ m{^image/}) {
 4569: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4570:             }  
 4571: 	}
 4572:     }
 4573:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4574:         if (ref($mimetype)) {
 4575:             if ($$mimetype eq '') {
 4576:                 my $mm = new File::MMagic;
 4577:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4578:                 $$mimetype = $type;
 4579:             }
 4580:         }
 4581:     }
 4582:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4583:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4584:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4585:                                                        $allfiles,$codebase);
 4586:             unless ($parse_result eq 'ok') {
 4587:                 &logthis('Failed to parse '.$filepath.$file.
 4588: 	   	         ' for embedded media: '.$parse_result); 
 4589:             }
 4590:         }
 4591:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4592:         my $format = $env{'form.scantron_format'};
 4593:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4594:     }
 4595:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4596:         my $input = $filepath.'/'.$file;
 4597:         my $output = $filepath.'/'.'tn-'.$file;
 4598:         my $makethumb; 
 4599:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4600:         if ($context eq 'toollogo') {
 4601:             my ($fullwidth,$fullheight) = &check_dimensions($input);
 4602:             if ($fullwidth ne '' && $fullheight ne '') {
 4603:                 if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 4604:                     $makethumb = 1;
 4605:                 }
 4606:             }
 4607:         } else {
 4608:             $makethumb = 1;
 4609:         }
 4610:         if ($makethumb) {
 4611:             my @args = ('convert','-sample',$thumbsize,$input,$output);
 4612:             system({$args[0]} @args);
 4613:             if (-e $filepath.'/'.'tn-'.$file) {
 4614:                 $fetchthumb  = 1; 
 4615:             }
 4616:         }
 4617:     }
 4618:  
 4619: # Notify homeserver to grep it
 4620: #
 4621:     my $docuhome=&homeserver($docuname,$docudom);	
 4622:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4623:     if ($fetchresult eq 'ok') {
 4624:         if ($fetchthumb) {
 4625:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4626:             if ($thumbresult ne 'ok') {
 4627:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4628:                          $docuhome.': '.$thumbresult);
 4629:             }
 4630:         }
 4631: #
 4632: # Return the URL to it
 4633:         return '/uploaded/'.$path.$file;
 4634:     } else {
 4635:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4636: 		 ': '.$fetchresult);
 4637:         return '/adm/notfound.html';
 4638:     }
 4639: }
 4640: 
 4641: sub extract_embedded_items {
 4642:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4643:     my @state = ();
 4644:     my (%lastids,%related,%shockwave,%flashvars);
 4645:     my %javafiles = (
 4646:                       codebase => '',
 4647:                       code => '',
 4648:                       archive => ''
 4649:                     );
 4650:     my %mediafiles = (
 4651:                       src => '',
 4652:                       movie => '',
 4653:                      );
 4654:     my $p;
 4655:     if ($content) {
 4656:         $p = HTML::LCParser->new($content);
 4657:     } else {
 4658:         $p = HTML::LCParser->new($fullpath);
 4659:     }
 4660:     while (my $t=$p->get_token()) {
 4661: 	if ($t->[0] eq 'S') {
 4662: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4663: 	    push(@state, $tagname);
 4664:             if (lc($tagname) eq 'allow') {
 4665:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4666:             }
 4667: 	    if (lc($tagname) eq 'img') {
 4668: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4669: 	    }
 4670: 	    if (lc($tagname) eq 'a') {
 4671:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4672:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4673:                 }
 4674: 	    }
 4675:             if (lc($tagname) eq 'script') {
 4676:                 my $src;
 4677:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4678:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4679:                 } else {
 4680:                     if ($attr->{'src'} ne '') {
 4681:                         $src = $attr->{'src'};
 4682:                         &add_filetype($allfiles,$src,'src');
 4683:                     }
 4684:                 }
 4685:                 my $text = $p->get_trimmed_text();
 4686:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4687:                     my @swfargs = split(/,/,$1);
 4688:                     foreach my $item (@swfargs) {
 4689:                         $item =~ s/["']//g;
 4690:                         $item =~ s/^\s+//;
 4691:                         $item =~ s/\s+$//;
 4692:                     }
 4693:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4694:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4695:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4696:                         } else {
 4697:                             $related{$swfargs[0]} = [$swfargs[2]];
 4698:                         }
 4699:                     }
 4700:                 }
 4701:             }
 4702:             if (lc($tagname) eq 'link') {
 4703:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4704:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4705:                 }
 4706:             }
 4707: 	    if (lc($tagname) eq 'object' ||
 4708: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4709: 		foreach my $item (keys(%javafiles)) {
 4710: 		    $javafiles{$item} = '';
 4711: 		}
 4712:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4713:                     $lastids{lc($tagname)} = $attr->{'id'};
 4714:                 }
 4715: 	    }
 4716: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4717: 		my $name = lc($attr->{'name'});
 4718: 		foreach my $item (keys(%javafiles)) {
 4719: 		    if ($name eq $item) {
 4720: 			$javafiles{$item} = $attr->{'value'};
 4721: 			last;
 4722: 		    }
 4723: 		}
 4724:                 my $pathfrom;
 4725: 		foreach my $item (keys(%mediafiles)) {
 4726: 		    if ($name eq $item) {
 4727:                         $pathfrom = $attr->{'value'};
 4728:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4729: 			&add_filetype($allfiles,$pathfrom,$name);
 4730: 			last;
 4731: 		    }
 4732: 		}
 4733:                 if ($name eq 'flashvars') {
 4734:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4735:                 }
 4736:                 if ($pathfrom ne '') {
 4737:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4738:                                          $pathfrom);
 4739:                 }
 4740: 	    }
 4741: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4742: 		foreach my $item (keys(%javafiles)) {
 4743: 		    if ($attr->{$item}) {
 4744: 			$javafiles{$item} = $attr->{$item};
 4745: 			last;
 4746: 		    }
 4747: 		}
 4748: 		foreach my $item (keys(%mediafiles)) {
 4749: 		    if ($attr->{$item}) {
 4750: 			&add_filetype($allfiles,$attr->{$item},$item);
 4751: 			last;
 4752: 		    }
 4753: 		}
 4754:                 if (lc($tagname) eq 'embed') {
 4755:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4756:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4757:                                              $attr->{'src'});
 4758:                     }
 4759:                 }
 4760: 	    }
 4761:             if (lc($tagname) eq 'iframe') {
 4762:                 my $src = $attr->{'src'} ;
 4763:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4764:                     &add_filetype($allfiles,$src,'src');
 4765:                 } elsif ($src =~ m{^/}) {
 4766:                     if ($env{'request.course.id'}) {
 4767:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4768:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4769:                         my $url = &hreflocation('',$fullpath);
 4770:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4771:                             my $relpath = $1;
 4772:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4773:                                 &add_filetype($allfiles,$1,'src');
 4774:                             }
 4775:                         }
 4776:                     }
 4777:                 }
 4778:             }
 4779:             if ($t->[4] =~ m{/>$}) {
 4780:                 pop(@state);
 4781:             }
 4782: 	} elsif ($t->[0] eq 'E') {
 4783: 	    my ($tagname) = ($t->[1]);
 4784: 	    if ($javafiles{'codebase'} ne '') {
 4785: 		$javafiles{'codebase'} .= '/';
 4786: 	    }  
 4787: 	    if (lc($tagname) eq 'applet' ||
 4788: 		lc($tagname) eq 'object' ||
 4789: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4790: 		) {
 4791: 		foreach my $item (keys(%javafiles)) {
 4792: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4793: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4794: 			&add_filetype($allfiles,$file,$item);
 4795: 		    }
 4796: 		}
 4797: 	    } 
 4798: 	    pop @state;
 4799: 	}
 4800:     }
 4801:     foreach my $id (sort(keys(%flashvars))) {
 4802:         if ($shockwave{$id} ne '') {
 4803:             my @pairs = split(/\&/,$flashvars{$id});
 4804:             foreach my $pair (@pairs) {
 4805:                 my ($key,$value) = split(/\=/,$pair);
 4806:                 if ($key eq 'thumb') {
 4807:                     &add_filetype($allfiles,$value,$key);
 4808:                 } elsif ($key eq 'content') {
 4809:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4810:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4811:                     if ($ext ne '') {
 4812:                         &add_filetype($allfiles,$path.$value,$ext);
 4813:                     }
 4814:                 }
 4815:             }
 4816:         }
 4817:     }
 4818:     return 'ok';
 4819: }
 4820: 
 4821: sub add_filetype {
 4822:     my ($allfiles,$file,$type)=@_;
 4823:     if (exists($allfiles->{$file})) {
 4824: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4825: 	    push(@{$allfiles->{$file}}, &escape($type));
 4826: 	}
 4827:     } else {
 4828: 	@{$allfiles->{$file}} = (&escape($type));
 4829:     }
 4830: }
 4831: 
 4832: sub embedded_dependency {
 4833:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4834:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4835:         if (($identifier ne '') &&
 4836:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4837:             ($pathfrom ne '')) {
 4838:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4839:             foreach my $dep (@{$related->{$identifier}}) {
 4840:                 &add_filetype($allfiles,$path.$dep,'object');
 4841:             }
 4842:         }
 4843:     }
 4844:     return;
 4845: }
 4846: 
 4847: sub check_dimensions {
 4848:     my ($inputfile) = @_;
 4849:     my ($fullwidth,$fullheight);
 4850:     if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
 4851:         my $mm = new File::MMagic;
 4852:         my $mime_type = $mm->checktype_filename($inputfile);
 4853:         if ($mime_type =~ m{^image/}) {
 4854:             if (open(PIPE,"identify $inputfile 2>&1 |")) {
 4855:                 my $imageinfo = <PIPE>;
 4856:                 if (!close(PIPE)) {
 4857:                     &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 4858:                 }
 4859:                 chomp($imageinfo);
 4860:                 my ($fullsize) =
 4861:                     ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 4862:                 if ($fullsize) {
 4863:                     ($fullwidth,$fullheight) = split(/x/,$fullsize);
 4864:                 }
 4865:             }
 4866:         }
 4867:     }
 4868:     return ($fullwidth,$fullheight);
 4869: }
 4870: 
 4871: sub bubblesheet_converter {
 4872:     my ($cdom,$fullpath,$config,$format) = @_;
 4873:     if ((&domain($cdom) ne '') &&
 4874:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4875:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4876:         my (%csvcols,%csvoptions);
 4877:         if (ref($config->{'fields'}) eq 'HASH') {  
 4878:             %csvcols = %{$config->{'fields'}};
 4879:         }
 4880:         if (ref($config->{'options'}) eq 'HASH') {
 4881:             %csvoptions = %{$config->{'options'}};
 4882:         }
 4883:         my %csvbynum = reverse(%csvcols);
 4884:         my %scantronconf = &get_scantron_config($format,$cdom);
 4885:         if (keys(%scantronconf)) {
 4886:             my %bynum = (
 4887:                           $scantronconf{CODEstart} => 'CODEstart',
 4888:                           $scantronconf{IDstart}   => 'IDstart',
 4889:                           $scantronconf{PaperID}   => 'PaperID',
 4890:                           $scantronconf{FirstName} => 'FirstName',
 4891:                           $scantronconf{LastName}  => 'LastName',
 4892:                           $scantronconf{Qstart}    => 'Qstart',
 4893:                         );
 4894:             my @ordered;
 4895:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4896:                 push(@ordered,$bynum{$item});
 4897:             }
 4898:             my %mapstart = (
 4899:                               CODEstart => 'CODE',
 4900:                               IDstart   => 'ID',
 4901:                               PaperID   => 'PaperID',
 4902:                               FirstName => 'FirstName',
 4903:                               LastName  => 'LastName',
 4904:                               Qstart    => 'FirstQuestion',
 4905:                            );
 4906:             my %maplength = (
 4907:                               CODEstart => 'CODElength',
 4908:                               IDstart   => 'IDlength',
 4909:                               PaperID   => 'PaperIDlength',
 4910:                               FirstName => 'FirstNamelength',
 4911:                               LastName  => 'LastNamelength',
 4912:             );
 4913:             if (open(my $fh,'<',$fullpath)) {
 4914:                 my $output;
 4915:                 my %lettdig = &letter_to_digits();
 4916:                 my %diglett = reverse(%lettdig);
 4917:                 my $numletts = scalar(keys(%lettdig));
 4918:                 my $num = 0;
 4919:                 while (my $line=<$fh>) {
 4920:                     $num ++;
 4921:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4922:                     $line =~ s{[\r\n]+$}{};
 4923:                     my %found;
 4924:                     my @values = split(/,/,$line,-1);
 4925:                     my ($qstart,$record);
 4926:                     for (my $i=0; $i<@values; $i++) {
 4927:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4928:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4929:                             if ($values[$i] eq '') {
 4930:                                 $values[$i] = $scantronconf{'Qoff'};
 4931:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4932:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4933:                                     $values[$i] = $lettdig{uc($values[$i])};
 4934:                                 }
 4935:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4936:                                 if ($values[$i] =~ /^[0-9]$/) {
 4937:                                     $values[$i] = $diglett{$values[$i]};
 4938:                                 }
 4939:                             } else {
 4940:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4941:                                     my $digit;
 4942:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4943:                                         $digit = $lettdig{uc($values[$i])}-1;
 4944:                                         if ($values[$i] eq 'J') {
 4945:                                             $digit += $numletts;
 4946:                                         }
 4947:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4948:                                         $digit = $values[$i]-1;
 4949:                                         if ($values[$i] eq '0') {
 4950:                                             $digit += $numletts;
 4951:                                         }
 4952:                                     }
 4953:                                     my $qval='';
 4954:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4955:                                         if ($j == $digit) {
 4956:                                             $qval .= $scantronconf{'Qon'};
 4957:                                         } else {
 4958:                                             $qval .= $scantronconf{'Qoff'};
 4959:                                         }
 4960:                                     }
 4961:                                     $values[$i] = $qval;
 4962:                                 }
 4963:                             }
 4964:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4965:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4966:                             }
 4967:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4968:                             if ($numblank > 0) {
 4969:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4970:                             }
 4971:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4972:                                 $qstart = $i;
 4973:                                 $found{$csvbynum{$i}} = $values[$i];
 4974:                             } else {
 4975:                                 $found{'FirstQuestion'} .= $values[$i];
 4976:                             }
 4977:                         } elsif (exists($csvbynum{$i})) {
 4978:                             if ($csvoptions{'rem'}) {
 4979:                                 $values[$i] =~ s/^\s+//;
 4980:                             }
 4981:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4982:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4983:                                     $values[$i] = '0'.$values[$i];
 4984:                                 }
 4985:                             }
 4986:                             $found{$csvbynum{$i}} = $values[$i];
 4987:                         }
 4988:                     }
 4989:                     foreach my $item (@ordered) {
 4990:                         my $currlength = 1+length($record);
 4991:                         my $numspaces = $scantronconf{$item} - $currlength;
 4992:                         if ($numspaces > 0) {
 4993:                             $record .= (' ' x $numspaces);
 4994:                         }
 4995:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4996:                             unless ($item eq 'Qstart') {
 4997:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4998:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4999:                                 }
 5000:                             }
 5001:                             $record .= $found{$mapstart{$item}};
 5002:                         }
 5003:                     }
 5004:                     $output .= "$record\n";
 5005:                 }
 5006:                 close($fh);
 5007:                 if ($output) {
 5008:                     if (open(my $fh,'>',$fullpath)) {
 5009:                         print $fh $output;
 5010:                         close($fh);
 5011:                     }
 5012:                 }
 5013:             }
 5014:         }
 5015:         return;
 5016:     }
 5017: }
 5018: 
 5019: sub letter_to_digits {
 5020:     my %lettdig = (
 5021:                     A => 1,
 5022:                     B => 2,
 5023:                     C => 3,
 5024:                     D => 4,
 5025:                     E => 5,
 5026:                     F => 6,
 5027:                     G => 7,
 5028:                     H => 8,
 5029:                     I => 9,
 5030:                     J => 0,
 5031:                   );
 5032:     return %lettdig;
 5033: }
 5034: 
 5035: sub get_scantron_config {
 5036:     my ($which,$cdom) = @_;
 5037:     my @lines = &get_scantronformat_file($cdom);
 5038:     my %config;
 5039:     #FIXME probably should move to XML it has already gotten a bit much now
 5040:     foreach my $line (@lines) {
 5041:         my ($name,$descrip)=split(/:/,$line);
 5042:         if ($name ne $which ) { next; }
 5043:         chomp($line);
 5044:         my @config=split(/:/,$line);
 5045:         $config{'name'}=$config[0];
 5046:         $config{'description'}=$config[1];
 5047:         $config{'CODElocation'}=$config[2];
 5048:         $config{'CODEstart'}=$config[3];
 5049:         $config{'CODElength'}=$config[4];
 5050:         $config{'IDstart'}=$config[5];
 5051:         $config{'IDlength'}=$config[6];
 5052:         $config{'Qstart'}=$config[7];
 5053:         $config{'Qlength'}=$config[8];
 5054:         $config{'Qoff'}=$config[9];
 5055:         $config{'Qon'}=$config[10];
 5056:         $config{'PaperID'}=$config[11];
 5057:         $config{'PaperIDlength'}=$config[12];
 5058:         $config{'FirstName'}=$config[13];
 5059:         $config{'FirstNamelength'}=$config[14];
 5060:         $config{'LastName'}=$config[15];
 5061:         $config{'LastNamelength'}=$config[16];
 5062:         $config{'BubblesPerRow'}=$config[17];
 5063:         last;
 5064:     }
 5065:     return %config;
 5066: }
 5067: 
 5068: sub get_scantronformat_file {
 5069:     my ($cdom) = @_;
 5070:     if ($cdom eq '') {
 5071:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5072:     }
 5073:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 5074:     my $gottab = 0;
 5075:     my @lines;
 5076:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 5077:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 5078:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 5079:             if ($formatfile ne '-1') {
 5080:                 @lines = split("\n",$formatfile,-1);
 5081:                 $gottab = 1;
 5082:             }
 5083:         }
 5084:     }
 5085:     if (!$gottab) {
 5086:         my $confname = $cdom.'-domainconfig';
 5087:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 5088:         my $formatfile = &getfile($default);
 5089:         if ($formatfile ne '-1') {
 5090:             @lines = split("\n",$formatfile,-1);
 5091:             $gottab = 1;
 5092:         }
 5093:     }
 5094:     if (!$gottab) {
 5095:         my @domains = &current_machine_domains();
 5096:         if (grep(/^\Q$cdom\E$/,@domains)) {
 5097:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 5098:                 @lines = <$fh>;
 5099:                 close($fh);
 5100:             }
 5101:         } else {
 5102:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 5103:                 @lines = <$fh>;
 5104:                 close($fh);
 5105:             }
 5106:         }
 5107:         chomp(@lines);
 5108:     }
 5109:     return @lines;
 5110: }
 5111: 
 5112: sub removeuploadedurl {
 5113:     my ($url)=@_;	
 5114:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 5115:     return &removeuserfile($uname,$udom,$fname);
 5116: }
 5117: 
 5118: sub removeuserfile {
 5119:     my ($docuname,$docudom,$fname)=@_;
 5120:     my $home=&homeserver($docuname,$docudom);    
 5121:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 5122:     if ($result eq 'ok') {	
 5123:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 5124:             my $metafile = $fname.'.meta';
 5125:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 5126: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 5127:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 5128:             my $sqlresult = 
 5129:                 &update_portfolio_table($docuname,$docudom,$file,
 5130:                                         'portfolio_metadata',$group,
 5131:                                         'delete');
 5132:         }
 5133:     }
 5134:     return $result;
 5135: }
 5136: 
 5137: sub mkdiruserfile {
 5138:     my ($docuname,$docudom,$dir)=@_;
 5139:     my $home=&homeserver($docuname,$docudom);
 5140:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 5141: }
 5142: 
 5143: sub renameuserfile {
 5144:     my ($docuname,$docudom,$old,$new)=@_;
 5145:     my $home=&homeserver($docuname,$docudom);
 5146:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 5147:                         &escape("$old").':'.&escape("$new"),$home);
 5148:     if ($result eq 'ok') {
 5149:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 5150:             my $oldmeta = $old.'.meta';
 5151:             my $newmeta = $new.'.meta';
 5152:             my $metaresult = 
 5153:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 5154: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 5155:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 5156:             my $sqlresult = 
 5157:                 &update_portfolio_table($docuname,$docudom,$file,
 5158:                                         'portfolio_metadata',$group,
 5159:                                         'delete');
 5160:         }
 5161:     }
 5162:     return $result;
 5163: }
 5164: 
 5165: # ------------------------------------------------------------------------- Log
 5166: 
 5167: sub log {
 5168:     my ($dom,$nam,$hom,$what)=@_;
 5169:     return critical("log:$dom:$nam:$what",$hom);
 5170: }
 5171: 
 5172: # ------------------------------------------------------------------ Course Log
 5173: #
 5174: # This routine flushes several buffers of non-mission-critical nature
 5175: #
 5176: 
 5177: sub flushcourselogs {
 5178:     &logthis('Flushing log buffers');
 5179: #
 5180: # course logs
 5181: # This is a log of all transactions in a course, which can be used
 5182: # for data mining purposes
 5183: #
 5184: # It also collects the courseid database, which lists last transaction
 5185: # times and course titles for all courseids
 5186: #
 5187:     my %courseidbuffer=();
 5188:     foreach my $crsid (keys(%courselogs)) {
 5189:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 5190: 		          &escape($courselogs{$crsid}),
 5191: 		          $coursehombuf{$crsid}) eq 'ok') {
 5192: 	    delete $courselogs{$crsid};
 5193:         } else {
 5194:             &logthis('Failed to flush log buffer for '.$crsid);
 5195:             if (length($courselogs{$crsid})>40000) {
 5196:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 5197:                         " exceeded maximum size, deleting.</font>");
 5198:                delete $courselogs{$crsid};
 5199:             }
 5200:         }
 5201:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 5202:             'description' => $coursedescrbuf{$crsid},
 5203:             'inst_code'    => $courseinstcodebuf{$crsid},
 5204:             'type'        => $coursetypebuf{$crsid},
 5205:             'owner'       => $courseownerbuf{$crsid},
 5206:         };
 5207:     }
 5208: #
 5209: # Write course id database (reverse lookup) to homeserver of courses 
 5210: # Is used in pickcourse
 5211: #
 5212:     foreach my $crs_home (keys(%courseidbuffer)) {
 5213:         my $response = &courseidput(&host_domain($crs_home),
 5214:                                     $courseidbuffer{$crs_home},
 5215:                                     $crs_home,'timeonly');
 5216:     }
 5217: #
 5218: # File accesses
 5219: # Writes to the dynamic metadata of resources to get hit counts, etc.
 5220: #
 5221:     foreach my $entry (keys(%accesshash)) {
 5222:         if ($entry =~ /___count$/) {
 5223:             my ($dom,$name);
 5224:             ($dom,$name,undef)=
 5225: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 5226:             if (! defined($dom) || $dom eq '' || 
 5227:                 ! defined($name) || $name eq '') {
 5228:                 my $cid = $env{'request.course.id'};
 5229: #
 5230: # FIXME 11/29/2021
 5231: # Typo in rev. 1.458 (2003/12/09)??
 5232: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
 5233: #
 5234: # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
 5235: # $dom and $name will always be null, so the &inc() call will default to storing this data
 5236: # in a nohist_accesscount.db file for the user rather than the course.
 5237: #
 5238: # That said there is a lot of noise in the data being stored.
 5239: # So counts for prtspool/  and adm/ etc. are recorded.
 5240: #
 5241: # A review of which items ending '___count' are written to %accesshash should likely be 
 5242: # made before deciding whether to set these to 'course.' instead of 'request.'
 5243: #
 5244: # Under the current scheme each user receives a nohist_accesscount.db file listing 
 5245: # accesses for things which are not published resources, regardless of course, and
 5246: # there is not a nohist_accesscount.db file in a course, which might log accesses from
 5247: # anyone in the course for things which are not published resources.
 5248: #
 5249: # For an author, nohist_accesscount.db ends up having records for other items
 5250: # mixed up with the legitimate access counts for the author's published resources.
 5251: #
 5252:                 $dom  = $env{'request.'.$cid.'.domain'};
 5253:                 $name = $env{'request.'.$cid.'.num'};
 5254:             }
 5255:             my $value = $accesshash{$entry};
 5256:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 5257:             my %temphash=($url => $value);
 5258:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 5259:             if ($result eq 'ok') {
 5260:                 delete $accesshash{$entry};
 5261:             }
 5262:         } else {
 5263:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 5264:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 5265:             my %temphash=($entry => $accesshash{$entry});
 5266:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 5267:                 delete $accesshash{$entry};
 5268:             }
 5269:         }
 5270:     }
 5271: #
 5272: # Roles
 5273: # Reverse lookup of user roles for course faculty/staff and co-authorship
 5274: #
 5275:     foreach my $entry (keys(%userrolehash)) {
 5276:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 5277: 	    split(/\:/,$entry);
 5278:         if (&put('nohist_userroles',
 5279:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 5280:                 $rudom,$runame) eq 'ok') {
 5281: 	    delete $userrolehash{$entry};
 5282:         }
 5283:     }
 5284: #
 5285: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 5286: #
 5287:     my %domrolebuffer = ();
 5288:     foreach my $entry (keys(%domainrolehash)) {
 5289:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 5290:         if ($domrolebuffer{$rudom}) {
 5291:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5292:                       '='.&escape($domainrolehash{$entry});
 5293:         } else {
 5294:             $domrolebuffer{$rudom}.=&escape($entry).
 5295:                       '='.&escape($domainrolehash{$entry});
 5296:         }
 5297:         delete $domainrolehash{$entry};
 5298:     }
 5299:     foreach my $dom (keys(%domrolebuffer)) {
 5300: 	my %servers;
 5301: 	if (defined(&domain($dom,'primary'))) {
 5302: 	    my $primary=&domain($dom,'primary');
 5303: 	    my $hostname=&hostname($primary);
 5304: 	    $servers{$primary} = $hostname;
 5305: 	} else { 
 5306: 	    %servers = &get_servers($dom,'library');
 5307: 	}
 5308: 	foreach my $tryserver (keys(%servers)) {
 5309: 	    if (&reply('domroleput:'.$dom.':'.
 5310: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5311: 		last;
 5312: 	    } else {  
 5313: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5314: 	    }
 5315:         }
 5316:     }
 5317:     $dumpcount++;
 5318: }
 5319: 
 5320: sub courselog {
 5321:     my $what=shift;
 5322:     $what=time.':'.$what;
 5323:     unless ($env{'request.course.id'}) { return ''; }
 5324:     $coursedombuf{$env{'request.course.id'}}=
 5325:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5326:     $coursenumbuf{$env{'request.course.id'}}=
 5327:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5328:     $coursehombuf{$env{'request.course.id'}}=
 5329:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5330:     $coursedescrbuf{$env{'request.course.id'}}=
 5331:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5332:     $courseinstcodebuf{$env{'request.course.id'}}=
 5333:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5334:     $courseownerbuf{$env{'request.course.id'}}=
 5335:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5336:     $coursetypebuf{$env{'request.course.id'}}=
 5337:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5338:     if (defined $courselogs{$env{'request.course.id'}}) {
 5339: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5340:     } else {
 5341: 	$courselogs{$env{'request.course.id'}}.=$what;
 5342:     }
 5343:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5344: 	&flushcourselogs();
 5345:     }
 5346: }
 5347: 
 5348: sub courseacclog {
 5349:     my $fnsymb=shift;
 5350:     unless ($env{'request.course.id'}) { return ''; }
 5351:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5352:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5353:         $what.=':POST';
 5354:         # FIXME: Probably ought to escape things....
 5355: 	foreach my $key (keys(%env)) {
 5356:             if ($key=~/^form\.(.*)/) {
 5357:                 my $formitem = $1;
 5358:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5359:                     $what.=':'.$formitem.'='.$env{$key};
 5360:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5361:                     if ($formitem eq 'proctorpassword') {
 5362:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5363:                     } else {
 5364:                         $what.=':'.$formitem.'='.$env{$key};
 5365:                     }
 5366:                 }
 5367:             }
 5368:         }
 5369:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5370:         # FIXME: We should not be depending on a form parameter that someone
 5371:         # editing lonsearchcat.pm might change in the future.
 5372:         if ($env{'form.phase'} eq 'course_search') {
 5373:             $what.= ':POST';
 5374:             # FIXME: Probably ought to escape things....
 5375:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5376:                                  'crsdiscuss') {
 5377:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5378:             }
 5379:         }
 5380:     }
 5381:     &courselog($what);
 5382: }
 5383: 
 5384: sub countacc {
 5385:     my $url=&declutter(shift);
 5386:     return if (! defined($url) || $url eq '');
 5387:     unless ($env{'request.course.id'}) { return ''; }
 5388: #
 5389: # Mark that this url was used in this course
 5390: #
 5391:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5392: #
 5393: # Increase the access count for this resource in this child process
 5394: #
 5395:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5396:     $accesshash{$key}++;
 5397: }
 5398: 
 5399: sub linklog {
 5400:     my ($from,$to)=@_;
 5401:     $from=&declutter($from);
 5402:     $to=&declutter($to);
 5403:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5404:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5405: }
 5406: 
 5407: sub statslog {
 5408:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5409:     if ($users<2) { return; }
 5410:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5411:             'course'       => $env{'request.course.id'},
 5412:             'sections'     => '"all"',
 5413:             'num_students' => $users,
 5414:             'part'         => $part,
 5415:             'symb'         => $symb,
 5416:             'mean_tries'   => $av_attempts,
 5417:             'deg_of_diff'  => $degdiff});
 5418:     foreach my $key (keys(%dynstore)) {
 5419:         $accesshash{$key}=$dynstore{$key};
 5420:     }
 5421: }
 5422:   
 5423: sub userrolelog {
 5424:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5425:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5426:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5427:        $userrolehash
 5428:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5429:                     =$tend.':'.$tstart;
 5430:     }
 5431:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5432:        $userrolehash
 5433:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5434:                     =$tend.':'.$tstart;
 5435:     }
 5436:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5437:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5438:        $domainrolehash
 5439:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5440:                     = $tend.':'.$tstart;
 5441:     }
 5442: }
 5443: 
 5444: sub courserolelog {
 5445:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,
 5446:         $context,$othdomby,$requester)=@_;
 5447:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5448:         my $cdom = $1;
 5449:         my $cnum = $2;
 5450:         my $sec = $3;
 5451:         my $namespace = 'rolelog';
 5452:         my %storehash = (
 5453:                            role    => $trole,
 5454:                            start   => $tstart,
 5455:                            end     => $tend,
 5456:                            selfenroll => $selfenroll,
 5457:                            context    => $context,
 5458:                         );
 5459:         if ($othdomby) {
 5460:             if ($othdomby eq 'othdombydc') {
 5461:                 $storehash{'approval'} = 'domain';
 5462:             } elsif ($othdomby eq 'othdombyuser') {
 5463:                 $storehash{'approval'} = 'user'; 
 5464:             }
 5465:             if ($requester ne '') {
 5466:                 $storehash{'requester'} = $requester;
 5467:             }
 5468:         }
 5469:         if ($trole eq 'gr') {
 5470:             $namespace = 'groupslog';
 5471:             $storehash{'group'} = $sec;
 5472:         } else {
 5473:             $storehash{'section'} = $sec;
 5474:         }
 5475:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5476:                    $domain,$cnum,$cdom);
 5477:         if (($trole ne 'st') || ($sec ne '')) {
 5478:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5479:         }
 5480:     }
 5481:     return;
 5482: }
 5483: 
 5484: sub domainrolelog {
 5485:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,
 5486:         $context,$othdomby,$requester)=@_;
 5487:     if ($area =~ m{^/($match_domain)/$}) {
 5488:         my $cdom = $1;
 5489:         my $domconfiguser = &get_domainconfiguser($cdom);
 5490:         my $namespace = 'rolelog';
 5491:         my %storehash = (
 5492:                            role    => $trole,
 5493:                            start   => $tstart,
 5494:                            end     => $tend,
 5495:                            context => $context,
 5496:                         );
 5497:         if ($othdomby) {
 5498:             if ($othdomby eq 'othdombydc') {
 5499:                 $storehash{'approval'} = 'domain';
 5500:             } elsif ($othdomby eq 'othdombyuser') {
 5501:                 $storehash{'approval'} = 'user';
 5502:             }
 5503:             if ($requester ne '') {
 5504:                 $storehash{'requester'} = $requester;
 5505:             }
 5506:         }
 5507:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5508:                    $domain,$domconfiguser,$cdom);
 5509:     }
 5510:     return;
 5511: 
 5512: }
 5513: 
 5514: sub coauthorrolelog {
 5515:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,
 5516:         $context,$othdomby,$requester)=@_;
 5517:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5518:         my $audom = $1;
 5519:         my $auname = $2;
 5520:         my $namespace = 'rolelog';
 5521:         my %storehash = (
 5522:                            role    => $trole,
 5523:                            start   => $tstart,
 5524:                            end     => $tend,
 5525:                            context => $context,
 5526:                         );
 5527:         if ($othdomby) {
 5528:             if ($othdomby eq 'othdombydc') {
 5529:                 $storehash{'approval'} = 'domain';
 5530:             } elsif ($othdomby eq 'othdombyuser') {
 5531:                 $storehash{'approval'} = 'user';
 5532:             }
 5533:             if ($requester ne '') {
 5534:                 $storehash{'requester'} = $requester;
 5535:             }
 5536:         }
 5537:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5538:                    $domain,$auname,$audom);
 5539:     }
 5540:     return;
 5541: }
 5542: 
 5543: sub get_course_adv_roles {
 5544:     my ($cid,$codes) = @_;
 5545:     $cid=$env{'request.course.id'} unless (defined($cid));
 5546:     my %coursehash=&coursedescription($cid);
 5547:     my $crstype = &Apache::loncommon::course_type($cid);
 5548:     my %nothide=();
 5549:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5550:         if ($user !~ /:/) {
 5551: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5552:         } else {
 5553:             $nothide{$user}=1;
 5554:         }
 5555:     }
 5556:     my @possdoms = ($coursehash{'domain'});
 5557:     if ($coursehash{'checkforpriv'}) {
 5558:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5559:     }
 5560:     my %returnhash=();
 5561:     my %dumphash=
 5562:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5563:     my $now=time;
 5564:     my %privileged;
 5565:     foreach my $entry (keys(%dumphash)) {
 5566: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5567:         if (($tstart) && ($tstart<0)) { next; }
 5568:         if (($tend) && ($tend<$now)) { next; }
 5569:         if (($tstart) && ($now<$tstart)) { next; }
 5570:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5571: 	if ($username eq '' || $domain eq '') { next; }
 5572:         if ((&privileged($username,$domain,\@possdoms)) &&
 5573:             (!$nothide{$username.':'.$domain})) { next; }
 5574: 	if ($role eq 'cr') { next; }
 5575:         if ($codes) {
 5576:             if ($section) { $role .= ':'.$section; }
 5577:             if ($returnhash{$role}) {
 5578:                 $returnhash{$role}.=','.$username.':'.$domain;
 5579:             } else {
 5580:                 $returnhash{$role}=$username.':'.$domain;
 5581:             }
 5582:         } else {
 5583:             my $key=&plaintext($role,$crstype);
 5584:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5585:             if ($returnhash{$key}) {
 5586: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5587:             } else {
 5588:                 $returnhash{$key}=$username.':'.$domain;
 5589:             }
 5590:         }
 5591:     }
 5592:     return %returnhash;
 5593: }
 5594: 
 5595: sub get_my_roles {
 5596:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5597:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5598:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5599:     my (%dumphash,%nothide);
 5600:     if ($context eq 'userroles') {
 5601:         %dumphash = &dump('roles',$udom,$uname);
 5602:     } else {
 5603:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5604:         if ($hidepriv) {
 5605:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5606:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5607:                 if ($user !~ /:/) {
 5608:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5609:                 } else {
 5610:                     $nothide{$user} = 1;
 5611:                 }
 5612:             }
 5613:         }
 5614:     }
 5615:     my %returnhash=();
 5616:     my $now=time;
 5617:     my %privileged;
 5618:     foreach my $entry (keys(%dumphash)) {
 5619:         my ($role,$tend,$tstart);
 5620:         if ($context eq 'userroles') {
 5621:             next if ($entry =~ /^rolesdef/);
 5622: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5623:         } else {
 5624:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5625:         }
 5626:         if (($tstart) && ($tstart<0)) { next; }
 5627:         my $status = 'active';
 5628:         if (($tend) && ($tend<=$now)) {
 5629:             $status = 'previous';
 5630:         } 
 5631:         if (($tstart) && ($now<$tstart)) {
 5632:             $status = 'future';
 5633:         }
 5634:         if (ref($types) eq 'ARRAY') {
 5635:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5636:                 next;
 5637:             } 
 5638:         } else {
 5639:             if ($status ne 'active') {
 5640:                 next;
 5641:             }
 5642:         }
 5643:         my ($rolecode,$username,$domain,$section,$area);
 5644:         if ($context eq 'userroles') {
 5645:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5646:             (undef,$domain,$username,$section) = split(/\//,$area);
 5647:         } else {
 5648:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5649:         }
 5650:         if (ref($roledoms) eq 'ARRAY') {
 5651:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5652:                 next;
 5653:             }
 5654:         }
 5655:         if (ref($roles) eq 'ARRAY') {
 5656:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5657:                 if ($role =~ /^cr\//) {
 5658:                     if (!grep(/^cr$/,@{$roles})) {
 5659:                         next;
 5660:                     }
 5661:                 } elsif ($role =~ /^gr\//) {
 5662:                     if (!grep(/^gr$/,@{$roles})) {
 5663:                         next;
 5664:                     }
 5665:                 } else {
 5666:                     next;
 5667:                 }
 5668:             }
 5669:         }
 5670:         if ($hidepriv) {
 5671:             my @privroles = ('dc','su');
 5672:             if ($context eq 'userroles') {
 5673:                 next if (grep(/^\Q$role\E$/,@privroles));
 5674:             } else {
 5675:                 my $possdoms = [$domain];
 5676:                 if (ref($roledoms) eq 'ARRAY') {
 5677:                    push(@{$possdoms},@{$roledoms}); 
 5678:                 }
 5679:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5680:                     if (!$nothide{$username.':'.$domain}) {
 5681:                         next;
 5682:                     }
 5683:                 }
 5684:             }
 5685:         }
 5686:         if ($withsec) {
 5687:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5688:                 $tstart.':'.$tend;
 5689:         } else {
 5690:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5691:         }
 5692:     }
 5693:     return %returnhash;
 5694: }
 5695: 
 5696: sub get_all_adhocroles {
 5697:     my ($dom) = @_;
 5698:     my @roles_by_num = ();
 5699:     my %domdefaults = &get_domain_defaults($dom);
 5700:     my (%description,%access_in_dom,%access_info);
 5701:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5702:         my $count = 0;
 5703:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5704:         my %ordered;
 5705:         foreach my $role (sort(keys(%domcurrent))) {
 5706:             my ($order,$desc,$access_in_dom);
 5707:             if (ref($domcurrent{$role}) eq 'HASH') {
 5708:                 $order = $domcurrent{$role}{'order'};
 5709:                 $desc = $domcurrent{$role}{'desc'};
 5710:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5711:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5712:             }
 5713:             if ($order eq '') {
 5714:                 $order = $count;
 5715:             }
 5716:             $ordered{$order} = $role;
 5717:             if ($desc ne '') {
 5718:                 $description{$role} = $desc;
 5719:             } else {
 5720:                 $description{$role}= $role;
 5721:             }
 5722:             $count++;
 5723:         }
 5724:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5725:             push(@roles_by_num,$ordered{$item});
 5726:         }
 5727:     }
 5728:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5729: }
 5730: 
 5731: sub get_my_adhocroles {
 5732:     my ($cid,$checkreg) = @_;
 5733:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5734:     if ($env{'request.course.id'} eq $cid) {
 5735:         $cdom = $env{'course.'.$cid.'.domain'};
 5736:         $cnum = $env{'course.'.$cid.'.num'};
 5737:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5738:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5739:         $cdom = $1;
 5740:         $cnum = $2;
 5741:         %info = &get('environment',['internal.coursecode'],
 5742:                      $cdom,$cnum);
 5743:     }
 5744:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5745:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5746:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5747:         if ($rosterhash{$user} ne '') {
 5748:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5749:             return ([],{}) if ($type eq 'auto');
 5750:         }
 5751:     }
 5752:     if (($cdom ne '') && ($cnum ne ''))  {
 5753:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5754:             my $then=$env{'user.login.time'};
 5755:             my $update=$env{'user.update.time'};
 5756:             if (!$update) {
 5757:                 $update = $then;
 5758:             }
 5759:             my @liveroles;
 5760:             foreach my $role ('dh','da') {
 5761:                 if ($env{"user.role.$role./$cdom/"}) {
 5762:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5763:                     my $limit = $update;
 5764:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5765:                         $limit = $then;
 5766:                     }
 5767:                     my $activerole = 1;
 5768:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5769:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5770:                     if ($activerole) {
 5771:                         push(@liveroles,$role);
 5772:                     }
 5773:                 }
 5774:             }
 5775:             if (@liveroles) {
 5776:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5777:                     my ($accessref,$accessinfo,%access_in_dom);
 5778:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5779:                     if (ref($roles_by_num) eq 'ARRAY') {
 5780:                         if (@{$roles_by_num}) {
 5781:                             my %settings;
 5782:                             if ($env{'request.course.id'} eq $cid) {
 5783:                                 foreach my $envkey (keys(%env)) {
 5784:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5785:                                         $settings{$1} = $env{$envkey};
 5786:                                     }
 5787:                                 }
 5788:                             } else {
 5789:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5790:                             }
 5791:                             my %setincrs;
 5792:                             if ($settings{'internal.adhocaccess'}) {
 5793:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5794:                             }
 5795:                             my @statuses;
 5796:                             if ($env{'environment.inststatus'}) {
 5797:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5798:                             }
 5799:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5800:                             if (ref($accessref) eq 'HASH') {
 5801:                                 %access_in_dom = %{$accessref};
 5802:                             }
 5803:                             foreach my $role (@{$roles_by_num}) {
 5804:                                 my ($curraccess,@okstatus,@personnel);
 5805:                                 if ($setincrs{$role}) {
 5806:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5807:                                     if ($curraccess eq 'status') {
 5808:                                         @okstatus = split(/\&/,$rest);
 5809:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5810:                                         @personnel = split(/\&/,$rest);
 5811:                                     }
 5812:                                 } else {
 5813:                                     $curraccess = $access_in_dom{$role};
 5814:                                     if (ref($accessinfo) eq 'HASH') {
 5815:                                         if ($curraccess eq 'status') {
 5816:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5817:                                                 @okstatus = @{$accessinfo->{$role}};
 5818:                                             }
 5819:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5820:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5821:                                                 @personnel = @{$accessinfo->{$role}};
 5822:                                             }
 5823:                                         }
 5824:                                     }
 5825:                                 }
 5826:                                 if ($curraccess eq 'none') {
 5827:                                     next;
 5828:                                 } elsif ($curraccess eq 'all') {
 5829:                                     push(@possroles,$role);
 5830:                                 } elsif ($curraccess eq 'dh') {
 5831:                                     if (grep(/^dh$/,@liveroles)) {
 5832:                                         push(@possroles,$role);
 5833:                                     } else {
 5834:                                         next;
 5835:                                     }
 5836:                                 } elsif ($curraccess eq 'da') {
 5837:                                     if (grep(/^da$/,@liveroles)) {
 5838:                                         push(@possroles,$role);
 5839:                                     } else {
 5840:                                         next;
 5841:                                     }
 5842:                                 } elsif ($curraccess eq 'status') {
 5843:                                     if (@okstatus) {
 5844:                                         if (!@statuses) {
 5845:                                             if (grep(/^default$/,@okstatus)) {
 5846:                                                 push(@possroles,$role);
 5847:                                             }
 5848:                                         } else {
 5849:                                             foreach my $status (@okstatus) {
 5850:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5851:                                                     push(@possroles,$role);
 5852:                                                     last;
 5853:                                                 }
 5854:                                             }
 5855:                                         }
 5856:                                     }
 5857:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5858:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5859:                                         if ($curraccess eq 'exc') {
 5860:                                             push(@possroles,$role);
 5861:                                         }
 5862:                                     } elsif ($curraccess eq 'inc') {
 5863:                                         push(@possroles,$role);
 5864:                                     }
 5865:                                 }
 5866:                             }
 5867:                         }
 5868:                     }
 5869:                 }
 5870:             }
 5871:         }
 5872:     }
 5873:     unless (ref($description) eq 'HASH') {
 5874:         if (ref($roles_by_num) eq 'ARRAY') {
 5875:             my %desc;
 5876:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5877:             $description = \%desc;
 5878:         } else {
 5879:             $description = {};
 5880:         }
 5881:     }
 5882:     return (\@possroles,$description);
 5883: }
 5884: 
 5885: # ----------------------------------------------------- Frontpage Announcements
 5886: #
 5887: #
 5888: 
 5889: sub postannounce {
 5890:     my ($server,$text)=@_;
 5891:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5892:     unless ($text=~/\w/) { $text=''; }
 5893:     return &reply('setannounce:'.&escape($text),$server);
 5894: }
 5895: 
 5896: sub getannounce {
 5897: 
 5898:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5899: 	my $announcement='';
 5900: 	while (my $line = <$fh>) { $announcement .= $line; }
 5901: 	close($fh);
 5902: 	if ($announcement=~/\w/) { 
 5903: 	    return 
 5904:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5905:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5906: 	} else {
 5907: 	    return '';
 5908: 	}
 5909:     } else {
 5910: 	return '';
 5911:     }
 5912: }
 5913: 
 5914: # ---------------------------------------------------------- Course ID routines
 5915: # Deal with domain's nohist_courseid.db files
 5916: #
 5917: 
 5918: sub courseidput {
 5919:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5920:     return unless (ref($storehash) eq 'HASH');
 5921:     my $outcome;
 5922:     if ($caller eq 'timeonly') {
 5923:         my $cids = '';
 5924:         foreach my $item (keys(%$storehash)) {
 5925:             $cids.=&escape($item).'&';
 5926:         }
 5927:         $cids=~s/\&$//;
 5928:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5929:                           $coursehome);       
 5930:     } else {
 5931:         my $items = '';
 5932:         foreach my $item (keys(%$storehash)) {
 5933:             $items.= &escape($item).'='.
 5934:                      &freeze_escape($$storehash{$item}).'&';
 5935:         }
 5936:         $items=~s/\&$//;
 5937:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5938:                           $coursehome);
 5939:     }
 5940:     if ($outcome eq 'unknown_cmd') {
 5941:         my $what;
 5942:         foreach my $cid (keys(%$storehash)) {
 5943:             $what .= &escape($cid).'=';
 5944:             foreach my $item ('description','inst_code','owner','type') {
 5945:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5946:             }
 5947:             $what =~ s/\:$/&/;
 5948:         }
 5949:         $what =~ s/\&$//;  
 5950:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5951:     } else {
 5952:         return $outcome;
 5953:     }
 5954: }
 5955: 
 5956: sub courseiddump {
 5957:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5958:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5959:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5960:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5961:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5962:     my $as_hash = 1;
 5963:     my %returnhash;
 5964:     if (!$domfilter) { $domfilter=''; }
 5965:     my %libserv = &all_library();
 5966:     foreach my $tryserver (keys(%libserv)) {
 5967:         if ( (  $hostidflag == 1 
 5968: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5969: 	     || (!defined($hostidflag)) ) {
 5970: 
 5971: 	    if (($domfilter eq '') ||
 5972: 		(&host_domain($tryserver) eq $domfilter)) {
 5973:                 my $rep;
 5974:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5975:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5976:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5977:                                 &escape($descfilter), &escape($instcodefilter), 
 5978:                                 &escape($ownerfilter), &escape($coursefilter),
 5979:                                 &escape($typefilter), &escape($regexp_ok), 
 5980:                                 $as_hash, &escape($selfenrollonly), 
 5981:                                 &escape($catfilter), $showhidden, $caller, 
 5982:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5983:                                 &escape($createdbefore), &escape($createdafter), 
 5984:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5985:                                 $reqcrsdom,&escape($reqinstcode))));
 5986:                 } else {
 5987:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5988:                              $sincefilter.':'.&escape($descfilter).':'.
 5989:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5990:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5991:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5992:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5993:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5994:                              &escape($cc_clone).':'.$cloneonly.':'.
 5995:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5996:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5997:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5998:                 }
 5999:                      
 6000:                 my @pairs=split(/\&/,$rep);
 6001:                 foreach my $item (@pairs) {
 6002:                     my ($key,$value)=split(/\=/,$item,2);
 6003:                     $key = &unescape($key);
 6004:                     next if ($key =~ /^error: 2 /);
 6005:                     my $result = &thaw_unescape($value);
 6006:                     if (ref($result) eq 'HASH') {
 6007:                         $returnhash{$key}=$result;
 6008:                     } else {
 6009:                         my @responses = split(/:/,$value);
 6010:                         my @items = ('description','inst_code','owner','type');
 6011:                         for (my $i=0; $i<@responses; $i++) {
 6012:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 6013:                         }
 6014:                     }
 6015:                 }
 6016:             }
 6017:         }
 6018:     }
 6019:     return %returnhash;
 6020: }
 6021: 
 6022: sub courselastaccess {
 6023:     my ($cdom,$cnum,$hostidref) = @_;
 6024:     my %returnhash;
 6025:     if ($cdom && $cnum) {
 6026:         my $chome = &homeserver($cnum,$cdom);
 6027:         if ($chome ne 'no_host') {
 6028:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 6029:             &extract_lastaccess(\%returnhash,$rep);
 6030:         }
 6031:     } else {
 6032:         if (!$cdom) { $cdom=''; }
 6033:         my %libserv = &all_library();
 6034:         foreach my $tryserver (keys(%libserv)) {
 6035:             if (ref($hostidref) eq 'ARRAY') {
 6036:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 6037:             } 
 6038:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 6039:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 6040:                 &extract_lastaccess(\%returnhash,$rep);
 6041:             }
 6042:         }
 6043:     }
 6044:     return %returnhash;
 6045: }
 6046: 
 6047: sub extract_lastaccess {
 6048:     my ($returnhash,$rep) = @_;
 6049:     if (ref($returnhash) eq 'HASH') {
 6050:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 6051:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 6052:                  $rep eq '') {
 6053:             my @pairs=split(/\&/,$rep);
 6054:             foreach my $item (@pairs) {
 6055:                 my ($key,$value)=split(/\=/,$item,2);
 6056:                 $key = &unescape($key);
 6057:                 next if ($key =~ /^error: 2 /);
 6058:                 $returnhash->{$key} = &thaw_unescape($value);
 6059:             }
 6060:         }
 6061:     }
 6062:     return;
 6063: }
 6064: 
 6065: # ---------------------------------------------------------- DC e-mail
 6066: 
 6067: sub dcmailput {
 6068:     my ($domain,$msgid,$message,$server)=@_;
 6069:     my $status = &critical(
 6070:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 6071:        &escape($message),$server);
 6072:     return $status;
 6073: }
 6074: 
 6075: sub dcmaildump {
 6076:     my ($dom,$startdate,$enddate,$senders) = @_;
 6077:     my %returnhash=();
 6078: 
 6079:     if (defined(&domain($dom,'primary'))) {
 6080:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 6081:                                                          &escape($enddate).':';
 6082: 	my @esc_senders=map { &escape($_)} @$senders;
 6083: 	$cmd.=&escape(join('&',@esc_senders));
 6084: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 6085:             my ($key,$value) = split(/\=/,$line,2);
 6086:             if (($key) && ($value)) {
 6087:                 $returnhash{&unescape($key)} = &unescape($value);
 6088:             }
 6089:         }
 6090:     }
 6091:     return %returnhash;
 6092: }
 6093: # ---------------------------------------------------------- Domain roles
 6094: 
 6095: sub get_domain_roles {
 6096:     my ($dom,$roles,$startdate,$enddate)=@_;
 6097:     if ((!defined($startdate)) || ($startdate eq '')) {
 6098:         $startdate = '.';
 6099:     }
 6100:     if ((!defined($enddate)) || ($enddate eq '')) {
 6101:         $enddate = '.';
 6102:     }
 6103:     my $rolelist;
 6104:     if (ref($roles) eq 'ARRAY') {
 6105:         $rolelist = join('&',@{$roles});
 6106:     }
 6107:     my %personnel = ();
 6108: 
 6109:     my %servers = &get_servers($dom,'library');
 6110:     foreach my $tryserver (keys(%servers)) {
 6111: 	%{$personnel{$tryserver}}=();
 6112: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 6113: 					    &escape($startdate).':'.
 6114: 					    &escape($enddate).':'.
 6115: 					    &escape($rolelist), $tryserver))) {
 6116: 	    my ($key,$value) = split(/\=/,$line,2);
 6117: 	    if (($key) && ($value)) {
 6118: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 6119: 	    }
 6120: 	}
 6121:     }
 6122:     return %personnel;
 6123: }
 6124: 
 6125: sub get_active_domroles {
 6126:     my ($dom,$roles) = @_;
 6127:     return () unless (ref($roles) eq 'ARRAY');
 6128:     my $now = time;
 6129:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 6130:     my %domroles;
 6131:     foreach my $server (keys(%dompersonnel)) {
 6132:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 6133:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 6134:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 6135:         }
 6136:     }
 6137:     return %domroles;
 6138: }
 6139: 
 6140: # ----------------------------------------------------------- Interval timing 
 6141: 
 6142: {
 6143: # Caches needed for speedup of navmaps
 6144: # We don't want to cache this for very long at all (5 seconds at most)
 6145: # 
 6146: # The user for whom we cache
 6147: my $cachedkey='';
 6148: # The cached times for this user
 6149: my %cachedtimes=();
 6150: # When this was last done
 6151: my $cachedtime='';
 6152: 
 6153: sub load_all_first_access {
 6154:     my ($uname,$udom,$ignorecache)=@_;
 6155:     if (($cachedkey eq $uname.':'.$udom) &&
 6156:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 6157:         (!$ignorecache)) {
 6158:         return;
 6159:     }
 6160:     $cachedtime=time;
 6161:     $cachedkey=$uname.':'.$udom;
 6162:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 6163: }
 6164: 
 6165: sub get_first_access {
 6166:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 6167:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 6168:     if ($argsymb) { $symb=$argsymb; }
 6169:     my ($map,$id,$res)=&decode_symb($symb);
 6170:     if ($argmap) { $map = $argmap; }
 6171:     if ($type eq 'course') {
 6172: 	$res='course';
 6173:     } elsif ($type eq 'map') {
 6174: 	$res=&symbread($map);
 6175:     } else {
 6176: 	$res=$symb;
 6177:     }
 6178:     &load_all_first_access($uname,$udom,$ignorecache);
 6179:     return $cachedtimes{"$courseid\0$res"};
 6180: }
 6181: 
 6182: sub set_first_access {
 6183:     my ($type,$interval)=@_;
 6184:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 6185:     my ($map,$id,$res)=&decode_symb($symb);
 6186:     if ($type eq 'course') {
 6187: 	$res='course';
 6188:     } elsif ($type eq 'map') {
 6189: 	$res=&symbread($map);
 6190:     } else {
 6191: 	$res=$symb;
 6192:     }
 6193:     $cachedkey='';
 6194:     my $firstaccess=&get_first_access($type,$symb,$map);
 6195:     if ($firstaccess) {
 6196:         &logthis("First access time already set ($firstaccess) when attempting ".
 6197:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 6198:                  "in $courseid");
 6199:         return 'already_set';
 6200:     } else {
 6201:         my $start = time;
 6202: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 6203:                           $udom,$uname);
 6204:         if ($putres eq 'ok') {
 6205:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 6206:                  $udom,$uname); 
 6207:             &appenv(
 6208:                      {
 6209:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 6210:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 6211:                      }
 6212:                   );
 6213:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 6214:                 $cachedtimes{"$courseid\0$res"} = $start;
 6215:             }
 6216:         } elsif ($putres ne 'refused') {
 6217:             &logthis("Result: $putres when attempting to set first access time ".
 6218:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 6219:         }
 6220:         return $putres;
 6221:     }
 6222:     return 'already_set';
 6223: }
 6224: }
 6225: 
 6226: # --------------------------------------------- Set Expire Date for Spreadsheet
 6227: 
 6228: sub expirespread {
 6229:     my ($uname,$udom,$stype,$usymb)=@_;
 6230:     my $cid=$env{'request.course.id'}; 
 6231:     if ($cid) {
 6232:        my $now=time;
 6233:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 6234:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 6235:                             $env{'course.'.$cid.'.num'}.
 6236: 	        	    ':nohist_expirationdates:'.
 6237:                             &escape($key).'='.$now,
 6238:                             $env{'course.'.$cid.'.home'})
 6239:     }
 6240:     return 'ok';
 6241: }
 6242: 
 6243: # ----------------------------------------------------- Devalidate Spreadsheets
 6244: 
 6245: sub devalidate {
 6246:     my ($symb,$uname,$udom)=@_;
 6247:     my $cid=$env{'request.course.id'}; 
 6248:     if ($cid) {
 6249:         # delete the stored spreadsheets for
 6250:         # - the student level sheet of this user in course's homespace
 6251:         # - the assessment level sheet for this resource 
 6252:         #   for this user in user's homespace
 6253: 	# - current conditional state info
 6254: 	my $key=$uname.':'.$udom.':';
 6255:         my $status=
 6256: 	    &del('nohist_calculatedsheets',
 6257: 		 [$key.'studentcalc:'],
 6258: 		 $env{'course.'.$cid.'.domain'},
 6259: 		 $env{'course.'.$cid.'.num'})
 6260: 		.' '.
 6261: 	    &del('nohist_calculatedsheets_'.$cid,
 6262: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 6263:         unless ($status eq 'ok ok') {
 6264:            &logthis('Could not devalidate spreadsheet '.
 6265:                     $uname.' at '.$udom.' for '.
 6266: 		    $symb.': '.$status);
 6267:         }
 6268: 	&delenv('user.state.'.$cid);
 6269:     }
 6270: }
 6271: 
 6272: sub get_scalar {
 6273:     my ($string,$end) = @_;
 6274:     my $value;
 6275:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 6276: 	$value = $1;
 6277:     } elsif ($$string =~ s/^([^&]*?)&//) {
 6278: 	$value = $1;
 6279:     }
 6280:     return &unescape($value);
 6281: }
 6282: 
 6283: sub array2str {
 6284:   my (@array) = @_;
 6285:   my $result=&arrayref2str(\@array);
 6286:   $result=~s/^__ARRAY_REF__//;
 6287:   $result=~s/__END_ARRAY_REF__$//;
 6288:   return $result;
 6289: }
 6290: 
 6291: sub arrayref2str {
 6292:   my ($arrayref) = @_;
 6293:   my $result='__ARRAY_REF__';
 6294:   foreach my $elem (@$arrayref) {
 6295:     if(ref($elem) eq 'ARRAY') {
 6296:       $result.=&arrayref2str($elem).'&';
 6297:     } elsif(ref($elem) eq 'HASH') {
 6298:       $result.=&hashref2str($elem).'&';
 6299:     } elsif(ref($elem)) {
 6300:       #print("Got a ref of ".(ref($elem))." skipping.");
 6301:     } else {
 6302:       $result.=&escape($elem).'&';
 6303:     }
 6304:   }
 6305:   $result=~s/\&$//;
 6306:   $result .= '__END_ARRAY_REF__';
 6307:   return $result;
 6308: }
 6309: 
 6310: sub hash2str {
 6311:   my (%hash) = @_;
 6312:   my $result=&hashref2str(\%hash);
 6313:   $result=~s/^__HASH_REF__//;
 6314:   $result=~s/__END_HASH_REF__$//;
 6315:   return $result;
 6316: }
 6317: 
 6318: sub hashref2str {
 6319:   my ($hashref)=@_;
 6320:   my $result='__HASH_REF__';
 6321:   foreach my $key (sort(keys(%$hashref))) {
 6322:     if (ref($key) eq 'ARRAY') {
 6323:       $result.=&arrayref2str($key).'=';
 6324:     } elsif (ref($key) eq 'HASH') {
 6325:       $result.=&hashref2str($key).'=';
 6326:     } elsif (ref($key)) {
 6327:       $result.='=';
 6328:       #print("Got a ref of ".(ref($key))." skipping.");
 6329:     } else {
 6330: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6331:     }
 6332: 
 6333:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6334:       $result.=&arrayref2str($hashref->{$key}).'&';
 6335:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6336:       $result.=&hashref2str($hashref->{$key}).'&';
 6337:     } elsif(ref($hashref->{$key})) {
 6338:        $result.='&';
 6339:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6340:     } else {
 6341:       $result.=&escape($hashref->{$key}).'&';
 6342:     }
 6343:   }
 6344:   $result=~s/\&$//;
 6345:   $result .= '__END_HASH_REF__';
 6346:   return $result;
 6347: }
 6348: 
 6349: sub str2hash {
 6350:     my ($string)=@_;
 6351:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6352:     return %$hash;
 6353: }
 6354: 
 6355: sub str2hashref {
 6356:   my ($string) = @_;
 6357: 
 6358:   my %hash;
 6359: 
 6360:   if($string !~ /^__HASH_REF__/) {
 6361:       if (! ($string eq '' || !defined($string))) {
 6362: 	  $hash{'error'}='Not hash reference';
 6363:       }
 6364:       return (\%hash, $string);
 6365:   }
 6366: 
 6367:   $string =~ s/^__HASH_REF__//;
 6368: 
 6369:   while($string !~ /^__END_HASH_REF__/) {
 6370:       #key
 6371:       my $key='';
 6372:       if($string =~ /^__HASH_REF__/) {
 6373:           ($key, $string)=&str2hashref($string);
 6374:           if(defined($key->{'error'})) {
 6375:               $hash{'error'}='Bad data';
 6376:               return (\%hash, $string);
 6377:           }
 6378:       } elsif($string =~ /^__ARRAY_REF__/) {
 6379:           ($key, $string)=&str2arrayref($string);
 6380:           if($key->[0] eq 'Array reference error') {
 6381:               $hash{'error'}='Bad data';
 6382:               return (\%hash, $string);
 6383:           }
 6384:       } else {
 6385:           $string =~ s/^(.*?)=//;
 6386: 	  $key=&unescape($1);
 6387:       }
 6388:       $string =~ s/^=//;
 6389: 
 6390:       #value
 6391:       my $value='';
 6392:       if($string =~ /^__HASH_REF__/) {
 6393:           ($value, $string)=&str2hashref($string);
 6394:           if(defined($value->{'error'})) {
 6395:               $hash{'error'}='Bad data';
 6396:               return (\%hash, $string);
 6397:           }
 6398:       } elsif($string =~ /^__ARRAY_REF__/) {
 6399:           ($value, $string)=&str2arrayref($string);
 6400:           if($value->[0] eq 'Array reference error') {
 6401:               $hash{'error'}='Bad data';
 6402:               return (\%hash, $string);
 6403:           }
 6404:       } else {
 6405: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6406:       }
 6407:       $string =~ s/^&//;
 6408: 
 6409:       $hash{$key}=$value;
 6410:   }
 6411: 
 6412:   $string =~ s/^__END_HASH_REF__//;
 6413: 
 6414:   return (\%hash, $string);
 6415: }
 6416: 
 6417: sub str2array {
 6418:     my ($string)=@_;
 6419:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6420:     return @$array;
 6421: }
 6422: 
 6423: sub str2arrayref {
 6424:   my ($string) = @_;
 6425:   my @array;
 6426: 
 6427:   if($string !~ /^__ARRAY_REF__/) {
 6428:       if (! ($string eq '' || !defined($string))) {
 6429: 	  $array[0]='Array reference error';
 6430:       }
 6431:       return (\@array, $string);
 6432:   }
 6433: 
 6434:   $string =~ s/^__ARRAY_REF__//;
 6435: 
 6436:   while($string !~ /^__END_ARRAY_REF__/) {
 6437:       my $value='';
 6438:       if($string =~ /^__HASH_REF__/) {
 6439:           ($value, $string)=&str2hashref($string);
 6440:           if(defined($value->{'error'})) {
 6441:               $array[0] ='Array reference error';
 6442:               return (\@array, $string);
 6443:           }
 6444:       } elsif($string =~ /^__ARRAY_REF__/) {
 6445:           ($value, $string)=&str2arrayref($string);
 6446:           if($value->[0] eq 'Array reference error') {
 6447:               $array[0] ='Array reference error';
 6448:               return (\@array, $string);
 6449:           }
 6450:       } else {
 6451: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6452:       }
 6453:       $string =~ s/^&//;
 6454: 
 6455:       push(@array, $value);
 6456:   }
 6457: 
 6458:   $string =~ s/^__END_ARRAY_REF__//;
 6459: 
 6460:   return (\@array, $string);
 6461: }
 6462: 
 6463: # -------------------------------------------------------------------Temp Store
 6464: 
 6465: sub tmpreset {
 6466:   my ($symb,$namespace,$domain,$stuname) = @_;
 6467:   if (!$symb) {
 6468:     $symb=&symbread();
 6469:     if (!$symb) { $symb= $env{'request.url'}; }
 6470:   }
 6471:   $symb=escape($symb);
 6472: 
 6473:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6474:   $namespace=~s/\//\_/g;
 6475:   $namespace=~s/\W//g;
 6476: 
 6477:   if (!$domain) { $domain=$env{'user.domain'}; }
 6478:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6479:   if ($domain eq 'public' && $stuname eq 'public') {
 6480:       $stuname=&get_requestor_ip();
 6481:   }
 6482:   my $path=LONCAPA::tempdir();
 6483:   my %hash;
 6484:   if (tie(%hash,'GDBM_File',
 6485: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6486: 	  &GDBM_WRCREAT(),0640)) {
 6487:     foreach my $key (keys(%hash)) {
 6488:       if ($key=~ /:$symb/) {
 6489: 	delete($hash{$key});
 6490:       }
 6491:     }
 6492:   }
 6493: }
 6494: 
 6495: sub tmpstore {
 6496:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6497: 
 6498:   if (!$symb) {
 6499:     $symb=&symbread();
 6500:     if (!$symb) { $symb= $env{'request.url'}; }
 6501:   }
 6502:   $symb=escape($symb);
 6503: 
 6504:   if (!$namespace) {
 6505:     # I don't think we would ever want to store this for a course.
 6506:     # it seems this will only be used if we don't have a course.
 6507:     #$namespace=$env{'request.course.id'};
 6508:     #if (!$namespace) {
 6509:       $namespace=$env{'request.state'};
 6510:     #}
 6511:   }
 6512:   $namespace=~s/\//\_/g;
 6513:   $namespace=~s/\W//g;
 6514:   if (!$domain) { $domain=$env{'user.domain'}; }
 6515:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6516:   if ($domain eq 'public' && $stuname eq 'public') {
 6517:       $stuname=&get_requestor_ip();
 6518:   }
 6519:   my $now=time;
 6520:   my %hash;
 6521:   my $path=LONCAPA::tempdir();
 6522:   if (tie(%hash,'GDBM_File',
 6523: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6524: 	  &GDBM_WRCREAT(),0640)) {
 6525:     $hash{"version:$symb"}++;
 6526:     my $version=$hash{"version:$symb"};
 6527:     my $allkeys=''; 
 6528:     foreach my $key (keys(%$storehash)) {
 6529:       $allkeys.=$key.':';
 6530:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6531:     }
 6532:     $hash{"$version:$symb:timestamp"}=$now;
 6533:     $allkeys.='timestamp';
 6534:     $hash{"$version:keys:$symb"}=$allkeys;
 6535:     if (untie(%hash)) {
 6536:       return 'ok';
 6537:     } else {
 6538:       return "error:$!";
 6539:     }
 6540:   } else {
 6541:     return "error:$!";
 6542:   }
 6543: }
 6544: 
 6545: # -----------------------------------------------------------------Temp Restore
 6546: 
 6547: sub tmprestore {
 6548:   my ($symb,$namespace,$domain,$stuname) = @_;
 6549: 
 6550:   if (!$symb) {
 6551:     $symb=&symbread();
 6552:     if (!$symb) { $symb= $env{'request.url'}; }
 6553:   }
 6554:   $symb=escape($symb);
 6555: 
 6556:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6557: 
 6558:   if (!$domain) { $domain=$env{'user.domain'}; }
 6559:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6560:   if ($domain eq 'public' && $stuname eq 'public') {
 6561:       $stuname=&get_requestor_ip();
 6562:   }
 6563:   my %returnhash;
 6564:   $namespace=~s/\//\_/g;
 6565:   $namespace=~s/\W//g;
 6566:   my %hash;
 6567:   my $path=LONCAPA::tempdir();
 6568:   if (tie(%hash,'GDBM_File',
 6569: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6570: 	  &GDBM_READER(),0640)) {
 6571:     my $version=$hash{"version:$symb"};
 6572:     $returnhash{'version'}=$version;
 6573:     my $scope;
 6574:     for ($scope=1;$scope<=$version;$scope++) {
 6575:       my $vkeys=$hash{"$scope:keys:$symb"};
 6576:       my @keys=split(/:/,$vkeys);
 6577:       my $key;
 6578:       $returnhash{"$scope:keys"}=$vkeys;
 6579:       foreach $key (@keys) {
 6580: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6581: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6582:       }
 6583:     }
 6584:     if (!(untie(%hash))) {
 6585:       return "error:$!";
 6586:     }
 6587:   } else {
 6588:     return "error:$!";
 6589:   }
 6590:   return %returnhash;
 6591: }
 6592: 
 6593: # ----------------------------------------------------------------------- Store
 6594: 
 6595: sub store {
 6596:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6597:     my $home='';
 6598: 
 6599:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6600: 
 6601:     $symb=&symbclean($symb);
 6602:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6603: 
 6604:     if (!$domain) { $domain=$env{'user.domain'}; }
 6605:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6606: 
 6607:     &devalidate($symb,$stuname,$domain);
 6608: 
 6609:     $symb=escape($symb);
 6610:     if (!$namespace) { 
 6611:        unless ($namespace=$env{'request.course.id'}) { 
 6612:           return ''; 
 6613:        } 
 6614:     }
 6615:     if (!$home) { $home=$env{'user.home'}; }
 6616: 
 6617:     $$storehash{'ip'}=&get_requestor_ip();
 6618:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6619: 
 6620:     my $namevalue='';
 6621:     foreach my $key (keys(%$storehash)) {
 6622:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6623:     }
 6624:     $namevalue=~s/\&$//;
 6625:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6626:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6627: }
 6628: 
 6629: # -------------------------------------------------------------- Critical Store
 6630: 
 6631: sub cstore {
 6632:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6633:     my $home='';
 6634: 
 6635:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6636: 
 6637:     $symb=&symbclean($symb);
 6638:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6639: 
 6640:     if (!$domain) { $domain=$env{'user.domain'}; }
 6641:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6642: 
 6643:     &devalidate($symb,$stuname,$domain);
 6644: 
 6645:     $symb=escape($symb);
 6646:     if (!$namespace) { 
 6647:        unless ($namespace=$env{'request.course.id'}) { 
 6648:           return ''; 
 6649:        } 
 6650:     }
 6651:     if (!$home) { $home=$env{'user.home'}; }
 6652: 
 6653:     $$storehash{'ip'}=&get_requestor_ip();
 6654:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6655: 
 6656:     my $namevalue='';
 6657:     foreach my $key (keys(%$storehash)) {
 6658:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6659:     }
 6660:     $namevalue=~s/\&$//;
 6661:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6662:     return critical
 6663:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6664: }
 6665: 
 6666: # --------------------------------------------------------------------- Restore
 6667: 
 6668: sub restore {
 6669:     my ($symb,$namespace,$domain,$stuname) = @_;
 6670:     my $home='';
 6671: 
 6672:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6673: 
 6674:     if (!$symb) {
 6675:         return if ($namespace eq 'courserequests');
 6676:         unless ($symb=escape(&symbread())) { return ''; }
 6677:     } else {
 6678:         unless ($namespace eq 'courserequests') {
 6679:             $symb=&escape(&symbclean($symb));
 6680:         }
 6681:     }
 6682:     if (!$namespace) { 
 6683:        unless ($namespace=$env{'request.course.id'}) { 
 6684:           return ''; 
 6685:        } 
 6686:     }
 6687:     if (!$domain) { $domain=$env{'user.domain'}; }
 6688:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6689:     if (!$home) { $home=$env{'user.home'}; }
 6690:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6691: 
 6692:     my %returnhash=();
 6693:     foreach my $line (split(/\&/,$answer)) {
 6694: 	my ($name,$value)=split(/\=/,$line);
 6695:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6696:     }
 6697:     my $version;
 6698:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6699:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6700:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6701:        }
 6702:     }
 6703:     return %returnhash;
 6704: }
 6705: 
 6706: # ---------------------------------------------------------- Course Description
 6707: #
 6708: #  
 6709: 
 6710: sub coursedescription {
 6711:     my ($courseid,$args)=@_;
 6712:     $courseid=~s/^\///;
 6713:     $courseid=~s/\_/\//g;
 6714:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6715:     my $chome=&homeserver($cnum,$cdomain);
 6716:     my $normalid=$cdomain.'_'.$cnum;
 6717:     # need to always cache even if we get errors otherwise we keep 
 6718:     # trying and trying and trying to get the course description.
 6719:     my %envhash=();
 6720:     my %returnhash=();
 6721:     
 6722:     my $expiretime=600;
 6723:     if ($env{'request.course.id'} eq $normalid) {
 6724: 	$expiretime=120;
 6725:     }
 6726: 
 6727:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6728:     if (!$args->{'freshen_cache'}
 6729: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6730: 	foreach my $key (keys(%env)) {
 6731: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6732: 	    my ($setting) = $1;
 6733: 	    $returnhash{$setting} = $env{$key};
 6734: 	}
 6735: 	return %returnhash;
 6736:     }
 6737: 
 6738:     # get the data again
 6739: 
 6740:     if (!$args->{'one_time'}) {
 6741: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6742:     }
 6743: 
 6744:     if ($chome ne 'no_host') {
 6745:        %returnhash=&dump('environment',$cdomain,$cnum);
 6746:        if (!exists($returnhash{'con_lost'})) {
 6747: 	   my $username = $env{'user.name'}; # Defult username
 6748: 	   if(defined $args->{'user'}) {
 6749: 	       $username = $args->{'user'};
 6750: 	   }
 6751:            $returnhash{'home'}= $chome;
 6752: 	   $returnhash{'domain'} = $cdomain;
 6753: 	   $returnhash{'num'} = $cnum;
 6754:            if (!defined($returnhash{'type'})) {
 6755:                $returnhash{'type'} = 'Course';
 6756:            }
 6757:            while (my ($name,$value) = each %returnhash) {
 6758:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6759:            }
 6760:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6761:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6762: 	       $username.'_'.$cdomain.'_'.$cnum;
 6763:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6764:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6765:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6766:        }
 6767:     }
 6768:     if (!$args->{'one_time'}) {
 6769: 	&appenv(\%envhash);
 6770:     }
 6771:     return %returnhash;
 6772: }
 6773: 
 6774: sub update_released_required {
 6775:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6776:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6777:         $cid = $env{'request.course.id'};
 6778:         $cdom = $env{'course.'.$cid.'.domain'};
 6779:         $cnum = $env{'course.'.$cid.'.num'};
 6780:         $chome = $env{'course.'.$cid.'.home'};
 6781:     }
 6782:     if ($needsrelease) {
 6783:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6784:         my $needsupdate;
 6785:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6786:             $needsupdate = 1;
 6787:         } else {
 6788:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6789:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6790:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6791:                 $needsupdate = 1;
 6792:             }
 6793:         }
 6794:         if ($needsupdate) {
 6795:             my %needshash = (
 6796:                              'internal.releaserequired' => $needsrelease,
 6797:                             );
 6798:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6799:             if ($putresult eq 'ok') {
 6800:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6801:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6802:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6803:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6804:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6805:                 }
 6806:             }
 6807:         }
 6808:     }
 6809:     return;
 6810: }
 6811: 
 6812: # -------------------------------------------------See if a user is privileged
 6813: 
 6814: sub privileged {
 6815:     my ($username,$domain,$possdomains,$possroles)=@_;
 6816:     my $now = time;
 6817:     my $roles;
 6818:     if (ref($possroles) eq 'ARRAY') {
 6819:         $roles = $possroles; 
 6820:     } else {
 6821:         $roles = ['dc','su'];
 6822:     }
 6823:     if (ref($possdomains) eq 'ARRAY') {
 6824:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6825:         foreach my $dom (@{$possdomains}) {
 6826:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6827:                 (ref($privileged{$dom}) eq 'HASH')) {
 6828:                 foreach my $role (@{$roles}) {
 6829:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6830:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6831:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6832:                             return 1 unless (($end && $end < $now) ||
 6833:                                              ($start && $start > $now));
 6834:                         }
 6835:                     }
 6836:                 }
 6837:             }
 6838:         }
 6839:     } else {
 6840:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6841:         my $now = time;
 6842: 
 6843:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6844:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6845:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6846:                 return 1 unless ($tend && $tend < $now) 
 6847:                         or ($tstart && $tstart > $now);
 6848:             }
 6849:         }
 6850:     }
 6851:     return 0;
 6852: }
 6853: 
 6854: sub privileged_by_domain {
 6855:     my ($domains,$roles) = @_;
 6856:     my %privileged = ();
 6857:     my $cachetime = 60*60*24;
 6858:     my $now = time;
 6859:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6860:         return %privileged;
 6861:     }
 6862:     foreach my $dom (@{$domains}) {
 6863:         next if (ref($privileged{$dom}) eq 'HASH');
 6864:         my $needroles;
 6865:         foreach my $role (@{$roles}) {
 6866:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6867:             if (defined($cached)) {
 6868:                 if (ref($result) eq 'HASH') {
 6869:                     $privileged{$dom}{$role} = $result;
 6870:                 }
 6871:             } else {
 6872:                 $needroles = 1;
 6873:             }
 6874:         }
 6875:         if ($needroles) {
 6876:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6877:             $privileged{$dom} = {};
 6878:             foreach my $server (keys(%dompersonnel)) {
 6879:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6880:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6881:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6882:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6883:                         next if ($end && $end < $now);
 6884:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6885:                             $dompersonnel{$server}{$item};
 6886:                     }
 6887:                 }
 6888:             }
 6889:             if (ref($privileged{$dom}) eq 'HASH') {
 6890:                 foreach my $role (@{$roles}) {
 6891:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6892:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6893:                     } else {
 6894:                         my %hash = ();
 6895:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6896:                     }
 6897:                 }
 6898:             }
 6899:         }
 6900:     }
 6901:     return %privileged;
 6902: }
 6903: 
 6904: # -------------------------------------------------------- Get user privileges
 6905: 
 6906: sub rolesinit {
 6907:     my ($domain, $username) = @_;
 6908:     my %userroles = ('user.login.time' => time);
 6909:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6910: 
 6911:     # firstaccess and timerinterval are related to timed maps/resources. 
 6912:     # also, blocking can be triggered by an activating timer
 6913:     # it's saved in the user's %env.
 6914:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6915:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6916:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6917:         %timerintchk, %timerintenv);
 6918: 
 6919:     foreach my $key (keys(%firstaccess)) {
 6920:         my ($cid, $rest) = split(/\0/, $key);
 6921:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6922:     }
 6923: 
 6924:     foreach my $key (keys(%timerinterval)) {
 6925:         my ($cid,$rest) = split(/\0/,$key);
 6926:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6927:     }
 6928: 
 6929:     my %allroles=();
 6930:     my %allgroups=();
 6931: 
 6932:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6933:         my $role = $rolesdump{$area};
 6934:         $area =~ s/\_\w\w$//;
 6935: 
 6936:         my ($trole, $tend, $tstart, $group_privs);
 6937: 
 6938:         if ($role =~ /^cr/) {
 6939:         # Custom role, defined by a user 
 6940:         # e.g., user.role.cr/msu/smith/mynewrole
 6941:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6942:                 $trole = $1;
 6943:                 ($tend, $tstart) = split('_', $2);
 6944:             } else {
 6945:                 $trole = $role;
 6946:             }
 6947:         } elsif ($role =~ m|^gr/|) {
 6948:         # Role of member in a group, defined within a course/community
 6949:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6950:             ($trole, $tend, $tstart) = split(/_/, $role);
 6951:             next if $tstart eq '-1';
 6952:             ($trole, $group_privs) = split(/\//, $trole);
 6953:             $group_privs = &unescape($group_privs);
 6954:         } else {
 6955:         # Just a normal role, defined in roles.tab
 6956:             ($trole, $tend, $tstart) = split(/_/,$role);
 6957:         }
 6958: 
 6959:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6960:                  $username);
 6961:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6962: 
 6963:         # role expired or not available yet?
 6964:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6965:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6966: 
 6967:         next if $area eq '' or $trole eq '';
 6968: 
 6969:         my $spec = "$trole.$area";
 6970:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6971: 
 6972:         if ($trole =~ /^cr\//) {
 6973:         # Custom role, defined by a user
 6974:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6975:         } elsif ($trole eq 'gr') {
 6976:         # Role of a member in a group, defined within a course/community
 6977:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6978:             next;
 6979:         } else {
 6980:         # Normal role, defined in roles.tab
 6981:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6982:         }
 6983: 
 6984:         my $cid = $tdomain.'_'.$trest;
 6985:         unless ($firstaccchk{$cid}) {
 6986:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6987:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6988:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6989:                         $coursetimerstarts{$cid}{$item}; 
 6990:                 }
 6991:             }
 6992:             $firstaccchk{$cid} = 1;
 6993:         }
 6994:         unless ($timerintchk{$cid}) {
 6995:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6996:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6997:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6998:                        $coursetimerintervals{$cid}{$item};
 6999:                 }
 7000:             }
 7001:             $timerintchk{$cid} = 1;
 7002:         }
 7003:     }
 7004: 
 7005:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 7006:                                                           \%allroles, \%allgroups);
 7007:     $env{'user.adv'} = $userroles{'user.adv'};
 7008:     $env{'user.rar'} = $userroles{'user.rar'};
 7009: 
 7010:     return (\%userroles,\%firstaccenv,\%timerintenv);
 7011: }
 7012: 
 7013: sub set_arearole {
 7014:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 7015:     unless ($nolog) {
 7016: # log the associated role with the area
 7017:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 7018:     }
 7019:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 7020: }
 7021: 
 7022: sub custom_roleprivs {
 7023:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 7024:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 7025:     my $homsvr = &homeserver($rauthor,$rdomain);
 7026:     if (&hostname($homsvr) ne '') {
 7027:         my ($rdummy,$roledef)=
 7028:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 7029:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 7030:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 7031:             if (defined($syspriv)) {
 7032:                 if ($trest =~ /^$match_community$/) {
 7033:                     $syspriv =~ s/bre\&S//; 
 7034:                 }
 7035:                 $$allroles{'cm./'}.=':'.$syspriv;
 7036:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 7037:             }
 7038:             if ($tdomain ne '') {
 7039:                 if (defined($dompriv)) {
 7040:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 7041:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 7042:                 }
 7043:                 if (($trest ne '') && (defined($coursepriv))) {
 7044:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 7045:                         my $rolename = $1;
 7046:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 7047:                     }
 7048:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 7049:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 7050:                 }
 7051:             }
 7052:         }
 7053:     }
 7054: }
 7055: 
 7056: sub course_adhocrole_privs {
 7057:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 7058:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 7059:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 7060:         my (%currprivs,%storeprivs);
 7061:         foreach my $item (split(/:/,$coursepriv)) {
 7062:             my ($priv,$restrict) = split(/\&/,$item);
 7063:             $currprivs{$priv} = $restrict;
 7064:         }
 7065:         my (%possadd,%possremove,%full);
 7066:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 7067:             my ($priv,$restrict)=split(/\&/,$item);
 7068:             $full{$priv} = $restrict;
 7069:         }
 7070:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 7071:             next if ($item eq '');
 7072:             my ($rule,$rest) = split(/=/,$item);
 7073:             next unless (($rule eq 'off') || ($rule eq 'on'));
 7074:             foreach my $priv (split(/:/,$rest)) {
 7075:                 if ($priv ne '') {
 7076:                     if ($rule eq 'off') {
 7077:                         $possremove{$priv} = 1;
 7078:                     } else {
 7079:                         $possadd{$priv} = 1;
 7080:                     }
 7081:                 }
 7082:             }
 7083:         }
 7084:         foreach my $priv (sort(keys(%full))) {
 7085:             if (exists($currprivs{$priv})) {
 7086:                 unless (exists($possremove{$priv})) {
 7087:                     $storeprivs{$priv} = $currprivs{$priv};
 7088:                 }
 7089:             } elsif (exists($possadd{$priv})) {
 7090:                 $storeprivs{$priv} = $full{$priv};
 7091:             }
 7092:         }
 7093:         $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 7094:     }
 7095:     return $coursepriv;
 7096: }
 7097: 
 7098: sub group_roleprivs {
 7099:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 7100:     my $access = 1;
 7101:     my $now = time;
 7102:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 7103:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 7104:     if ($access) {
 7105:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 7106:         $$allgroups{$course}{$group} .=':'.$group_privs;
 7107:     }
 7108: }
 7109: 
 7110: sub standard_roleprivs {
 7111:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 7112:     if (defined($pr{$trole.':s'})) {
 7113:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 7114:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 7115:     }
 7116:     if ($tdomain ne '') {
 7117:         if (defined($pr{$trole.':d'})) {
 7118:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 7119:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 7120:         }
 7121:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 7122:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 7123:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 7124:         }
 7125:     }
 7126: }
 7127: 
 7128: sub set_userprivs {
 7129:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 7130:     my $author=0;
 7131:     my $adv=0;
 7132:     my $rar=0;
 7133:     my %grouproles = ();
 7134:     if (keys(%{$allgroups}) > 0) {
 7135:         my @groupkeys; 
 7136:         foreach my $role (keys(%{$allroles})) {
 7137:             push(@groupkeys,$role);
 7138:         }
 7139:         if (ref($groups_roles) eq 'HASH') {
 7140:             foreach my $key (keys(%{$groups_roles})) {
 7141:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 7142:                     push(@groupkeys,$key);
 7143:                 }
 7144:             }
 7145:         }
 7146:         if (@groupkeys > 0) {
 7147:             foreach my $role (@groupkeys) {
 7148:                 my ($trole,$area,$sec,$extendedarea);
 7149:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 7150:                     $trole = $1;
 7151:                     $area = $2;
 7152:                     $sec = $3;
 7153:                     $extendedarea = $area.$sec;
 7154:                     if (exists($$allgroups{$area})) {
 7155:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 7156:                             my $spec = $trole.'.'.$extendedarea;
 7157:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 7158:                                                 $$allgroups{$area}{$group};
 7159:                         }
 7160:                     }
 7161:                 }
 7162:             }
 7163:         }
 7164:     }
 7165:     foreach my $group (keys(%grouproles)) {
 7166:         $$allroles{$group} = $grouproles{$group};
 7167:     }
 7168:     foreach my $role (keys(%{$allroles})) {
 7169:         my %thesepriv;
 7170:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 7171:         foreach my $item (split(/:/,$$allroles{$role})) {
 7172:             if ($item ne '') {
 7173:                 my ($privilege,$restrictions)=split(/&/,$item);
 7174:                 if ($restrictions eq '') {
 7175:                     $thesepriv{$privilege}='F';
 7176:                 } elsif ($thesepriv{$privilege} ne 'F') {
 7177:                     $thesepriv{$privilege}.=$restrictions;
 7178:                 }
 7179:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 7180:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 7181:             }
 7182:         }
 7183:         my $thesestr='';
 7184:         foreach my $priv (sort(keys(%thesepriv))) {
 7185: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 7186: 	}
 7187:         $userroles->{'user.priv.'.$role} = $thesestr;
 7188:     }
 7189:     return ($author,$adv,$rar);
 7190: }
 7191: 
 7192: sub role_status {
 7193:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 7194:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 7195:         my ($one,$two) = split(m{\./},$rolekey,2);
 7196:         (undef,undef,$$role) = split(/\./,$one,3);
 7197:         unless (!defined($$role) || $$role eq '') {
 7198:             $$where = '/'.$two;
 7199:             $$trolecode=$$role.'.'.$$where;
 7200:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 7201:             $$tstatus='is';
 7202:             if ($$tstart && $$tstart>$update) {
 7203:                 $$tstatus='future';
 7204:                 if ($$tstart<$now) {
 7205:                     if ($$tstart && $$tstart>$refresh) {
 7206:                         if (($$where ne '') && ($$role ne '')) {
 7207:                             my (%allroles,%allgroups,$group_privs,
 7208:                                 %groups_roles,@rolecodes);
 7209:                             my %userroles = (
 7210:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 7211:                             );
 7212:                             @rolecodes = ('cm'); 
 7213:                             my $spec=$$role.'.'.$$where;
 7214:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 7215:                             if ($$role =~ /^cr\//) {
 7216:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 7217:                                 push(@rolecodes,'cr');
 7218:                             } elsif ($$role eq 'gr') {
 7219:                                 push(@rolecodes,$$role);
 7220:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 7221:                                                     $env{'user.name'});
 7222:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 7223:                                 (undef,my $group_privs) = split(/\//,$trole);
 7224:                                 $group_privs = &unescape($group_privs);
 7225:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 7226:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 7227:                                 &get_groups_roles($tdomain,$trest,
 7228:                                                   \%course_roles,\@rolecodes,
 7229:                                                   \%groups_roles);
 7230:                             } else {
 7231:                                 push(@rolecodes,$$role);
 7232:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 7233:                             }
 7234:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 7235:                                                                    \%groups_roles);
 7236:                             &appenv(\%userroles,\@rolecodes);
 7237:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7238:                         }
 7239:                     }
 7240:                     $$tstatus = 'is';
 7241:                 }
 7242:             }
 7243:             if ($$tend) {
 7244:                 if ($$tend<$update) {
 7245:                     $$tstatus='expired';
 7246:                 } elsif ($$tend<$now) {
 7247:                     $$tstatus='will_not';
 7248:                 }
 7249:             }
 7250:         }
 7251:     }
 7252: }
 7253: 
 7254: sub get_groups_roles {
 7255:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 7256:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 7257:                   (ref($rolecodes) eq 'ARRAY') && 
 7258:                   (ref($groups_roles) eq 'HASH')); 
 7259:     if (keys(%{$cdom_courseroles}) > 0) {
 7260:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 7261:         if ($cdom ne '' && $cnum ne '') {
 7262:             foreach my $key (keys(%{$cdom_courseroles})) {
 7263:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 7264:                     my $crsrole = $1;
 7265:                     my $crssec = $2;
 7266:                     if ($crsrole =~ /^cr/) {
 7267:                         unless (grep(/^cr$/,@{$rolecodes})) {
 7268:                             push(@{$rolecodes},'cr');
 7269:                         }
 7270:                     } else {
 7271:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 7272:                             push(@{$rolecodes},$crsrole);
 7273:                         }
 7274:                     }
 7275:                     my $rolekey = "$crsrole./$cdom/$cnum";
 7276:                     if ($crssec ne '') {
 7277:                         $rolekey .= "/$crssec";
 7278:                     }
 7279:                     $rolekey .= './';
 7280:                     $groups_roles->{$rolekey} = $rolecodes;
 7281:                 }
 7282:             }
 7283:         }
 7284:     }
 7285:     return;
 7286: }
 7287: 
 7288: sub delete_env_groupprivs {
 7289:     my ($where,$courseroles,$possroles) = @_;
 7290:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 7291:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 7292:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 7293:         %{$courseroles->{$udom}} =
 7294:             &get_my_roles('','','userroles',['active'],
 7295:                           $possroles,[$udom],1);
 7296:     }
 7297:     if (ref($courseroles->{$udom}) eq 'HASH') {
 7298:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 7299:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 7300:             my $area = '/'.$cdom.'/'.$cnum;
 7301:             my $privkey = "user.priv.$crsrole.$area";
 7302:             if ($crssec ne '') {
 7303:                 $privkey .= '/'.$crssec;
 7304:             }
 7305:             $privkey .= ".$area/$group";
 7306:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 7307:         }
 7308:     }
 7309:     return;
 7310: }
 7311: 
 7312: sub check_adhoc_privs {
 7313:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 7314:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 7315:     if ($sec) {
 7316:         $cckey .= '/'.$sec;
 7317:     } 
 7318:     my $setprivs;
 7319:     if ($env{$cckey}) {
 7320:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 7321:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 7322:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 7323:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7324:             $setprivs = 1;
 7325:         }
 7326:     } else {
 7327:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7328:         $setprivs = 1;
 7329:     }
 7330:     return $setprivs;
 7331: }
 7332: 
 7333: sub set_adhoc_privileges {
 7334: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7335:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7336:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7337:     if ($sec ne '') {
 7338:         $area .= '/'.$sec;
 7339:     }
 7340:     my $spec = $role.'.'.$area;
 7341:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7342:                                   $env{'user.name'},1);
 7343:     my %rolehash = ();
 7344:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7345:         my $rolename = $1;
 7346:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7347:         my %domdef = &get_domain_defaults($dcdom);
 7348:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7349:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7350:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7351:             }
 7352:         }
 7353:     } else {
 7354:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7355:     }
 7356:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7357:     &appenv(\%userroles,[$role,'cm']);
 7358:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7359:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7360:             ($caller eq 'tiny')) {
 7361:         &appenv( {'request.role'        => $spec,
 7362:                   'request.role.domain' => $dcdom,
 7363:                   'request.course.sec'  => $sec,
 7364:                  }
 7365:                );
 7366:         my $tadv=0;
 7367:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7368:         &appenv({'request.role.adv'    => $tadv});
 7369:     }
 7370: }
 7371: 
 7372: # --------------------------------------------------------------- get interface
 7373: 
 7374: sub get {
 7375:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7376:    my $items='';
 7377:    foreach my $item (@$storearr) {
 7378:        $items.=&escape($item).'&';
 7379:    }
 7380:    $items=~s/\&$//;
 7381:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7382:    if (!$uname) { $uname=$env{'user.name'}; }
 7383:    my $uhome=&homeserver($uname,$udomain);
 7384: 
 7385:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7386:    my @pairs=split(/\&/,$rep);
 7387:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7388:      return @pairs;
 7389:    }
 7390:    my %returnhash=();
 7391:    my $i=0;
 7392:    foreach my $item (@$storearr) {
 7393:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7394:       $i++;
 7395:    }
 7396:    return %returnhash;
 7397: }
 7398: 
 7399: # --------------------------------------------------------------- del interface
 7400: 
 7401: sub del {
 7402:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7403:    my $items='';
 7404:    foreach my $item (@$storearr) {
 7405:        $items.=&escape($item).'&';
 7406:    }
 7407: 
 7408:    $items=~s/\&$//;
 7409:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7410:    if (!$uname) { $uname=$env{'user.name'}; }
 7411:    my $uhome=&homeserver($uname,$udomain);
 7412:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7413: }
 7414: 
 7415: # -------------------------------------------------------------- dump interface
 7416: 
 7417: sub unserialize {
 7418:     my ($rep, $escapedkeys) = @_;
 7419: 
 7420:     return {} if $rep =~ /^error/;
 7421: 
 7422:     my %returnhash=();
 7423: 	foreach my $item (split(/\&/,$rep)) {
 7424: 	    my ($key, $value) = split(/=/, $item, 2);
 7425: 	    $key = unescape($key) unless $escapedkeys;
 7426: 	    next if $key =~ /^error: 2 /;
 7427: 	    $returnhash{$key} = &thaw_unescape($value);
 7428: 	}
 7429:     #return %returnhash;
 7430:     return \%returnhash;
 7431: }        
 7432: 
 7433: # see Lond::dump_with_regexp
 7434: # if $escapedkeys hash keys won't get unescaped.
 7435: sub dump {
 7436:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
 7437:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7438:     if (!$uname) { $uname=$env{'user.name'}; }
 7439:     my $uhome=&homeserver($uname,$udomain);
 7440: 
 7441:     if ($regexp) {
 7442:         $regexp=&escape($regexp);
 7443:     } else {
 7444:         $regexp='.';
 7445:     }
 7446:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7447:         # user is hosted on this machine
 7448:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7449:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7450:         return %{unserialize($reply, $escapedkeys)};
 7451:     }
 7452:     my $rep;
 7453:     if ($encrypt) {
 7454:         $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7455:     } else {
 7456:         $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7457:     }
 7458:     my @pairs=split(/\&/,$rep);
 7459:     my %returnhash=();
 7460:     if (!($rep =~ /^error/ )) {
 7461: 	foreach my $item (@pairs) {
 7462: 	    my ($key,$value)=split(/=/,$item,2);
 7463:         $key = unescape($key) unless $escapedkeys;
 7464:         #$key = &unescape($key);
 7465: 	    next if ($key =~ /^error: 2 /);
 7466: 	    $returnhash{$key}=&thaw_unescape($value);
 7467: 	}
 7468:     }
 7469:     return %returnhash;
 7470: }
 7471: 
 7472: 
 7473: # --------------------------------------------------------- dumpstore interface
 7474: 
 7475: sub dumpstore {
 7476:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7477:    # same as dump but keys must be escaped. They may contain colon separated
 7478:    # lists of values that may themself contain colons (e.g. symbs).
 7479:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7480: }
 7481: 
 7482: # -------------------------------------------------------------- keys interface
 7483: 
 7484: sub getkeys {
 7485:    my ($namespace,$udomain,$uname)=@_;
 7486:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7487:    if (!$uname) { $uname=$env{'user.name'}; }
 7488:    my $uhome=&homeserver($uname,$udomain);
 7489:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7490:    my @keyarray=();
 7491:    foreach my $key (split(/\&/,$rep)) {
 7492:       next if ($key =~ /^error: 2 /);
 7493:       push(@keyarray,&unescape($key));
 7494:    }
 7495:    return @keyarray;
 7496: }
 7497: 
 7498: # --------------------------------------------------------------- currentdump
 7499: sub currentdump {
 7500:    my ($courseid,$sdom,$sname)=@_;
 7501:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7502:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7503:    $sname    = $env{'user.name'}         if (! defined($sname));
 7504:    my $uhome = &homeserver($sname,$sdom);
 7505:    my $rep;
 7506: 
 7507:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7508:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7509:                    $courseid)));
 7510:    } else {
 7511:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7512:    }
 7513: 
 7514:    return if ($rep =~ /^(error:|no_such_host)/);
 7515:    #
 7516:    my %returnhash=();
 7517:    #
 7518:    if ($rep eq 'unknown_cmd') {
 7519:        # an old lond will not know currentdump
 7520:        # Do a dump and make it look like a currentdump
 7521:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7522:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7523:        my %hash = @tmp;
 7524:        @tmp=();
 7525:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7526:    } else {
 7527:        my @pairs=split(/\&/,$rep);
 7528:        foreach my $pair (@pairs) {
 7529:            my ($key,$value)=split(/=/,$pair,2);
 7530:            my ($symb,$param) = split(/:/,$key);
 7531:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7532:                                                         &thaw_unescape($value);
 7533:        }
 7534:    }
 7535:    return %returnhash;
 7536: }
 7537: 
 7538: sub convert_dump_to_currentdump{
 7539:     my %hash = %{shift()};
 7540:     my %returnhash;
 7541:     # Code ripped from lond, essentially.  The only difference
 7542:     # here is the unescaping done by lonnet::dump().  Conceivably
 7543:     # we might run in to problems with parameter names =~ /^v\./
 7544:     while (my ($key,$value) = each(%hash)) {
 7545:         my ($v,$symb,$param) = split(/:/,$key);
 7546: 	$symb  = &unescape($symb);
 7547: 	$param = &unescape($param);
 7548:         next if ($v eq 'version' || $symb eq 'keys');
 7549:         next if (exists($returnhash{$symb}) &&
 7550:                  exists($returnhash{$symb}->{$param}) &&
 7551:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7552:         $returnhash{$symb}->{$param}=$value;
 7553:         $returnhash{$symb}->{'v.'.$param}=$v;
 7554:     }
 7555:     #
 7556:     # Remove all of the keys in the hashes which keep track of
 7557:     # the version of the parameter.
 7558:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7559:         # use a foreach because we are going to delete from the hash.
 7560:         foreach my $key (keys(%$param_hash)) {
 7561:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7562:         }
 7563:     }
 7564:     return \%returnhash;
 7565: }
 7566: 
 7567: # ------------------------------------------------------ critical inc interface
 7568: 
 7569: sub cinc {
 7570:     return &inc(@_,'critical');
 7571: }
 7572: 
 7573: # --------------------------------------------------------------- inc interface
 7574: 
 7575: sub inc {
 7576:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7577:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7578:     if (!$uname) { $uname=$env{'user.name'}; }
 7579:     my $uhome=&homeserver($uname,$udomain);
 7580:     my $items='';
 7581:     if (! ref($store)) {
 7582:         # got a single value, so use that instead
 7583:         $items = &escape($store).'=&';
 7584:     } elsif (ref($store) eq 'SCALAR') {
 7585:         $items = &escape($$store).'=&';        
 7586:     } elsif (ref($store) eq 'ARRAY') {
 7587:         $items = join('=&',map {&escape($_);} @{$store});
 7588:     } elsif (ref($store) eq 'HASH') {
 7589:         while (my($key,$value) = each(%{$store})) {
 7590:             $items.= &escape($key).'='.&escape($value).'&';
 7591:         }
 7592:     }
 7593:     $items=~s/\&$//;
 7594:     if ($critical) {
 7595: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7596:     } else {
 7597: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7598:     }
 7599: }
 7600: 
 7601: # --------------------------------------------------------------- put interface
 7602: 
 7603: sub put {
 7604:    my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
 7605:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7606:    if (!$uname) { $uname=$env{'user.name'}; }
 7607:    my $uhome=&homeserver($uname,$udomain);
 7608:    my $items='';
 7609:    foreach my $item (keys(%$storehash)) {
 7610:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7611:    }
 7612:    $items=~s/\&$//;
 7613:    if ($encrypt) {
 7614:        return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
 7615:    } else {
 7616:        return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7617:    }
 7618: }
 7619: 
 7620: # ------------------------------------------------------------ newput interface
 7621: 
 7622: sub newput {
 7623:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7624:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7625:    if (!$uname) { $uname=$env{'user.name'}; }
 7626:    my $uhome=&homeserver($uname,$udomain);
 7627:    my $items='';
 7628:    foreach my $key (keys(%$storehash)) {
 7629:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7630:    }
 7631:    $items=~s/\&$//;
 7632:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7633: }
 7634: 
 7635: # ---------------------------------------------------------  putstore interface
 7636: 
 7637: sub putstore {
 7638:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7639:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7640:    if (!$uname) { $uname=$env{'user.name'}; }
 7641:    my $uhome=&homeserver($uname,$udomain);
 7642:    my $items='';
 7643:    foreach my $key (keys(%$storehash)) {
 7644:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7645:    }
 7646:    $items=~s/\&$//;
 7647:    my $esc_symb=&escape($symb);
 7648:    my $esc_v=&escape($version);
 7649:    my $reply =
 7650:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7651: 	      $uhome);
 7652:    if (($tolog) && ($reply eq 'ok')) {
 7653:        my $namevalue='';
 7654:        foreach my $key (keys(%{$storehash})) {
 7655:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7656:        }
 7657:        my $ip = &get_requestor_ip();
 7658:        $namevalue .= 'ip='.&escape($ip).
 7659:                      '&host='.&escape($perlvar{'lonHostID'}).
 7660:                      '&version='.$esc_v.
 7661:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7662:        &courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7663:    }
 7664:    if ($reply eq 'unknown_cmd') {
 7665:        # gfall back to way things use to be done
 7666:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7667: 			    $uname);
 7668:    }
 7669:    return $reply;
 7670: }
 7671: 
 7672: sub old_putstore {
 7673:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7674:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7675:     if (!$uname) { $uname=$env{'user.name'}; }
 7676:     my $uhome=&homeserver($uname,$udomain);
 7677:     my %newstorehash;
 7678:     foreach my $item (keys(%$storehash)) {
 7679: 	my $key = $version.':'.&escape($symb).':'.$item;
 7680: 	$newstorehash{$key} = $storehash->{$item};
 7681:     }
 7682:     my $items='';
 7683:     my %allitems = ();
 7684:     foreach my $item (keys(%newstorehash)) {
 7685: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7686: 	    my $key = $1.':keys:'.$2;
 7687: 	    $allitems{$key} .= $3.':';
 7688: 	}
 7689: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7690:     }
 7691:     foreach my $item (keys(%allitems)) {
 7692: 	$allitems{$item} =~ s/\:$//;
 7693: 	$items.= $item.'='.$allitems{$item}.'&';
 7694:     }
 7695:     $items=~s/\&$//;
 7696:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7697: }
 7698: 
 7699: # ------------------------------------------------------ critical put interface
 7700: 
 7701: sub cput {
 7702:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7703:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7704:    if (!$uname) { $uname=$env{'user.name'}; }
 7705:    my $uhome=&homeserver($uname,$udomain);
 7706:    my $items='';
 7707:    foreach my $item (keys(%$storehash)) {
 7708:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7709:    }
 7710:    $items=~s/\&$//;
 7711:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7712: }
 7713: 
 7714: # -------------------------------------------------------------- eget interface
 7715: 
 7716: sub eget {
 7717:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7718:    my $items='';
 7719:    foreach my $item (@$storearr) {
 7720:        $items.=&escape($item).'&';
 7721:    }
 7722:    $items=~s/\&$//;
 7723:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7724:    if (!$uname) { $uname=$env{'user.name'}; }
 7725:    my $uhome=&homeserver($uname,$udomain);
 7726:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7727:    my @pairs=split(/\&/,$rep);
 7728:    my %returnhash=();
 7729:    my $i=0;
 7730:    foreach my $item (@$storearr) {
 7731:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7732:       $i++;
 7733:    }
 7734:    return %returnhash;
 7735: }
 7736: 
 7737: # ------------------------------------------------------------ tmpput interface
 7738: sub tmpput {
 7739:     my ($storehash,$server,$context)=@_;
 7740:     my $items='';
 7741:     foreach my $item (keys(%$storehash)) {
 7742: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7743:     }
 7744:     $items=~s/\&$//;
 7745:     if (defined($context)) {
 7746:         $items .= ':'.&escape($context);
 7747:     }
 7748:     return &reply("tmpput:$items",$server);
 7749: }
 7750: 
 7751: # ------------------------------------------------------------ tmpget interface
 7752: sub tmpget {
 7753:     my ($token,$server)=@_;
 7754:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7755:     my $rep=&reply("tmpget:$token",$server);
 7756:     my %returnhash;
 7757:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7758:         return %returnhash;
 7759:     }
 7760:     foreach my $item (split(/\&/,$rep)) {
 7761: 	my ($key,$value)=split(/=/,$item);
 7762: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7763:     }
 7764:     return %returnhash;
 7765: }
 7766: 
 7767: # ------------------------------------------------------------ tmpdel interface
 7768: sub tmpdel {
 7769:     my ($token,$server)=@_;
 7770:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7771:     return &reply("tmpdel:$token",$server);
 7772: }
 7773: 
 7774: # ------------------------------------------------------------ get_timebased_id 
 7775: 
 7776: sub get_timebased_id {
 7777:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7778:         $maxtries) = @_;
 7779:     my ($newid,$error,$dellock);
 7780:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7781:         return ('','ok','invalid call to get suffix');
 7782:     }
 7783: 
 7784: # set defaults for any optional args for which values were not supplied
 7785:     if ($who eq '') {
 7786:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7787:     }
 7788:     if (!$locktries) {
 7789:         $locktries = 3;
 7790:     }
 7791:     if (!$maxtries) {
 7792:         $maxtries = 10;
 7793:     }
 7794:     
 7795:     if (($cdom eq '') || ($cnum eq '')) {
 7796:         if ($env{'request.course.id'}) {
 7797:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7798:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7799:         }
 7800:         if (($cdom eq '') || ($cnum eq '')) {
 7801:             return ('','ok','call to get suffix not in course context');
 7802:         }
 7803:     }
 7804: 
 7805: # construct locking item
 7806:     my $lockhash = {
 7807:                       $prefix."\0".'locked_'.$keyid => $who,
 7808:                    };
 7809:     my $tries = 0;
 7810: 
 7811: # attempt to get lock on nohist_$namespace file
 7812:     my $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7813:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7814:         $tries ++;
 7815:         sleep 1;
 7816:         $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7817:     }
 7818: 
 7819: # attempt to get unique identifier, based on current timestamp
 7820:     if ($gotlock eq 'ok') {
 7821:         my %inuse = &dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7822:         my $id = time;
 7823:         $newid = $id;
 7824:         if ($idtype eq 'addcode') {
 7825:             $newid .= &sixnum_code();
 7826:         }
 7827:         my $idtries = 0;
 7828:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7829:             if ($idtype eq 'concat') {
 7830:                 $newid = $id.$idtries;
 7831:             } elsif ($idtype eq 'addcode') {
 7832:                 $newid = $newid.&sixnum_code();
 7833:             } else {
 7834:                 $newid ++;
 7835:             }
 7836:             $idtries ++;
 7837:         }
 7838:         if (!exists($inuse{$prefix."\0".$newid})) {
 7839:             my %new_item =  (
 7840:                               $prefix."\0".$newid => $who,
 7841:                             );
 7842:             my $putresult = &put('nohist_'.$namespace,\%new_item,
 7843:                                                  $cdom,$cnum);
 7844:             if ($putresult ne 'ok') {
 7845:                 undef($newid);
 7846:                 $error = 'error saving new item: '.$putresult;
 7847:             }
 7848:         } else {
 7849:              undef($newid);
 7850:              $error = ('error: no unique suffix available for the new item ');
 7851:         }
 7852: #  remove lock
 7853:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7854:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7855:     } else {
 7856:         $error = "error: could not obtain lockfile\n";
 7857:         $dellock = 'ok';
 7858:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7859:             $dellock = 'nolock';
 7860:         }
 7861:     }
 7862:     return ($newid,$dellock,$error);
 7863: }
 7864: 
 7865: sub sixnum_code {
 7866:     my $code;
 7867:     for (0..6) {
 7868:         $code .= int( rand(9) );
 7869:     }
 7870:     return $code;
 7871: }
 7872: 
 7873: # -------------------------------------------------- portfolio access checking
 7874: 
 7875: sub portfolio_access {
 7876:     my ($requrl,$clientip) = @_;
 7877:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7878:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7879:     if ($result) {
 7880:         my %setters;
 7881:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7882:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 7883:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
 7884:             if (($startblock && $endblock) || ($by_ip)) {
 7885:                 return 'B';
 7886:             }
 7887:         } else {
 7888:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 7889:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 7890:             if (($startblock && $endblock) || ($by_ip)) {
 7891:                 return 'B';
 7892:             }
 7893:         }
 7894:     }
 7895:     if ($result eq 'ok') {
 7896:        return 'F';
 7897:     } elsif ($result =~ /^[^:]+:guest_/) {
 7898:        return 'A';
 7899:     }
 7900:     return '';
 7901: }
 7902: 
 7903: sub get_portfolio_access {
 7904:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7905: 
 7906:     if (!ref($access_hash)) {
 7907: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7908: 	my %access_controls = &get_access_controls($current_perms,$group,
 7909: 						   $file_name);
 7910: 	$access_hash = $access_controls{$file_name};
 7911:     }
 7912: 
 7913:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7914:     my $now = time;
 7915:     if (ref($access_hash) eq 'HASH') {
 7916:         foreach my $key (keys(%{$access_hash})) {
 7917:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7918:             if ($start > $now) {
 7919:                 next;
 7920:             }
 7921:             if ($end && $end<$now) {
 7922:                 next;
 7923:             }
 7924:             if ($scope eq 'public') {
 7925:                 $public = $key;
 7926:                 last;
 7927:             } elsif ($scope eq 'guest') {
 7928:                 $guest = $key;
 7929:             } elsif ($scope eq 'domains') {
 7930:                 push(@domains,$key);
 7931:             } elsif ($scope eq 'users') {
 7932:                 push(@users,$key);
 7933:             } elsif ($scope eq 'course') {
 7934:                 push(@courses,$key);
 7935:             } elsif ($scope eq 'group') {
 7936:                 push(@groups,$key);
 7937:             } elsif ($scope eq 'ip') {
 7938:                 push(@ips,$key);
 7939:             }
 7940:         }
 7941:         if ($public) {
 7942:             return 'ok';
 7943:         } elsif (@ips > 0) {
 7944:             my $allowed;
 7945:             foreach my $ipkey (@ips) {
 7946:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7947:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7948:                         $allowed = 1;
 7949:                         last; 
 7950:                     }
 7951:                 }
 7952:             }
 7953:             if ($allowed) {
 7954:                 return 'ok';
 7955:             }
 7956:         }
 7957:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7958:             if ($guest) {
 7959:                 return $guest;
 7960:             }
 7961:         } else {
 7962:             if (@domains > 0) {
 7963:                 foreach my $domkey (@domains) {
 7964:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7965:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7966:                             return 'ok';
 7967:                         }
 7968:                     }
 7969:                 }
 7970:             }
 7971:             if (@users > 0) {
 7972:                 foreach my $userkey (@users) {
 7973:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7974:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7975:                             if (ref($item) eq 'HASH') {
 7976:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7977:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7978:                                     return 'ok';
 7979:                                 }
 7980:                             }
 7981:                         }
 7982:                     } 
 7983:                 }
 7984:             }
 7985:             my %roleshash;
 7986:             my @courses_and_groups = @courses;
 7987:             push(@courses_and_groups,@groups); 
 7988:             if (@courses_and_groups > 0) {
 7989:                 my (%allgroups,%allroles); 
 7990:                 my ($start,$end,$role,$sec,$group);
 7991:                 foreach my $envkey (%env) {
 7992:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7993:                         my $cid = $2.'_'.$3; 
 7994:                         if ($1 eq 'gr') {
 7995:                             $group = $4;
 7996:                             $allgroups{$cid}{$group} = $env{$envkey};
 7997:                         } else {
 7998:                             if ($4 eq '') {
 7999:                                 $sec = 'none';
 8000:                             } else {
 8001:                                 $sec = $4;
 8002:                             }
 8003:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 8004:                         }
 8005:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 8006:                         my $cid = $2.'_'.$3;
 8007:                         if ($4 eq '') {
 8008:                             $sec = 'none';
 8009:                         } else {
 8010:                             $sec = $4;
 8011:                         }
 8012:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 8013:                     }
 8014:                 }
 8015:                 if (keys(%allroles) == 0) {
 8016:                     return;
 8017:                 }
 8018:                 foreach my $key (@courses_and_groups) {
 8019:                     my %content = %{$$access_hash{$key}};
 8020:                     my $cnum = $content{'number'};
 8021:                     my $cdom = $content{'domain'};
 8022:                     my $cid = $cdom.'_'.$cnum;
 8023:                     if (!exists($allroles{$cid})) {
 8024:                         next;
 8025:                     }    
 8026:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 8027:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 8028:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 8029:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 8030:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 8031:                         foreach my $role (keys(%{$allroles{$cid}})) {
 8032:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 8033:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 8034:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 8035:                                         if (grep/^all$/,@sections) {
 8036:                                             return 'ok';
 8037:                                         } else {
 8038:                                             if (grep/^$sec$/,@sections) {
 8039:                                                 return 'ok';
 8040:                                             }
 8041:                                         }
 8042:                                     }
 8043:                                 }
 8044:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 8045:                                     if (grep/^none$/,@groups) {
 8046:                                         return 'ok';
 8047:                                     }
 8048:                                 } else {
 8049:                                     if (grep/^all$/,@groups) {
 8050:                                         return 'ok';
 8051:                                     } 
 8052:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 8053:                                         if (grep/^$group$/,@groups) {
 8054:                                             return 'ok';
 8055:                                         }
 8056:                                     }
 8057:                                 } 
 8058:                             }
 8059:                         }
 8060:                     }
 8061:                 }
 8062:             }
 8063:             if ($guest) {
 8064:                 return $guest;
 8065:             }
 8066:         }
 8067:     }
 8068:     return;
 8069: }
 8070: 
 8071: sub course_group_datechecker {
 8072:     my ($dates,$now,$status) = @_;
 8073:     my ($start,$end) = split(/\./,$dates);
 8074:     if (!$start && !$end) {
 8075:         return 'ok';
 8076:     }
 8077:     if (grep/^active$/,@{$status}) {
 8078:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 8079:             return 'ok';
 8080:         }
 8081:     }
 8082:     if (grep/^previous$/,@{$status}) {
 8083:         if ($end > $now ) {
 8084:             return 'ok';
 8085:         }
 8086:     }
 8087:     if (grep/^future$/,@{$status}) {
 8088:         if ($start > $now) {
 8089:             return 'ok';
 8090:         }
 8091:     }
 8092:     return; 
 8093: }
 8094: 
 8095: sub parse_portfolio_url {
 8096:     my ($url) = @_;
 8097: 
 8098:     my ($type,$udom,$unum,$group,$file_name);
 8099:     
 8100:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 8101: 	$type = 1;
 8102:         $udom = $1;
 8103:         $unum = $2;
 8104:         $file_name = $3;
 8105:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 8106: 	$type = 2;
 8107:         $udom = $1;
 8108:         $unum = $2;
 8109:         $group = $3;
 8110:         $file_name = $3.'/'.$4;
 8111:     }
 8112:     if (wantarray) {
 8113: 	return ($type,$udom,$unum,$file_name,$group);
 8114:     }
 8115:     return $type;
 8116: }
 8117: 
 8118: sub is_portfolio_url {
 8119:     my ($url) = @_;
 8120:     return scalar(&parse_portfolio_url($url));
 8121: }
 8122: 
 8123: sub is_portfolio_file {
 8124:     my ($file) = @_;
 8125:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 8126:         return 1;
 8127:     }
 8128:     return;
 8129: }
 8130: 
 8131: sub is_coursetool_logo {
 8132:     my ($uri) = @_;
 8133:     if ($env{'request.course.id'}) {
 8134:         my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 8135:         if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
 8136:             return 1;
 8137:         }
 8138:     }
 8139:     return;
 8140: }
 8141: 
 8142: sub usertools_access {
 8143:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 8144:     my ($access,%tools);
 8145:     if ($context eq '') {
 8146:         $context = 'tools';
 8147:     }
 8148:     if ($context eq 'requestcourses') {
 8149:         %tools = (
 8150:                       official   => 1,
 8151:                       unofficial => 1,
 8152:                       community  => 1,
 8153:                       textbook   => 1,
 8154:                       placement  => 1,
 8155:                       lti        => 1,
 8156:                  );
 8157:     } elsif ($context eq 'requestauthor') {
 8158:         %tools = (
 8159:                       requestauthor => 1,
 8160:                  );
 8161:     } else {
 8162:         %tools = (
 8163:                       aboutme   => 1,
 8164:                       blog      => 1,
 8165:                       webdav    => 1,
 8166:                       portfolio => 1,
 8167:                       timezone  => 1,
 8168:                  );
 8169:     }
 8170:     return if (!defined($tools{$tool}));
 8171: 
 8172:     if (($udom eq '') || ($uname eq '')) {
 8173:         $udom = $env{'user.domain'};
 8174:         $uname = $env{'user.name'};
 8175:     }
 8176: 
 8177:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 8178:         if ($action ne 'reload') {
 8179:             if ($context eq 'requestcourses') {
 8180:                 return $env{'environment.canrequest.'.$tool};
 8181:             } elsif ($context eq 'requestauthor') {
 8182:                 return $env{'environment.canrequest.author'};
 8183:             } else {
 8184:                 return $env{'environment.availabletools.'.$tool};
 8185:             }
 8186:         }
 8187:     }
 8188: 
 8189:     my ($toolstatus,$inststatus,$envkey);
 8190:     if ($context eq 'requestauthor') {
 8191:         $envkey = $context; 
 8192:     } else {
 8193:         $envkey = $context.'.'.$tool;
 8194:     }
 8195: 
 8196:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 8197:          ($action ne 'reload')) {
 8198:         $toolstatus = $env{'environment.'.$envkey};
 8199:         $inststatus = $env{'environment.inststatus'};
 8200:     } else {
 8201:         if (ref($userenvref) eq 'HASH') {
 8202:             $toolstatus = $userenvref->{$envkey};
 8203:             $inststatus = $userenvref->{'inststatus'};
 8204:         } else {
 8205:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 8206:             $toolstatus = $userenv{$envkey};
 8207:             $inststatus = $userenv{'inststatus'};
 8208:         }
 8209:     }
 8210: 
 8211:     if ($toolstatus ne '') {
 8212:         if ($toolstatus) {
 8213:             $access = 1;
 8214:         } else {
 8215:             $access = 0;
 8216:         }
 8217:         return $access;
 8218:     }
 8219: 
 8220:     my ($is_adv,%domdef);
 8221:     if (ref($is_advref) eq 'HASH') {
 8222:         $is_adv = $is_advref->{'is_adv'};
 8223:     } else {
 8224:         $is_adv = &is_advanced_user($udom,$uname);
 8225:     }
 8226:     if (ref($domdefref) eq 'HASH') {
 8227:         %domdef = %{$domdefref};
 8228:     } else {
 8229:         %domdef = &get_domain_defaults($udom);
 8230:     }
 8231:     if (ref($domdef{$tool}) eq 'HASH') {
 8232:         if ($is_adv) {
 8233:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 8234:                 if ($domdef{$tool}{'_LC_adv'}) { 
 8235:                     $access = 1;
 8236:                 } else {
 8237:                     $access = 0;
 8238:                 }
 8239:                 return $access;
 8240:             }
 8241:         }
 8242:         if ($inststatus ne '') {
 8243:             my ($hasaccess,$hasnoaccess);
 8244:             foreach my $affiliation (split(/:/,$inststatus)) {
 8245:                 if ($domdef{$tool}{$affiliation} ne '') { 
 8246:                     if ($domdef{$tool}{$affiliation}) {
 8247:                         $hasaccess = 1;
 8248:                     } else {
 8249:                         $hasnoaccess = 1;
 8250:                     }
 8251:                 }
 8252:             }
 8253:             if ($hasaccess || $hasnoaccess) {
 8254:                 if ($hasaccess) {
 8255:                     $access = 1;
 8256:                 } elsif ($hasnoaccess) {
 8257:                     $access = 0; 
 8258:                 }
 8259:                 return $access;
 8260:             }
 8261:         } else {
 8262:             if ($domdef{$tool}{'default'} ne '') {
 8263:                 if ($domdef{$tool}{'default'}) {
 8264:                     $access = 1;
 8265:                 } elsif ($domdef{$tool}{'default'} == 0) {
 8266:                     $access = 0;
 8267:                 }
 8268:                 return $access;
 8269:             }
 8270:         }
 8271:     } else {
 8272:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 8273:             $access = 1;
 8274:         } else {
 8275:             $access = 0;
 8276:         }
 8277:         return $access;
 8278:     }
 8279: }
 8280: 
 8281: sub is_course_owner {
 8282:     my ($cdom,$cnum,$udom,$uname) = @_;
 8283:     if (($udom eq '') || ($uname eq '')) {
 8284:         $udom = $env{'user.domain'};
 8285:         $uname = $env{'user.name'};
 8286:     }
 8287:     unless (($udom eq '') || ($uname eq '')) {
 8288:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 8289:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 8290:                 return 1;
 8291:             } else {
 8292:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
 8293:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 8294:                     return 1;
 8295:                 }
 8296:             }
 8297:         }
 8298:     }
 8299:     return;
 8300: }
 8301: 
 8302: sub is_advanced_user {
 8303:     my ($udom,$uname,$nocache) = @_;
 8304:     my ($is_adv,$is_author,$use_cache,$hashid);
 8305:     if ($udom ne '' && $uname ne '') {
 8306:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 8307:             if (wantarray) {
 8308:                 return ($env{'user.adv'},$env{'user.author'});
 8309:             } else {
 8310:                 return $env{'user.adv'};
 8311:             }
 8312:         } elsif (!$nocache) {
 8313:             $use_cache = 1;
 8314:             $hashid = "$udom:$uname";  
 8315:             my ($info,$cached)=&is_cached_new('isadvau',$hashid);
 8316:             if ($cached) {
 8317:                 ($is_adv,$is_author) = split(/:/,$info);
 8318:                 if (wantarray) {
 8319:                     return ($is_adv,$is_author);
 8320:                 }
 8321:                 return $is_adv; 
 8322:             }
 8323:         }
 8324:     }
 8325:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 8326:     my %allroles;
 8327:     foreach my $role (keys(%roleshash)) {
 8328:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 8329:         my $area = '/'.$tdomain.'/'.$trest;
 8330:         if ($sec ne '') {
 8331:             $area .= '/'.$sec;
 8332:         }
 8333:         if (($area ne '') && ($trole ne '')) {
 8334:             my $spec=$trole.'.'.$area;
 8335:             if ($trole =~ /^cr\//) {
 8336:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 8337:             } elsif ($trole ne 'gr') {
 8338:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 8339:             }
 8340:             if ($trole eq 'au') {
 8341:                 $is_author = 1;
 8342:             }
 8343:         }
 8344:     }
 8345:     foreach my $role (keys(%allroles)) {
 8346:         last if ($is_adv);
 8347:         foreach my $item (split(/:/,$allroles{$role})) {
 8348:             if ($item ne '') {
 8349:                 my ($privilege,$restrictions)=split(/&/,$item);
 8350:                 if ($privilege eq 'adv') {
 8351:                     $is_adv = 1;
 8352:                     last;
 8353:                 }
 8354:             }
 8355:         }
 8356:     }
 8357:     if ($use_cache) {
 8358:         my $cachetime = 600;
 8359:         &do_cache_new('isadvau',$hashid,$is_adv.':'.$is_author,$cachetime);
 8360:     }
 8361:     if (wantarray) {
 8362:         return ($is_adv,$is_author);
 8363:     }
 8364:     return $is_adv;
 8365: }
 8366: 
 8367: sub check_can_request {
 8368:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8369:     my $canreq = 0;
 8370:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8371:         $uname = $env{'user.name'};
 8372:         $udom = $env{'user.domain'};
 8373:     }
 8374:     my ($types,$typename) = &Apache::loncommon::course_types();
 8375:     my @options = ('approval','validate','autolimit');
 8376:     my $optregex = join('|',@options);
 8377:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8378:         my %willtrust;
 8379:         foreach my $type (@{$types}) {
 8380:             if (&usertools_access($uname,$udom,$type,undef,
 8381:                                   'requestcourses')) {
 8382:                 $canreq ++;
 8383:                 if (ref($request_domains) eq 'HASH') {
 8384:                     push(@{$request_domains->{$type}},$udom);
 8385:                 }
 8386:                 if ($dom eq $udom) {
 8387:                     $can_request->{$type} = 1;
 8388:                 }
 8389:             }
 8390:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8391:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8392:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8393:                 if (@curr > 0) {
 8394:                     foreach my $item (@curr) {
 8395:                         if (ref($request_domains) eq 'HASH') {
 8396:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8397:                             if ($otherdom ne '') {
 8398:                                 unless (exists($willtrust{$otherdom})) {
 8399:                                     $willtrust{$otherdom} = &will_trust('reqcrs',$env{'user.domain'},$otherdom);
 8400:                                 }
 8401:                                 if ($willtrust{$otherdom}) {
 8402:                                     if (ref($request_domains->{$type}) eq 'ARRAY') {
 8403:                                         unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8404:                                             push(@{$request_domains->{$type}},$otherdom);
 8405:                                         }
 8406:                                     } else {
 8407:                                         push(@{$request_domains->{$type}},$otherdom);
 8408:                                     }
 8409:                                 }
 8410:                             }
 8411:                         }
 8412:                     }
 8413:                     unless ($dom eq $env{'user.domain'}) {
 8414:                         $canreq ++;
 8415:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8416:                             $can_request->{$type} = 1;
 8417:                         }
 8418:                     }
 8419:                 }
 8420:             }
 8421:         }
 8422:     }
 8423:     return $canreq;
 8424: }
 8425: 
 8426: # ---------------------------------------------- Custom access rule evaluation
 8427: 
 8428: sub customaccess {
 8429:     my ($priv,$uri)=@_;
 8430:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8431:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8432:     $udom = &LONCAPA::clean_domain($udom);
 8433:     $ucrs = &LONCAPA::clean_username($ucrs);
 8434:     my $access=0;
 8435:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8436: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8437: 	if ($type eq 'user') {
 8438: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8439: 		my ($tdom,$tuname)=split(m{/},$scope);
 8440: 		if ($tdom) {
 8441: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8442: 		}
 8443: 		if ($tuname) {
 8444: 		    if ($tuname ne $env{'user.name'}) { next; }
 8445: 		}
 8446: 		$access=($effect eq 'allow');
 8447: 		last;
 8448: 	    }
 8449: 	} else {
 8450: 	    if ($role) {
 8451: 		if ($role ne $urole) { next; }
 8452: 	    }
 8453: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8454: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8455: 		if ($tdom) {
 8456: 		    if ($tdom ne $udom) { next; }
 8457: 		}
 8458: 		if ($tcrs) {
 8459: 		    if ($tcrs ne $ucrs) { next; }
 8460: 		}
 8461: 		if ($tsec) {
 8462: 		    if ($tsec ne $usec) { next; }
 8463: 		}
 8464: 		$access=($effect eq 'allow');
 8465: 		last;
 8466: 	    }
 8467: 	    if ($realm eq '' && $role eq '') {
 8468: 		$access=($effect eq 'allow');
 8469: 	    }
 8470: 	}
 8471:     }
 8472:     return $access;
 8473: }
 8474: 
 8475: # ------------------------------------------------- Check for a user privilege
 8476: 
 8477: sub allowed {
 8478:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
 8479:     my $ver_orguri=$uri;
 8480:     $uri=&deversion($uri);
 8481:     my $orguri=$uri;
 8482:     $uri=&declutter($uri);
 8483: 
 8484:     if ($priv eq 'evb') {
 8485: # Evade communication block restrictions for specified role in a course or domain
 8486:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8487:             return $1;
 8488:         } else {
 8489:             return;
 8490:         }
 8491:     }
 8492: 
 8493:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8494: # Free bre access to adm and meta resources
 8495:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8496: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8497: 	&& ($priv eq 'bre')) {
 8498: 	return 'F';
 8499:     }
 8500: 
 8501: # Free bre access to user's own portfolio contents
 8502:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8503:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8504: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8505:         my %setters;
 8506:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
 8507:             &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 8508:         if (($startblock && $endblock) || ($by_ip)) {
 8509:             return 'B';
 8510:         } else {
 8511:             return 'F';
 8512:         }
 8513:     }
 8514: 
 8515: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8516:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8517:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8518:         if (exists($env{'request.course.id'})) {
 8519:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8520:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8521:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8522:                 my $courseprivid=$env{'request.course.id'};
 8523:                 $courseprivid=~s/\_/\//;
 8524:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8525:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8526:                     return $1; 
 8527:                 } else {
 8528:                     if ($env{'request.course.sec'}) {
 8529:                         $courseprivid.='/'.$env{'request.course.sec'};
 8530:                     }
 8531:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8532:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8533:                         return $2;
 8534:                     }
 8535:                 }
 8536:             }
 8537:         }
 8538:     }
 8539: 
 8540: # Free bre to public access
 8541: 
 8542:     if ($priv eq 'bre') {
 8543:         my $copyright;
 8544:         unless ($uri =~ /ext\.tool/) {
 8545:             $copyright=&metadata($uri,'copyright');
 8546:         }
 8547: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8548:            return 'F'; 
 8549:         }
 8550:         if ($copyright eq 'priv') {
 8551:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8552: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8553: 		return '';
 8554:             }
 8555:         }
 8556:         if ($copyright eq 'domain') {
 8557:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8558: 	    unless (($env{'user.domain'} eq $1) ||
 8559:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8560: 		return '';
 8561:             }
 8562:         }
 8563:         if ($env{'request.role'}=~ /li\.\//) {
 8564:             # Library role, so allow browsing of resources in this domain.
 8565:             return 'F';
 8566:         }
 8567:         if ($copyright eq 'custom') {
 8568: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8569:         }
 8570:     }
 8571:     # Domain coordinator is trying to create a course
 8572:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8573:         # uri is the requested domain in this case.
 8574:         # comparison to 'request.role.domain' shows if the user has selected
 8575:         # a role of dc for the domain in question.
 8576:         return 'F' if ($uri eq $env{'request.role.domain'});
 8577:     }
 8578: 
 8579:     my $thisallowed='';
 8580:     my $statecond=0;
 8581:     my $courseprivid='';
 8582: 
 8583:     my $ownaccess;
 8584:     # Community Coordinator or Assistant Co-author browsing resource space.
 8585:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8586:         if ($uri eq '') {
 8587:             $ownaccess = 1;
 8588:         } else {
 8589:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8590:                 my $udom = $env{'user.domain'};
 8591:                 my $uname = $env{'user.name'};
 8592:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8593:                     $ownaccess = 1;
 8594:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8595:                     unless ($uri =~ m{\.\./}) {
 8596:                         $ownaccess = 1;
 8597:                     }
 8598:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8599:                     my $now = time;
 8600:                     if ($uri =~ m{^([^/]+)/?$}) {
 8601:                         my $adom = $1;
 8602:                         foreach my $key (keys(%env)) {
 8603:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8604:                                 my ($start,$end) = split(/\./,$env{$key});
 8605:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8606:                                     $ownaccess = 1;
 8607:                                     last;
 8608:                                 }
 8609:                             }
 8610:                         }
 8611:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8612:                         my $adom = $1;
 8613:                         my $aname = $2;
 8614:                         foreach my $role ('ca','aa') { 
 8615:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8616:                                 my ($start,$end) =
 8617:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
 8618:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8619:                                     $ownaccess = 1;
 8620:                                     last;
 8621:                                 }
 8622:                             }
 8623:                         }
 8624:                     }
 8625:                 }
 8626:             }
 8627:         }
 8628:     }
 8629: 
 8630: # Course
 8631: 
 8632:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8633:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8634:             $thisallowed.=$1;
 8635:         }
 8636:     }
 8637: 
 8638: # Domain
 8639: 
 8640:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8641:        =~/\Q$priv\E\&([^\:]*)/) {
 8642:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8643:             $thisallowed.=$1;
 8644:         }
 8645:     }
 8646: 
 8647: # User who is not author or co-author might still be able to edit
 8648: # resource of an author in the domain (e.g., if Domain Coordinator).
 8649:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8650:         (&allowed('mdc',$env{'request.course.id'}))) {
 8651:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8652:             $thisallowed.=$1;
 8653:         }
 8654:     }
 8655: 
 8656: # Course: uri itself is a course
 8657:     my $courseuri=$uri;
 8658:     $courseuri=~s/\_(\d)/\/$1/;
 8659:     $courseuri=~s/^([^\/])/\/$1/;
 8660: 
 8661:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8662:        =~/\Q$priv\E\&([^\:]*)/) {
 8663:         if ($priv eq 'mip') {
 8664:             my $rem = $1;
 8665:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8666:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8667:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8668:                 if ($cdom ne '') {
 8669:                     my %passwdconf = &get_passwdconf($cdom);
 8670:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8671:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8672:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8673:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8674:                                 unless (@inststatuses) {
 8675:                                     @inststatuses = ('default');
 8676:                                 }
 8677:                                 foreach my $status (@inststatuses) {
 8678:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8679:                                         $thisallowed.=$rem;
 8680:                                     }
 8681:                                 }
 8682:                             }
 8683:                         }
 8684:                     }
 8685:                 }
 8686:             }
 8687:         } else {
 8688:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8689:                 $thisallowed.=$1;
 8690:             }
 8691:         }
 8692:     }
 8693: 
 8694: # URI is an uploaded document for this course, default permissions don't matter
 8695: # not allowing 'edit' access (editupload) to uploaded course docs
 8696:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8697: 	$thisallowed='';
 8698:         my ($match)=&is_on_map($uri);
 8699:         if ($match) {
 8700:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8701:                   =~/\Q$priv\E\&([^\:]*)/) {
 8702:                 my $value = $1;
 8703:                 my $deeplinkblock;
 8704:                 unless ($nodeeplinkcheck) {
 8705:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8706:                 }
 8707:                 if ($deeplinkblock) {
 8708:                     $thisallowed='D';
 8709:                 } elsif ($noblockcheck) {
 8710:                     $thisallowed.=$value;
 8711:                 } else {
 8712:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8713:                     if (@blockers > 0) {
 8714:                         $thisallowed = 'B';
 8715:                     } else {
 8716:                         $thisallowed.=$value;
 8717:                     }
 8718:                 }
 8719:             }
 8720:         } else {
 8721:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8722:             if ($refuri) {
 8723:                 if ($refuri =~ m|^/adm/|) {
 8724:                     $thisallowed='F';
 8725:                 } else {
 8726:                     $refuri=&declutter($refuri);
 8727:                     my ($match) = &is_on_map($refuri);
 8728:                     if ($match) {
 8729:                         my $deeplinkblock;
 8730:                         unless ($nodeeplinkcheck) {
 8731:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8732:                         }
 8733:                         if ($deeplinkblock) {
 8734:                             $thisallowed='D';
 8735:                         } elsif ($noblockcheck) {
 8736:                             $thisallowed='F';
 8737:                         } else {
 8738:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8739:                             if (@blockers > 0) {
 8740:                                 $thisallowed = 'B';
 8741:                             } else {
 8742:                                 $thisallowed='F';
 8743:                             }
 8744:                         }
 8745:                     }
 8746:                 }
 8747:             }
 8748:         }
 8749:     }
 8750: 
 8751:     if ($priv eq 'bre'
 8752: 	&& $thisallowed ne 'F' 
 8753: 	&& $thisallowed ne '2'
 8754: 	&& &is_portfolio_url($uri)) {
 8755: 	$thisallowed = &portfolio_access($uri,$clientip);
 8756:     }
 8757: 
 8758: # Full access at system, domain or course-wide level? Exit.
 8759:     if ($thisallowed=~/F/) {
 8760: 	return 'F';
 8761:     }
 8762: 
 8763: # If this is generating or modifying users, exit with special codes
 8764: 
 8765:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8766: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8767: 	    my ($audom,$auname)=split('/',$uri);
 8768: # no author name given, so this just checks on the general right to make a co-author in this domain
 8769: 	    unless ($auname) { return $thisallowed; }
 8770: # an author name is given, so we are about to actually make a co-author for a certain account
 8771: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8772: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8773: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8774: 	}
 8775: 	return $thisallowed;
 8776:     }
 8777: #
 8778: # Gathered so far: system, domain and course wide privileges
 8779: #
 8780: # Course: See if uri or referer is an individual resource that is part of 
 8781: # the course
 8782: 
 8783:     if ($env{'request.course.id'}) {
 8784: 
 8785:         if ($priv eq 'bre') {
 8786:             if (&is_coursetool_logo($uri)) {
 8787:                 return 'F';
 8788:             }
 8789:         }
 8790: 
 8791: # If this is modifying password (internal auth) domains must match for user and user's role.
 8792: 
 8793:         if ($priv eq 'mip') {
 8794:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8795:                 return $thisallowed;
 8796:             } else {
 8797:                 return '';
 8798:             }
 8799:         }
 8800: 
 8801:        $courseprivid=$env{'request.course.id'};
 8802:        if ($env{'request.course.sec'}) {
 8803:           $courseprivid.='/'.$env{'request.course.sec'};
 8804:        }
 8805:        $courseprivid=~s/\_/\//;
 8806:        my $checkreferer=1;
 8807:        my ($match,$cond)=&is_on_map($uri);
 8808:        if ($match) {
 8809:            $statecond=$cond;
 8810:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8811:                =~/\Q$priv\E\&([^\:]*)/) {
 8812:                my $value = $1;
 8813:                if ($priv eq 'bre') {
 8814:                    my $deeplinkblock;
 8815:                    unless ($nodeeplinkcheck) {
 8816:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8817:                    }
 8818:                    if ($deeplinkblock) {
 8819:                        $thisallowed = 'D';
 8820:                    } elsif ($noblockcheck) {
 8821:                        $thisallowed.=$value;
 8822:                    } else {
 8823:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8824:                        if (@blockers > 0) {
 8825:                            $thisallowed = 'B';
 8826:                        } else {
 8827:                            $thisallowed.=$value;
 8828:                        }
 8829:                    }
 8830:                } else {
 8831:                    $thisallowed.=$value;
 8832:                }
 8833:                $checkreferer=0;
 8834:            }
 8835:        }
 8836: 
 8837:        if ($checkreferer) {
 8838: 	  my $refuri=$env{'httpref.'.$orguri};
 8839:             unless ($refuri) {
 8840:                 foreach my $key (keys(%env)) {
 8841: 		    if ($key=~/^httpref\..*\*/) {
 8842: 			my $pattern=$key;
 8843:                         $pattern=~s/^httpref\.\/res\///;
 8844:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8845:                         $pattern=~s/\//\\\//g;
 8846:                         if ($orguri=~/$pattern/) {
 8847: 			    $refuri=$env{$key};
 8848:                         }
 8849:                     }
 8850:                 }
 8851:             }
 8852: 
 8853:          if ($refuri) { 
 8854: 	  $refuri=&declutter($refuri);
 8855:           my ($match,$cond)=&is_on_map($refuri);
 8856:             if ($match) {
 8857:               my $refstatecond=$cond;
 8858:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8859:                   =~/\Q$priv\E\&([^\:]*)/) {
 8860:                   my $value = $1;
 8861:                   if ($priv eq 'bre') {
 8862:                       my $deeplinkblock;
 8863:                       unless ($nodeeplinkcheck) {
 8864:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8865:                       }
 8866:                       if ($deeplinkblock) {
 8867:                           $thisallowed = 'D';
 8868:                       } elsif ($noblockcheck) {
 8869:                           $thisallowed.=$value;
 8870:                       } else {
 8871:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8872:                           if (@blockers > 0) {
 8873:                               $thisallowed = 'B';
 8874:                           } else {
 8875:                               $thisallowed.=$value;
 8876:                           }
 8877:                       }
 8878:                   } else {
 8879:                       $thisallowed.=$value;
 8880:                   }
 8881:                   $uri=$refuri;
 8882:                   $statecond=$refstatecond;
 8883:               }
 8884:           }
 8885:         }
 8886:        }
 8887:    }
 8888: 
 8889: #
 8890: # Gathered now: all privileges that could apply, and condition number
 8891: # 
 8892: #
 8893: # Full or no access?
 8894: #
 8895: 
 8896:     if ($thisallowed=~/F/) {
 8897: 	return 'F';
 8898:     }
 8899: 
 8900:     unless ($thisallowed) {
 8901:         return '';
 8902:     }
 8903: 
 8904: # Restrictions exist, deal with them
 8905: #
 8906: #   C:according to course preferences
 8907: #   R:according to resource settings
 8908: #   L:unless locked
 8909: #   X:according to user session state
 8910: #
 8911: 
 8912: # Possibly locked functionality, check all courses
 8913: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
 8914: # Locks might take effect only after 10 minutes cache expiration for other
 8915: # courses, and 2 minutes for current course, in which user has st or ta role
 8916: # which is neither expired nor a future role (unless current course).
 8917: 
 8918:     my ($needlockcheck,$now,$crsonly);
 8919:     if ($thisallowed=~/L/) {
 8920:         $now = time;
 8921:         if ($priv eq 'bre') {
 8922:             if ($uri ne '') {
 8923:                 if ($orguri =~ m{^/+res/}) {
 8924:                     if ($uri =~ m{^lib/templates/}) {
 8925:                         if ($env{'request.course.id'}) {
 8926:                             $crsonly = 1;
 8927:                             $needlockcheck = 1;
 8928:                         }
 8929:                     } else {
 8930:                         $needlockcheck = 1;
 8931:                     }
 8932:                 } elsif ($env{'request.course.id'}) {
 8933:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
 8934:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
 8935:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
 8936:                         $crsonly = 1;
 8937:                     }
 8938:                     $needlockcheck = 1;
 8939:                 }
 8940:             }
 8941:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
 8942:             $needlockcheck = 1;
 8943:         }
 8944:     }
 8945:     if ($needlockcheck) {
 8946:         foreach my $envkey (keys(%env)) {
 8947:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8948:                my $courseid=$2;
 8949:                my $roleid=$1.'.'.$2;
 8950:                $courseid=~s/^\///;
 8951:                unless ($env{'request.role'} eq $roleid) {
 8952:                    my ($start,$end) = split(/\./,$env{$envkey});
 8953:                    next unless (($now >= $start) && (!$end || $end > $now));
 8954:                }
 8955:                my $expiretime=600;
 8956:                if ($env{'request.role'} eq $roleid) {
 8957: 		  $expiretime=120;
 8958:                }
 8959: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8960:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8961:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8962: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8963:                }
 8964:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8965:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8966: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8967:                        &log($env{'user.domain'},$env{'user.name'},
 8968:                             $env{'user.home'},
 8969:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8970:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8971:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8972: 		       return '';
 8973:                    }
 8974:                }
 8975:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8976:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8977: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
 8978:                        &log($env{'user.domain'},$env{'user.name'},
 8979:                             $env{'user.home'},
 8980:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8981:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8982:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8983: 		       return '';
 8984:                    }
 8985:                }
 8986: 	   }
 8987:        }
 8988:     }
 8989: 
 8990: #
 8991: # Rest of the restrictions depend on selected course
 8992: #
 8993: 
 8994:     unless ($env{'request.course.id'}) {
 8995: 	if ($thisallowed eq 'A') {
 8996: 	    return 'A';
 8997:         } elsif ($thisallowed eq 'B') {
 8998:             return 'B';
 8999: 	} else {
 9000: 	    return '1';
 9001: 	}
 9002:     }
 9003: 
 9004: #
 9005: # Now user is definitely in a course
 9006: #
 9007: 
 9008: 
 9009: # Course preferences
 9010: 
 9011:    if ($thisallowed=~/C/) {
 9012:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 9013:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 9014:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 9015: 	   =~/\Q$rolecode\E/) {
 9016: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 9017: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 9018: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 9019: 			$env{'request.course.id'});
 9020: 	   }
 9021:            return '';
 9022:        }
 9023: 
 9024:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 9025: 	   =~/\Q$unamedom\E/) {
 9026: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 9027: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 9028: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 9029: 			$env{'request.course.id'});
 9030: 	   }
 9031:            return '';
 9032:        }
 9033:    }
 9034: 
 9035: # Resource preferences
 9036: 
 9037:    if ($thisallowed=~/R/) {
 9038:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 9039:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 9040: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 9041: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 9042: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 9043: 	   }
 9044: 	   return '';
 9045:        }
 9046:    }
 9047: 
 9048: # Restricted for deeplinked session?
 9049: 
 9050:     if ($env{'request.deeplink.login'}) {
 9051:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
 9052:             if (!$symb) { $symb=&symbread($uri,1); }
 9053:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
 9054:                 return '';
 9055:             }
 9056:         }
 9057:     }
 9058: 
 9059: # Restricted by state or randomout?
 9060: 
 9061:    if ($thisallowed=~/X/) {
 9062:       if ($env{'acc.randomout'}) {
 9063: 	 if (!$symb) { $symb=&symbread($uri,1); }
 9064:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 9065:             return ''; 
 9066:          }
 9067:       }
 9068:       if (&condval($statecond)) {
 9069: 	 return '2';
 9070:       } else {
 9071:          return '';
 9072:       }
 9073:    }
 9074: 
 9075:     if ($thisallowed eq 'A') {
 9076: 	return 'A';
 9077:     } elsif ($thisallowed eq 'B') {
 9078:         return 'B';
 9079:     } elsif ($thisallowed eq 'D') {
 9080:         return 'D';
 9081:     }
 9082:    return 'F';
 9083: }
 9084: 
 9085: # ------------------------------------------- Check construction space access
 9086: 
 9087: sub constructaccess {
 9088:     my ($url,$setpriv)=@_;
 9089: 
 9090: # We do not allow editing of previous versions of files
 9091:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 9092: 
 9093: # Get username and domain from URL
 9094:     my ($ownername,$ownerdomain,$ownerhome);
 9095: 
 9096:     ($ownerdomain,$ownername) =
 9097:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 9098: 
 9099: # The URL does not really point to any authorspace, forget it
 9100:     unless (($ownername) && ($ownerdomain)) { return ''; }
 9101: 
 9102: # Now we need to see if the user has access to the authorspace of
 9103: # $ownername at $ownerdomain
 9104: 
 9105:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 9106: # Real author for this?
 9107:        $ownerhome = $env{'user.home'};
 9108:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 9109:           return ($ownername,$ownerdomain,$ownerhome);
 9110:        }
 9111:     } elsif (&is_course($ownerdomain,$ownername)) {
 9112: # Course Authoring Space?
 9113:         if ($env{'request.course.id'}) {
 9114:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 9115:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 9116:                 if (&allowed('mdc',$env{'request.course.id'})) {
 9117:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 9118:                     return ($ownername,$ownerdomain,$ownerhome);
 9119:                 }
 9120:             }
 9121:         }
 9122:         return '';
 9123:     } else {
 9124: # Co-author for this?
 9125:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 9126:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 9127:             $ownerhome = &homeserver($ownername,$ownerdomain);
 9128:             return ($ownername,$ownerdomain,$ownerhome);
 9129:         }
 9130:     }
 9131: 
 9132: # We don't have any access right now. If we are not possibly going to do anything about this,
 9133: # we might as well leave
 9134:    unless ($setpriv) { return ''; }
 9135: 
 9136: # Backdoor access?
 9137:     my $allowed=&allowed('eco',$ownerdomain);
 9138: # Nope
 9139:     unless ($allowed) { return ''; }
 9140: # Looks like we may have access, but could be locked by the owner of the construction space
 9141:     if ($allowed eq 'U') {
 9142:         my %blocked=&get('environment',['domcoord.author'],
 9143:                          $ownerdomain,$ownername);
 9144: # Is blocked by owner
 9145:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 9146:     }
 9147:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 9148: # Grant temporary access
 9149:         my $then=$env{'user.login.time'};
 9150:         my $update=$env{'user.update.time'};
 9151:         if (!$update) { $update = $then; }
 9152:         my $refresh=$env{'user.refresh.time'};
 9153:         if (!$refresh) { $refresh = $update; }
 9154:         my $now = time;
 9155:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 9156:                            $now,'ca','constructaccess');
 9157:         $ownerhome = &homeserver($ownername,$ownerdomain);
 9158:         return($ownername,$ownerdomain,$ownerhome);
 9159:     }
 9160: # No business here
 9161:     return '';
 9162: }
 9163: 
 9164: # ----------------------------------------------------------- Content Blocking
 9165: 
 9166: {
 9167: # Caches for faster Course Contents display where content blocking
 9168: # is in operation (i.e., interval param set) for timed quiz.
 9169: #
 9170: # User for whom data are being temporarily cached.
 9171: my $cacheduser='';
 9172: # Course for which data are being temporarily cached.
 9173: my $cachedcid='';
 9174: # Cached blockers for this user (a hash of blocking items). 
 9175: my %cachedblockers=();
 9176: # When the data were last cached.
 9177: my $cachedlast='';
 9178: 
 9179: sub load_all_blockers {
 9180:     my ($uname,$udom)=@_;
 9181:     if (($uname ne '') && ($udom ne '')) { 
 9182:         if (($cacheduser eq $uname.':'.$udom) &&
 9183:             ($cachedcid eq $env{'request.course.id'}) &&
 9184:             (abs($cachedlast-time)<5)) {
 9185:             return;
 9186:         }
 9187:     }
 9188:     $cachedlast=time;
 9189:     $cacheduser=$uname.':'.$udom;
 9190:     $cachedcid=$env{'request.course.id'};
 9191:     %cachedblockers = &get_commblock_resources();
 9192:     return;
 9193: }
 9194: 
 9195: sub get_comm_blocks {
 9196:     my ($cdom,$cnum) = @_;
 9197:     if ($cdom eq '' || $cnum eq '') {
 9198:         return unless ($env{'request.course.id'});
 9199:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9200:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9201:     }
 9202:     my %commblocks;
 9203:     my $hashid=$cdom.'_'.$cnum;
 9204:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 9205:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 9206:         %commblocks = %{$blocksref};
 9207:     } else {
 9208:         %commblocks = &dump('comm_block',$cdom,$cnum);
 9209:         my $cachetime = 600;
 9210:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 9211:     }
 9212:     return %commblocks;
 9213: }
 9214: 
 9215: sub get_commblock_resources {
 9216:     my ($blocks) = @_;
 9217:     my %blockers = ();
 9218:     return %blockers unless ($env{'request.course.id'});
 9219:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9220:     if ($env{'request.course.sec'}) {
 9221:         $courseurl .= '/'.$env{'request.course.sec'};
 9222:     }
 9223:     return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9224:     my %commblocks;
 9225:     if (ref($blocks) eq 'HASH') {
 9226:         %commblocks = %{$blocks};
 9227:     } else {
 9228:         %commblocks = &get_comm_blocks();
 9229:     }
 9230:     return %blockers unless (keys(%commblocks) > 0); 
 9231:     my $navmap = Apache::lonnavmaps::navmap->new();
 9232:     return %blockers unless (ref($navmap));
 9233:     my $now = time;
 9234:     foreach my $block (keys(%commblocks)) {
 9235:         if ($block =~ /^(\d+)____(\d+)$/) {
 9236:             my ($start,$end) = ($1,$2);
 9237:             if ($start <= $now && $end >= $now) {
 9238:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 9239:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 9240:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 9241:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 9242:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 9243:                             }
 9244:                         }
 9245:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 9246:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 9247:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 9248:                             }
 9249:                         }
 9250:                     }
 9251:                 }
 9252:             }
 9253:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 9254:             my $item = $1;
 9255:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 9256:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 9257:                     my (@interval,$mapname);
 9258:                     my $type = 'map';
 9259:                     if ($item eq 'course') {
 9260:                         $type = 'course';
 9261:                         @interval=&EXT("resource.0.interval");
 9262:                     } else {
 9263:                         if ($item =~ /___\d+___/) {
 9264:                             $type = 'resource';
 9265:                             @interval=&EXT("resource.0.interval",$item);
 9266:                         } else {
 9267:                             $mapname = &deversion($item);
 9268:                             if (ref($navmap)) {
 9269:                                 my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
 9270:                                 @interval = ($timelimit,'map');
 9271:                             }
 9272:                         }
 9273:                     }
 9274:                     if ($interval[0] =~ /^(\d+)/) {
 9275:                         my $timelimit = $1; 
 9276:                         my $first_access;
 9277:                         if ($type eq 'resource') {
 9278:                             $first_access=&get_first_access($interval[1],$item);
 9279:                         } elsif ($type eq 'map') {
 9280:                             $first_access=&get_first_access($interval[1],undef,$item);
 9281:                         } else {
 9282:                             $first_access=&get_first_access($interval[1]);
 9283:                         }
 9284:                         if ($first_access) {
 9285:                             my $timesup = $first_access+$timelimit;
 9286:                             if ($timesup > $now) {
 9287:                                 my $activeblock;
 9288:                                 if ($type eq 'resource') {
 9289:                                     if (ref($navmap)) {
 9290:                                         my $res = $navmap->getBySymb($item);
 9291:                                         if ($res->answerable()) {
 9292:                                             $activeblock = 1;
 9293:                                         }
 9294:                                     }
 9295:                                 } elsif ($type eq 'map') {
 9296:                                     my $mapsymb = &symbread($mapname,1);
 9297:                                     if (($mapsymb) && (ref($navmap))) {
 9298:                                         my $mapres = $navmap->getBySymb($mapsymb);
 9299:                                         if (ref($mapres)) {
 9300:                                             my $first = $mapres->map_start();
 9301:                                             my $finish = $mapres->map_finish();
 9302:                                             my $it = $navmap->getIterator($first,$finish,undef,0,0);
 9303:                                             if (ref($it)) {
 9304:                                                 my $res;
 9305:                                                 while ($res = $it->next(undef,1)) {
 9306:                                                     next unless (ref($res));
 9307:                                                     my $symb = $res->symb();
 9308:                                                     next if (($symb eq $mapsymb) || ($symb eq ''));
 9309:                                                     @interval=&EXT("resource.0.interval",$symb);
 9310:                                                     if ($interval[1] eq 'map') {
 9311:                                                         if ($res->answerable()) {
 9312:                                                             $activeblock = 1;
 9313:                                                             last;
 9314:                                                         }
 9315:                                                     }
 9316:                                                 }
 9317:                                             }
 9318:                                         }
 9319:                                     }
 9320:                                 }
 9321:                                 if ($activeblock) {
 9322:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 9323:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 9324:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 9325:                                          }
 9326:                                     }
 9327:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 9328:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 9329:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 9330:                                         }
 9331:                                     }
 9332:                                 }
 9333:                             }
 9334:                         }
 9335:                     }
 9336:                 }
 9337:             }
 9338:         }
 9339:     }
 9340:     return %blockers;
 9341: }
 9342: 
 9343: sub has_comm_blocking {
 9344:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 9345:     my @blockers;
 9346:     return unless ($env{'request.course.id'});
 9347:     return unless ($priv eq 'bre');
 9348:     return if ($env{'request.state'} eq 'construct');
 9349:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9350:     if ($env{'request.course.sec'}) {
 9351:         $courseurl .= '/'.$env{'request.course.sec'};
 9352:     }
 9353:     return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9354:     my %blockinfo;
 9355:     if (ref($blocks) eq 'HASH') {
 9356:         %blockinfo = &get_commblock_resources($blocks);
 9357:     } else {
 9358:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 9359:         %blockinfo = %cachedblockers;
 9360:     }
 9361:     return unless (keys(%blockinfo) > 0);
 9362:     my (%possibles,@symbs);
 9363:     if (!$symb) {
 9364:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 9365:     }
 9366:     if ($symb) {
 9367:         @symbs = ($symb);
 9368:     } elsif (keys(%possibles)) { 
 9369:         @symbs = keys(%possibles);
 9370:     }
 9371:     my $noblock;
 9372:     foreach my $symb (@symbs) {
 9373:         last if ($noblock);
 9374:         my ($map,$resid,$resurl)=&decode_symb($symb);
 9375:         foreach my $block (keys(%blockinfo)) {
 9376:             if ($block =~ /^firstaccess____(.+)$/) {
 9377:                 my $item = $1;
 9378:                 unless ($blocked) {
 9379:                     if (($item eq $map) || ($item eq $symb)) {
 9380:                         $noblock = 1;
 9381:                         last;
 9382:                     }
 9383:                 }
 9384:             }
 9385:             if (ref($blockinfo{$block}) eq 'HASH') {
 9386:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 9387:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 9388:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9389:                             push(@blockers,$block);
 9390:                         }
 9391:                     }
 9392:                 }
 9393:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 9394:                     if ($blockinfo{$block}{'maps'}{$map}) {
 9395:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9396:                             push(@blockers,$block);
 9397:                         }
 9398:                     }
 9399:                 }
 9400:             }
 9401:         }
 9402:     }
 9403:     unless ($noblock) { 
 9404:         return @blockers;
 9405:     }
 9406:     return;
 9407: }
 9408: }
 9409: 
 9410: sub deeplink_check {
 9411:     my ($priv,$symb,$uri) = @_;
 9412:     return unless ($env{'request.course.id'});
 9413:     return unless ($priv eq 'bre');
 9414:     return if ($env{'request.state'} eq 'construct');
 9415:     return if ($env{'request.role.adv'});
 9416:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9417:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9418:     my (%possibles,@symbs);
 9419:     if (!$symb) {
 9420:         $symb = &symbread($uri,1,1,1,\%possibles);
 9421:     }
 9422:     if ($symb) {
 9423:         @symbs = ($symb);
 9424:     } elsif (keys(%possibles)) {
 9425:         @symbs = keys(%possibles);
 9426:     }
 9427: 
 9428:     my ($deeplink_symb,$allow);
 9429:     if ($env{'request.deeplink.login'}) {
 9430:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
 9431:     }
 9432:     foreach my $symb (@symbs) {
 9433:         last if ($allow);
 9434:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9435:         if ($deeplink eq '') {
 9436:             $allow = 1;
 9437:         } else {
 9438:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
 9439:             if ($state ne 'only') {
 9440:                 $allow = 1;
 9441:             } else {
 9442:                 my $check_deeplink_entry;
 9443:                 if ($protect ne 'none') {
 9444:                     my ($acctype,$item) = split(/:/,$protect);
 9445:                     if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
 9446:                         if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
 9447:                             $check_deeplink_entry = 1
 9448:                         }
 9449:                     } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
 9450:                         if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
 9451:                             $check_deeplink_entry = 1;
 9452:                         }
 9453:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9454:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9455:                             $check_deeplink_entry = 1;
 9456:                         }
 9457:                     }
 9458:                 }
 9459:                 if (($protect eq 'none') || ($check_deeplink_entry)) {
 9460:                     if ($scope eq 'res') {
 9461:                         if ($symb eq $deeplink_symb) {
 9462:                             $allow = 1;
 9463:                         }
 9464:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
 9465:                         my ($map_from_symb,$map_from_login);
 9466:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
 9467:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
 9468:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
 9469:                         } else {
 9470:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
 9471:                         }
 9472:                         if (($map_from_symb) && ($map_from_login)) {
 9473:                             if ($map_from_symb eq $map_from_login) {
 9474:                                 $allow = 1;
 9475:                             } elsif ($scope eq 'rec') {
 9476:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
 9477:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
 9478:                                     $allow = 1;
 9479:                                 }
 9480:                             }
 9481:                         }
 9482:                     }
 9483:                 }
 9484:             }
 9485:         }
 9486:     }
 9487:     return if ($allow);
 9488:     return 1;
 9489: }
 9490: 
 9491: # -------------------------------- Deversion and split uri into path an filename   
 9492: 
 9493: #
 9494: #   Removes the version from a URI and
 9495: #   splits it in to its filename and path to the filename.
 9496: #   Seems like File::Basename could have done this more clearly.
 9497: #   Parameters:
 9498: #      $uri   - input URI
 9499: #   Returns:
 9500: #     Two element list consisting of 
 9501: #     $pathname  - the URI up to and excluding the trailing /
 9502: #     $filename  - The part of the URI following the last /
 9503: #  NOTE:
 9504: #    Another realization of this is simply:
 9505: #    use File::Basename;
 9506: #    ...
 9507: #    $uri = shift;
 9508: #    $filename = basename($uri);
 9509: #    $path     = dirname($uri);
 9510: #    return ($filename, $path);
 9511: #
 9512: #     The implementation below is probably faster however.
 9513: #
 9514: sub split_uri_for_cond {
 9515:     my $uri=&deversion(&declutter(shift));
 9516:     my @uriparts=split(/\//,$uri);
 9517:     my $filename=pop(@uriparts);
 9518:     my $pathname=join('/',@uriparts);
 9519:     return ($pathname,$filename);
 9520: }
 9521: # --------------------------------------------------- Is a resource on the map?
 9522: 
 9523: sub is_on_map {
 9524:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9525:     #Trying to find the conditional for the file
 9526:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9527: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9528:     if ($match) {
 9529: 	return (1,$1);
 9530:     } else {
 9531: 	return (0,0);
 9532:     }
 9533: }
 9534: 
 9535: # --------------------------------------------------------- Get symb from alias
 9536: 
 9537: sub get_symb_from_alias {
 9538:     my $symb=shift;
 9539:     my ($map,$resid,$url)=&decode_symb($symb);
 9540: # Already is a symb
 9541:     if ($url) { return $symb; }
 9542: # Must be an alias
 9543:     my $aliassymb='';
 9544:     my %bighash;
 9545:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9546:                             &GDBM_READER(),0640)) {
 9547:         my $rid=$bighash{'mapalias_'.$symb};
 9548: 	if ($rid) {
 9549: 	    my ($mapid,$resid)=split(/\./,$rid);
 9550: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9551: 				    $resid,$bighash{'src_'.$rid});
 9552: 	}
 9553:         untie %bighash;
 9554:     }
 9555:     return $aliassymb;
 9556: }
 9557: 
 9558: # ----------------------------------------------------------------- Define Role
 9559: 
 9560: sub definerole {
 9561:   if (allowed('mcr','/')) {
 9562:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9563:     foreach my $role (split(':',$sysrole)) {
 9564: 	my ($crole,$cqual)=split(/\&/,$role);
 9565:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9566:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9567: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9568:                return "refused:s:$crole&$cqual"; 
 9569:             }
 9570:         }
 9571:     }
 9572:     foreach my $role (split(':',$domrole)) {
 9573: 	my ($crole,$cqual)=split(/\&/,$role);
 9574:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9575:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9576: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9577:                return "refused:d:$crole&$cqual"; 
 9578:             }
 9579:         }
 9580:     }
 9581:     foreach my $role (split(':',$courole)) {
 9582: 	my ($crole,$cqual)=split(/\&/,$role);
 9583:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9584:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9585: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9586:                return "refused:c:$crole&$cqual"; 
 9587:             }
 9588:         }
 9589:     }
 9590:     my $uhome;
 9591:     if (($uname ne '') && ($udom ne '')) {
 9592:         $uhome = &homeserver($uname,$udom);
 9593:         return $uhome if ($uhome eq 'no_host');
 9594:     } else {
 9595:         $uname = $env{'user.name'};
 9596:         $udom = $env{'user.domain'};
 9597:         $uhome = $env{'user.home'};
 9598:     }
 9599:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9600:                 "$udom:$uname:rolesdef_$rolename=".
 9601:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9602:     return reply($command,$uhome);
 9603:   } else {
 9604:     return 'refused';
 9605:   }
 9606: }
 9607: 
 9608: # ---------------- Make a metadata query against the network of library servers
 9609: 
 9610: sub metadata_query {
 9611:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9612:     my %rhash;
 9613:     my %libserv = &all_library();
 9614:     my @server_list = (defined($server_array) ? @$server_array
 9615:                                               : keys(%libserv) );
 9616:     for my $server (@server_list) {
 9617:         my $domains = ''; 
 9618:         if (ref($domains_hash) eq 'HASH') {
 9619:             $domains = $domains_hash->{$server}; 
 9620:         }
 9621: 	unless ($custom or $customshow) {
 9622: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9623: 	    $rhash{$server}=$reply;
 9624: 	}
 9625: 	else {
 9626: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9627: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9628: 			     $server);
 9629: 	    $rhash{$server}=$reply;
 9630: 	}
 9631:     }
 9632:     return \%rhash;
 9633: }
 9634: 
 9635: # ----------------------------------------- Send log queries and wait for reply
 9636: 
 9637: sub log_query {
 9638:     my ($uname,$udom,$query,%filters)=@_;
 9639:     my $uhome=&homeserver($uname,$udom);
 9640:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9641:     my $uhost=&hostname($uhome);
 9642:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9643:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9644:                        $uhome);
 9645:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9646:     return get_query_reply($queryid);
 9647: }
 9648: 
 9649: # -------------------------- Update MySQL table for portfolio file
 9650: 
 9651: sub update_portfolio_table {
 9652:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9653:     if ($group ne '') {
 9654:         $file_name =~s /^\Q$group\E//;
 9655:     }
 9656:     my $homeserver = &homeserver($uname,$udom);
 9657:     my $queryid=
 9658:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9659:                ':'.&escape($file_name).':'.$action,$homeserver);
 9660:     my $reply = &get_query_reply($queryid);
 9661:     return $reply;
 9662: }
 9663: 
 9664: # -------------------------- Update MySQL allusers table
 9665: 
 9666: sub update_allusers_table {
 9667:     my ($uname,$udom,$names) = @_;
 9668:     my $homeserver = &homeserver($uname,$udom);
 9669:     my $queryid=
 9670:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9671:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9672:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9673:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9674:                'generation='.&escape($names->{'generation'}).'%%'.
 9675:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9676:                'id='.&escape($names->{'id'}),$homeserver);
 9677:     return;
 9678: }
 9679: 
 9680: # ------- Request retrieval of institutional classlists for course(s)
 9681: 
 9682: sub fetch_enrollment_query {
 9683:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9684:     my ($homeserver,$sleep,$loopmax);
 9685:     my $maxtries = 1;
 9686:     if ($context eq 'automated') {
 9687:         $homeserver = $perlvar{'lonHostID'};
 9688:         $sleep = 2;
 9689:         $loopmax = 100;
 9690:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9691:     } else {
 9692:         $homeserver = &homeserver($cnum,$dom);
 9693:     }
 9694:     my $host=&hostname($homeserver);
 9695:     my $cmd = '';
 9696:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9697:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9698:     }
 9699:     $cmd =~ s/%%$//;
 9700:     $cmd = &escape($cmd);
 9701:     my $query = 'fetchenrollment';
 9702:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9703:     unless ($queryid=~/^\Q$host\E\_/) { 
 9704:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9705:         return 'error: '.$queryid;
 9706:     }
 9707:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9708:     my $tries = 1;
 9709:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9710:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9711:         $tries ++;
 9712:     }
 9713:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9714:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9715:     } else {
 9716:         my @responses = split(/:/,$reply);
 9717:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9718:             foreach my $line (@responses) {
 9719:                 my ($key,$value) = split(/=/,$line,2);
 9720:                 $$replyref{$key} = $value;
 9721:             }
 9722:         } else {
 9723:             my $pathname = LONCAPA::tempdir();
 9724:             foreach my $line (@responses) {
 9725:                 my ($key,$value) = split(/=/,$line);
 9726:                 $$replyref{$key} = $value;
 9727:                 if ($value > 0) {
 9728:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9729:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9730:                         my $destname = $pathname.'/'.$filename;
 9731:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9732:                         if ($xml_classlist =~ /^error/) {
 9733:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9734:                         } else {
 9735:                             if ( open(FILE,">",$destname) ) {
 9736:                                 print FILE &unescape($xml_classlist);
 9737:                                 close(FILE);
 9738:                             } else {
 9739:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9740:                             }
 9741:                         }
 9742:                     }
 9743:                 }
 9744:             }
 9745:         }
 9746:         return 'ok';
 9747:     }
 9748:     return 'error';
 9749: }
 9750: 
 9751: sub get_query_reply {
 9752:     my ($queryid,$sleep,$loopmax) = @_;
 9753:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9754:         $sleep = 0.2;
 9755:     }
 9756:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9757:         $loopmax = 100;
 9758:     }
 9759:     my $replyfile=LONCAPA::tempdir().$queryid;
 9760:     my $reply='';
 9761:     for (1..$loopmax) {
 9762: 	sleep($sleep);
 9763:         if (-e $replyfile.'.end') {
 9764: 	    if (open(my $fh,"<",$replyfile)) {
 9765: 		$reply = join('',<$fh>);
 9766: 		close($fh);
 9767: 	   } else { return 'error: reply_file_error'; }
 9768:            return &unescape($reply);
 9769: 	}
 9770:     }
 9771:     return 'timeout:'.$queryid;
 9772: }
 9773: 
 9774: sub courselog_query {
 9775: #
 9776: # possible filters:
 9777: # url: url or symb
 9778: # username
 9779: # domain
 9780: # action: view, submit, grade
 9781: # start: timestamp
 9782: # end: timestamp
 9783: #
 9784:     my (%filters)=@_;
 9785:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9786:     if ($filters{'url'}) {
 9787: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9788:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9789:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9790:     }
 9791:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9792:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9793:     return &log_query($cname,$cdom,'courselog',%filters);
 9794: }
 9795: 
 9796: sub userlog_query {
 9797: #
 9798: # possible filters:
 9799: # action: log check role
 9800: # start: timestamp
 9801: # end: timestamp
 9802: #
 9803:     my ($uname,$udom,%filters)=@_;
 9804:     return &log_query($uname,$udom,'userlog',%filters);
 9805: }
 9806: 
 9807: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9808: 
 9809: sub auto_run {
 9810:     my ($cnum,$cdom) = @_;
 9811:     my $response = 0;
 9812:     my $settings;
 9813:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9814:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9815:         $settings = $domconfig{'autoenroll'};
 9816:         if ($settings->{'run'} eq '1') {
 9817:             $response = 1;
 9818:         }
 9819:     } else {
 9820:         my $homeserver;
 9821:         if (&is_course($cdom,$cnum)) {
 9822:             $homeserver = &homeserver($cnum,$cdom);
 9823:         } else {
 9824:             $homeserver = &domain($cdom,'primary');
 9825:         }
 9826:         if ($homeserver ne 'no_host') {
 9827:             $response = &reply('autorun:'.$cdom,$homeserver);
 9828:         }
 9829:     }
 9830:     return $response;
 9831: }
 9832: 
 9833: sub auto_get_sections {
 9834:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9835:     my $homeserver;
 9836:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9837:         $homeserver = &homeserver($cnum,$cdom);
 9838:     }
 9839:     if (!defined($homeserver)) { 
 9840:         if ($cdom =~ /^$match_domain$/) {
 9841:             $homeserver = &domain($cdom,'primary');
 9842:         }
 9843:     }
 9844:     my @secs;
 9845:     if (defined($homeserver)) {
 9846:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9847:         unless ($response eq 'refused') {
 9848:             @secs = split(/:/,$response);
 9849:         }
 9850:     }
 9851:     return @secs;
 9852: }
 9853: 
 9854: sub auto_new_course {
 9855:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9856:     my $homeserver = &homeserver($cnum,$cdom);
 9857:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9858:     return $response;
 9859: }
 9860: 
 9861: sub auto_validate_courseID {
 9862:     my ($cnum,$cdom,$inst_course_id) = @_;
 9863:     my $homeserver = &homeserver($cnum,$cdom);
 9864:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9865:     return $response;
 9866: }
 9867: 
 9868: sub auto_validate_instcode {
 9869:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9870:     my ($homeserver,$response);
 9871:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9872:         $homeserver = &homeserver($cnum,$cdom);
 9873:     }
 9874:     if (!defined($homeserver)) {
 9875:         if ($cdom =~ /^$match_domain$/) {
 9876:             $homeserver = &domain($cdom,'primary');
 9877:         }
 9878:     }
 9879:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9880:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9881:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9882:     return ($outcome,$description,$defaultcredits);
 9883: }
 9884: 
 9885: sub auto_validate_inst_crosslist {
 9886:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
 9887:     my ($homeserver,$response);
 9888:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9889:         $homeserver = &homeserver($cnum,$cdom);
 9890:     }
 9891:     if (!defined($homeserver)) {
 9892:         if ($cdom =~ /^$match_domain$/) {
 9893:             $homeserver = &domain($cdom,'primary');
 9894:         }
 9895:     }
 9896:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
 9897:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
 9898:                          &escape($instcode).':'.&escape($inst_xlist).':'.
 9899:                          &escape($coowner),$homeserver);
 9900:     }
 9901:     return $response;
 9902: }
 9903: 
 9904: sub auto_create_password {
 9905:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9906:     my ($homeserver,$response);
 9907:     my $create_passwd = 0;
 9908:     my $authchk = '';
 9909:     if ($udom =~ /^$match_domain$/) {
 9910:         $homeserver = &domain($udom,'primary');
 9911:     }
 9912:     if ($homeserver eq '') {
 9913:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9914:             $homeserver = &homeserver($cnum,$cdom);
 9915:         }
 9916:     }
 9917:     if ($homeserver eq '') {
 9918:         $authchk = 'nodomain';
 9919:     } else {
 9920:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9921:         if ($response eq 'refused') {
 9922:             $authchk = 'refused';
 9923:         } else {
 9924:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9925:         }
 9926:     }
 9927:     return ($authparam,$create_passwd,$authchk);
 9928: }
 9929: 
 9930: sub auto_photo_permission {
 9931:     my ($cnum,$cdom,$students) = @_;
 9932:     my $homeserver = &homeserver($cnum,$cdom);
 9933:     my ($outcome,$perm_reqd,$conditions) = 
 9934: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9935:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9936: 	return (undef,undef);
 9937:     }
 9938:     return ($outcome,$perm_reqd,$conditions);
 9939: }
 9940: 
 9941: sub auto_checkphotos {
 9942:     my ($uname,$udom,$pid) = @_;
 9943:     my $homeserver = &homeserver($uname,$udom);
 9944:     my ($result,$resulttype);
 9945:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9946: 				   &escape($uname).':'.&escape($pid),
 9947: 				   $homeserver));
 9948:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9949: 	return (undef,undef);
 9950:     }
 9951:     if ($outcome) {
 9952:         ($result,$resulttype) = split(/:/,$outcome);
 9953:     } 
 9954:     return ($result,$resulttype);
 9955: }
 9956: 
 9957: sub auto_photochoice {
 9958:     my ($cnum,$cdom) = @_;
 9959:     my $homeserver = &homeserver($cnum,$cdom);
 9960:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9961: 						       &escape($cdom),
 9962: 						       $homeserver)));
 9963:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9964: 	return (undef,undef);
 9965:     }
 9966:     return ($update,$comment);
 9967: }
 9968: 
 9969: sub auto_photoupdate {
 9970:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9971:     my $homeserver = &homeserver($cnum,$dom);
 9972:     my $host=&hostname($homeserver);
 9973:     my $cmd = '';
 9974:     my $maxtries = 1;
 9975:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9976:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9977:     }
 9978:     $cmd =~ s/%%$//;
 9979:     $cmd = &escape($cmd);
 9980:     my $query = 'institutionalphotos';
 9981:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9982:     unless ($queryid=~/^\Q$host\E\_/) {
 9983:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9984:         return 'error: '.$queryid;
 9985:     }
 9986:     my $reply = &get_query_reply($queryid);
 9987:     my $tries = 1;
 9988:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9989:         $reply = &get_query_reply($queryid);
 9990:         $tries ++;
 9991:     }
 9992:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9993:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9994:     } else {
 9995:         my @responses = split(/:/,$reply);
 9996:         my $outcome = shift(@responses); 
 9997:         foreach my $item (@responses) {
 9998:             my ($key,$value) = split(/=/,$item);
 9999:             $$photo{$key} = $value;
10000:         }
10001:         return $outcome;
10002:     }
10003:     return 'error';
10004: }
10005: 
10006: sub auto_instcode_format {
10007:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
10008: 	$cat_order) = @_;
10009:     my $courses = '';
10010:     my @homeservers;
10011:     if ($caller eq 'global') {
10012: 	my %servers = &get_servers($codedom,'library');
10013: 	foreach my $tryserver (keys(%servers)) {
10014: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10015: 		push(@homeservers,$tryserver);
10016: 	    }
10017:         }
10018:     } elsif ($caller eq 'requests') {
10019:         if ($codedom =~ /^$match_domain$/) {
10020:             my $chome = &domain($codedom,'primary');
10021:             unless ($chome eq 'no_host') {
10022:                 push(@homeservers,$chome);
10023:             }
10024:         }
10025:     } else {
10026:         push(@homeservers,&homeserver($caller,$codedom));
10027:     }
10028:     foreach my $code (keys(%{$instcodes})) {
10029:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
10030:     }
10031:     chop($courses);
10032:     my $ok_response = 0;
10033:     my $response;
10034:     while (@homeservers > 0 && $ok_response == 0) {
10035:         my $server = shift(@homeservers); 
10036:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
10037:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
10038:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
10039: 		split(/:/,$response);
10040:             %{$codes} = (%{$codes},&str2hash($codes_str));
10041:             push(@{$codetitles},&str2array($codetitles_str));
10042:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
10043:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
10044:             $ok_response = 1;
10045:         }
10046:     }
10047:     if ($ok_response) {
10048:         return 'ok';
10049:     } else {
10050:         return $response;
10051:     }
10052: }
10053: 
10054: sub auto_instcode_defaults {
10055:     my ($domain,$returnhash,$code_order) = @_;
10056:     my @homeservers;
10057: 
10058:     my %servers = &get_servers($domain,'library');
10059:     foreach my $tryserver (keys(%servers)) {
10060: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10061: 	    push(@homeservers,$tryserver);
10062: 	}
10063:     }
10064: 
10065:     my $response;
10066:     foreach my $server (@homeservers) {
10067:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
10068:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10069: 	
10070: 	foreach my $pair (split(/\&/,$response)) {
10071: 	    my ($name,$value)=split(/\=/,$pair);
10072: 	    if ($name eq 'code_order') {
10073: 		@{$code_order} = split(/\&/,&unescape($value));
10074: 	    } else {
10075: 		$returnhash->{&unescape($name)}=&unescape($value);
10076: 	    }
10077: 	}
10078: 	return 'ok';
10079:     }
10080: 
10081:     return $response;
10082: }
10083: 
10084: sub auto_possible_instcodes {
10085:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10086:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
10087:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10088:         return;
10089:     }
10090:     my (@homeservers,$uhome);
10091:     if (defined(&domain($domain,'primary'))) {
10092:         $uhome=&domain($domain,'primary');
10093:         push(@homeservers,&domain($domain,'primary'));
10094:     } else {
10095:         my %servers = &get_servers($domain,'library');
10096:         foreach my $tryserver (keys(%servers)) {
10097:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10098:                 push(@homeservers,$tryserver);
10099:             }
10100:         }
10101:     }
10102:     my $response;
10103:     foreach my $server (@homeservers) {
10104:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
10105:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10106:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
10107:             split(':',$response);
10108:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10109:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
10110:         foreach my $item (split('&',$cat_title)) {   
10111:             my ($name,$value)=split('=',$item);
10112:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
10113:         }
10114:         foreach my $item (split('&',$cat_order)) {
10115:             my ($name,$value)=split('=',$item);
10116:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
10117:         }
10118:         return 'ok';
10119:     }
10120:     return $response;
10121: }
10122: 
10123: sub auto_courserequest_checks {
10124:     my ($dom) = @_;
10125:     my ($homeserver,%validations);
10126:     if ($dom =~ /^$match_domain$/) {
10127:         $homeserver = &domain($dom,'primary');
10128:     }
10129:     unless ($homeserver eq 'no_host') {
10130:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10131:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10132:             my @items = split(/&/,$response);
10133:             foreach my $item (@items) {
10134:                 my ($key,$value) = split('=',$item);
10135:                 $validations{&unescape($key)} = &thaw_unescape($value);
10136:             }
10137:         }
10138:     }
10139:     return %validations; 
10140: }
10141: 
10142: sub auto_courserequest_validation {
10143:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
10144:     my ($homeserver,$response);
10145:     if ($dom =~ /^$match_domain$/) {
10146:         $homeserver = &domain($dom,'primary');
10147:     }
10148:     unless ($homeserver eq 'no_host') {
10149:         my $customdata;
10150:         if (ref($custominfo) eq 'HASH') {
10151:             $customdata = &freeze_escape($custominfo);
10152:         }
10153:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
10154:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
10155:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
10156:                                     $customdata,$homeserver));
10157:     }
10158:     return $response;
10159: }
10160: 
10161: sub auto_validate_class_sec {
10162:     my ($cdom,$cnum,$owners,$inst_class) = @_;
10163:     my $homeserver = &homeserver($cnum,$cdom);
10164:     my $ownerlist;
10165:     if (ref($owners) eq 'ARRAY') {
10166:         $ownerlist = join(',',@{$owners});
10167:     } else {
10168:         $ownerlist = $owners;
10169:     }
10170:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
10171:                         &escape($ownerlist).':'.$cdom,$homeserver);
10172:     return $response;
10173: }
10174: 
10175: sub auto_instsec_reformat {
10176:     my ($cdom,$action,$instsecref) = @_;
10177:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
10178:     my @homeservers;
10179:     if (defined(&domain($cdom,'primary'))) {
10180:         push(@homeservers,&domain($cdom,'primary'));
10181:     } else {
10182:         my %servers = &get_servers($cdom,'library');
10183:         foreach my $tryserver (keys(%servers)) {
10184:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10185:                 push(@homeservers,$tryserver);
10186:             }
10187:         }
10188:     }
10189:     my $response;
10190:     my %reformatted = %{$instsecref};
10191:     foreach my $server (@homeservers) {
10192:         if (ref($instsecref) eq 'HASH') {
10193:             my $info = &freeze_escape($instsecref);
10194:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10195:                                 $action.':'.$info,$server);
10196:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
10197:             my @items = split(/&/,$response);
10198:             foreach my $item (@items) {
10199:                 my ($key,$value) = split(/=/,$item);
10200:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
10201:             }
10202:         }
10203:     }
10204:     return %reformatted;
10205: }
10206: 
10207: sub auto_validate_instclasses {
10208:     my ($cdom,$cnum,$owners,$classesref) = @_;
10209:     my ($homeserver,%validations);
10210:     $homeserver = &homeserver($cnum,$cdom);
10211:     unless ($homeserver eq 'no_host') {
10212:         my $ownerlist;
10213:         if (ref($owners) eq 'ARRAY') {
10214:             $ownerlist = join(',',@{$owners});
10215:         } else {
10216:             $ownerlist = $owners;
10217:         }
10218:         if (ref($classesref) eq 'HASH') {
10219:             my $classes = &freeze_escape($classesref);
10220:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10221:                                 ':'.$cdom.':'.$classes,$homeserver);
10222:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10223:                 my @items = split(/&/,$response);
10224:                 foreach my $item (@items) {
10225:                     my ($key,$value) = split('=',$item);
10226:                     $validations{&unescape($key)} = &thaw_unescape($value);
10227:                 }
10228:             }
10229:         }
10230:     }
10231:     return %validations;
10232: }
10233: 
10234: sub auto_crsreq_update {
10235:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
10236:         $code,$accessstart,$accessend,$inbound) = @_;
10237:     my ($homeserver,%crsreqresponse);
10238:     if ($cdom =~ /^$match_domain$/) {
10239:         $homeserver = &domain($cdom,'primary');
10240:     }
10241:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10242:         my $info;
10243:         if (ref($inbound) eq 'HASH') {
10244:             $info = &freeze_escape($inbound);
10245:         }
10246:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10247:                             ':'.&escape($action).':'.&escape($ownername).':'.
10248:                             &escape($ownerdomain).':'.&escape($fullname).':'.
10249:                             &escape($title).':'.&escape($code).':'.
10250:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
10251:                             $homeserver);
10252:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10253:             my @items = split(/&/,$response);
10254:             foreach my $item (@items) {
10255:                 my ($key,$value) = split('=',$item);
10256:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10257:             }
10258:         }
10259:     }
10260:     return \%crsreqresponse;
10261: }
10262: 
10263: sub auto_export_grades {
10264:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
10265:     my ($homeserver,%exportresponse);
10266:     if ($cdom =~ /^$match_domain$/) {
10267:         $homeserver = &domain($cdom,'primary');
10268:     }
10269:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10270:         my $info;
10271:         if (ref($inforef) eq 'HASH') {
10272:             $info = &freeze_escape($inforef);
10273:         }
10274:         if (ref($gradesref) eq 'HASH') {
10275:             my $grades = &freeze_escape($gradesref);
10276:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10277:                                 $info.':'.$grades,$homeserver);
10278:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
10279:                 my @items = split(/&/,$response);
10280:                 foreach my $item (@items) {
10281:                     my ($key,$value) = split('=',$item);
10282:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
10283:                 }
10284:             }
10285:         }
10286:     }
10287:     return \%exportresponse;
10288: }
10289: 
10290: sub check_instcode_cloning {
10291:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10292:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10293:         return;
10294:     }
10295:     my $canclone;
10296:     if (@{$code_order} > 0) {
10297:         my $instcoderegexp ='^';
10298:         my @clonecodes = split(/\&/,$cloner);
10299:         foreach my $item (@{$code_order}) {
10300:             if (grep(/^\Q$item\E=/,@clonecodes)) {
10301:                 foreach my $pair (@clonecodes) {
10302:                     my ($key,$val) = split(/\=/,$pair,2);
10303:                     $val = &unescape($val);
10304:                     if ($key eq $item) {
10305:                         $instcoderegexp .= '('.$val.')';
10306:                         last;
10307:                     }
10308:                 }
10309:             } else {
10310:                 $instcoderegexp .= $codedefaults->{$item};
10311:             }
10312:         }
10313:         $instcoderegexp .= '$';
10314:         my (@from,@to);
10315:         eval {
10316:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
10317:                (@to) = ($clonetocode =~ /$instcoderegexp/);
10318:         };
10319:         if ((@from > 0) && (@to > 0)) {
10320:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10321:             if (!@diffs) {
10322:                 $canclone = 1;
10323:             }
10324:         }
10325:     }
10326:     return $canclone;
10327: }
10328: 
10329: sub default_instcode_cloning {
10330:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10331:     my (%codedefaults,@code_order,$canclone);
10332:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10333:         %codedefaults = %{$codedefaultsref};
10334:         @code_order = @{$codeorderref};
10335:     } elsif ($clonedom) {
10336:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10337:     }
10338:     if (($domdefclone) && (@code_order)) {
10339:         my @clonecodes = split(/\+/,$domdefclone);
10340:         my $instcoderegexp ='^';
10341:         foreach my $item (@code_order) {
10342:             if (grep(/^\Q$item\E$/,@clonecodes)) {
10343:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
10344:             } else {
10345:                 $instcoderegexp .= $codedefaults{$item};
10346:             }
10347:         }
10348:         $instcoderegexp .= '$';
10349:         my (@from,@to);
10350:         eval {
10351:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
10352:             (@to) = ($clonetocode =~ /$instcoderegexp/);
10353:         };
10354:         if ((@from > 0) && (@to > 0)) {
10355:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10356:             if (!@diffs) {
10357:                 $canclone = 1;
10358:             }
10359:         }
10360:     }
10361:     return $canclone;
10362: }
10363: 
10364: # ------------------------------------------------------- Course Group routines
10365: 
10366: sub get_coursegroups {
10367:     my ($cdom,$cnum,$group,$namespace) = @_;
10368:     return(&dump($namespace,$cdom,$cnum,$group));
10369: }
10370: 
10371: sub modify_coursegroup {
10372:     my ($cdom,$cnum,$groupsettings) = @_;
10373:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10374: }
10375: 
10376: sub toggle_coursegroup_status {
10377:     my ($cdom,$cnum,$group,$action) = @_;
10378:     my ($from_namespace,$to_namespace);
10379:     if ($action eq 'delete') {
10380:         $from_namespace = 'coursegroups';
10381:         $to_namespace = 'deleted_groups';
10382:     } else {
10383:         $from_namespace = 'deleted_groups';
10384:         $to_namespace = 'coursegroups';
10385:     }
10386:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
10387:     if (my $tmp = &error(%curr_group)) {
10388:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10389:         return ('read error',$tmp);
10390:     } else {
10391:         my %savedsettings = %curr_group; 
10392:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
10393:         my $deloutcome;
10394:         if ($result eq 'ok') {
10395:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
10396:         } else {
10397:             return ('write error',$result);
10398:         }
10399:         if ($deloutcome eq 'ok') {
10400:             return 'ok';
10401:         } else {
10402:             return ('delete error',$deloutcome);
10403:         }
10404:     }
10405: }
10406: 
10407: sub modify_group_roles {
10408:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context,
10409:         $othdomby,$requester) = @_;
10410:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10411:     my $role = 'gr/'.&escape($userprivs);
10412:     my ($uname,$udom) = split(/:/,$user);
10413:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context,
10414:                              $othdomby,$requester);
10415:     if ($result eq 'ok') {
10416:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10417:     }
10418:     return $result;
10419: }
10420: 
10421: sub modify_coursegroup_membership {
10422:     my ($cdom,$cnum,$membership) = @_;
10423:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
10424:     return $result;
10425: }
10426: 
10427: sub get_active_groups {
10428:     my ($udom,$uname,$cdom,$cnum) = @_;
10429:     my $now = time;
10430:     my %groups = ();
10431:     foreach my $key (keys(%env)) {
10432:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
10433:             my ($start,$end) = split(/\./,$env{$key});
10434:             if (($end!=0) && ($end<$now)) { next; }
10435:             if (($start!=0) && ($start>$now)) { next; }
10436:             if ($1 eq $cdom && $2 eq $cnum) {
10437:                 $groups{$3} = $env{$key} ;
10438:             }
10439:         }
10440:     }
10441:     return %groups;
10442: }
10443: 
10444: sub get_group_membership {
10445:     my ($cdom,$cnum,$group) = @_;
10446:     return(&dump('groupmembership',$cdom,$cnum,$group));
10447: }
10448: 
10449: sub get_users_groups {
10450:     my ($udom,$uname,$courseid) = @_;
10451:     my @usersgroups;
10452:     my $cachetime=1800;
10453: 
10454:     my $hashid="$udom:$uname:$courseid";
10455:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10456:     if (defined($cached)) {
10457:         @usersgroups = split(/:/,$grouplist);
10458:     } else {  
10459:         $grouplist = '';
10460:         my $courseurl = &courseid_to_courseurl($courseid);
10461:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
10462:         my $access_end = $env{'course.'.$courseid.
10463:                               '.default_enrollment_end_date'};
10464:         my $now = time;
10465:         foreach my $key (keys(%roleshash)) {
10466:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10467:                 my $group = $1;
10468:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10469:                     my $start = $2;
10470:                     my $end = $1;
10471:                     if ($start == -1) { next; } # deleted from group
10472:                     if (($start!=0) && ($start>$now)) { next; }
10473:                     if (($end!=0) && ($end<$now)) {
10474:                         if ($access_end && $access_end < $now) {
10475:                             if ($access_end - $end < 86400) {
10476:                                 push(@usersgroups,$group);
10477:                             }
10478:                         }
10479:                         next;
10480:                     }
10481:                     push(@usersgroups,$group);
10482:                 }
10483:             }
10484:         }
10485:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10486:         $grouplist = join(':',@usersgroups);
10487:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10488:     }
10489:     return @usersgroups;
10490: }
10491: 
10492: sub devalidate_getgroups_cache {
10493:     my ($udom,$uname,$cdom,$cnum)=@_;
10494:     my $courseid = $cdom.'_'.$cnum;
10495: 
10496:     my $hashid="$udom:$uname:$courseid";
10497:     &devalidate_cache_new('getgroups',$hashid);
10498: }
10499: 
10500: # ------------------------------------------------------------------ Plain Text
10501: 
10502: sub plaintext {
10503:     my ($short,$type,$cid,$forcedefault) = @_;
10504:     if ($short =~ m{^cr/}) {
10505: 	return (split('/',$short))[-1];
10506:     }
10507:     if (!defined($cid)) {
10508:         $cid = $env{'request.course.id'};
10509:     }
10510:     my %rolenames = (
10511:                       Course    => 'std',
10512:                       Community => 'alt1',
10513:                       Placement => 'std',
10514:                     );
10515:     if ($cid ne '') {
10516:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10517:             unless ($forcedefault) {
10518:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10519:                 &Apache::lonlocal::mt_escape(\$roletext);
10520:                 return &Apache::lonlocal::mt($roletext);
10521:             }
10522:         }
10523:     }
10524:     if ((defined($type)) && (defined($rolenames{$type})) &&
10525:         (defined($rolenames{$type})) && 
10526:         (defined($prp{$short}{$rolenames{$type}}))) {
10527:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10528:     } elsif ($cid ne '') {
10529:         my $crstype = $env{'course.'.$cid.'.type'};
10530:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10531:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10532:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10533:         }
10534:     }
10535:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10536: }
10537: 
10538: # ----------------------------------------------------------------- Assign Role
10539: 
10540: sub assignrole {
10541:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10542:         $context,$othdomby,$requester,$reqsec,$reqrole)=@_;
10543:     my $mrole;
10544:     if ($role =~ /^cr\//) {
10545:         my $cwosec=$url;
10546:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10547:         if ((!&allowed('ccr',$cwosec)) && (!&allowed('ccr',$udom))) {
10548:             my $refused = 1;
10549:             if ($context eq 'requestcourses') {
10550:                 if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10551:                     if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10552:                         if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10553:                             my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10554:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10555:                             if ($crsenv{'internal.courseowner'} eq
10556:                                 $env{'user.name'}.':'.$env{'user.domain'}) {
10557:                                 $refused = '';
10558:                             }
10559:                         }
10560:                     }
10561:                 }
10562:             } elsif (($context eq 'course') && ($othdomby eq 'othdombyuser')) {
10563:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10564:                 my ($sec) = ($url =~ m{^/\Q$cwosec\E/(.*)$});
10565:                 my $key = "$uname:$udom:$role:$sec";
10566:                 my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
10567:                 if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10568:                     if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10569:                         $refused = '';
10570:                     }
10571:                 }
10572:             }
10573:             if ($refused) {
10574:                 &logthis('Refused custom assignrole: '.
10575:                          $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10576:                          ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10577:                 return 'refused';
10578:             }
10579:         }
10580:         $mrole='cr';
10581:     } elsif ($role =~ /^gr\//) {
10582:         my $cwogrp=$url;
10583:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10584:         if (!&allowed('mdg',$cwogrp)) {
10585:             my $refused = 1;
10586:             if (($refused) && ($othdomby eq 'othdombyuser') && ($requester ne '') && ($reqrole ne '')) {
10587:                 my ($cdom,$cnum) = ($cwogrp =~ m{^/?($match_domain)/($match_courseid)$});
10588:                 my $key = "$uname:$udom:$reqrole:$reqsec";
10589:                 my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
10590:                 if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10591:                     if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10592:                         $refused = '';
10593:                     }
10594:                 }
10595:             }
10596:             if ($refused) {
10597:                 &logthis('Refused group assignrole: '.
10598:                          $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10599:                          $env{'user.name'}.' at '.$env{'user.domain'});
10600:                 return 'refused';
10601:             }
10602:         }
10603:         $mrole='gr';
10604:     } else {
10605:         my $cwosec=$url;
10606:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10607:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10608:             my $refused;
10609:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10610:                 if (!(&allowed('c'.$role,$url))) {
10611:                     $refused = 1;
10612:                 }
10613:             } else {
10614:                 $refused = 1;
10615:             }
10616:             if ($refused) {
10617:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10618:                 if (!$selfenroll && ($othdomby ne 'othdombyuser') &&
10619:                    (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10620:                     my %crsenv;
10621:                     if ($role eq 'cc' || $role eq 'co') {
10622:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10623:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10624:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10625:                                 if ($crsenv{'internal.courseowner'} eq 
10626:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10627:                                     $refused = '';
10628:                                 }
10629:                             }
10630:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10631:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10632:                                 if ($crsenv{'internal.courseowner'} eq 
10633:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10634:                                     $refused = '';
10635:                                 }
10636:                             }
10637:                         }
10638:                     }
10639:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10640:                     if ($role eq 'st') {
10641:                         $refused = '';
10642:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10643:                         $refused = '';
10644:                     }
10645:                 } elsif ($othdomby eq 'othdombyuser') {
10646:                     my ($key,%queuedrolereq);
10647:                     if ($context eq 'course') {
10648:                         my ($sec) = ($url =~ m{^/\Q$cwosec\E/(.*)$});
10649:                         $key = "$uname:$udom:$role:$sec";
10650:                         %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
10651:                         if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10652:                             if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10653:                                 if ((($role eq 'cc') && ($cnum !~ /^$match_community$/)) || 
10654:                                     (($role eq 'co') && ($cnum =~ /^$match_community$/))) {
10655:                                     my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10656:                                     if ($crsenv{'internal.courseowner'} eq $requester) {
10657:                                         $refused = '';
10658:                                     }
10659:                                 } elsif ($role =~ /^(?:in|ta|ep|st)$/) {
10660:                                     $refused = '';
10661:                                 }
10662:                             }
10663:                         }
10664:                     } elsif (($context eq 'author') && ($role =~ /^ca|aa$/)) {
10665:                         my $key = "$uname:$udom:$role"; 
10666:                         my ($audom,$auname) = ($url =~ m{^/($match_domain)/($match_username)$});
10667:                         if (($audom ne '') && ($auname ne '')) {
10668:                             my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$audom,$auname);
10669:                             if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10670:                                 if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10671:                                     $refused = '';
10672:                                 }
10673:                             }
10674:                         }
10675:                     } elsif (($context eq 'domain') && ($role ne 'dc') && ($role ne 'su')) {
10676:                         my $key = "$uname:$udom:$role";
10677:                         my ($roledom) = ($url =~ m{^/($match_domain)/\Q$role\E$});
10678:                         if ($roledom ne '') {
10679:                             my $confname = $roledom.'-domainconfig';
10680:                             my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$roledom,$confname);
10681:                             if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10682:                                 if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10683:                                     $refused = '';
10684:                                 }
10685:                             }
10686:                         }
10687:                     }
10688:                 } elsif ($context eq 'requestcourses') {
10689:                     my @possroles = ('st','ta','ep','in','cc','co');
10690:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10691:                         my $wrongcc;
10692:                         if ($cnum =~ /^$match_community$/) {
10693:                             $wrongcc = 1 if ($role eq 'cc');
10694:                         } else {
10695:                             $wrongcc = 1 if ($role eq 'co');
10696:                         }
10697:                         unless ($wrongcc) {
10698:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10699:                             if ($crsenv{'internal.courseowner'} eq 
10700:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10701:                                 $refused = '';
10702:                             }
10703:                         }
10704:                     }
10705:                 } elsif ($context eq 'requestauthor') {
10706:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10707:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10708:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10709:                             $refused = '';
10710:                         } else {
10711:                             my %domdefaults = &get_domain_defaults($udom);
10712:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10713:                                 my $checkbystatus;
10714:                                 if ($env{'user.adv'}) { 
10715:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10716:                                     if ($disposition eq 'automatic') {
10717:                                         $refused = '';
10718:                                     } elsif ($disposition eq '') {
10719:                                         $checkbystatus = 1;
10720:                                     } 
10721:                                 } else {
10722:                                     $checkbystatus = 1;
10723:                                 }
10724:                                 if ($checkbystatus) {
10725:                                     if ($env{'environment.inststatus'}) {
10726:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10727:                                         foreach my $type (@inststatuses) {
10728:                                             if (($type ne '') &&
10729:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10730:                                                 $refused = '';
10731:                                             }
10732:                                         }
10733:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10734:                                         $refused = '';
10735:                                     }
10736:                                 }
10737:                             }
10738:                         }
10739:                     }
10740:                 }
10741:                 if ($refused) {
10742:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10743:                              ' '.$role.' '.$end.' '.$start.' by '.
10744: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10745:                     return 'refused';
10746:                 }
10747:             }
10748:         } elsif ($role eq 'au') {
10749:             if ($url ne '/'.$udom.'/') {
10750:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10751:                          ' to assign author role for '.$uname.':'.$udom.
10752:                          ' in domain: '.$url.' refused (wrong domain).');
10753:                 return 'refused';
10754:             }
10755:         }
10756:         $mrole=$role;
10757:     }
10758:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10759:                 "$udom:$uname:$url".'_'."$mrole=$role";
10760:     if ($end) { $command.='_'.$end; }
10761:     if ($start) {
10762: 	if ($end) { 
10763:            $command.='_'.$start; 
10764:         } else {
10765:            $command.='_0_'.$start;
10766:         }
10767:     }
10768:     my $origstart = $start;
10769:     my $origend = $end;
10770:     my $delflag;
10771: # actually delete
10772:     if ($deleteflag) {
10773: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10774: # modify command to delete the role
10775:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10776:                 "$udom:$uname:$url".'_'."$mrole";
10777: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10778: # set start and finish to negative values for userrolelog
10779:            $start=-1;
10780:            $end=-1;
10781:            $delflag = 1;
10782:         }
10783:     }
10784: # send command
10785:     my $answer=&reply($command,&homeserver($uname,$udom));
10786: # log new user role if status is ok
10787:     if ($answer eq 'ok') {
10788: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10789:         if (($role eq 'cc') || ($role eq 'in') ||
10790:             ($role eq 'ep') || ($role eq 'ad') ||
10791:             ($role eq 'ta') || ($role eq 'st') ||
10792:             ($role=~/^cr/) || ($role eq 'gr') ||
10793:             ($role eq 'co')) {
10794: # for course roles, perform group memberships changes triggered by role change.
10795:             unless ($role =~ /^gr/) {
10796:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10797:                                                  $origstart,$selfenroll,$context);
10798:             }
10799:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10800:                            $selfenroll,$context,$othdomby,$requester);
10801:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10802:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10803:                  ($role eq 'da')) {
10804:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10805:                            $context,$othdomby,$requester);
10806:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10807:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10808:                              $context,$othdomby,$requester); 
10809:         }
10810:         if ($role eq 'cc') {
10811:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10812:         }
10813:     }
10814:     return $answer;
10815: }
10816: 
10817: sub autoupdate_coowners {
10818:     my ($url,$end,$start,$uname,$udom) = @_;
10819:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10820:     if (($cdom ne '') && ($cnum ne '')) {
10821:         my $now = time;
10822:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10823:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10824:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10825:             my $instcode = $coursehash{'internal.coursecode'};
10826:             my $xlists = $coursehash{'internal.crosslistings'};
10827:             if ($instcode ne '') {
10828:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10829:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10830:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10831:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10832:                         unless ($result eq 'valid') {
10833:                             if ($xlists ne '') {
10834:                                 foreach my $xlist (split(',',$xlists)) {
10835:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
10836:                                     $result =
10837:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10838:                                                                       $inst_crosslist,$uname.':'.$udom);
10839:                                     last if ($result eq 'valid');
10840:                                 }
10841:                             }
10842:                         }
10843:                         if ($result eq 'valid') {
10844:                             if ($coursehash{'internal.co-owners'}) {
10845:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10846:                                     push(@newcoowners,$coowner);
10847:                                 }
10848:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10849:                                     push(@newcoowners,$uname.':'.$udom);
10850:                                 }
10851:                                 @newcoowners = sort(@newcoowners);
10852:                             } else {
10853:                                 push(@newcoowners,$uname.':'.$udom);
10854:                             }
10855:                         } elsif ($coursehash{'internal.co-owners'}) {
10856:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10857:                                 unless ($coowner eq $uname.':'.$udom) {
10858:                                     push(@newcoowners,$coowner);
10859:                                 }
10860:                             }
10861:                             unless (@newcoowners > 0) {
10862:                                 $delcoowners = 1;
10863:                                 $coowners = '';
10864:                             }
10865:                         }
10866:                         if (@newcoowners || $delcoowners) {
10867:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10868:                                             $delcoowners,@newcoowners);
10869:                         }
10870:                     }
10871:                 }
10872:             }
10873:         }
10874:     }
10875: }
10876: 
10877: sub store_coowners {
10878:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10879:     my $cid = $cdom.'_'.$cnum;
10880:     my ($coowners,$delresult,$putresult);
10881:     if (@newcoowners) {
10882:         $coowners = join(',',@newcoowners);
10883:         my %coownershash = (
10884:                             'internal.co-owners' => $coowners,
10885:                            );
10886:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10887:         if ($putresult eq 'ok') {
10888:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10889:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10890:             }
10891:         }
10892:     }
10893:     if ($delcoowners) {
10894:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10895:         if ($delresult eq 'ok') {
10896:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10897:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10898:             }
10899:         }
10900:     }
10901:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10902:         my %crsinfo =
10903:             &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10904:         if (ref($crsinfo{$cid}) eq 'HASH') {
10905:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10906:             my $cidput = &courseidput($cdom,\%crsinfo,$chome,'notime');
10907:         }
10908:     }
10909: }
10910: 
10911: # -------------------------------------------------- Modify user authentication
10912: # Overrides without validation
10913: 
10914: sub modifyuserauth {
10915:     my ($udom,$uname,$umode,$upass)=@_;
10916:     my $uhome=&homeserver($uname,$udom);
10917:     my $allowed;
10918:     if (&allowed('mau',$udom)) {
10919:         $allowed = 1;
10920:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10921:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10922:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10923:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10924:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10925:         if (($cdom ne '') && ($cnum ne '')) {
10926:             my $is_owner = &is_course_owner($cdom,$cnum);
10927:             if ($is_owner) {
10928:                 $allowed = 1;
10929:             }
10930:         }
10931:     }
10932:     unless ($allowed) { return 'refused'; }
10933:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10934:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10935:              ' in domain '.$env{'request.role.domain'});  
10936:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10937: 		     &escape($upass),$uhome);
10938:     my $ip = &get_requestor_ip();
10939:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10940:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10941:          '(Remote '.$ip.'): '.$reply);
10942:     &log($udom,,$uname,$uhome,
10943:         'Authentication changed by '.$env{'user.domain'}.', '.
10944:                                      $env{'user.name'}.', '.$umode.
10945:          '(Remote '.$ip.'): '.$reply);
10946:     unless ($reply eq 'ok') {
10947:         &logthis('Authentication mode error: '.$reply);
10948: 	return 'error: '.$reply;
10949:     }   
10950:     return 'ok';
10951: }
10952: 
10953: # --------------------------------------------------------------- Modify a user
10954: 
10955: sub modifyuser {
10956:     my ($udom,    $uname, $uid,
10957:         $umode,   $upass, $first,
10958:         $middle,  $last,  $gene,
10959:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10960:     $udom= &LONCAPA::clean_domain($udom);
10961:     $uname=&LONCAPA::clean_username($uname);
10962:     my $showcandelete = 'none';
10963:     if (ref($candelete) eq 'ARRAY') {
10964:         if (@{$candelete} > 0) {
10965:             $showcandelete = join(', ',@{$candelete});
10966:         }
10967:     }
10968:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10969:              $umode.', '.$first.', '.$middle.', '.
10970: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10971:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10972:                                      ' desiredhome not specified'). 
10973:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10974:              ' in domain '.$env{'request.role.domain'});
10975:     my $uhome=&homeserver($uname,$udom,'true');
10976:     my $newuser;
10977:     if ($uhome eq 'no_host') {
10978:         $newuser = 1;
10979:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10980:                 ($umode eq 'lti')) {
10981:             return 'error: more information needed to create new user';
10982:         }
10983:     }
10984: # ----------------------------------------------------------------- Create User
10985:     if (($uhome eq 'no_host') && 
10986: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10987:         my $unhome='';
10988:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10989:             $unhome = $desiredhome;
10990: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10991: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10992:         } else { # load balancing routine for determining $unhome
10993:             my $loadm=10000000;
10994: 	    my %servers = &get_servers($udom,'library');
10995: 	    foreach my $tryserver (keys(%servers)) {
10996: 		my $answer=reply('load',$tryserver);
10997: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10998: 		    $loadm=$answer;
10999: 		    $unhome=$tryserver;
11000: 		}
11001: 	    }
11002:         }
11003:         if (($unhome eq '') || ($unhome eq 'no_host')) {
11004: 	    return 'error: unable to find a home server for '.$uname.
11005:                    ' in domain '.$udom;
11006:         }
11007:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
11008:                          &escape($upass),$unhome);
11009: 	unless ($reply eq 'ok') {
11010:             return 'error: '.$reply;
11011:         }   
11012:         $uhome=&homeserver($uname,$udom,'true');
11013:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
11014: 	    return 'error: unable verify users home machine.';
11015:         }
11016:     }   # End of creation of new user
11017: # ---------------------------------------------------------------------- Add ID
11018:     if ($uid) {
11019:        $uid=~tr/A-Z/a-z/;
11020:        my %uidhash=&idrget($udom,$uname);
11021:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
11022:          && (!$forceid)) {
11023: 	  unless ($uid eq $uidhash{$uname}) {
11024: 	      return 'error: user id "'.$uid.'" does not match '.
11025:                   'current user id "'.$uidhash{$uname}.'".';
11026:           }
11027:        } else {
11028: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
11029:        }
11030:     }
11031: # -------------------------------------------------------------- Add names, etc
11032:     my @tmp=&get('environment',
11033: 		   ['firstname','middlename','lastname','generation','id',
11034:                     'permanentemail','inststatus'],
11035: 		   $udom,$uname);
11036:     my (%names,%oldnames);
11037:     if ($tmp[0] =~ m/^error:.*/) { 
11038:         %names=(); 
11039:     } else {
11040:         %names = @tmp;
11041:         %oldnames = %names;
11042:     }
11043: #
11044: # If name, email and/or uid are blank (e.g., because an uploaded file
11045: # of users did not contain them), do not overwrite existing values
11046: # unless field is in $candelete array ref.  
11047: #
11048: 
11049:     my @fields = ('firstname','middlename','lastname','generation',
11050:                   'permanentemail','id');
11051:     my %newvalues;
11052:     if (ref($candelete) eq 'ARRAY') {
11053:         foreach my $field (@fields) {
11054:             if (grep(/^\Q$field\E$/,@{$candelete})) {
11055:                 if ($field eq 'firstname') {
11056:                     $names{$field} = $first;
11057:                 } elsif ($field eq 'middlename') {
11058:                     $names{$field} = $middle;
11059:                 } elsif ($field eq 'lastname') {
11060:                     $names{$field} = $last;
11061:                 } elsif ($field eq 'generation') { 
11062:                     $names{$field} = $gene;
11063:                 } elsif ($field eq 'permanentemail') {
11064:                     $names{$field} = $email;
11065:                 } elsif ($field eq 'id') {
11066:                     $names{$field}  = $uid;
11067:                 }
11068:             }
11069:         }
11070:     }
11071:     if ($first)  { $names{'firstname'}  = $first; }
11072:     if (defined($middle)) { $names{'middlename'} = $middle; }
11073:     if ($last)   { $names{'lastname'}   = $last; }
11074:     if (defined($gene))   { $names{'generation'} = $gene; }
11075:     if ($email) {
11076:        $email=~s/[^\w\@\.\-\,]//gs;
11077:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
11078:     }
11079:     if ($uid) { $names{'id'}  = $uid; }
11080:     if (defined($inststatus)) {
11081:         $names{'inststatus'} = '';
11082:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
11083:         if (ref($usertypes) eq 'HASH') {
11084:             my @okstatuses; 
11085:             foreach my $item (split(/:/,$inststatus)) {
11086:                 if (defined($usertypes->{$item})) {
11087:                     push(@okstatuses,$item);  
11088:                 }
11089:             }
11090:             if (@okstatuses) {
11091:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
11092:             }
11093:         }
11094:     }
11095:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
11096:                  $umode.', '.$first.', '.$middle.', '.
11097:                  $last.', '.$gene.', '.$email.', '.$inststatus;
11098:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
11099:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
11100:     } else {
11101:         $logmsg .= ' during self creation';
11102:     }
11103:     my $changed;
11104:     if ($newuser) {
11105:         $changed = 1;
11106:     } else {
11107:         foreach my $field (@fields) {
11108:             if ($names{$field} ne $oldnames{$field}) {
11109:                 $changed = 1;
11110:                 last;
11111:             }
11112:         }
11113:     }
11114:     unless ($changed) {
11115:         $logmsg = 'No changes in user information needed for: '.$logmsg;
11116:         &logthis($logmsg);
11117:         return 'ok';
11118:     }
11119:     my $reply = &put('environment', \%names, $udom,$uname);
11120:     if ($reply ne 'ok') { 
11121:         return 'error: '.$reply;
11122:     }
11123:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
11124:         &devalidate_cache_new('emailscache',$uname.':'.$udom);
11125:     }
11126:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
11127:     &devalidate_cache_new('namescache',$uname.':'.$udom);
11128:     $logmsg = 'Success modifying user '.$logmsg;
11129:     &logthis($logmsg);
11130:     return 'ok';
11131: }
11132: 
11133: # -------------------------------------------------------------- Modify student
11134: 
11135: sub modifystudent {
11136:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
11137:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
11138:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
11139:     if (!$cid) {
11140: 	unless ($cid=$env{'request.course.id'}) {
11141: 	    return 'not_in_class';
11142: 	}
11143:     }
11144: # --------------------------------------------------------------- Make the user
11145:     my $reply=&modifyuser
11146: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
11147:          $desiredhome,$email,$inststatus);
11148:     unless ($reply eq 'ok') { return $reply; }
11149:     # This will cause &modify_student_enrollment to get the uid from the
11150:     # student's environment
11151:     $uid = undef if (!$forceid);
11152:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
11153:                                         $gene,$usec,$end,$start,$type,$locktype,
11154:                                         $cid,$selfenroll,$context,$credits,$instsec);
11155:     return $reply;
11156: }
11157: 
11158: sub modify_student_enrollment {
11159:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
11160:         $locktype,$cid,$selfenroll,$context,$credits,$instsec,$othdomby,$requester) = @_;
11161:     my ($cdom,$cnum,$chome);
11162:     if (!$cid) {
11163: 	unless ($cid=$env{'request.course.id'}) {
11164: 	    return 'not_in_class';
11165: 	}
11166: 	$cdom=$env{'course.'.$cid.'.domain'};
11167: 	$cnum=$env{'course.'.$cid.'.num'};
11168:     } else {
11169: 	($cdom,$cnum)=split(/_/,$cid);
11170:     }
11171:     $chome=$env{'course.'.$cid.'.home'};
11172:     if (!$chome) {
11173: 	$chome=&homeserver($cnum,$cdom);
11174:     }
11175:     if (!$chome) { return 'unknown_course'; }
11176:     # Make sure the user exists
11177:     my $uhome=&homeserver($uname,$udom);
11178:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
11179: 	return 'error: no such user';
11180:     }
11181:     # Get student data if we were not given enough information
11182:     if (!defined($first)  || $first  eq '' || 
11183:         !defined($last)   || $last   eq '' || 
11184:         !defined($uid)    || $uid    eq '' || 
11185:         !defined($middle) || $middle eq '' || 
11186:         !defined($gene)   || $gene   eq '') {
11187:         # They did not supply us with enough data to enroll the student, so
11188:         # we need to pick up more information.
11189:         my %tmp = &get('environment',
11190:                        ['firstname','middlename','lastname', 'generation','id']
11191:                        ,$udom,$uname);
11192: 
11193:         #foreach my $key (keys(%tmp)) {
11194:         #    &logthis("key $key = ".$tmp{$key});
11195:         #}
11196:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
11197:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11198:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
11199:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
11200:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
11201:     }
11202:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
11203:     my $user = "$uname:$udom";
11204:     my %old_entry = &get('classlist',[$user],$cdom,$cnum);
11205:     my $reply=cput('classlist',
11206: 		   {$user => 
11207: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
11208: 		   $cdom,$cnum);
11209:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
11210:         &devalidate_getsection_cache($udom,$uname,$cid);
11211:     } else { 
11212: 	return 'error: '.$reply;
11213:     }
11214:     # Add student role to user
11215:     my $uurl='/'.$cid;
11216:     $uurl=~s/\_/\//g;
11217:     if ($usec) {
11218: 	$uurl.='/'.$usec;
11219:     }
11220:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11221:                              $selfenroll,$context,$othdomby,$requester);
11222:     if ($result ne 'ok') {
11223:         if ($old_entry{$user} ne '') {
11224:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11225:         } else {
11226:             $reply = &del('classlist',[$user],$cdom,$cnum);
11227:         }
11228:     }
11229:     return $result; 
11230: }
11231: 
11232: sub format_name {
11233:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11234:     my $name;
11235:     if ($first ne 'lastname') {
11236: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11237:     } else {
11238: 	if ($lastname=~/\S/) {
11239: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11240: 	    $name=~s/\s+,/,/;
11241: 	} else {
11242: 	    $name.= $firstname.' '.$middlename.' '.$generation;
11243: 	}
11244:     }
11245:     $name=~s/^\s+//;
11246:     $name=~s/\s+$//;
11247:     $name=~s/\s+/ /g;
11248:     return $name;
11249: }
11250: 
11251: # ------------------------------------------------- Write to course preferences
11252: 
11253: sub writecoursepref {
11254:     my ($courseid,%prefs)=@_;
11255:     $courseid=~s/^\///;
11256:     $courseid=~s/\_/\//g;
11257:     my ($cdomain,$cnum)=split(/\//,$courseid);
11258:     my $chome=homeserver($cnum,$cdomain);
11259:     if (($chome eq '') || ($chome eq 'no_host')) { 
11260: 	return 'error: no such course';
11261:     }
11262:     my $cstring='';
11263:     foreach my $pref (keys(%prefs)) {
11264: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
11265:     }
11266:     $cstring=~s/\&$//;
11267:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11268: }
11269: 
11270: # ---------------------------------------------------------- Make/modify course
11271: 
11272: sub createcourse {
11273:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
11274:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
11275:     $url=&declutter($url);
11276:     my $cid='';
11277:     if ($context eq 'requestcourses') {
11278:         my $can_create = 0;
11279:         my ($ownername,$ownerdom) = split(':',$course_owner);
11280:         if ($udom eq $ownerdom) {
11281:             my $reload;
11282:             if (($callercontext eq 'auto') &&
11283:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11284:                 $reload = 'reload';
11285:             }
11286:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
11287:                                   $context)) {
11288:                 $can_create = 1;
11289:             }
11290:         } else {
11291:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11292:                                            $category);
11293:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11294:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11295:                 if (@curr > 0) {
11296:                     my @options = qw(approval validate autolimit);
11297:                     my $optregex = join('|',@options);
11298:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11299:                         $can_create = 1;
11300:                     }
11301:                 }
11302:             }
11303:         }
11304:         if ($can_create) {
11305:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11306:                 unless (&allowed('ccc',$udom)) {
11307:                     return 'refused'; 
11308:                 }
11309:             }
11310:         } else {
11311:             return 'refused';
11312:         }
11313:     } elsif (!&allowed('ccc',$udom)) {
11314:         return 'refused';
11315:     }
11316: # --------------------------------------------------------------- Get Unique ID
11317:     my $uname;
11318:     if ($cnum =~ /^$match_courseid$/) {
11319:         my $chome=&homeserver($cnum,$udom,'true');
11320:         if (($chome eq '') || ($chome eq 'no_host')) {
11321:             $uname = $cnum;
11322:         } else {
11323:             $uname = &generate_coursenum($udom,$crstype);
11324:         }
11325:     } else {
11326:         $uname = &generate_coursenum($udom,$crstype);
11327:     }
11328:     return $uname if ($uname =~ /^error/);
11329: # -------------------------------------------------- Check supplied server name
11330:     if (!defined($course_server)) {
11331:         if (defined(&domain($udom,'primary'))) {
11332:             $course_server = &domain($udom,'primary');
11333:         } else {
11334:             $course_server = $env{'user.home'}; 
11335:         }
11336:     }
11337:     my %host_servers =
11338:         &get_servers($udom,'library');
11339:     unless ($host_servers{$course_server}) {
11340:         return 'error: invalid home server for course: '.$course_server;
11341:     }
11342: # ------------------------------------------------------------- Make the course
11343:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
11344:                       $course_server);
11345:     unless ($reply eq 'ok') { return 'error: '.$reply; }
11346:     my $uhome=&homeserver($uname,$udom,'true');
11347:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
11348: 	return 'error: no such course';
11349:     }
11350: # ----------------------------------------------------------------- Course made
11351: # log existence
11352:     my $now = time;
11353:     my $newcourse = {
11354:                     $udom.'_'.$uname => {
11355:                                      description => $description,
11356:                                      inst_code   => $inst_code,
11357:                                      owner       => $course_owner,
11358:                                      type        => $crstype,
11359:                                      creator     => $env{'user.name'}.':'.
11360:                                                     $env{'user.domain'},
11361:                                      created     => $now,
11362:                                      context     => $context,
11363:                                                 },
11364:                     };
11365:     &courseidput($udom,$newcourse,$uhome,'notime');
11366: # set toplevel url
11367:     my $topurl=$url;
11368:     unless ($nonstandard) {
11369: # ------------------------------------------ For standard courses, make top url
11370:         my $mapurl=&clutter($url);
11371:         if ($mapurl eq '/res/') { $mapurl=''; }
11372:         $env{'form.initmap'}=(<<ENDINITMAP);
11373: <map>
11374: <resource id="1" type="start"></resource>
11375: <resource id="2" src="$mapurl"></resource>
11376: <resource id="3" type="finish"></resource>
11377: <link index="1" from="1" to="2"></link>
11378: <link index="2" from="2" to="3"></link>
11379: </map>
11380: ENDINITMAP
11381:         $topurl=&declutter(
11382:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
11383:                           );
11384:     }
11385: # ----------------------------------------------------------- Write preferences
11386:     &writecoursepref($udom.'_'.$uname,
11387:                      ('description'              => $description,
11388:                       'url'                      => $topurl,
11389:                       'internal.creator'         => $env{'user.name'}.':'.
11390:                                                     $env{'user.domain'},
11391:                       'internal.created'         => $now,
11392:                       'internal.creationcontext' => $context)
11393:                     );
11394:     return '/'.$udom.'/'.$uname;
11395: }
11396: 
11397: # ------------------------------------------------------------------- Create ID
11398: sub generate_coursenum {
11399:     my ($udom,$crstype) = @_;
11400:     my $domdesc = &domain($udom);
11401:     return 'error: invalid domain' if ($domdesc eq '');
11402:     my $first;
11403:     if ($crstype eq 'Community') {
11404:         $first = '0';
11405:     } else {
11406:         $first = int(1+rand(9)); 
11407:     } 
11408:     my $uname=$first.
11409:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11410:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
11411:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11412: # ----------------------------------------------- Make sure that does not exist
11413:     my $uhome=&homeserver($uname,$udom,'true');
11414:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
11415:         if ($crstype eq 'Community') {
11416:             $first = '0';
11417:         } else {
11418:             $first = int(1+rand(9));
11419:         }
11420:         $uname=$first.
11421:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11422:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
11423:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11424:         $uhome=&homeserver($uname,$udom,'true');
11425:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
11426:             return 'error: unable to generate unique course-ID';
11427:         }
11428:     }
11429:     return $uname;
11430: }
11431: 
11432: sub is_course {
11433:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
11434:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
11435: 
11436:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11437:     my $uhome=&homeserver($cnum,$cdom);
11438:     my $iscourse;
11439:     if (grep { $_ eq $uhome } current_machine_ids()) {
11440:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11441:     } else {
11442:         my $hashid = $cdom.':'.$cnum;
11443:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11444:         unless (defined($cached)) {
11445:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11446:                                         $cnum,undef,undef,'.');
11447:             $iscourse = 0;
11448:             if (exists($courses{$cdom.'_'.$cnum})) {
11449:                 $iscourse = 1;
11450:             }
11451:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
11452:         }
11453:     }
11454:     return unless ($iscourse);
11455:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
11456: }
11457: 
11458: sub store_userdata {
11459:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
11460:     my $result;
11461:     if ($datakey ne '') {
11462:         if (ref($storehash) eq 'HASH') {
11463:             if ($udom eq '' || $uname eq '') {
11464:                 $udom = $env{'user.domain'};
11465:                 $uname = $env{'user.name'};
11466:             }
11467:             my $uhome=&homeserver($uname,$udom);
11468:             if (($uhome eq '') || ($uhome eq 'no_host')) {
11469:                 $result = 'error: no_host';
11470:             } else {
11471:                 $storehash->{'ip'} = &get_requestor_ip();
11472:                 $storehash->{'host'} = $perlvar{'lonHostID'};
11473: 
11474:                 my $namevalue='';
11475:                 foreach my $key (keys(%{$storehash})) {
11476:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11477:                 }
11478:                 $namevalue=~s/\&$//;
11479:                 unless ($namespace eq 'courserequests') {
11480:                     $datakey = &escape($datakey);
11481:                 }
11482:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
11483:                                   $namevalue,$uhome);
11484:             }
11485:         } else {
11486:             $result = 'error: data to store was not a hash reference'; 
11487:         }
11488:     } else {
11489:         $result= 'error: invalid requestkey'; 
11490:     }
11491:     return $result;
11492: }
11493: 
11494: # ---------------------------------------------------------- Assign Custom Role
11495: 
11496: sub assigncustomrole {
11497:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,
11498:         $selfenroll,$context,$othdomby,$requester)=@_;
11499:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
11500:                        $end,$start,$deleteflag,$selfenroll,$context,$othdomby,
11501:                        $requester);
11502: }
11503: 
11504: # ----------------------------------------------------------------- Revoke Role
11505: 
11506: sub revokerole {
11507:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
11508:     my $now=time;
11509:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
11510: }
11511: 
11512: # ---------------------------------------------------------- Revoke Custom Role
11513: 
11514: sub revokecustomrole {
11515:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
11516:     my $now=time;
11517:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
11518:            $deleteflag,$selfenroll,$context);
11519: }
11520: 
11521: # ------------------------------------------------------------ Disk usage
11522: sub diskusage {
11523:     my ($udom,$uname,$directorypath,$getpropath)=@_;
11524:     $directorypath =~ s/\/$//;
11525:     my $listing=&reply('du2:'.&escape($directorypath).':'
11526:                        .&escape($getpropath).':'.&escape($uname).':'
11527:                        .&escape($udom),homeserver($uname,$udom));
11528:     if ($listing eq 'unknown_cmd') {
11529:         if ($getpropath) {
11530:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
11531:         }
11532:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11533:     }
11534:     return $listing;
11535: }
11536: 
11537: sub is_locked {
11538:     my ($file_name, $domain, $user, $which) = @_;
11539:     my @check;
11540:     my $is_locked;
11541:     push (@check,$file_name);
11542:     my %locked = &get('file_permissions',\@check,
11543: 		      $env{'user.domain'},$env{'user.name'});
11544:     my ($tmp)=keys(%locked);
11545:     if ($tmp=~/^error:/) { undef(%locked); }
11546:     
11547:     if (ref($locked{$file_name}) eq 'ARRAY') {
11548:         $is_locked = 'false';
11549:         foreach my $entry (@{$locked{$file_name}}) {
11550:            if (ref($entry) eq 'ARRAY') {
11551:                $is_locked = 'true';
11552:                if (ref($which) eq 'ARRAY') {
11553:                    push(@{$which},$entry);
11554:                } else {
11555:                    last;
11556:                }
11557:            }
11558:        }
11559:     } else {
11560:         $is_locked = 'false';
11561:     }
11562:     return $is_locked;
11563: }
11564: 
11565: sub declutter_portfile {
11566:     my ($file) = @_;
11567:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11568:     return $file;
11569: }
11570: 
11571: # ------------------------------------------------------------- Mark as Read Only
11572: 
11573: sub mark_as_readonly {
11574:     my ($domain,$user,$files,$what) = @_;
11575:     my %current_permissions = &dump('file_permissions',$domain,$user);
11576:     my ($tmp)=keys(%current_permissions);
11577:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11578:     foreach my $file (@{$files}) {
11579: 	$file = &declutter_portfile($file);
11580:         push(@{$current_permissions{$file}},$what);
11581:     }
11582:     &put('file_permissions',\%current_permissions,$domain,$user);
11583:     return;
11584: }
11585: 
11586: # ------------------------------------------------------------Save Selected Files
11587: 
11588: sub save_selected_files {
11589:     my ($user, $path, @files) = @_;
11590:     my $filename = $user."savedfiles";
11591:     my @other_files = &files_not_in_path($user, $path);
11592:     open (OUT,'>',LONCAPA::tempdir().$filename);
11593:     foreach my $file (@files) {
11594:         print (OUT $env{'form.currentpath'}.$file."\n");
11595:     }
11596:     foreach my $file (@other_files) {
11597:         print (OUT $file."\n");
11598:     }
11599:     close (OUT);
11600:     return 'ok';
11601: }
11602: 
11603: sub clear_selected_files {
11604:     my ($user) = @_;
11605:     my $filename = $user."savedfiles";
11606:     open (OUT,'>',LONCAPA::tempdir().$filename);
11607:     print (OUT undef);
11608:     close (OUT);
11609:     return ("ok");    
11610: }
11611: 
11612: sub files_in_path {
11613:     my ($user, $path) = @_;
11614:     my $filename = $user."savedfiles";
11615:     my %return_files;
11616:     open (IN,'<',LONCAPA::tempdir().$filename);
11617:     while (my $line_in = <IN>) {
11618:         chomp ($line_in);
11619:         my @paths_and_file = split (m!/!, $line_in);
11620:         my $file_part = pop (@paths_and_file);
11621:         my $path_part = join ('/', @paths_and_file);
11622:         $path_part.='/';
11623:         my $path_and_file = $path_part.$file_part;
11624:         if ($path_part eq $path) {
11625:             $return_files{$file_part}= 'selected';
11626:         }
11627:     }
11628:     close (IN);
11629:     return (\%return_files);
11630: }
11631: 
11632: # called in portfolio select mode, to show files selected NOT in current directory
11633: sub files_not_in_path {
11634:     my ($user, $path) = @_;
11635:     my $filename = $user."savedfiles";
11636:     my @return_files;
11637:     my $path_part;
11638:     open(IN, '<',LONCAPA::tempdir().$filename);
11639:     while (my $line = <IN>) {
11640:         #ok, I know it's clunky, but I want it to work
11641:         my @paths_and_file = split(m|/|, $line);
11642:         my $file_part = pop(@paths_and_file);
11643:         chomp($file_part);
11644:         my $path_part = join('/', @paths_and_file);
11645:         $path_part .= '/';
11646:         my $path_and_file = $path_part.$file_part;
11647:         if ($path_part ne $path) {
11648:             push(@return_files, ($path_and_file));
11649:         }
11650:     }
11651:     close(OUT);
11652:     return (@return_files);
11653: }
11654: 
11655: #------------------------------Submitted/Handedback Portfolio Files Versioning
11656:  
11657: sub portfiles_versioning {
11658:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11659:     my $portfolio_root = '/userfiles/portfolio';
11660:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11661:     foreach my $file (@{$portfiles}) {
11662:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11663:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11664:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11665:         my $getpropath = 1;
11666:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11667:                                              $stu_name,$getpropath);
11668:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11669:         my $new_answer = 
11670:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11671:         if ($new_answer ne 'problem getting file') {
11672:             push(@{$versioned_portfiles}, $directory.$new_answer);
11673:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11674:                               [$symb,$env{'request.course.id'},'graded']);
11675:         }
11676:     }
11677: }
11678: 
11679: sub get_next_version {
11680:     my ($answer_name, $answer_ext, $dir_list) = @_;
11681:     my $version;
11682:     if (ref($dir_list) eq 'ARRAY') {
11683:         foreach my $row (@{$dir_list}) {
11684:             my ($file) = split(/\&/,$row,2);
11685:             my ($file_name,$file_version,$file_ext) =
11686:                 &file_name_version_ext($file);
11687:             if (($file_name eq $answer_name) &&
11688:                 ($file_ext eq $answer_ext)) {
11689:                      # gets here if filename and extension match,
11690:                      # regardless of version
11691:                 if ($file_version ne '') {
11692:                     # a versioned file is found  so save it for later
11693:                     if ($file_version > $version) {
11694:                         $version = $file_version;
11695:                     }
11696:                 }
11697:             }
11698:         }
11699:     }
11700:     $version ++;
11701:     return($version);
11702: }
11703: 
11704: sub version_selected_portfile {
11705:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11706:     my ($answer_name,$answer_ver,$answer_ext) =
11707:         &file_name_version_ext($file_name);
11708:     my $new_answer;
11709:     $env{'form.copy'} =
11710:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11711:     if($env{'form.copy'} eq '-1') {
11712:         $new_answer = 'problem getting file';
11713:     } else {
11714:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11715:         my $copy_result = 
11716:             &finishuserfileupload($stu_name,$domain,'copy',
11717:                                   '/portfolio'.$directory.$new_answer);
11718:     }
11719:     undef($env{'form.copy'});
11720:     return ($new_answer);
11721: }
11722: 
11723: sub file_name_version_ext {
11724:     my ($file)=@_;
11725:     my @file_parts = split(/\./, $file);
11726:     my ($name,$version,$ext);
11727:     if (@file_parts > 1) {
11728:         $ext=pop(@file_parts);
11729:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11730:             $version=pop(@file_parts);
11731:         }
11732:         $name=join('.',@file_parts);
11733:     } else {
11734:         $name=join('.',@file_parts);
11735:     }
11736:     return($name,$version,$ext);
11737: }
11738: 
11739: #----------------------------------------------Get portfolio file permissions
11740: 
11741: sub get_portfile_permissions {
11742:     my ($domain,$user) = @_;
11743:     my %current_permissions = &dump('file_permissions',$domain,$user);
11744:     my ($tmp)=keys(%current_permissions);
11745:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11746:     return \%current_permissions;
11747: }
11748: 
11749: #---------------------------------------------Get portfolio file access controls
11750: 
11751: sub get_access_controls {
11752:     my ($current_permissions,$group,$file) = @_;
11753:     my %access;
11754:     my $real_file = $file;
11755:     $file =~ s/\.meta$//;
11756:     if (defined($file)) {
11757:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11758:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11759:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11760:             }
11761:         }
11762:     } else {
11763:         foreach my $key (keys(%{$current_permissions})) {
11764:             if ($key =~ /\0accesscontrol$/) {
11765:                 if (defined($group)) {
11766:                     if ($key !~ m-^\Q$group\E/-) {
11767:                         next;
11768:                     }
11769:                 }
11770:                 my ($fullpath) = split(/\0/,$key);
11771:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11772:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11773:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11774:                     }
11775:                 }
11776:             }
11777:         }
11778:     }
11779:     return %access;
11780: }
11781: 
11782: sub modify_access_controls {
11783:     my ($file_name,$changes,$domain,$user)=@_;
11784:     my ($outcome,$deloutcome);
11785:     my %store_permissions;
11786:     my %new_values;
11787:     my %new_control;
11788:     my %translation;
11789:     my @deletions = ();
11790:     my $now = time;
11791:     if (exists($$changes{'activate'})) {
11792:         if (ref($$changes{'activate'}) eq 'HASH') {
11793:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11794:             my $numnew = scalar(@newitems);
11795:             for (my $i=0; $i<$numnew; $i++) {
11796:                 my $newkey = $newitems[$i];
11797:                 my $newid = &Apache::loncommon::get_cgi_id();
11798:                 if ($newkey =~ /^\d+:/) { 
11799:                     $newkey =~ s/^(\d+)/$newid/;
11800:                     $translation{$1} = $newid;
11801:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11802:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11803:                     $translation{$1} = $newid;
11804:                 }
11805:                 $new_values{$file_name."\0".$newkey} = 
11806:                                           $$changes{'activate'}{$newitems[$i]};
11807:                 $new_control{$newkey} = $now;
11808:             }
11809:         }
11810:     }
11811:     my %todelete;
11812:     my %changed_items;
11813:     foreach my $action ('delete','update') {
11814:         if (exists($$changes{$action})) {
11815:             if (ref($$changes{$action}) eq 'HASH') {
11816:                 foreach my $key (keys(%{$$changes{$action}})) {
11817:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11818:                     if ($action eq 'delete') { 
11819:                         $todelete{$itemnum} = 1;
11820:                     } else {
11821:                         $changed_items{$itemnum} = $key;
11822:                     }
11823:                 }
11824:             }
11825:         }
11826:     }
11827:     # get lock on access controls for file.
11828:     my $lockhash = {
11829:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11830:                                                        ':'.$env{'user.domain'},
11831:                    }; 
11832:     my $tries = 0;
11833:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11834:    
11835:     while (($gotlock ne 'ok') && $tries < 10) {
11836:         $tries ++;
11837:         sleep(0.1);
11838:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11839:     }
11840:     if ($gotlock eq 'ok') {
11841:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11842:         my ($tmp)=keys(%curr_permissions);
11843:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11844:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11845:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11846:             if (ref($curr_controls) eq 'HASH') {
11847:                 foreach my $control_item (keys(%{$curr_controls})) {
11848:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11849:                     if (defined($todelete{$itemnum})) {
11850:                         push(@deletions,$file_name."\0".$control_item);
11851:                     } else {
11852:                         if (defined($changed_items{$itemnum})) {
11853:                             $new_control{$changed_items{$itemnum}} = $now;
11854:                             push(@deletions,$file_name."\0".$control_item);
11855:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11856:                         } else {
11857:                             $new_control{$control_item} = $$curr_controls{$control_item};
11858:                         }
11859:                     }
11860:                 }
11861:             }
11862:         }
11863:         my ($group);
11864:         if (&is_course($domain,$user)) {
11865:             ($group,my $file) = split(/\//,$file_name,2);
11866:         }
11867:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11868:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11869:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11870:         #  remove lock
11871:         my @del_lock = ($file_name."\0".'locked_access_records');
11872:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11873:         my $sqlresult =
11874:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11875:                                     $group);
11876:     } else {
11877:         $outcome = "error: could not obtain lockfile\n";  
11878:     }
11879:     return ($outcome,$deloutcome,\%new_values,\%translation);
11880: }
11881: 
11882: sub make_public_indefinitely {
11883:     my (@requrl) = @_;
11884:     return &automated_portfile_access('public',\@requrl);
11885: }
11886: 
11887: sub automated_portfile_access {
11888:     my ($accesstype,$addsref,$delsref,$info) = @_;
11889:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11890:         return 'invalid';
11891:     }
11892:     my %urls;
11893:     if (ref($addsref) eq 'ARRAY') {
11894:         foreach my $requrl (@{$addsref}) {
11895:             if (&is_portfolio_url($requrl)) {
11896:                 unless (exists($urls{$requrl})) {
11897:                     $urls{$requrl} = 'add';
11898:                 }
11899:             }
11900:         }
11901:     }
11902:     if (ref($delsref) eq 'ARRAY') {
11903:         foreach my $requrl (@{$delsref}) { 
11904:             if (&is_portfolio_url($requrl)) {
11905:                 unless (exists($urls{$requrl})) {
11906:                     $urls{$requrl} = 'delete'; 
11907:                 }
11908:             }
11909:         }
11910:     }
11911:     unless (keys(%urls)) {
11912:         return 'invalid';
11913:     }
11914:     my $ip;
11915:     if ($accesstype eq 'ip') {
11916:         if (ref($info) eq 'HASH') {
11917:             if ($info->{'ip'} ne '') {
11918:                 $ip = $info->{'ip'};
11919:             }
11920:         }
11921:         if ($ip eq '') {
11922:             return 'invalid';
11923:         }
11924:     }
11925:     my $errors;
11926:     my $now = time;
11927:     my %current_perms;
11928:     foreach my $requrl (sort(keys(%urls))) {
11929:         my $action;
11930:         if ($urls{$requrl} eq 'add') {
11931:             $action = 'activate';
11932:         } else {
11933:             $action = 'none';
11934:         }
11935:         my $aclnum = 0;
11936:         my (undef,$udom,$unum,$file_name,$group) =
11937:             &parse_portfolio_url($requrl);
11938:         unless (exists($current_perms{$unum.':'.$udom})) {
11939:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11940:         }
11941:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11942:                                                    $group,$file_name);
11943:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11944:             my ($num,$scope,$end,$start) = 
11945:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11946:             if ($scope eq $accesstype) {
11947:                 if (($start <= $now) && ($end == 0)) {
11948:                     if ($accesstype eq 'ip') {
11949:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11950:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11951:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11952:                                     if ($urls{$requrl} eq 'add') {
11953:                                         $action = 'none';
11954:                                         last;
11955:                                     } else {
11956:                                         $action = 'delete';
11957:                                         $aclnum = $num;
11958:                                         last;
11959:                                     }
11960:                                 }
11961:                             }
11962:                         }
11963:                     } elsif ($accesstype eq 'public') {
11964:                         if ($urls{$requrl} eq 'add') {
11965:                             $action = 'none';
11966:                             last;
11967:                         } else {
11968:                             $action = 'delete';
11969:                             $aclnum = $num;
11970:                             last;
11971:                         }
11972:                     }
11973:                 } elsif ($accesstype eq 'public') {
11974:                     $action = 'update';
11975:                     $aclnum = $num;
11976:                     last;
11977:                 }
11978:             }
11979:         }
11980:         if ($action eq 'none') {
11981:             next;
11982:         } else {
11983:             my %changes;
11984:             my $newend = 0;
11985:             my $newstart = $now;
11986:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11987:             $changes{$action}{$newkey} = {
11988:                 type => $accesstype,
11989:                 time => {
11990:                     start => $newstart,
11991:                     end   => $newend,
11992:                 },
11993:             };
11994:             if ($accesstype eq 'ip') {
11995:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11996:             }
11997:             my ($outcome,$deloutcome,$new_values,$translation) =
11998:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11999:             unless ($outcome eq 'ok') {
12000:                 $errors .= $outcome.' ';
12001:             }
12002:         }
12003:     }
12004:     if ($errors) {
12005:         $errors =~ s/\s$//;
12006:         return $errors;
12007:     } else {
12008:         return 'ok';
12009:     }
12010: }
12011: 
12012: #------------------------------------------------------Get Marked as Read Only
12013: 
12014: sub get_marked_as_readonly {
12015:     my ($domain,$user,$what,$group) = @_;
12016:     my $current_permissions = &get_portfile_permissions($domain,$user);
12017:     my @readonly_files;
12018:     my $cmp1=$what;
12019:     if (ref($what)) { $cmp1=join('',@{$what}) };
12020:     while (my ($file_name,$value) = each(%{$current_permissions})) {
12021:         if (defined($group)) {
12022:             if ($file_name !~ m-^\Q$group\E/-) {
12023:                 next;
12024:             }
12025:         }
12026:         if (ref($value) eq "ARRAY"){
12027:             foreach my $stored_what (@{$value}) {
12028:                 my $cmp2=$stored_what;
12029:                 if (ref($stored_what) eq 'ARRAY') {
12030:                     $cmp2=join('',@{$stored_what});
12031:                 }
12032:                 if ($cmp1 eq $cmp2) {
12033:                     push(@readonly_files, $file_name);
12034:                     last;
12035:                 } elsif (!defined($what)) {
12036:                     push(@readonly_files, $file_name);
12037:                     last;
12038:                 }
12039:             }
12040:         }
12041:     }
12042:     return @readonly_files;
12043: }
12044: #-----------------------------------------------------------Get Marked as Read Only Hash
12045: 
12046: sub get_marked_as_readonly_hash {
12047:     my ($current_permissions,$group,$what) = @_;
12048:     my %readonly_files;
12049:     while (my ($file_name,$value) = each(%{$current_permissions})) {
12050:         if (defined($group)) {
12051:             if ($file_name !~ m-^\Q$group\E/-) {
12052:                 next;
12053:             }
12054:         }
12055:         if (ref($value) eq "ARRAY"){
12056:             foreach my $stored_what (@{$value}) {
12057:                 if (ref($stored_what) eq 'ARRAY') {
12058:                     foreach my $lock_descriptor(@{$stored_what}) {
12059:                         if ($lock_descriptor eq 'graded') {
12060:                             $readonly_files{$file_name} = 'graded';
12061:                         } elsif ($lock_descriptor eq 'handback') {
12062:                             $readonly_files{$file_name} = 'handback';
12063:                         } else {
12064:                             if (!exists($readonly_files{$file_name})) {
12065:                                 $readonly_files{$file_name} = 'locked';
12066:                             }
12067:                         }
12068:                     }
12069:                 } 
12070:             }
12071:         } 
12072:     }
12073:     return %readonly_files;
12074: }
12075: # ------------------------------------------------------------ Unmark as Read Only
12076: 
12077: sub unmark_as_readonly {
12078:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
12079:     # for portfolio submissions, $what contains [$symb,$crsid] 
12080:     my ($domain,$user,$what,$file_name,$group) = @_;
12081:     $file_name = &declutter_portfile($file_name);
12082:     my $symb_crs = $what;
12083:     if (ref($what)) { $symb_crs=join('',@$what); }
12084:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
12085:     my ($tmp)=keys(%current_permissions);
12086:     if ($tmp=~/^error:/) { undef(%current_permissions); }
12087:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
12088:     foreach my $file (@readonly_files) {
12089: 	my $clean_file = &declutter_portfile($file);
12090: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
12091: 	my $current_locks = $current_permissions{$file};
12092:         my @new_locks;
12093:         my @del_keys;
12094:         if (ref($current_locks) eq "ARRAY"){
12095:             foreach my $locker (@{$current_locks}) {
12096:                 my $compare=$locker;
12097:                 if (ref($locker) eq 'ARRAY') {
12098:                     $compare=join('',@{$locker});
12099:                     if ($compare ne $symb_crs) {
12100:                         push(@new_locks, $locker);
12101:                     }
12102:                 }
12103:             }
12104:             if (scalar(@new_locks) > 0) {
12105:                 $current_permissions{$file} = \@new_locks;
12106:             } else {
12107:                 push(@del_keys, $file);
12108:                 &del('file_permissions',\@del_keys, $domain, $user);
12109:                 delete($current_permissions{$file});
12110:             }
12111:         }
12112:     }
12113:     &put('file_permissions',\%current_permissions,$domain,$user);
12114:     return;
12115: }
12116: 
12117: # ------------------------------------------------------------ Directory lister
12118: 
12119: sub dirlist {
12120:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
12121:     $uri=~s/^\///;
12122:     $uri=~s/\/$//;
12123:     my ($udom, $uname);
12124:     if ($getuserdir) {
12125:         $udom = $userdomain;
12126:         $uname = $username;
12127:     } else {
12128:         (undef,$udom,$uname)=split(/\//,$uri);
12129:         if(defined($userdomain)) {
12130:             $udom = $userdomain;
12131:         }
12132:         if(defined($username)) {
12133:             $uname = $username;
12134:         }
12135:     }
12136:     my ($dirRoot,$listing,@listing_results);
12137: 
12138:     $dirRoot = $perlvar{'lonDocRoot'};
12139:     if (defined($getpropath)) {
12140:         $dirRoot = &propath($udom,$uname);
12141:         $dirRoot =~ s/\/$//;
12142:     } elsif (defined($getuserdir)) {
12143:         my $subdir=$uname.'__';
12144:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
12145:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
12146:                    ."/$udom/$subdir/$uname";
12147:     } elsif (defined($alternateRoot)) {
12148:         $dirRoot = $alternateRoot;
12149:     }
12150: 
12151:     if($udom) {
12152:         if($uname) {
12153:             my $uhome = &homeserver($uname,$udom);
12154:             if ($uhome eq 'no_host') {
12155:                 return ([],'no_host');
12156:             }
12157:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
12158:                               .$getuserdir.':'.&escape($dirRoot)
12159:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
12160:             if ($listing eq 'unknown_cmd') {
12161:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
12162:             } else {
12163:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
12164:             }
12165:             if ($listing eq 'unknown_cmd') {
12166:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
12167:                 @listing_results = split(/:/,$listing);
12168:             } else {
12169:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
12170:             }
12171:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
12172:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
12173:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
12174:                 return ([],$listing);
12175:             } else {
12176:                 return (\@listing_results);
12177:             }
12178:         } elsif(!$alternateRoot) {
12179:             my (%allusers,%listerror);
12180: 	    my %servers = &get_servers($udom,'library');
12181:  	    foreach my $tryserver (keys(%servers)) {
12182:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
12183:                                   &escape($udom),$tryserver);
12184:                 if ($listing eq 'unknown_cmd') {
12185: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
12186: 				      $udom, $tryserver);
12187:                 } else {
12188:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
12189:                 }
12190: 		if ($listing eq 'unknown_cmd') {
12191: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
12192: 				      $udom, $tryserver);
12193: 		    @listing_results = split(/:/,$listing);
12194: 		} else {
12195: 		    @listing_results =
12196: 			map { &unescape($_); } split(/:/,$listing);
12197: 		}
12198:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
12199:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
12200:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
12201:                     $listerror{$tryserver} = $listing;
12202:                 } else {
12203: 		    foreach my $line (@listing_results) {
12204: 			my ($entry) = split(/&/,$line,2);
12205: 			$allusers{$entry} = 1;
12206: 		    }
12207: 		}
12208:             }
12209:             my @alluserslist=();
12210:             foreach my $user (sort(keys(%allusers))) {
12211:                 push(@alluserslist,$user.'&user');
12212:             }
12213: 
12214:             if (!%listerror) {
12215:                 # no errors
12216:                 return (\@alluserslist);
12217:             } elsif (scalar(keys(%servers)) == 1) {
12218:                 # one library server, one error 
12219:                 my ($key) = keys(%listerror);
12220:                 return (\@alluserslist, $listerror{$key});
12221:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
12222:                 # con_lost indicates that we might miss data from at least one
12223:                 # library server
12224:                 return (\@alluserslist, 'con_lost');
12225:             } else {
12226:                 # multiple library servers and no con_lost -> data should be
12227:                 # complete. 
12228:                 return (\@alluserslist);
12229:             }
12230: 
12231:         } else {
12232:             return ([],'missing username');
12233:         }
12234:     } elsif(!defined($getpropath)) {
12235:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
12236:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
12237:         return (\@all_domains);
12238:     } else {
12239:         return ([],'missing domain');
12240:     }
12241: }
12242: 
12243: # --------------------------------------------- GetFileTimestamp
12244: # This function utilizes dirlist and returns the date stamp for
12245: # when it was last modified.  It will also return an error of -1
12246: # if an error occurs
12247: 
12248: sub GetFileTimestamp {
12249:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
12250:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
12251:     $studentName   = &LONCAPA::clean_username($studentName);
12252:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
12253:                                     undef,$getuserdir);
12254:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12255:         return -1;
12256:     }
12257:     if (ref($fileref) eq 'ARRAY') {
12258:         my @stats = split('&',$fileref->[0]);
12259:         # @stats contains first the filename, then the stat output
12260:         return $stats[10]; # so this is 10 instead of 9.
12261:     } else {
12262:         return -1;
12263:     }
12264: }
12265: 
12266: sub stat_file {
12267:     my ($uri) = @_;
12268:     $uri = &clutter_with_no_wrapper($uri);
12269: 
12270:     my ($udom,$uname,$file);
12271:     if ($uri =~ m-^/(uploaded|editupload)/-) {
12272: 	($udom,$uname,$file) =
12273: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
12274: 	$file = 'userfiles/'.$file;
12275:     }
12276:     if ($uri =~ m-^/res/-) {
12277: 	($udom,$uname) = 
12278: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
12279: 	$file = $uri;
12280:     }
12281: 
12282:     if (!$udom || !$uname || !$file) {
12283: 	# unable to handle the uri
12284: 	return ();
12285:     }
12286:     my $getpropath;
12287:     if ($file =~ /^userfiles\//) {
12288:         $getpropath = 1;
12289:     }
12290:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
12291:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12292:         return ();
12293:     } else {
12294:         if (ref($listref) eq 'ARRAY') {
12295:             my @stats = split('&',$listref->[0]);
12296: 	    shift(@stats); #filename is first
12297: 	    return @stats;
12298:         }
12299:     }
12300:     return ();
12301: }
12302: 
12303: # --------------------------------------------------------- recursedirs
12304: # Recursive function to traverse either a specific user's Authoring Space
12305: # or corresponding Published Resource Space, and populate the hash ref:
12306: # $dirhashref with URLs of all directories, and if $filehashref hash
12307: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
12308: # or .rights files in resource space, and .meta, .save, .log, .bak and
12309: # .rights files in Authoring Space.
12310: #
12311: # Inputs:
12312: #
12313: # $is_home - true if current server is home server for user's space
12314: # $recurse - if true will also traverse subdirectories recursively
12315: # $include - reference to hash containing allowed file extensions.  If provided,
12316: #             files which do not have a matching extension will be ignored.
12317: # $exclude - reference to hash containing excluded file extensions.  If provided,
12318: #             files which have a matching extension will be ignored.
12319: # $nonemptydir - if true, will only populate $fileshashref hash entry for a particular
12320: #             directory with first file found (with acceptable extension).
12321: # $addtopdir - if true, set $dirhashref->{'/'} = 1 
12322: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
12323: # $relpath - Current path (relative to top level).
12324: # $dirhashref - reference to hash to populate with URLs of directories (Required)
12325: # $filehashref - reference to hash to populate with URLs of files (Optional)
12326: #
12327: # Returns: nothing
12328: #
12329: # Side Effects: populates $dirhashref, and $filehashref (if provided).
12330: #
12331: # Currently used by interface/londocs.pm to create linked select boxes for
12332: # directory and filename to import a Course "Author" resource into a course, and
12333: # also to create linked select boxes for Authoring Space and Directory to choose
12334: # save location for creation of a new "standard" problem from the Course Editor.
12335: #
12336: 
12337: sub recursedirs {
12338:     my ($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$relpath,$dirhashref,$filehashref) = @_;
12339:     return unless (ref($dirhashref) eq 'HASH');
12340:     my $docroot = $perlvar{'lonDocRoot'};
12341:     my $currpath = $docroot.$toppath;
12342:     if ($relpath ne '') {
12343:         $currpath .= "/$relpath";
12344:     }
12345:     my ($savefile,$checkinc,$checkexc);
12346:     if (ref($filehashref)) {
12347:         $savefile = 1;
12348:     }
12349:     if (ref($include) eq 'HASH') {
12350:         $checkinc = 1;
12351:     }
12352:     if (ref($exclude) eq 'HASH') {
12353:         $checkexc = 1;
12354:     }
12355:     if ($is_home) {
12356:         if ((-e $currpath) && (opendir(my $dirh,$currpath))) {
12357:             my $filecount = 0;
12358:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
12359:                 next if ($item eq '');
12360:                 if (-d "$currpath/$item") {
12361:                     my $newpath;
12362:                     if ($relpath ne '') {
12363:                         $newpath = "$relpath/$item";
12364:                     } else {
12365:                         $newpath = $item;
12366:                     }
12367:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
12368:                     if ($recurse) {
12369:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
12370:                     }
12371:                 } elsif (($savefile) || ($relpath eq '')) {
12372:                     next if ($nonemptydir && $filecount);
12373:                     if ($checkinc || $checkexc) {
12374:                         my ($extension) = ($item =~ /\.(\w+)$/);
12375:                         if ($checkinc) {
12376:                             next unless ($extension && $include->{$extension});
12377:                         }
12378:                         if ($checkexc) {
12379:                             next if ($extension && $exclude->{$extension});
12380:                         }
12381:                     }
12382:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12383:                         $dirhashref->{'/'} = 1;
12384:                     }
12385:                     if ($savefile) {
12386:                         if ($relpath eq '') {
12387:                             $filehashref->{'/'}{$item} = 1;
12388:                         } else {
12389:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
12390:                         }
12391:                     }
12392:                     $filecount ++;
12393:                 }
12394:             }
12395:             closedir($dirh);
12396:         }
12397:     } else {
12398:         my ($dirlistref,$listerror) =
12399:             &dirlist($toppath.$relpath);
12400:         my @dir_lines;
12401:         my $dirptr=16384;
12402:         if (ref($dirlistref) eq 'ARRAY') {
12403:             my $filecount = 0;
12404:             foreach my $dir_line (sort
12405:                               {
12406:                                   my ($afile)=split('&',$a,2);
12407:                                   my ($bfile)=split('&',$b,2);
12408:                                   return (lc($afile) cmp lc($bfile));
12409:                               } (@{$dirlistref})) {
12410:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
12411:                     split(/\&/,$dir_line,16);
12412:                 $item =~ s/\s+$//;
12413:                 next if (($item =~ /^\.\.?$/) || ($obs));
12414:                 if ($dirptr&$testdir) {
12415:                     my $newpath;
12416:                     if ($relpath) {
12417:                         $newpath = "$relpath/$item";
12418:                     } else {
12419:                         $newpath = $item;
12420:                     }
12421:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
12422:                     if ($recurse) {
12423:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
12424:                     }
12425:                 } elsif (($savefile) || ($relpath eq '')) {
12426:                     next if ($nonemptydir && $filecount);
12427:                     if ($checkinc || $checkexc) {
12428:                         my $extension;
12429:                         if ($checkinc) {
12430:                             next unless ($extension && $include->{$extension});
12431:                         }
12432:                         if ($checkexc) {
12433:                             next if ($extension && $exclude->{$extension});
12434:                         }
12435:                     }
12436:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12437:                         $dirhashref->{'/'} = 1;
12438:                     }
12439:                     if ($savefile) {
12440:                         if ($relpath eq '') {
12441:                             $filehashref->{'/'}{$item} = 1;
12442:                         } else {
12443:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
12444:                         }
12445:                     }
12446:                     $filecount ++; 
12447:                 }
12448:             }
12449:         }
12450:     }
12451:     if ($addtopdir) {
12452:         if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12453:             $dirhashref->{'/'} = 1;
12454:         }
12455:     }
12456:     return;
12457: }
12458: 
12459: sub priv_exclude {
12460:     return {
12461:              meta => 1,
12462:              save => 1,
12463:              log => 1,
12464:              bak => 1,
12465:              rights => 1,
12466:              DS_Store => 1,
12467:            };
12468: }
12469: 
12470: # -------------------------------------------------------- Value of a Condition
12471: 
12472: # gets the value of a specific preevaluated condition
12473: #    stored in the string  $env{user.state.<cid>}
12474: # or looks up a condition reference in the bighash and if if hasn't
12475: # already been evaluated recurses into docondval to get the value of
12476: # the condition, then memoizing it to 
12477: #   $env{user.state.<cid>.<condition>}
12478: sub directcondval {
12479:     my $number=shift;
12480:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
12481: 	&Apache::lonuserstate::evalstate();
12482:     }
12483:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12484: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12485:     } elsif ($number =~ /^_/) {
12486: 	my $sub_condition;
12487: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12488: 		&GDBM_READER(),0640)) {
12489: 	    $sub_condition=$bighash{'conditions'.$number};
12490: 	    untie(%bighash);
12491: 	}
12492: 	my $value = &docondval($sub_condition);
12493: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
12494: 	return $value;
12495:     }
12496:     if ($env{'user.state.'.$env{'request.course.id'}}) {
12497:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
12498:     } else {
12499:        return 2;
12500:     }
12501: }
12502: 
12503: # get the collection of conditions for this resource
12504: sub condval {
12505:     my $condidx=shift;
12506:     my $allpathcond='';
12507:     foreach my $cond (split(/\|/,$condidx)) {
12508: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12509: 	    $allpathcond.=
12510: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12511: 	}
12512:     }
12513:     $allpathcond=~s/\|$//;
12514:     return &docondval($allpathcond);
12515: }
12516: 
12517: #evaluates an expression of conditions
12518: sub docondval {
12519:     my ($allpathcond) = @_;
12520:     my $result=0;
12521:     if ($env{'request.course.id'}
12522: 	&& defined($allpathcond)) {
12523: 	my $operand='|';
12524: 	my @stack;
12525: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12526: 	    if ($chunk eq '(') {
12527: 		push @stack,($operand,$result);
12528: 	    } elsif ($chunk eq ')') {
12529: 		my $before=pop @stack;
12530: 		if (pop @stack eq '&') {
12531: 		    $result=$result>$before?$before:$result;
12532: 		} else {
12533: 		    $result=$result>$before?$result:$before;
12534: 		}
12535: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
12536: 		$operand=$chunk;
12537: 	    } else {
12538: 		my $new=directcondval($chunk);
12539: 		if ($operand eq '&') {
12540: 		    $result=$result>$new?$new:$result;
12541: 		} else {
12542: 		    $result=$result>$new?$result:$new;
12543: 		}
12544: 	    }
12545: 	}
12546:     }
12547:     return $result;
12548: }
12549: 
12550: # ---------------------------------------------------- Devalidate courseresdata
12551: 
12552: sub devalidatecourseresdata {
12553:     my ($coursenum,$coursedomain)=@_;
12554:     my $hashid=$coursenum.':'.$coursedomain;
12555:     &devalidate_cache_new('courseres',$hashid);
12556: }
12557: 
12558: 
12559: # --------------------------------------------------- Course Resourcedata Query
12560: #
12561: #  Parameters:
12562: #      $coursenum    - Number of the course.
12563: #      $coursedomain - Domain at which the course was created.
12564: #  Returns:
12565: #     A hash of the course parameters along (I think) with timestamps
12566: #     and version info.
12567: 
12568: sub get_courseresdata {
12569:     my ($coursenum,$coursedomain)=@_;
12570:     my $coursehom=&homeserver($coursenum,$coursedomain);
12571:     my $hashid=$coursenum.':'.$coursedomain;
12572:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
12573:     my %dumpreply;
12574:     unless (defined($cached)) {
12575: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
12576: 	$result=\%dumpreply;
12577: 	my ($tmp) = keys(%dumpreply);
12578: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12579: 	    &do_cache_new('courseres',$hashid,$result,600);
12580: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12581: 	    return $tmp;
12582: 	} elsif ($tmp =~ /^(error)/) {
12583: 	    $result=undef;
12584: 	    &do_cache_new('courseres',$hashid,$result,600);
12585: 	}
12586:     }
12587:     return $result;
12588: }
12589: 
12590: sub devalidateuserresdata {
12591:     my ($uname,$udom)=@_;
12592:     my $hashid="$udom:$uname";
12593:     &devalidate_cache_new('userres',$hashid);
12594: }
12595: 
12596: sub get_userresdata {
12597:     my ($uname,$udom)=@_;
12598:     #most student don\'t have any data set, check if there is some data
12599:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12600: 
12601:     my $hashid="$udom:$uname";
12602:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12603:     if (!defined($cached)) {
12604: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12605: 	$result=\%resourcedata;
12606: 	&do_cache_new('userres',$hashid,$result,600);
12607:     }
12608:     my ($tmp)=keys(%$result);
12609:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12610: 	return $result;
12611:     }
12612:     #error 2 occurs when the .db doesn't exist
12613:     if ($tmp!~/error: 2 /) {
12614:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12615: 	    &logthis("<font color=\"blue\">WARNING:".
12616: 		     " Trying to get resource data for ".
12617: 		     $uname." at ".$udom.": ".
12618: 		     $tmp."</font>");
12619:         }
12620:     } elsif ($tmp=~/error: 2 /) {
12621: 	#&EXT_cache_set($udom,$uname);
12622: 	&do_cache_new('userres',$hashid,undef,600);
12623: 	undef($tmp); # not really an error so don't send it back
12624:     }
12625:     return $tmp;
12626: }
12627: #----------------------------------------------- resdata - return resource data
12628: #  Purpose:
12629: #    Return resource data for either users or for a course.
12630: #  Parameters:
12631: #     $name      - Course/user name.
12632: #     $domain    - Name of the domain the user/course is registered on.
12633: #     $type      - Type of thing $name is (must be 'course' or 'user')
12634: #     $mapp      - decluttered URL of enclosing map  
12635: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12636: #     $recurseup - Ref to array of map URLs, starting with map containing
12637: #                  $mapp up through hierarchy of nested maps to top level map.  
12638: #     $courseid  - CourseID (first part of param identifier).
12639: #     $modifier  - Middle part of param identifier.
12640: #     $what      - Last part of param identifier.
12641: #     @which     - Array of names of resources desired.
12642: #  Returns:
12643: #     The value of the first reasource in @which that is found in the
12644: #     resource hash.
12645: #  Exceptional Conditions:
12646: #     If the $type passed in is not valid (not the string 'course' or 
12647: #     'user', an undefined  reference is returned.
12648: #     If none of the resources are found, an undef is returned
12649: sub resdata {
12650:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12651:         $modifier,$what,@which)=@_;
12652:     my $result;
12653:     if ($type eq 'course') {
12654: 	$result=&get_courseresdata($name,$domain);
12655:     } elsif ($type eq 'user') {
12656: 	$result=&get_userresdata($name,$domain);
12657:     }
12658:     if (!ref($result)) { return $result; }    
12659:     foreach my $item (@which) {
12660:         if ($item->[1] eq 'course') {
12661:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12662:                 unless ($$recursed) {
12663:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12664:                     $$recursed = 1;
12665:                 }
12666:                 foreach my $item (@${recurseup}) {
12667:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12668:                     last if (defined($result->{$norecursechk}));
12669:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12670:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12671:                 }
12672:             }
12673:         }
12674:         if (defined($result->{$item->[0]})) {
12675: 	    return [$result->{$item->[0]},$item->[1]];
12676: 	}
12677:     }
12678:     return undef;
12679: }
12680: 
12681: sub get_domain_lti {
12682:     my ($cdom,$context) = @_;
12683:     my ($name,$cachename,%lti);
12684:     if ($context eq 'consumer') {
12685:         $name = 'ltitools';
12686:     } elsif ($context eq 'provider') {
12687:         $name = 'lti';
12688:     } elsif ($context eq 'linkprot') {
12689:         $name = 'ltisec';
12690:     } else {
12691:         return %lti;
12692:     }
12693:     if ($context eq 'linkprot') {
12694:         $cachename = $context;
12695:     } else {
12696:         $cachename = $name;
12697:     }
12698:     my ($result,$cached)=&is_cached_new($cachename,$cdom);
12699:     if (defined($cached)) {
12700:         if (ref($result) eq 'HASH') {
12701:             %lti = %{$result};
12702:         }
12703:     } else {
12704:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12705:         if (ref($domconfig{$name}) eq 'HASH') {
12706:             if ($context eq 'linkprot') {
12707:                 if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12708:                     %lti = %{$domconfig{$name}{'linkprot'}};
12709:                 }
12710:             } else {
12711:                 %lti = %{$domconfig{$name}};
12712:             }
12713:         }
12714:         my $cachetime = 24*60*60;
12715:         &do_cache_new($cachename,$cdom,\%lti,$cachetime);
12716:     }
12717:     return %lti;
12718: }
12719: 
12720: sub get_course_lti {
12721:     my ($cnum,$cdom,$context) = @_;
12722:     my ($name,$cachename,%lti);
12723:     if ($context eq 'consumer') {
12724:         $name = 'ltitools';
12725:         $cachename = 'courseltitools';
12726:     } elsif ($context eq 'provider') {
12727:         $name = 'lti';
12728:         $cachename = 'courselti';
12729:     } else {
12730:         return %lti;
12731:     }
12732:     my $hashid=$cdom.'_'.$cnum;
12733:     my ($result,$cached)=&is_cached_new($cachename,$hashid);
12734:     if (defined($cached)) {
12735:         if (ref($result) eq 'HASH') {
12736:             %lti = %{$result};
12737:         }
12738:     } else {
12739:         %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
12740:         my $cachetime = 24*60*60;
12741:         &do_cache_new($cachename,$hashid,\%lti,$cachetime);
12742:     }
12743:     return %lti;
12744: }
12745: 
12746: sub courselti_itemid {
12747:     my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12748:     my ($chome,$itemid);
12749:     $chome = &homeserver($cnum,$cdom);
12750:     return if ($chome eq 'no_host');
12751:     if (ref($params) eq 'HASH') {
12752:         my $rep;
12753:         if (grep { $_ eq $chome } current_machine_ids()) {
12754:             $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12755:         } else {
12756:             my $escurl = &escape($url);
12757:             my $escmethod = &escape($method);
12758:             my $items = &freeze_escape($params);
12759:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12760:         }
12761:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12762:                 ($rep eq 'unknown_cmd')) {
12763:             $itemid = $rep;
12764:         }
12765:     }
12766:     return $itemid;
12767: }
12768: 
12769: sub domainlti_itemid {
12770:     my ($cdom,$url,$method,$params,$context) = @_;
12771:     my ($primary_id,$itemid);
12772:     $primary_id = &domain($cdom,'primary');
12773:     return if ($primary_id eq '');
12774:     if (ref($params) eq 'HASH') {
12775:         my $rep;
12776:         if (grep { $_ eq $primary_id } current_machine_ids()) {
12777:             $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12778:         } else {
12779:             my $cnum = '';
12780:             my $escurl = &escape($url);
12781:             my $escmethod = &escape($method);
12782:             my $items = &freeze_escape($params);
12783:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12784:         }
12785:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12786:                 ($rep eq 'unknown_cmd')) {
12787:             $itemid = $rep;
12788:         }
12789:     }
12790:     return $itemid;
12791: }
12792: 
12793: sub get_ltitools_id {
12794:     my ($context,$cdom,$cnum,$title) = @_;
12795:     my ($lockhash,$tries,$gotlock,$id,$error);
12796: 
12797:     # get lock on ltitools db
12798:     $lockhash = {
12799:                    lock => $env{'user.name'}.
12800:                            ':'.$env{'user.domain'},
12801:                 };
12802:     $tries = 0;
12803:     if ($context eq 'domain') {
12804:         $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12805:     } else {
12806:         $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12807:     }
12808:     while (($gotlock ne 'ok') && ($tries<10)) {
12809:         $tries ++;
12810:         sleep (0.1);
12811:         if ($context eq 'domain') {
12812:             $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
12813:         } else {
12814:             $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
12815:         }
12816:     }
12817:     if ($gotlock eq 'ok') {
12818:         my %currids;
12819:         if ($context eq 'domain') {
12820:             %currids = &dump_dom('ltitools',$cdom);
12821:         } else {
12822:             %currids = &dump('ltitools',$cdom,$cnum);
12823:         }
12824:         if ($currids{'lock'}) {
12825:             delete($currids{'lock'});
12826:             if (keys(%currids)) {
12827:                 my @curr = sort { $a <=> $b } keys(%currids);
12828:                 if ($curr[-1] =~ /^\d+$/) {
12829:                     $id = 1 + $curr[-1];
12830:                 }
12831:             } else {
12832:                 $id = 1;
12833:             }
12834:             if ($id) {
12835:                 if ($context eq 'domain') {
12836:                     unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12837:                         $error = 'nostore';
12838:                     }
12839:                 } else {
12840:                     unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
12841:                         $error = 'nostore';
12842:                     }
12843:                 }
12844:             } else {
12845:                 $error = 'nonumber';
12846:             }
12847:         }
12848:         my $dellockoutcome;
12849:         if ($context eq 'domain') {
12850:             $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
12851:         } else {
12852:             $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
12853:         }
12854:     } else {
12855:         $error = 'nolock';
12856:     }
12857:     return ($id,$error);
12858: }
12859: 
12860: sub count_supptools {
12861:     my ($cnum,$cdom,$ignorecache,$reload)=@_;
12862:     my $hashid=$cnum.':'.$cdom;
12863:     my ($numexttools,$cached);
12864:     unless ($ignorecache) {
12865:         ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
12866:     }
12867:     unless (defined($cached)) {
12868:         my $chome=&homeserver($cnum,$cdom);
12869:         $numexttools = 0;
12870:         unless ($chome eq 'no_host') {
12871:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12872:             if (ref($supplemental) eq 'HASH') {
12873:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12874:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
12875:                         if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12876:                             $numexttools ++;
12877:                         }
12878:                     }
12879:                 }
12880:             }
12881:         }
12882:         &do_cache_new('supptools',$hashid,$numexttools,600);
12883:     }
12884:     return $numexttools;
12885: }
12886: 
12887: sub has_unhidden_suppfiles {
12888:     my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12889:     my $hashid=$cnum.':'.$cdom;
12890:     my ($showsupp,$cached);
12891:     unless ($ignorecache) {
12892:         ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12893:     }
12894:     unless (defined($cached)) {
12895:         my $chome=&homeserver($cnum,$cdom);
12896:         unless ($chome eq 'no_host') {
12897:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12898:             if (ref($supplemental) eq 'HASH') {
12899:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12900:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
12901:                         next if ($key =~ /\.sequence$/);
12902:                         if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12903:                             foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12904:                                 unless ($supplemental->{'hidden'}->{$id}) {
12905:                                     $showsupp = 1;
12906:                                     last;
12907:                                 }
12908:                             }
12909:                         }
12910:                         last if ($showsupp);
12911:                     }
12912:                 }
12913:             }
12914:         }
12915:         &do_cache_new('showsupp',$hashid,$showsupp,600);
12916:     }
12917:     return $showsupp;
12918: }
12919: 
12920: #
12921: # EXT resource caching routines
12922: #
12923: 
12924: {
12925: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12926: #
12927: # The course for which we cache
12928: my $cachedmapkey='';
12929: # The cached recursive maps for this course
12930: my %cachedmaps=();
12931: # When this was last done
12932: my $cachedmaptime='';
12933: 
12934: sub clear_EXT_cache_status {
12935:     &delenv('cache.EXT.');
12936: }
12937: 
12938: sub EXT_cache_status {
12939:     my ($target_domain,$target_user) = @_;
12940:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12941:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12942:         # We know already the user has no data
12943:         return 1;
12944:     } else {
12945:         return 0;
12946:     }
12947: }
12948: 
12949: sub EXT_cache_set {
12950:     my ($target_domain,$target_user) = @_;
12951:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12952:     #&appenv({$cachename => time});
12953: }
12954: 
12955: # --------------------------------------------------------- Value of a Variable
12956: sub EXT {
12957: 
12958:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
12959:     unless ($varname) { return ''; }
12960:     #get real user name/domain, courseid and symb
12961:     my $courseid;
12962:     my $publicuser;
12963:     if ($symbparm) {
12964: 	$symbparm=&get_symb_from_alias($symbparm);
12965:     }
12966:     if (!($uname && $udom)) {
12967:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12968:       if (!$symbparm) {	$symbparm=$cursymb; }
12969:     } else {
12970: 	$courseid=$env{'request.course.id'};
12971:     }
12972:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12973:     my $rest;
12974:     if (defined($therest[0])) {
12975:        $rest=join('.',@therest);
12976:     } else {
12977:        $rest='';
12978:     }
12979: 
12980:     my $qualifierrest=$qualifier;
12981:     if ($rest) { $qualifierrest.='.'.$rest; }
12982:     my $spacequalifierrest=$space;
12983:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12984:     if ($realm eq 'user') {
12985: # --------------------------------------------------------------- user.resource
12986: 	if ($space eq 'resource') {
12987: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12988: 		  || defined($Apache::lonhomework::parsing_a_task))
12989: 		 &&
12990: 		 ($symbparm eq &symbread()) ) {
12991: 		# if we are in the middle of processing the resource the
12992: 		# get the value we are planning on committing
12993:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12994:                     return $Apache::lonhomework::results{$qualifierrest};
12995:                 } else {
12996:                     return $Apache::lonhomework::history{$qualifierrest};
12997:                 }
12998: 	    } else {
12999: 		my %restored;
13000: 		if ($publicuser || $env{'request.state'} eq 'construct') {
13001: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
13002: 		} else {
13003: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
13004: 		}
13005: 		return $restored{$qualifierrest};
13006: 	    }
13007: # ----------------------------------------------------------------- user.access
13008:         } elsif ($space eq 'access') {
13009: 	    # FIXME - not supporting calls for a specific user
13010:             return &allowed($qualifier,$rest);
13011: # ------------------------------------------ user.preferences, user.environment
13012:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
13013: 	    if (($uname eq $env{'user.name'}) &&
13014: 		($udom eq $env{'user.domain'})) {
13015: 		return $env{join('.',('environment',$qualifierrest))};
13016: 	    } else {
13017: 		my %returnhash;
13018: 		if (!$publicuser) {
13019: 		    %returnhash=&userenvironment($udom,$uname,
13020: 						 $qualifierrest);
13021: 		}
13022: 		return $returnhash{$qualifierrest};
13023: 	    }
13024: # ----------------------------------------------------------------- user.course
13025:         } elsif ($space eq 'course') {
13026: 	    # FIXME - not supporting calls for a specific user
13027:             return $env{join('.',('request.course',$qualifier))};
13028: # ------------------------------------------------------------------- user.role
13029:         } elsif ($space eq 'role') {
13030: 	    # FIXME - not supporting calls for a specific user
13031:             my ($role,$where)=split(/\./,$env{'request.role'});
13032:             if ($qualifier eq 'value') {
13033: 		return $role;
13034:             } elsif ($qualifier eq 'extent') {
13035:                 return $where;
13036:             }
13037: # ----------------------------------------------------------------- user.domain
13038:         } elsif ($space eq 'domain') {
13039:             return $udom;
13040: # ------------------------------------------------------------------- user.name
13041:         } elsif ($space eq 'name') {
13042:             return $uname;
13043: # ---------------------------------------------------- Any other user namespace
13044:         } else {
13045: 	    my %reply;
13046: 	    if (!$publicuser) {
13047: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
13048: 	    }
13049: 	    return $reply{$qualifierrest};
13050:         }
13051:     } elsif ($realm eq 'query') {
13052: # ---------------------------------------------- pull stuff out of query string
13053:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
13054: 						[$spacequalifierrest]);
13055: 	return $env{'form.'.$spacequalifierrest}; 
13056:    } elsif ($realm eq 'request') {
13057: # ------------------------------------------------------------- request.browser
13058:         if ($space eq 'browser') {
13059:             return $env{'browser.'.$qualifier};
13060: # ------------------------------------------------------------ request.filename
13061:         } else {
13062:             return $env{'request.'.$spacequalifierrest};
13063:         }
13064:     } elsif ($realm eq 'course') {
13065: # ---------------------------------------------------------- course.description
13066:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
13067:     } elsif ($realm eq 'resource') {
13068: 
13069: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
13070: 	    if (!$symbparm) { $symbparm=&symbread(); }
13071: 	}
13072: 
13073:         if ($qualifier eq '') {
13074: 	    if ($space eq 'title') {
13075: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
13076: 	        return &gettitle($symbparm);
13077: 	    }
13078: 	
13079: 	    if ($space eq 'map') {
13080: 	        my ($map) = &decode_symb($symbparm);
13081: 	        return &symbread($map);
13082: 	    }
13083:             if ($space eq 'maptitle') {
13084:                 my ($map) = &decode_symb($symbparm);
13085:                 return &gettitle($map);
13086:             }
13087: 	    if ($space eq 'filename') {
13088: 	        if ($symbparm) {
13089: 		    return &clutter((&decode_symb($symbparm))[2]);
13090: 	        }
13091: 	        return &hreflocation('',$env{'request.filename'});
13092: 	    }
13093: 
13094:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
13095:                 if ($space eq 'visibleparts') {
13096:                     my $navmap = Apache::lonnavmaps::navmap->new();
13097:                     my $item;
13098:                     if (ref($navmap)) {
13099:                         my $res = $navmap->getBySymb($symbparm);
13100:                         my $parts = $res->parts();
13101:                         if (ref($parts) eq 'ARRAY') {
13102:                             $item = join(',',@{$parts});
13103:                         }
13104:                         undef($navmap);
13105:                     }
13106:                     return $item;
13107:                 }
13108:             }
13109:         }
13110: 
13111: 	my ($section, $group, @groups, @recurseup, $recursed);
13112:         if (ref($recurseupref) eq 'ARRAY') {
13113:             @recurseup = @{$recurseupref};
13114:             $recursed = 1;
13115:         }
13116: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
13117:         if (($courseid eq '') && ($cid)) {
13118:             $courseid = $cid;
13119:         }
13120: 	if (($symbparm && $courseid) && 
13121: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
13122: 
13123: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
13124: 
13125: # ----------------------------------------------------- Cascading lookup scheme
13126: 	    my $symbp=$symbparm;
13127: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
13128: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
13129:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
13130: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
13131: 	    if (($env{'user.name'} eq $uname) &&
13132: 		($env{'user.domain'} eq $udom)) {
13133: 		$section=$env{'request.course.sec'};
13134:                 @groups = split(/:/,$env{'request.course.groups'});  
13135:                 @groups=&sort_course_groups($courseid,@groups); 
13136: 	    } else {
13137: 		if (! defined($usection)) {
13138: 		    $section=&getsection($udom,$uname,$courseid);
13139: 		} else {
13140: 		    $section = $usection;
13141: 		}
13142:                 @groups = &get_users_groups($udom,$uname,$courseid);
13143: 	    }
13144: 
13145: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
13146: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
13147:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
13148: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
13149: 
13150: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
13151: 	    my $courselevelr=$courseid.'.'.$symbparm;
13152:             $courseleveli=$courseid.'.'.$recurseparm;
13153: 	    $courselevelm=$courseid.'.'.$mapparm;
13154: 
13155: # ----------------------------------------------------------- first, check user
13156: 
13157: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
13158:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
13159: 				       ([$courselevelr,'resource'],
13160: 					[$courselevelm,'map'     ],
13161:                                         [$courseleveli,'map'     ],
13162: 					[$courselevel, 'course'  ]));
13163: 	    if (defined($userreply)) { return &get_reply($userreply); }
13164: 
13165: # ------------------------------------------------ second, check some of course
13166:             my $coursereply;
13167:             if (@groups > 0) {
13168:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
13169:                                        $recurseparm,$mapparm,$spacequalifierrest,
13170:                                        $mapp,\$recursed,\@recurseup);
13171:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
13172:             }
13173: 
13174: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
13175: 				  $env{'course.'.$courseid.'.domain'},
13176: 				  'course',$mapp,\$recursed,\@recurseup,
13177:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
13178: 				  ([$seclevelr,   'resource'],
13179: 				   [$seclevelm,   'map'     ],
13180:                                    [$secleveli,   'map'     ],
13181: 				   [$seclevel,    'course'  ],
13182: 				   [$courselevelr,'resource']));
13183: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
13184: 
13185: # ------------------------------------------------------ third, check map parms
13186: 	    my %parmhash=();
13187: 	    my $thisparm='';
13188: 	    if (tie(%parmhash,'GDBM_File',
13189: 		    $env{'request.course.fn'}.'_parms.db',
13190: 		    &GDBM_READER(),0640)) {
13191: 		$thisparm=$parmhash{$symbparm};
13192: 		untie(%parmhash);
13193: 	    }
13194: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
13195: 	}
13196: # ------------------------------------------ fourth, look in resource metadata
13197:  
13198:         my $what = $spacequalifierrest;
13199: 	$what=~s/\./\_/;
13200: 	my $filename;
13201: 	if (!$symbparm) { $symbparm=&symbread(); }
13202: 	if ($symbparm) {
13203: 	    $filename=(&decode_symb($symbparm))[2];
13204: 	} else {
13205: 	    $filename=$env{'request.filename'};
13206: 	}
13207:         my $toolsymb;
13208:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
13209:             $toolsymb = $symbparm;
13210:         }
13211: 	my $metadata=&metadata($filename,$what,$toolsymb);
13212: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
13213: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
13214: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
13215: 
13216: # ----------------------------------------------- fifth, look in rest of course
13217: 	if ($symbparm && defined($courseid) && 
13218: 	    $courseid eq $env{'request.course.id'}) {
13219: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
13220: 				     $env{'course.'.$courseid.'.domain'},
13221: 				     'course',$mapp,\$recursed,\@recurseup,
13222:                                      $courseid,'.',$spacequalifierrest,
13223: 				     ([$courselevelm,'map'   ],
13224:                                       [$courseleveli,'map'   ],
13225: 				      [$courselevel, 'course']));
13226: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
13227: 	}
13228: # ------------------------------------------------------------------ Cascade up
13229: 	unless ($space eq '0') {
13230: 	    my @parts=split(/_/,$space);
13231: 	    my $id=pop(@parts);
13232: 	    my $part=join('_',@parts);
13233: 	    if ($part eq '') { $part='0'; }
13234: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
13235: 				 $symbparm,$udom,$uname,$section,1);
13236: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
13237: 	}
13238: 	if ($recurse) { return undef; }
13239: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
13240: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
13241: # ---------------------------------------------------- Any other user namespace
13242:     } elsif ($realm eq 'environment') {
13243: # ----------------------------------------------------------------- environment
13244: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
13245: 	    return $env{'environment.'.$spacequalifierrest};
13246: 	} else {
13247: 	    if ($uname eq 'anonymous' && $udom eq '') {
13248: 		return '';
13249: 	    }
13250: 	    my %returnhash=&userenvironment($udom,$uname,
13251: 					    $spacequalifierrest);
13252: 	    return $returnhash{$spacequalifierrest};
13253: 	}
13254:     } elsif ($realm eq 'system') {
13255: # ----------------------------------------------------------------- system.time
13256: 	if ($space eq 'time') {
13257: 	    return time;
13258:         }
13259:     } elsif ($realm eq 'server') {
13260: # ----------------------------------------------------------------- system.time
13261: 	if ($space eq 'name') {
13262: 	    return $ENV{'SERVER_NAME'};
13263:         }
13264:     } elsif ($realm eq 'client') {
13265:         if ($space eq 'remote_addr') {
13266:             return &get_requestor_ip();
13267:         }
13268:     }
13269:     return '';
13270: }
13271: 
13272: sub get_reply {
13273:     my ($reply_value) = @_;
13274:     if (ref($reply_value) eq 'ARRAY') {
13275:         if (wantarray) {
13276: 	    return @$reply_value;
13277:         }
13278:         return $reply_value->[0];
13279:     } else {
13280:         return $reply_value;
13281:     }
13282: }
13283: 
13284: sub check_group_parms {
13285:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
13286:         $recursed,$recurseupref) = @_;
13287:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
13288:                   [$what,'course']);
13289:     my $coursereply;
13290:     foreach my $group (@{$groups}) {
13291:         my @groupitems = ();
13292:         foreach my $level (@levels) {
13293:              my $item = $courseid.'.['.$group.'].'.$level->[0];
13294:              push(@groupitems,[$item,$level->[1]]);
13295:         }
13296:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
13297:                                    $env{'course.'.$courseid.'.domain'},
13298:                                    'course',$mapp,$recursed,$recurseupref,
13299:                                    $courseid,'.['.$group.'].',$what,
13300:                                    @groupitems);
13301:         last if (defined($coursereply));
13302:     }
13303:     return $coursereply;
13304: }
13305: 
13306: sub get_map_hierarchy {
13307:     my ($mapname,$courseid) = @_;
13308:     my @recurseup = ();
13309:     if ($mapname) {
13310:         if (($cachedmapkey eq $courseid) &&
13311:             (abs($cachedmaptime-time)<5)) {
13312:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
13313:                 return @{$cachedmaps{$mapname}};
13314:             }
13315:         }
13316:         my $navmap = Apache::lonnavmaps::navmap->new();
13317:         if (ref($navmap)) {
13318:             @recurseup = $navmap->recurseup_maps($mapname);
13319:             undef($navmap);
13320:             $cachedmaps{$mapname} = \@recurseup;
13321:             $cachedmaptime=time;
13322:             $cachedmapkey=$courseid;
13323:         }
13324:     }
13325:     return @recurseup;
13326: }
13327: 
13328: }
13329: 
13330: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
13331:     my ($courseid,@groups) = @_;
13332:     @groups = sort(@groups);
13333:     return @groups;
13334: }
13335: 
13336: sub packages_tab_default {
13337:     my ($uri,$varname,$toolsymb)=@_;
13338:     my (undef,$part,$name)=split(/\./,$varname);
13339: 
13340:     my (@extension,@specifics,$do_default);
13341:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
13342: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
13343: 	if ($pack_type eq 'default') {
13344: 	    $do_default=1;
13345: 	} elsif ($pack_type eq 'extension') {
13346: 	    push(@extension,[$package,$pack_type,$pack_part]);
13347: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
13348: 	    # only look at packages defaults for packages that this id is
13349: 	    push(@specifics,[$package,$pack_type,$pack_part]);
13350: 	}
13351:     }
13352:     # first look for a package that matches the requested part id
13353:     foreach my $package (@specifics) {
13354: 	my (undef,$pack_type,$pack_part)=@{$package};
13355: 	next if ($pack_part ne $part);
13356: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13357: 	    return $packagetab{"$pack_type&$name&default"};
13358: 	}
13359:     }
13360:     # look for any possible matching non extension_ package
13361:     foreach my $package (@specifics) {
13362: 	my (undef,$pack_type,$pack_part)=@{$package};
13363: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13364: 	    return $packagetab{"$pack_type&$name&default"};
13365: 	}
13366: 	if ($pack_type eq 'part') { $pack_part='0'; }
13367: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
13368: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
13369: 	}
13370:     }
13371:     # look for any posible extension_ match
13372:     foreach my $package (@extension) {
13373: 	my ($package,$pack_type)=@{$package};
13374: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13375: 	    return $packagetab{"$pack_type&$name&default"};
13376: 	}
13377: 	if (defined($packagetab{$package."&$name&default"})) {
13378: 	    return $packagetab{$package."&$name&default"};
13379: 	}
13380:     }
13381:     # look for a global default setting
13382:     if ($do_default && defined($packagetab{"default&$name&default"})) {
13383: 	return $packagetab{"default&$name&default"};
13384:     }
13385:     return undef;
13386: }
13387: 
13388: sub add_prefix_and_part {
13389:     my ($prefix,$part)=@_;
13390:     my $keyroot;
13391:     if (defined($prefix) && $prefix !~ /^__/) {
13392: 	# prefix that has a part already
13393: 	$keyroot=$prefix;
13394:     } elsif (defined($prefix)) {
13395: 	# prefix that is missing a part
13396: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
13397:     } else {
13398: 	# no prefix at all
13399: 	if (defined($part)) { $keyroot='_'.$part; }
13400:     }
13401:     return $keyroot;
13402: }
13403: 
13404: # ---------------------------------------------------------------- Get metadata
13405: 
13406: my %metaentry;
13407: my %importedpartids;
13408: my %importedrespids;
13409: sub metadata {
13410:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
13411:     $uri=&declutter($uri);
13412:     # if it is a non metadata possible uri return quickly
13413:     if (($uri eq '') || 
13414: 	(($uri =~ m|^/*adm/|) && 
13415: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
13416:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
13417: 	return undef;
13418:     }
13419:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
13420: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
13421: 	return undef;
13422:     }
13423:     my $filename=$uri;
13424:     $uri=~s/\.meta$//;
13425: #
13426: # Is the metadata already cached?
13427: # Look at timestamp of caching
13428: # Everything is cached by the main uri, libraries are never directly cached
13429: #
13430:     if (!defined($liburi)) {
13431: 	my ($result,$cached)=&is_cached_new('meta',$uri);
13432: 	if (defined($cached)) { return $result->{':'.$what}; }
13433:     }
13434: 
13435: #
13436: # If the uri is for an external tool the file from
13437: # which metadata should be retrieved depends on whether
13438: # the tool had been configured to be gradable (set in the Course
13439: # Editor or Resource Editor).
13440: #
13441: # If a valid symb has been included as the third arg in the call
13442: # to &metadata() that can be used to retrieve the value of
13443: # parameter_0_gradable set for the resource, and included in the
13444: # uploaded map containing the tool. The value is retrieved via
13445: # &EXT(), if a valid symb is available.  Otherwise the value of
13446: # gradable in the exttool_$marker.db file for the tool instance
13447: # is retrieved via &get().
13448: #
13449: # When lonuserstate::traceroute() calls lonnet::EXT() for 
13450: # hiddenresource and encrypturl (during course initialization)
13451: # the map-level parameter for resource.0.gradable included in the 
13452: # uploaded map containing the tool will not yet have been stored
13453: # in the user_course_parms.db file for the user's session, so in 
13454: # this case fall back to retrieving gradable status from the
13455: # exttool_$marker.db file.
13456: #
13457: # In order to avoid an infinite loop, &metadata() will return
13458: # before a call to &EXT(), if the uri is for an external tool
13459: # and the $what for which metadata is being requested is
13460: # parameter_0_gradable or 0_gradable.
13461: #
13462: 
13463:     if ($uri =~ /ext\.tool$/) {
13464:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
13465:             return;
13466:         } else {
13467:             my ($checked,$use_passback);
13468:             if ($toolsymb ne '') {
13469:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
13470:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
13471:                     $checked = 1;
13472:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
13473:                         $use_passback = 1;
13474:                     }
13475:                 }
13476:             }
13477:             unless ($checked) {
13478:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
13479:                 $marker=~s/\D//g;
13480:                 if ($marker) {
13481:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
13482:                     $use_passback = $toolsettings{'gradable'};
13483:                 }
13484:             }
13485:             if ($use_passback) {
13486:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
13487:             } else {
13488:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
13489:             }
13490:         }
13491:     }
13492: 
13493:     {
13494: # Imported parts would go here
13495:         my @origfiletagids=();
13496:         my $importedparts=0;
13497: 
13498: # Imported responseids would go here
13499:         my $importedresponses=0;
13500: #
13501: # Is this a recursive call for a library?
13502: #
13503: #	if (! exists($metacache{$uri})) {
13504: #	    $metacache{$uri}={};
13505: #	}
13506: 	my $cachetime = 60*60;
13507:         if ($liburi) {
13508: 	    $liburi=&declutter($liburi);
13509:             $filename=$liburi;
13510:         } else {
13511: 	    &devalidate_cache_new('meta',$uri);
13512: 	    undef(%metaentry);
13513: 	}
13514:         my %metathesekeys=();
13515:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
13516: 	my $metastring;
13517: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
13518: 	    my $which = &hreflocation('','/'.($liburi || $uri));
13519: 	    $metastring = 
13520: 		&Apache::lonnet::ssi_body($which,
13521: 					  ('grade_target' => 'meta'));
13522: 	    $cachetime = 1; # only want this cached in the child not long term
13523: 	} elsif (($uri !~ m -^(editupload)/-) && 
13524:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
13525: 	    my $file=&filelocation('',&clutter($filename));
13526: 	    #push(@{$metaentry{$uri.'.file'}},$file);
13527: 	    $metastring=&getfile($file);
13528: 	}
13529:         my $parser=HTML::LCParser->new(\$metastring);
13530:         my $token;
13531:         undef %metathesekeys;
13532:         while ($token=$parser->get_token) {
13533: 	    if ($token->[0] eq 'S') {
13534: 		if (defined($token->[2]->{'package'})) {
13535: #
13536: # This is a package - get package info
13537: #
13538: 		    my $package=$token->[2]->{'package'};
13539: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13540: 		    if (defined($token->[2]->{'id'})) { 
13541: 			$keyroot.='_'.$token->[2]->{'id'}; 
13542: 		    }
13543: 		    if ($metaentry{':packages'}) {
13544: 			$metaentry{':packages'}.=','.$package.$keyroot;
13545: 		    } else {
13546: 			$metaentry{':packages'}=$package.$keyroot;
13547: 		    }
13548: 		    foreach my $pack_entry (keys(%packagetab)) {
13549: 			my $part=$keyroot;
13550: 			$part=~s/^\_//;
13551: 			if ($pack_entry=~/^\Q$package\E\&/ || 
13552: 			    $pack_entry=~/^\Q$package\E_0\&/) {
13553: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
13554: 			    # ignore package.tab specified default values
13555:                             # here &package_tab_default() will fetch those
13556: 			    if ($subp eq 'default') { next; }
13557: 			    my $value=$packagetab{$pack_entry};
13558: 			    my $unikey;
13559: 			    if ($pack =~ /_0$/) {
13560: 				$unikey='parameter_0_'.$name;
13561: 				$part=0;
13562: 			    } else {
13563: 				$unikey='parameter'.$keyroot.'_'.$name;
13564: 			    }
13565: 			    if ($subp eq 'display') {
13566: 				$value.=' [Part: '.$part.']';
13567: 			    }
13568: 			    $metaentry{':'.$unikey.'.part'}=$part;
13569: 			    $metathesekeys{$unikey}=1;
13570: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13571: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
13572: 			    }
13573: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
13574: 				$metaentry{':'.$unikey}=
13575: 				    $metaentry{':'.$unikey.'.default'};
13576: 			    }
13577: 			}
13578: 		    }
13579: 		} else {
13580: #
13581: # This is not a package - some other kind of start tag
13582: #
13583: 		    my $entry=$token->[1];
13584: 		    my $unikey='';
13585: 
13586: 		    if ($entry eq 'import') {
13587: #
13588: # Importing a library here
13589: #
13590:                         my $location=$parser->get_text('/import');
13591:                         my $dir=$filename;
13592:                         $dir=~s|[^/]*$||;
13593:                         $location=&filelocation($dir,$location);
13594: 
13595:                         my $importid=$token->[2]->{'id'};
13596:                         my $importmode=$token->[2]->{'importmode'};
13597: #
13598: # Check metadata for imported file to
13599: # see if it contained response items
13600: #
13601:                         my ($origfile,@libfilekeys);
13602:                         my %currmetaentry = %metaentry;
13603:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
13604:                                                            $depthcount+1));
13605:                         if (grep(/^responseorder$/,@libfilekeys)) {
13606:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
13607:                                                              undef,$depthcount+1);
13608:                             if ($libresponseorder ne '') {
13609:                                 if ($#origfiletagids<0) {
13610:                                     undef(%importedrespids);
13611:                                     undef(%importedpartids);
13612:                                 }
13613:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
13614:                                 if (@respids) {
13615:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
13616:                                 }
13617:                                 if ($importedrespids{$importid} ne '') {
13618:                                     $importedresponses = 1;
13619: # We need to get the original file and the imported file to get the response order correct
13620: # Load and inspect original file
13621:                                     if ($#origfiletagids<0) {
13622:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13623:                                         $origfile=&getfile($origfilelocation);
13624:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13625:                                     }
13626:                                 }
13627:                             }
13628:                         }
13629: # Do not overwrite contents of %metaentry hash for resource itself with 
13630: # hash populated for imported library file
13631:                         %metaentry = %currmetaentry;
13632:                         undef(%currmetaentry);
13633:                         if ($importmode eq 'part') {
13634: # Import as part(s)
13635:                            $importedparts=1;
13636: # We need to get the original file and the imported file to get the part order correct
13637: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
13638: # Load and inspect original file if we didn't do that already
13639:                            if ($#origfiletagids<0) {
13640:                                undef(%importedrespids);
13641:                                undef(%importedpartids);
13642:                                if ($origfile eq '') {
13643:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13644:                                    $origfile=&getfile($origfilelocation);
13645:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13646:                                }
13647:                            }
13648:                            my @impfilepartids;
13649: # If <partorder> tag is included in metadata for the imported file
13650: # get the parts in the imported file from that.
13651:                            if (grep(/^partorder$/,@libfilekeys)) {
13652:                                %currmetaentry = %metaentry;
13653:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13654:                                                             $depthcount+1);
13655:                                %metaentry = %currmetaentry;
13656:                                undef(%currmetaentry);
13657:                                if ($libpartorder ne '') {
13658:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
13659:                                }
13660:                            } else {
13661: # If no <partorder> tag available, load and inspect imported file
13662:                                my $impfile=&getfile($location);
13663:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13664:                            }
13665:                            if ($#impfilepartids>=0) {
13666: # This problem had parts
13667:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
13668:                            } else {
13669: # Importing by turning a single problem into a problem part
13670: # It gets the import-tags ID as part-ID
13671:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
13672:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
13673:                            }
13674:                         } else {
13675: # Import as problem or as normal import
13676:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13677:                             unless ($importmode eq 'problem') {
13678: # Normal import
13679:                                 if (defined($token->[2]->{'id'})) {
13680:                                     $unikey.='_'.$token->[2]->{'id'};
13681:                                 }
13682:                             }
13683: # Check metadata for imported file to
13684: # see if it contained parts
13685:                             if (grep(/^partorder$/,@libfilekeys)) {
13686:                                 %currmetaentry = %metaentry;
13687:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13688:                                                              $depthcount+1);
13689:                                 %metaentry = %currmetaentry;
13690:                                 undef(%currmetaentry);
13691:                                 if ($libpartorder ne '') {
13692:                                     $importedparts = 1;
13693:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
13694:                                 }
13695:                             }
13696:                         }
13697: 			if ($depthcount<20) {
13698: 			    my $metadata = 
13699: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
13700: 					  $depthcount+1);
13701: 			    foreach my $meta (split(',',$metadata)) {
13702: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
13703: 				$metathesekeys{$meta}=1;
13704: 			    }
13705:                         }
13706: 		    } else {
13707: #
13708: # Not importing, some other kind of non-package, non-library start tag
13709: # 
13710:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13711:                         if (defined($token->[2]->{'id'})) {
13712:                             $unikey.='_'.$token->[2]->{'id'};
13713:                         }
13714: 			if (defined($token->[2]->{'name'})) { 
13715: 			    $unikey.='_'.$token->[2]->{'name'}; 
13716: 			}
13717: 			$metathesekeys{$unikey}=1;
13718: 			foreach my $param (@{$token->[3]}) {
13719: 			    $metaentry{':'.$unikey.'.'.$param} =
13720: 				$token->[2]->{$param};
13721: 			}
13722: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
13723: 			my $default=$metaentry{':'.$unikey.'.default'};
13724: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13725: 		 # only ws inside the tag, and not in default, so use default
13726: 		 # as value
13727: 			    $metaentry{':'.$unikey}=$default;
13728: 			} elsif ( $internaltext =~ /\S/ ) {
13729: 		  # something interesting inside the tag
13730: 			    $metaentry{':'.$unikey}=$internaltext;
13731: 			} else {
13732: 		  # no interesting values, don't set a default
13733: 			}
13734: # end of not-a-package not-a-library import
13735: 		    }
13736: # end of not-a-package start tag
13737: 		}
13738: # the next is the end of "start tag"
13739: 	    }
13740: 	}
13741: 	my ($extension) = ($uri =~ /\.(\w+)$/);
13742: 	$extension = lc($extension);
13743: 	if ($extension eq 'htm') { $extension='html'; }
13744: 
13745: 	foreach my $key (keys(%packagetab)) {
13746: 	    #no specific packages #how's our extension
13747: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
13748: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
13749: 					 \%metathesekeys);
13750: 	}
13751: 
13752: 	if (!exists($metaentry{':packages'})
13753: 	    || $packagetab{"import_defaults&extension_$extension"}) {
13754: 	    foreach my $key (keys(%packagetab)) {
13755: 		#no specific packages well let's get default then
13756: 		if ($key!~/^default&/) { next; }
13757: 		&metadata_create_package_def($uri,$key,'default',
13758: 					     \%metathesekeys);
13759: 	    }
13760: 	}
13761: # are there custom rights to evaluate
13762: 	if ($metaentry{':copyright'} eq 'custom') {
13763: 
13764:     #
13765:     # Importing a rights file here
13766:     #
13767: 	    unless ($depthcount) {
13768: 		my $location=$metaentry{':customdistributionfile'};
13769: 		my $dir=$filename;
13770: 		$dir=~s|[^/]*$||;
13771: 		$location=&filelocation($dir,$location);
13772: 		my $rights_metadata =
13773: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
13774: 			      $depthcount+1);
13775: 		foreach my $rights (split(',',$rights_metadata)) {
13776: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13777: 		    $metathesekeys{$rights}=1;
13778: 		}
13779: 	    }
13780: 	}
13781: 	# uniqifiy package listing
13782: 	my %seen;
13783: 	my @uniq_packages =
13784: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13785: 	$metaentry{':packages'} = join(',',@uniq_packages);
13786: 
13787:         if (($importedresponses) || ($importedparts)) {
13788:             if ($importedparts) {
13789: # We had imported parts and need to rebuild partorder
13790:                 $metaentry{':partorder'}='';
13791:                 $metathesekeys{'partorder'}=1;
13792:             }
13793:             if ($importedresponses) {
13794: # We had imported responses and need to rebuil responseorder
13795:                 $metaentry{':responseorder'}='';
13796:                 $metathesekeys{'responseorder'}=1;
13797:             }
13798:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
13799:                 my $origid = $origfiletagids[$index+1];
13800:                 if ($origfiletagids[$index] eq 'part') {
13801: # Original part, part of the problem
13802:                     if ($importedparts) {
13803:                         $metaentry{':partorder'}.=','.$origid;
13804:                     }
13805:                 } elsif ($origfiletagids[$index] eq 'import') {
13806:                     if ($importedparts) {
13807: # We have imported parts at this position
13808:                         if ($importedpartids{$origid} ne '') {
13809:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
13810:                         }
13811:                     }
13812:                     if ($importedresponses) {
13813: # We have imported responses at this position
13814:                         if ($importedrespids{$origid} ne '') {
13815:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
13816:                         }
13817:                     }
13818:                 } else {
13819: # Original response item, part of the problem
13820:                     if ($importedresponses) {
13821:                         $metaentry{':responseorder'}.=','.$origid;
13822:                     }
13823:                 }
13824:             }
13825:             if ($importedparts) {
13826:                 $metaentry{':partorder'}=~s/^\,//;
13827:             }
13828:             if ($importedresponses) {
13829:                 $metaentry{':responseorder'}=~s/^\,//;
13830:             }
13831:         }
13832: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
13833: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
13834: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
13835:         unless ($liburi) {
13836: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
13837:         }
13838: # this is the end of "was not already recently cached
13839:     }
13840:     return $metaentry{':'.$what};
13841: }
13842: 
13843: sub metadata_create_package_def {
13844:     my ($uri,$key,$package,$metathesekeys)=@_;
13845:     my ($pack,$name,$subp)=split(/\&/,$key);
13846:     if ($subp eq 'default') { next; }
13847:     
13848:     if (defined($metaentry{':packages'})) {
13849: 	$metaentry{':packages'}.=','.$package;
13850:     } else {
13851: 	$metaentry{':packages'}=$package;
13852:     }
13853:     my $value=$packagetab{$key};
13854:     my $unikey;
13855:     $unikey='parameter_0_'.$name;
13856:     $metaentry{':'.$unikey.'.part'}=0;
13857:     $$metathesekeys{$unikey}=1;
13858:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13859: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
13860:     }
13861:     if (defined($metaentry{':'.$unikey.'.default'})) {
13862: 	$metaentry{':'.$unikey}=
13863: 	    $metaentry{':'.$unikey.'.default'};
13864:     }
13865: }
13866: 
13867: sub metadata_generate_part0 {
13868:     my ($metadata,$metacache,$uri) = @_;
13869:     my %allnames;
13870:     foreach my $metakey (keys(%$metadata)) {
13871: 	if ($metakey=~/^parameter\_(.*)/) {
13872: 	  my $part=$$metacache{':'.$metakey.'.part'};
13873: 	  my $name=$$metacache{':'.$metakey.'.name'};
13874: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
13875: 	    $allnames{$name}=$part;
13876: 	  }
13877: 	}
13878:     }
13879:     foreach my $name (keys(%allnames)) {
13880:       $$metadata{"parameter_0_$name"}=1;
13881:       my $key=":parameter_0_$name";
13882:       $$metacache{"$key.part"}='0';
13883:       $$metacache{"$key.name"}=$name;
13884:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
13885: 					   $allnames{$name}.'_'.$name.
13886: 					   '.type'};
13887:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
13888: 			     '.display'};
13889:       my $expr='[Part: '.$allnames{$name}.']';
13890:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
13891:       $$metacache{"$key.display"}=$olddis;
13892:     }
13893: }
13894: 
13895: # ------------------------------------------------------ Devalidate title cache
13896: 
13897: sub devalidate_title_cache {
13898:     my ($url)=@_;
13899:     if (!$env{'request.course.id'}) { return; }
13900:     my $symb=&symbread($url);
13901:     if (!$symb) { return; }
13902:     my $key=$env{'request.course.id'}."\0".$symb;
13903:     &devalidate_cache_new('title',$key);
13904: }
13905: 
13906: # ------------------------------------------------- Get the title of a course
13907: 
13908: sub current_course_title {
13909:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13910: }
13911: # ------------------------------------------------- Get the title of a resource
13912: 
13913: sub gettitle {
13914:     my $urlsymb=shift;
13915:     my $symb=&symbread($urlsymb);
13916:     if ($symb) {
13917: 	my $key=$env{'request.course.id'}."\0".$symb;
13918: 	my ($result,$cached)=&is_cached_new('title',$key);
13919: 	if (defined($cached)) { 
13920: 	    return $result;
13921: 	}
13922: 	my ($map,$resid,$url)=&decode_symb($symb);
13923: 	my $title='';
13924: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13925: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13926: 	} else {
13927: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13928: 		    &GDBM_READER(),0640)) {
13929: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13930: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13931: 		untie(%bighash);
13932: 	    }
13933: 	}
13934: 	$title=~s/\&colon\;/\:/gs;
13935: 	if ($title) {
13936: # Remember both $symb and $title for dynamic metadata
13937:             $accesshash{$symb.'___crstitle'}=$title;
13938:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13939: # Cache this title and then return it
13940: 	    return &do_cache_new('title',$key,$title,600);
13941: 	}
13942: 	$urlsymb=$url;
13943:     }
13944:     my $title=&metadata($urlsymb,'title');
13945:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13946:     return $title;
13947: }
13948: 
13949: sub get_slot {
13950:     my ($which,$cnum,$cdom)=@_;
13951:     if (!$cnum || !$cdom) {
13952: 	(undef,my $courseid)=&whichuser();
13953: 	$cdom=$env{'course.'.$courseid.'.domain'};
13954: 	$cnum=$env{'course.'.$courseid.'.num'};
13955:     }
13956:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13957:     my %slotinfo;
13958:     if (exists($remembered{$key})) {
13959: 	$slotinfo{$which} = $remembered{$key};
13960:     } else {
13961: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13962: 	&Apache::lonhomework::showhash(%slotinfo);
13963: 	my ($tmp)=keys(%slotinfo);
13964: 	if ($tmp=~/^error:/) { return (); }
13965: 	$remembered{$key} = $slotinfo{$which};
13966:     }
13967:     if (ref($slotinfo{$which}) eq 'HASH') {
13968: 	return %{$slotinfo{$which}};
13969:     }
13970:     return $slotinfo{$which};
13971: }
13972: 
13973: sub get_reservable_slots {
13974:     my ($cnum,$cdom,$uname,$udom) = @_;
13975:     my $now = time;
13976:     my $reservable_info;
13977:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13978:     if (exists($remembered{$key})) {
13979:         $reservable_info = $remembered{$key};
13980:     } else {
13981:         my %resv;
13982:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13983:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13984:         $reservable_info = \%resv;
13985:         $remembered{$key} = $reservable_info;
13986:     }
13987:     return $reservable_info;
13988: }
13989: 
13990: sub get_course_slots {
13991:     my ($cnum,$cdom) = @_;
13992:     my $hashid=$cnum.':'.$cdom;
13993:     my ($result,$cached) = &is_cached_new('allslots',$hashid);
13994:     if (defined($cached)) {
13995:         if (ref($result) eq 'HASH') {
13996:             return %{$result};
13997:         }
13998:     } else {
13999:         my %slots=&dump('slots',$cdom,$cnum);
14000:         my ($tmp) = keys(%slots);
14001:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
14002:             &do_cache_new('allslots',$hashid,\%slots,600);
14003:             return %slots;
14004:         }
14005:     }
14006:     return;
14007: }
14008: 
14009: sub devalidate_slots_cache {
14010:     my ($cnum,$cdom)=@_;
14011:     my $hashid=$cnum.':'.$cdom;
14012:     &devalidate_cache_new('allslots',$hashid);
14013: }
14014: 
14015: sub get_coursechange {
14016:     my ($cdom,$cnum) = @_;
14017:     if ($cdom eq '' || $cnum eq '') {
14018:         return unless ($env{'request.course.id'});
14019:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
14020:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
14021:     }
14022:     my $hashid=$cdom.'_'.$cnum;
14023:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
14024:     if ((defined($cached)) && ($change ne '')) {
14025:         return $change;
14026:     } else {
14027:         my %crshash;
14028:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
14029:         if ($crshash{'internal.contentchange'} eq '') {
14030:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
14031:             if ($change eq '') {
14032:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
14033:                 $change = $crshash{'internal.created'};
14034:             }
14035:         } else {
14036:             $change = $crshash{'internal.contentchange'};
14037:         }
14038:         my $cachetime = 600;
14039:         &do_cache_new('crschange',$hashid,$change,$cachetime);
14040:     }
14041:     return $change;
14042: }
14043: 
14044: sub devalidate_coursechange_cache {
14045:     my ($cdom,$cnum)=@_;
14046:     my $hashid=$cdom.'_'.$cnum;
14047:     &devalidate_cache_new('crschange',$hashid);
14048: }
14049: 
14050: sub get_suppchange {
14051:     my ($cdom,$cnum) = @_;
14052:     if ($cdom eq '' || $cnum eq '') {
14053:         return unless ($env{'request.course.id'});
14054:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
14055:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
14056:     }
14057:     my $hashid=$cdom.'_'.$cnum;
14058:     my ($change,$cached)=&is_cached_new('suppchange',$hashid);
14059:     if ((defined($cached)) && ($change ne '')) {
14060:         return $change;
14061:     } else {
14062:         my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
14063:         if ($crshash{'internal.supplementalchange'} eq '') {
14064:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
14065:             if ($change eq '') {
14066:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
14067:                 $change = $crshash{'internal.created'};
14068:             }
14069:         } else {
14070:             $change = $crshash{'internal.supplementalchange'};
14071:         }
14072:         my $cachetime = 600;
14073:         &do_cache_new('suppchange',$hashid,$change,$cachetime);
14074:     }
14075:     return $change;
14076: }
14077: 
14078: sub devalidate_suppchange_cache {
14079:     my ($cdom,$cnum)=@_;
14080:     my $hashid=$cdom.'_'.$cnum;
14081:     &devalidate_cache_new('suppchange',$hashid);
14082: }
14083: 
14084: sub update_supp_caches {
14085:     my ($cdom,$cnum) = @_;
14086:     my %servers = &internet_dom_servers($cdom);
14087:     my @ids=&current_machine_ids();
14088:     foreach my $server (keys(%servers)) {
14089:         next if (grep(/^\Q$server\E$/,@ids));
14090:         my $hashid=$cnum.':'.$cdom;
14091:         my $cachekey = &escape('showsupp').':'.&escape($hashid);
14092:         &remote_devalidate_cache($server,[$cachekey]);
14093:     }
14094:     &has_unhidden_suppfiles($cnum,$cdom,1,1);
14095:     &count_supptools($cnum,$cdom,1);
14096:     my $now = time;
14097:     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
14098:         &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
14099:     }
14100:     &put('environment',{'internal.supplementalchange' => $now},
14101:          $cdom,$cnum);
14102:     &Apache::lonnet::appenv(
14103:         {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
14104:     &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
14105: }
14106: 
14107: # ------------------------------------------------- Update symbolic store links
14108: 
14109: sub symblist {
14110:     my ($mapname,%newhash)=@_;
14111:     $mapname=&deversion(&declutter($mapname));
14112:     my %hash;
14113:     if (($env{'request.course.fn'}) && (%newhash)) {
14114:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
14115:                       &GDBM_WRCREAT(),0640)) {
14116: 	    foreach my $url (keys(%newhash)) {
14117: 		next if ($url eq 'last_known'
14118: 			 && $env{'form.no_update_last_known'});
14119: 		$hash{declutter($url)}=&encode_symb($mapname,
14120: 						    $newhash{$url}->[1],
14121: 						    $newhash{$url}->[0]);
14122:             }
14123:             if (untie(%hash)) {
14124: 		return 'ok';
14125:             }
14126:         }
14127:     }
14128:     return 'error';
14129: }
14130: 
14131: # --------------------------------------------------------------- Verify a symb
14132: 
14133: sub symbverify {
14134:     my ($symb,$thisurl,$encstate)=@_;
14135:     my $thisfn=$thisurl;
14136:     $thisfn=&declutter($thisfn);
14137: # direct jump to resource in page or to a sequence - will construct own symbs
14138:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
14139: # check URL part
14140:     my ($map,$resid,$url)=&decode_symb($symb);
14141: 
14142:     unless ($url eq $thisfn) { return 0; }
14143: 
14144:     $symb=&symbclean($symb);
14145:     $thisurl=&deversion($thisurl);
14146:     $thisfn=&deversion($thisfn);
14147: 
14148:     my %bighash;
14149:     my $okay=0;
14150: 
14151:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14152:                             &GDBM_READER(),0640)) {
14153:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
14154:             $thisurl =~ s/\?.+$//;
14155:             if ($map =~ m{^uploaded/.+\.page$}) {
14156:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
14157:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
14158:             }
14159:         }
14160:         my $ids;
14161:         if ($map =~ m{^uploaded/.+\.page$}) {
14162:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
14163:         } else {
14164:             $ids=$bighash{'ids_'.&clutter($thisurl)};
14165:         }
14166:         unless ($ids) {
14167:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
14168:             $ids=$bighash{$idkey};
14169:         }
14170:         if ($ids) {
14171: # ------------------------------------------------------------------- Has ID(s)
14172:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
14173:                 $symb =~ s/\?.+$//;
14174:             }
14175: 	    foreach my $id (split(/\,/,$ids)) {
14176: 	       my ($mapid,$resid)=split(/\./,$id);
14177:                if (
14178:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
14179:    eq $symb) {
14180:                    if (ref($encstate)) {
14181:                        $$encstate = $bighash{'encrypted_'.$id};
14182:                    }
14183: 		   if (($env{'request.role.adv'}) ||
14184: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
14185:                        ($thisurl eq '/adm/navmaps')) {
14186: 		       $okay=1;
14187:                        last;
14188: 		   }
14189: 	       }
14190: 	   }
14191:         }
14192: 	untie(%bighash);
14193:     }
14194:     return $okay;
14195: }
14196: 
14197: # --------------------------------------------------------------- Clean-up symb
14198: 
14199: sub symbclean {
14200:     my $symb=shift;
14201:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
14202: # remove version from map
14203:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
14204: 
14205: # remove version from URL
14206:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
14207: 
14208: # remove wrapper
14209: 
14210:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
14211:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
14212:     return $symb;
14213: }
14214: 
14215: # ---------------------------------------------- Split symb to find map and url
14216: 
14217: sub encode_symb {
14218:     my ($map,$resid,$url)=@_;
14219:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
14220: }
14221: 
14222: sub decode_symb {
14223:     my $symb=shift;
14224:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
14225:     my ($map,$resid,$url)=split(/___/,$symb);
14226:     return (&fixversion($map),$resid,&fixversion($url));
14227: }
14228: 
14229: sub fixversion {
14230:     my $fn=shift;
14231:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
14232:     my %bighash;
14233:     my $uri=&clutter($fn);
14234:     my $key=$env{'request.course.id'}.'_'.$uri;
14235: # is this cached?
14236:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
14237:     if (defined($cached)) { return $result; }
14238: # unfortunately not cached, or expired
14239:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14240: 	    &GDBM_READER(),0640)) {
14241:  	if ($bighash{'version_'.$uri}) {
14242:  	    my $version=$bighash{'version_'.$uri};
14243:  	    unless (($version eq 'mostrecent') || 
14244: 		    ($version==&getversion($uri))) {
14245:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
14246:  	    }
14247:  	}
14248:  	untie %bighash;
14249:     }
14250:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
14251: }
14252: 
14253: sub deversion {
14254:     my $url=shift;
14255:     $url=~s/\.\d+\.(\w+)$/\.$1/;
14256:     return $url;
14257: }
14258: 
14259: # ------------------------------------------------------ Return symb list entry
14260: 
14261: sub symbread {
14262:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
14263:         $ignoresymbdb,$noenccheck)=@_;
14264:     my $cache_str='request.symbread.cached.'.$thisfn;
14265:     if (defined($env{$cache_str})) {
14266:         unless (ref($possibles) eq 'HASH') {
14267:             if ($ignorecachednull) {
14268:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
14269:             } else {
14270:                 return $env{$cache_str};
14271:             }
14272:         }
14273:     }
14274: # no filename provided? try from environment
14275:     unless ($thisfn) {
14276:         if ($env{'request.symb'}) {
14277:             return $env{$cache_str}=&symbclean($env{'request.symb'});
14278: 	}
14279: 	$thisfn=$env{'request.filename'};
14280:     }
14281:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14282: # is that filename actually a symb? Verify, clean, and return
14283:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
14284: 	if (&symbverify($thisfn,$1)) {
14285: 	    return $env{$cache_str}=&symbclean($thisfn);
14286: 	}
14287:     }
14288:     $thisfn=declutter($thisfn);
14289:     my %hash;
14290:     my %bighash;
14291:     my $syval='';
14292:     if (($env{'request.course.fn'}) && ($thisfn)) {
14293:         unless ($ignoresymbdb) {
14294:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
14295:                           &GDBM_READER(),0640)) {
14296: 	        $syval=$hash{$thisfn};
14297:                 untie(%hash);
14298:             }
14299:             if ($syval && $checkforblock) {
14300:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
14301:                 if (@blockers) {
14302:                     $syval='';
14303:                 }
14304:             }
14305:         }
14306: # ---------------------------------------------------------- There was an entry
14307:         if ($syval) {
14308: 	    #unless ($syval=~/\_\d+$/) {
14309: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
14310: 		    #&appenv({'request.ambiguous' => $thisfn});
14311: 		    #return $env{$cache_str}='';
14312: 		#}    
14313: 		#$syval.=$1;
14314: 	    #}
14315:         } else {
14316: # ------------------------------------------------------- Was not in symb table
14317:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14318:                             &GDBM_READER(),0640)) {
14319: # ---------------------------------------------- Get ID(s) for current resource
14320:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
14321:               unless ($ids) { 
14322:                  $ids=$bighash{'ids_/'.$thisfn};
14323:               }
14324:               unless ($ids) {
14325: # alias?
14326: 		  $ids=$bighash{'mapalias_'.$thisfn};
14327:               }
14328:               if ($ids) {
14329: # ------------------------------------------------------------------- Has ID(s)
14330:                  my @possibilities=split(/\,/,$ids);
14331:                  if ($#possibilities==0) {
14332: # ----------------------------------------------- There is only one possibility
14333: 		     my ($mapid,$resid)=split(/\./,$ids);
14334: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
14335: 						    $resid,$thisfn);
14336:                      if (ref($possibles) eq 'HASH') {
14337:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
14338:                              $possibles->{$syval} = 1;
14339:                          }
14340:                      }
14341:                      if ($checkforblock) {
14342:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
14343:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
14344:                              if (@blockers) {
14345:                                  $syval = '';
14346:                                  untie(%bighash);
14347:                                  return $env{$cache_str}='';
14348:                              }
14349:                          }
14350:                      }
14351:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
14352: # ------------------------------------------ There is more than one possibility
14353:                      my $realpossible=0;
14354:                      foreach my $id (@possibilities) {
14355: 			 my $file=$bighash{'src_'.$id};
14356:                          my $canaccess;
14357:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
14358:                              $canaccess = 1;
14359:                          } else { 
14360:                              $canaccess = &allowed('bre',$file);
14361:                          }
14362:                          if ($canaccess) {
14363:          		     my ($mapid,$resid)=split(/\./,$id);
14364:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
14365:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
14366: 						             $resid,$thisfn);
14367:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
14368:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
14369:                                  if ($checkforblock) {
14370:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
14371:                                      if (@blockers > 0) {
14372:                                          $syval = '';
14373:                                      } else {
14374:                                          $syval = $poss_syval;
14375:                                          $realpossible++;
14376:                                      }
14377:                                  } else {
14378:                                      $syval = $poss_syval;
14379:                                      $realpossible++;
14380:                                  }
14381:                                  if ($syval) {
14382:                                      if (ref($possibles) eq 'HASH') {
14383:                                          $possibles->{$syval} = 1;
14384:                                      }
14385:                                  }
14386:                              }
14387: 			 }
14388:                      }
14389: 		     if ($realpossible!=1) { $syval=''; }
14390:                  } else {
14391:                      $syval='';
14392:                  }
14393: 	      }
14394:               untie(%bighash);
14395:            }
14396:         }
14397:         if ($syval) {
14398: 	    return $env{$cache_str}=$syval;
14399:         }
14400:     }
14401:     &appenv({'request.ambiguous' => $thisfn});
14402:     return $env{$cache_str}='';
14403: }
14404: 
14405: # ---------------------------------------------------------- Return random seed
14406: 
14407: sub numval {
14408:     my $txt=shift;
14409:     $txt=~tr/A-J/0-9/;
14410:     $txt=~tr/a-j/0-9/;
14411:     $txt=~tr/K-T/0-9/;
14412:     $txt=~tr/k-t/0-9/;
14413:     $txt=~tr/U-Z/0-5/;
14414:     $txt=~tr/u-z/0-5/;
14415:     $txt=~s/\D//g;
14416:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
14417:     return int($txt);
14418: }
14419: 
14420: sub numval2 {
14421:     my $txt=shift;
14422:     $txt=~tr/A-J/0-9/;
14423:     $txt=~tr/a-j/0-9/;
14424:     $txt=~tr/K-T/0-9/;
14425:     $txt=~tr/k-t/0-9/;
14426:     $txt=~tr/U-Z/0-5/;
14427:     $txt=~tr/u-z/0-5/;
14428:     $txt=~s/\D//g;
14429:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
14430:     my $total;
14431:     foreach my $val (@txts) { $total+=$val; }
14432:     if ($_64bit) { if ($total > 2**32) { return -1; } }
14433:     return int($total);
14434: }
14435: 
14436: sub numval3 {
14437:     use integer;
14438:     my $txt=shift;
14439:     $txt=~tr/A-J/0-9/;
14440:     $txt=~tr/a-j/0-9/;
14441:     $txt=~tr/K-T/0-9/;
14442:     $txt=~tr/k-t/0-9/;
14443:     $txt=~tr/U-Z/0-5/;
14444:     $txt=~tr/u-z/0-5/;
14445:     $txt=~s/\D//g;
14446:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
14447:     my $total;
14448:     foreach my $val (@txts) { $total+=$val; }
14449:     if ($_64bit) { $total=(($total<<32)>>32); }
14450:     return $total;
14451: }
14452: 
14453: sub digest {
14454:     my ($data)=@_;
14455:     my $digest=&Digest::MD5::md5($data);
14456:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
14457:     my ($e,$f);
14458:     {
14459:         use integer;
14460:         $e=($a+$b);
14461:         $f=($c+$d);
14462:         if ($_64bit) {
14463:             $e=(($e<<32)>>32);
14464:             $f=(($f<<32)>>32);
14465:         }
14466:     }
14467:     if (wantarray) {
14468: 	return ($e,$f);
14469:     } else {
14470: 	my $g;
14471: 	{
14472: 	    use integer;
14473: 	    $g=($e+$f);
14474: 	    if ($_64bit) {
14475: 		$g=(($g<<32)>>32);
14476: 	    }
14477: 	}
14478: 	return $g;
14479:     }
14480: }
14481: 
14482: sub latest_rnd_algorithm_id {
14483:     return '64bit5';
14484: }
14485: 
14486: sub get_rand_alg {
14487:     my ($courseid)=@_;
14488:     if (!$courseid) { $courseid=(&whichuser())[1]; }
14489:     if ($courseid) {
14490: 	return $env{"course.$courseid.rndseed"};
14491:     }
14492:     return &latest_rnd_algorithm_id();
14493: }
14494: 
14495: sub validCODE {
14496:     my ($CODE)=@_;
14497:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
14498:     return 0;
14499: }
14500: 
14501: sub getCODE {
14502:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
14503:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
14504: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
14505: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
14506: 	return $Apache::lonhomework::history{'resource.CODE'};
14507:     }
14508:     return undef;
14509: }
14510: #
14511: #  Determines the random seed for a specific context:
14512: #
14513: # parameters:
14514: #   symb      - in course context the symb for the seed.
14515: #   course_id - The course id of the form domain_coursenum.
14516: #   domain    - Domain for the user.
14517: #   course    - Course for the user.
14518: #   cenv      - environment of the course.
14519: #
14520: # NOTE:
14521: #   All parameters are picked out of the environment if missing
14522: #   or not defined.
14523: #   If a symb cannot be determined the current time is used instead.
14524: #
14525: #  For a given well defined symb, courside, domain, username,
14526: #  and course environment, the seed is reproducible.
14527: #
14528: sub rndseed {
14529:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
14530:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
14531:     if (!defined($symb)) {
14532: 	unless ($symb=$wsymb) { return time; }
14533:     }
14534:     if (!defined $courseid) { 
14535: 	$courseid=$wcourseid; 
14536:     }
14537:     if (!defined $domain) { $domain=$wdomain; }
14538:     if (!defined $username) { $username=$wusername }
14539: 
14540:     my $which;
14541:     if (defined($cenv->{'rndseed'})) {
14542: 	$which = $cenv->{'rndseed'};
14543:     } else {
14544: 	$which =&get_rand_alg($courseid);
14545:     }
14546:     if (defined(&getCODE())) {
14547: 
14548: 	if ($which eq '64bit5') {
14549: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14550: 	} elsif ($which eq '64bit4') {
14551: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14552: 	} else {
14553: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14554: 	}
14555:     } elsif ($which eq '64bit5') {
14556: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
14557:     } elsif ($which eq '64bit4') {
14558: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
14559:     } elsif ($which eq '64bit3') {
14560: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
14561:     } elsif ($which eq '64bit2') {
14562: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
14563:     } elsif ($which eq '64bit') {
14564: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
14565:     }
14566:     return &rndseed_32bit($symb,$courseid,$domain,$username);
14567: }
14568: 
14569: sub rndseed_32bit {
14570:     my ($symb,$courseid,$domain,$username)=@_;
14571:     {
14572: 	use integer;
14573: 	my $symbchck=unpack("%32C*",$symb) << 27;
14574: 	my $symbseed=numval($symb) << 22;
14575: 	my $namechck=unpack("%32C*",$username) << 17;
14576: 	my $nameseed=numval($username) << 12;
14577: 	my $domainseed=unpack("%32C*",$domain) << 7;
14578: 	my $courseseed=unpack("%32C*",$courseid);
14579: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
14580: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14581: 	#&logthis("rndseed :$num:$symb");
14582: 	if ($_64bit) { $num=(($num<<32)>>32); }
14583: 	return $num;
14584:     }
14585: }
14586: 
14587: sub rndseed_64bit {
14588:     my ($symb,$courseid,$domain,$username)=@_;
14589:     {
14590: 	use integer;
14591: 	my $symbchck=unpack("%32S*",$symb) << 21;
14592: 	my $symbseed=numval($symb) << 10;
14593: 	my $namechck=unpack("%32S*",$username);
14594: 	
14595: 	my $nameseed=numval($username) << 21;
14596: 	my $domainseed=unpack("%32S*",$domain) << 10;
14597: 	my $courseseed=unpack("%32S*",$courseid);
14598: 	
14599: 	my $num1=$symbchck+$symbseed+$namechck;
14600: 	my $num2=$nameseed+$domainseed+$courseseed;
14601: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14602: 	#&logthis("rndseed :$num:$symb");
14603: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14604: 	return "$num1,$num2";
14605:     }
14606: }
14607: 
14608: sub rndseed_64bit2 {
14609:     my ($symb,$courseid,$domain,$username)=@_;
14610:     {
14611: 	use integer;
14612: 	# strings need to be an even # of cahracters long, it it is odd the
14613:         # last characters gets thrown away
14614: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14615: 	my $symbseed=numval($symb) << 10;
14616: 	my $namechck=unpack("%32S*",$username.' ');
14617: 	
14618: 	my $nameseed=numval($username) << 21;
14619: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14620: 	my $courseseed=unpack("%32S*",$courseid.' ');
14621: 	
14622: 	my $num1=$symbchck+$symbseed+$namechck;
14623: 	my $num2=$nameseed+$domainseed+$courseseed;
14624: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14625: 	#&logthis("rndseed :$num:$symb");
14626: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14627: 	return "$num1,$num2";
14628:     }
14629: }
14630: 
14631: sub rndseed_64bit3 {
14632:     my ($symb,$courseid,$domain,$username)=@_;
14633:     {
14634: 	use integer;
14635: 	# strings need to be an even # of cahracters long, it it is odd the
14636:         # last characters gets thrown away
14637: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14638: 	my $symbseed=numval2($symb) << 10;
14639: 	my $namechck=unpack("%32S*",$username.' ');
14640: 	
14641: 	my $nameseed=numval2($username) << 21;
14642: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14643: 	my $courseseed=unpack("%32S*",$courseid.' ');
14644: 	
14645: 	my $num1=$symbchck+$symbseed+$namechck;
14646: 	my $num2=$nameseed+$domainseed+$courseseed;
14647: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14648: 	#&logthis("rndseed :$num1:$num2:$_64bit");
14649: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14650: 	
14651: 	return "$num1:$num2";
14652:     }
14653: }
14654: 
14655: sub rndseed_64bit4 {
14656:     my ($symb,$courseid,$domain,$username)=@_;
14657:     {
14658: 	use integer;
14659: 	# strings need to be an even # of cahracters long, it it is odd the
14660:         # last characters gets thrown away
14661: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14662: 	my $symbseed=numval3($symb) << 10;
14663: 	my $namechck=unpack("%32S*",$username.' ');
14664: 	
14665: 	my $nameseed=numval3($username) << 21;
14666: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14667: 	my $courseseed=unpack("%32S*",$courseid.' ');
14668: 	
14669: 	my $num1=$symbchck+$symbseed+$namechck;
14670: 	my $num2=$nameseed+$domainseed+$courseseed;
14671: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14672: 	#&logthis("rndseed :$num1:$num2:$_64bit");
14673: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14674: 	
14675: 	return "$num1:$num2";
14676:     }
14677: }
14678: 
14679: sub rndseed_64bit5 {
14680:     my ($symb,$courseid,$domain,$username)=@_;
14681:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14682:     return "$num1:$num2";
14683: }
14684: 
14685: sub rndseed_CODE_64bit {
14686:     my ($symb,$courseid,$domain,$username)=@_;
14687:     {
14688: 	use integer;
14689: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
14690: 	my $symbseed=numval2($symb);
14691: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14692: 	my $CODEseed=numval(&getCODE());
14693: 	my $courseseed=unpack("%32S*",$courseid.' ');
14694: 	my $num1=$symbseed+$CODEchck;
14695: 	my $num2=$CODEseed+$courseseed+$symbchck;
14696: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14697: 	#&logthis("rndseed :$num1:$num2:$symb");
14698: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
14699: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
14700: 	return "$num1:$num2";
14701:     }
14702: }
14703: 
14704: sub rndseed_CODE_64bit4 {
14705:     my ($symb,$courseid,$domain,$username)=@_;
14706:     {
14707: 	use integer;
14708: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
14709: 	my $symbseed=numval3($symb);
14710: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14711: 	my $CODEseed=numval3(&getCODE());
14712: 	my $courseseed=unpack("%32S*",$courseid.' ');
14713: 	my $num1=$symbseed+$CODEchck;
14714: 	my $num2=$CODEseed+$courseseed+$symbchck;
14715: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14716: 	#&logthis("rndseed :$num1:$num2:$symb");
14717: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
14718: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
14719: 	return "$num1:$num2";
14720:     }
14721: }
14722: 
14723: sub rndseed_CODE_64bit5 {
14724:     my ($symb,$courseid,$domain,$username)=@_;
14725:     my $code = &getCODE();
14726:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
14727:     return "$num1:$num2";
14728: }
14729: 
14730: sub setup_random_from_rndseed {
14731:     my ($rndseed)=@_;
14732:     if ($rndseed =~/([,:])/) {
14733:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14734:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14735:             &Math::Random::random_set_seed_from_phrase($rndseed);
14736:         } else {
14737:             &Math::Random::random_set_seed($num1,$num2);
14738:         }
14739:     } else {
14740: 	&Math::Random::random_set_seed_from_phrase($rndseed);
14741:     }
14742: }
14743: 
14744: sub latest_receipt_algorithm_id {
14745:     return 'receipt3';
14746: }
14747: 
14748: sub recunique {
14749:     my $fucourseid=shift;
14750:     my $unique;
14751:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14752: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14753: 	$unique=$env{"course.$fucourseid.internal.encseed"};
14754:     } else {
14755: 	$unique=$perlvar{'lonReceipt'};
14756:     }
14757:     return unpack("%32C*",$unique);
14758: }
14759: 
14760: sub recprefix {
14761:     my $fucourseid=shift;
14762:     my $prefix;
14763:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14764: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14765: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
14766:     } else {
14767: 	$prefix=$perlvar{'lonHostID'};
14768:     }
14769:     return unpack("%32C*",$prefix);
14770: }
14771: 
14772: sub ireceipt {
14773:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
14774: 
14775:     my $return =&recprefix($fucourseid).'-';
14776: 
14777:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14778: 	$env{'request.state'} eq 'construct') {
14779: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14780: 	return $return;
14781:     }
14782: 
14783:     my $cuname=unpack("%32C*",$funame);
14784:     my $cudom=unpack("%32C*",$fudom);
14785:     my $cucourseid=unpack("%32C*",$fucourseid);
14786:     my $cusymb=unpack("%32C*",$fusymb);
14787:     my $cunique=&recunique($fucourseid);
14788:     my $cpart=unpack("%32S*",$part);
14789:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14790: 
14791: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
14792: 			       
14793: 	$return.= ($cunique%$cuname+
14794: 		   $cunique%$cudom+
14795: 		   $cusymb%$cuname+
14796: 		   $cusymb%$cudom+
14797: 		   $cucourseid%$cuname+
14798: 		   $cucourseid%$cudom+
14799: 		   $cpart%$cuname+
14800: 		   $cpart%$cudom);
14801:     } else {
14802: 	$return.= ($cunique%$cuname+
14803: 		   $cunique%$cudom+
14804: 		   $cusymb%$cuname+
14805: 		   $cusymb%$cudom+
14806: 		   $cucourseid%$cuname+
14807: 		   $cucourseid%$cudom);
14808:     }
14809:     return $return;
14810: }
14811: 
14812: sub receipt {
14813:     my ($part)=@_;
14814:     my ($symb,$courseid,$domain,$name) = &whichuser();
14815:     return &ireceipt($name,$domain,$courseid,$symb,$part);
14816: }
14817: 
14818: sub whichuser {
14819:     my ($passedsymb)=@_;
14820:     my ($symb,$courseid,$domain,$name,$publicuser);
14821:     if (defined($env{'form.grade_symb'})) {
14822: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14823: 	my $allowed=&allowed('vgr',$tmp_courseid);
14824: 	if (!$allowed &&
14825: 	    exists($env{'request.course.sec'}) &&
14826: 	    $env{'request.course.sec'} !~ /^\s*$/) {
14827: 	    $allowed=&allowed('vgr',$tmp_courseid.
14828: 			      '/'.$env{'request.course.sec'});
14829: 	}
14830: 	if ($allowed) {
14831: 	    ($symb)=&get_env_multiple('form.grade_symb');
14832: 	    $courseid=$tmp_courseid;
14833: 	    ($domain)=&get_env_multiple('form.grade_domain');
14834: 	    ($name)=&get_env_multiple('form.grade_username');
14835: 	    return ($symb,$courseid,$domain,$name,$publicuser);
14836: 	}
14837:     }
14838:     if (!$passedsymb) {
14839: 	$symb=&symbread();
14840:     } else {
14841: 	$symb=$passedsymb;
14842:     }
14843:     $courseid=$env{'request.course.id'};
14844:     $domain=$env{'user.domain'};
14845:     $name=$env{'user.name'};
14846:     if ($name eq 'public' && $domain eq 'public') {
14847: 	if (!defined($env{'form.username'})) {
14848: 	    $env{'form.username'}.=time.rand(10000000);
14849: 	}
14850: 	$name.=$env{'form.username'};
14851:     }
14852:     return ($symb,$courseid,$domain,$name,$publicuser);
14853: 
14854: }
14855: 
14856: # ------------------------------------------------------------ Serves up a file
14857: # returns either the contents of the file or 
14858: # -1 if the file doesn't exist
14859: #
14860: # if the target is a file that was uploaded via DOCS, 
14861: # a check will be made to see if a current copy exists on the local server,
14862: # if it does this will be served, otherwise a copy will be retrieved from
14863: # the home server for the course and stored in /home/httpd/html/userfiles on
14864: # the local server.   
14865: 
14866: sub getfile {
14867:     my ($file) = @_;
14868:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
14869:     &repcopy($file);
14870:     return &readfile($file);
14871: }
14872: 
14873: sub repcopy_userfile {
14874:     my ($file)=@_;
14875:     my $londocroot = $perlvar{'lonDocRoot'};
14876:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
14877:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
14878:     my ($cdom,$cnum,$filename) = 
14879: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
14880:     my $uri="/uploaded/$cdom/$cnum/$filename";
14881:     if (-e "$file") {
14882: # we already have a local copy, check it out
14883: 	my @fileinfo = stat($file);
14884: 	my $rtncode;
14885: 	my $info;
14886: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
14887: 	if ($lwpresp ne 'ok') {
14888: # there is no such file anymore, even though we had a local copy
14889: 	    if ($rtncode eq '404') {
14890: 		unlink($file);
14891: 	    }
14892: 	    return -1;
14893: 	}
14894: 	if ($info < $fileinfo[9]) {
14895: # nice, the file we have is up-to-date, just say okay
14896: 	    return 'ok';
14897: 	} else {
14898: # the file is outdated, get rid of it
14899: 	    unlink($file);
14900: 	}
14901:     }
14902: # one way or the other, at this point, we don't have the file
14903: # construct the correct path for the file
14904:     my @parts = ($cdom,$cnum); 
14905:     if ($filename =~ m|^(.+)/[^/]+$|) {
14906: 	push @parts, split(/\//,$1);
14907:     }
14908:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14909:     foreach my $part (@parts) {
14910: 	$path .= '/'.$part;
14911: 	if (!-e $path) {
14912: 	    mkdir($path,0770);
14913: 	}
14914:     }
14915: # now the path exists for sure
14916: # get a user agent
14917:     my $transferfile=$file.'.in.transfer';
14918: # FIXME: this should flock
14919:     if (-e $transferfile) { return 'ok'; }
14920:     my $request;
14921:     $uri=~s/^\///;
14922:     my $homeserver = &homeserver($cnum,$cdom);
14923:     my $hostname = &hostname($homeserver);
14924:     my $protocol = $protocol{$homeserver};
14925:     $protocol = 'http' if ($protocol ne 'https');
14926:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
14927:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
14928: # did it work?
14929:     if ($response->is_error()) {
14930: 	unlink($transferfile);
14931: 	&logthis("Userfile repcopy failed for $uri");
14932: 	return -1;
14933:     }
14934: # worked, rename the transfer file
14935:     rename($transferfile,$file);
14936:     return 'ok';
14937: }
14938: 
14939: sub tokenwrapper {
14940:     my $uri=shift;
14941:     $uri=~s|^https?\://([^/]+)||;
14942:     $uri=~s|^/||;
14943:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14944:     my $token=$1;
14945:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14946:     if ($udom && $uname && $file) {
14947: 	$file=~s|(\?\.*)*$||;
14948:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14949:         my $homeserver = &homeserver($uname,$udom);
14950:         my $hostname = &hostname($homeserver);
14951:         my $protocol = $protocol{$homeserver};
14952:         $protocol = 'http' if ($protocol ne 'https');
14953:         return $protocol.'://'.$hostname.'/'.$uri.
14954:                (($uri=~/\?/)?'&':'?').'token='.$token.
14955:                                '&tokenissued='.$perlvar{'lonHostID'};
14956:     } else {
14957:         return '/adm/notfound.html';
14958:     }
14959: }
14960: 
14961: # call with reqtype HEAD: get last modification time
14962: # call with reqtype GET: get the file contents
14963: # Do not call this with reqtype GET for large files! It loads everything into memory
14964: #
14965: sub getuploaded {
14966:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14967:     $uri=~s/^\///;
14968:     my $homeserver = &homeserver($cnum,$cdom);
14969:     my $hostname = &hostname($homeserver);
14970:     my $protocol = $protocol{$homeserver};
14971:     $protocol = 'http' if ($protocol ne 'https');
14972:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14973:     my $request=new HTTP::Request($reqtype,$uri);
14974:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14975:     $$rtncode = $response->code;
14976:     if (! $response->is_success()) {
14977: 	return 'failed';
14978:     }      
14979:     if ($reqtype eq 'HEAD') {
14980: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14981:     } elsif ($reqtype eq 'GET') {
14982: 	$$info = $response->content;
14983:     }
14984:     return 'ok';
14985: }
14986: 
14987: sub readfile {
14988:     my $file = shift;
14989:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14990:     my $fh;
14991:     open($fh,"<",$file);
14992:     my $a='';
14993:     while (my $line = <$fh>) { $a .= $line; }
14994:     return $a;
14995: }
14996: 
14997: sub filelocation {
14998:     my ($dir,$file) = @_;
14999:     my $location;
15000:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
15001: 
15002:     if ($file =~ m-^/adm/-) {
15003: 	$file=~s-^/adm/wrapper/-/-;
15004: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
15005:     }
15006: 
15007:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
15008:         $location = $file;
15009:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
15010:         my ($udom,$uname,$filename)=
15011:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
15012:         my $home=&homeserver($uname,$udom);
15013:         my $is_me=0;
15014:         my @ids=&current_machine_ids();
15015:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
15016:         if ($is_me) {
15017:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
15018:         } else {
15019:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
15020:   	      $udom.'/'.$uname.'/'.$filename;
15021:         }
15022:     } elsif ($file =~ m-^/adm/-) {
15023: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
15024:     } else {
15025:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
15026:         $file=~s:^/(res|priv)/:/:;
15027:         my $space=$1;
15028:         if ( !( $file =~ m:^/:) ) {
15029:             $location = $dir. '/'.$file;
15030:         } else {
15031:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
15032:         }
15033:     }
15034:     $location=~s://+:/:g; # remove duplicate /
15035:     while ($location=~m{/\.\./}) {
15036: 	if ($location =~ m{/[^/]+/\.\./}) {
15037: 	    $location=~ s{/[^/]+/\.\./}{/}g;
15038: 	} else {
15039: 	    $location=~ s{/\.\./}{/}g;
15040: 	}
15041:     } #remove dir/..
15042:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
15043:     return $location;
15044: }
15045: 
15046: sub hreflocation {
15047:     my ($dir,$file)=@_;
15048:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
15049: 	$file=filelocation($dir,$file);
15050:     } elsif ($file=~m-^/adm/-) {
15051: 	$file=~s-^/adm/wrapper/-/-;
15052: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
15053:     }
15054:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
15055: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
15056:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
15057: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
15058: 	        {/uploaded/$1/$2/}x;
15059:     }
15060:     if ($file=~ m{^/userfiles/}) {
15061: 	$file =~ s{^/userfiles/}{/uploaded/};
15062:     }
15063:     return $file;
15064: }
15065: 
15066: 
15067: 
15068: 
15069: 
15070: sub current_machine_domains {
15071:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
15072: }
15073: 
15074: sub machine_domains {
15075:     my ($hostname) = @_;
15076:     my @domains;
15077:     my %hostname = &all_hostnames();
15078:     while( my($id, $name) = each(%hostname)) {
15079: #	&logthis("-$id-$name-$hostname-");
15080: 	if ($hostname eq $name) {
15081: 	    push(@domains,&host_domain($id));
15082: 	}
15083:     }
15084:     return @domains;
15085: }
15086: 
15087: sub current_machine_ids {
15088:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
15089: }
15090: 
15091: sub machine_ids {
15092:     my ($hostname) = @_;
15093:     $hostname ||= &hostname($perlvar{'lonHostID'});
15094:     my @ids;
15095:     my %name_to_host = &all_names();
15096:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
15097: 	return @{ $name_to_host{$hostname} };
15098:     }
15099:     return;
15100: }
15101: 
15102: sub additional_machine_domains {
15103:     my @domains;
15104:     if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
15105:         if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
15106:             while (my $line = <$fh>) {
15107:                 chomp($line);           
15108:                 $line =~ s/\s//g;
15109:                 push(@domains,$line);
15110:             }
15111:             close($fh);
15112:         }
15113:     }
15114:     return @domains;
15115: }
15116: 
15117: sub default_login_domain {
15118:     my $domain = $perlvar{'lonDefDomain'};
15119:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
15120:     foreach my $posdom (&current_machine_domains(),
15121:                         &additional_machine_domains()) {
15122:         if (lc($posdom) eq lc($testdomain)) {
15123:             $domain=$posdom;
15124:             last;
15125:         }
15126:     }
15127:     return $domain;
15128: }
15129: 
15130: sub shared_institution {
15131:     my ($dom,$lonhost) = @_;
15132:     if ($lonhost eq '') {
15133:         $lonhost = $perlvar{'lonHostID'};
15134:     }
15135:     my $same_intdom;
15136:     my $hostintdom = &internet_dom($lonhost);
15137:     if ($hostintdom ne '') {
15138:         my %iphost = &get_iphost();
15139:         my $primary_id = &domain($dom,'primary');
15140:         my $primary_ip = &get_host_ip($primary_id);
15141:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
15142:             foreach my $id (@{$iphost{$primary_ip}}) {
15143:                 my $intdom = &internet_dom($id);
15144:                 if ($intdom eq $hostintdom) {
15145:                     $same_intdom = 1;
15146:                     last;
15147:                 }
15148:             }
15149:         }
15150:     }
15151:     return $same_intdom;
15152: }
15153: 
15154: sub uses_sts {
15155:     my ($ignore_cache) = @_;
15156:     my $lonhost = $perlvar{'lonHostID'};
15157:     my $hostname = &hostname($lonhost);
15158:     my $sts_on;
15159:     if ($protocol{$lonhost} eq 'https') {
15160:         my $cachetime = 12*3600;
15161:         if (!$ignore_cache) {
15162:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
15163:             if (defined($cached)) {
15164:                 return $sts_on;
15165:             }
15166:         }
15167:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
15168:         my $request=new HTTP::Request('HEAD',$url);
15169:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
15170:         if ($response->is_success) {
15171:             my $has_sts = $response->header('Strict-Transport-Security');
15172:             if ($has_sts eq '') {
15173:                 $sts_on = 0;
15174:             } else {
15175:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
15176:                     my $maxage = $1;
15177:                     if ($maxage) {
15178:                         $sts_on = 1;
15179:                     } else {
15180:                         $sts_on = 0;
15181:                     }
15182:                 } else {
15183:                     $sts_on = 0;
15184:                 }
15185:             }
15186:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
15187:         }
15188:     }
15189:     return;
15190: }
15191: 
15192: sub waf_allssl {
15193:     my ($host_name) = @_;
15194:     my $alias = &get_proxy_alias();
15195:     if ($host_name eq '') {
15196:         $host_name = $ENV{'SERVER_NAME'};
15197:     }
15198:     if (($host_name ne '') && ($alias eq $host_name)) {
15199:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
15200:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
15201:         if ($defdomdefaults{'waf_sslopt'}) {
15202:             return $defdomdefaults{'waf_sslopt'};
15203:         }
15204:     }
15205:     return;
15206: }
15207: 
15208: sub get_requestor_ip {
15209:     my ($r,$nolookup,$noproxy) = @_;
15210:     my $from_ip;
15211:     if (ref($r)) {
15212:         if ($r->can('useragent_ip')) {
15213:             if ($noproxy && $r->can('client_ip')) {
15214:                 $from_ip = $r->client_ip();
15215:             } else {
15216:                 $from_ip = $r->useragent_ip();
15217:             }
15218:         } elsif ($r->connection->can('remote_ip')) {
15219:             $from_ip = $r->connection->remote_ip();
15220:         } else {
15221:             $from_ip = $r->get_remote_host($nolookup);
15222:         }
15223:     } else {
15224:         $from_ip = $ENV{'REMOTE_ADDR'};
15225:     }
15226:     return $from_ip if ($noproxy); 
15227:     # Who controls proxy settings for server
15228:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
15229:     my $proxyinfo = &get_proxy_settings($dom_in_use);
15230:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
15231:         if ($proxyinfo->{'vpnint'}) {
15232:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
15233:                 return $from_ip;
15234:             }
15235:         }
15236:         if ($proxyinfo->{'trusted'}) {
15237:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
15238:                 my $ipheader = $proxyinfo->{'ipheader'};
15239:                 my ($ip,$xfor);
15240:                 if (ref($r)) {
15241:                     if ($ipheader) {
15242:                         $ip = $r->headers_in->{$ipheader};
15243:                     }
15244:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
15245:                 } else {
15246:                     if ($ipheader) {
15247:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
15248:                     }
15249:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
15250:                 }
15251:                 if (($ip eq '') && ($xfor ne '')) {
15252:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
15253:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
15254:                             $ip = $poss_ip;
15255:                             last;
15256:                         }
15257:                     }
15258:                 }
15259:                 if ($ip ne '') {
15260:                     return $ip;
15261:                 }
15262:             }
15263:         }
15264:     }
15265:     return $from_ip;
15266: }
15267: 
15268: sub get_proxy_settings {
15269:     my ($dom_in_use) = @_;
15270:     my %domdefaults = &get_domain_defaults($dom_in_use);
15271:     my $proxyinfo = {
15272:                        ipheader => $domdefaults{'waf_ipheader'},
15273:                        trusted  => $domdefaults{'waf_trusted'},
15274:                        vpnint   => $domdefaults{'waf_vpnint'},
15275:                        vpnext   => $domdefaults{'waf_vpnext'},
15276:                        sslopt   => $domdefaults{'waf_sslopt'},
15277:                     };
15278:     return $proxyinfo;
15279: }
15280: 
15281: sub ip_match {
15282:     my ($ip,$pattern_str) = @_;
15283:     $ip=Net::CIDR::cidrvalidate($ip);
15284:     if ($ip) {
15285:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
15286:     }
15287:     return;
15288: }
15289: 
15290: sub get_proxy_alias {
15291:     my ($lonid) = @_;
15292:     if ($lonid eq '') {
15293:         $lonid = $perlvar{'lonHostID'};
15294:     }
15295:     if (!defined(&hostname($lonid))) {
15296:         return;
15297:     }
15298:     if ($lonid ne '') {
15299:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
15300:         if ($cached) {
15301:             return $alias;
15302:         }
15303:         my $dom = &host_domain($lonid);
15304:         if ($dom ne '') {
15305:             my $cachetime = 60*60*24;
15306:             my %domconfig =
15307:                 &get_dom('configuration',['wafproxy'],$dom);
15308:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
15309:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
15310:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
15311:                 }
15312:             }
15313:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
15314:         }
15315:     }
15316:     return;
15317: }
15318: 
15319: sub use_proxy_alias {
15320:     my ($r,$lonid) = @_;
15321:     my $alias = &get_proxy_alias($lonid);
15322:     if ($alias) {
15323:         my $dom = &host_domain($lonid);
15324:         if ($dom ne '') {
15325:             my $proxyinfo = &get_proxy_settings($dom);
15326:             my ($vpnint,$remote_ip);
15327:             if (ref($proxyinfo) eq 'HASH') {
15328:                 $vpnint = $proxyinfo->{'vpnint'};
15329:                 if ($vpnint) {
15330:                     $remote_ip = &get_requestor_ip($r,1,1);
15331:                 }
15332:             }
15333:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
15334:                 return $alias;
15335:             }
15336:         }
15337:     }
15338:     return;
15339: }
15340: 
15341: sub alias_sso {
15342:     my ($lonid) = @_;
15343:     if ($lonid eq '') {
15344:         $lonid = $perlvar{'lonHostID'};
15345:     }
15346:     if (!defined(&hostname($lonid))) {
15347:         return;
15348:     }
15349:     if ($lonid ne '') {
15350:         my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
15351:         if ($cached) {
15352:             return $use_alias;
15353:         }
15354:         my $dom = &host_domain($lonid);
15355:         if ($dom ne '') {
15356:             my $cachetime = 60*60*24;
15357:             my %domconfig =
15358:                 &get_dom('configuration',['wafproxy'],$dom);
15359:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
15360:                 if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
15361:                     $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
15362:                 }
15363:             }
15364:             return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
15365:         }
15366:     }
15367:     return;
15368: }
15369: 
15370: sub get_saml_landing {
15371:     my ($lonid) = @_;
15372:     if ($lonid eq '') {
15373:         my $defdom = &default_login_domain();
15374:         my @hosts = &current_machine_ids();
15375:         if (@hosts > 1) {
15376:             foreach my $hostid (@hosts) {
15377:                 if (&host_domain($hostid) eq $defdom) {
15378:                     $lonid = $hostid;
15379:                     last;
15380:                 }
15381:             }
15382:         } else {
15383:             $lonid = $perlvar{'lonHostID'};
15384:         }
15385:         if ($lonid) {
15386:             unless (&host_domain($lonid) eq $defdom) {
15387:                 return;
15388:             }
15389:         } else {
15390:             return;
15391:         }
15392:     } elsif (!defined(&hostname($lonid))) {
15393:         return;
15394:     }
15395:     my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
15396:     if ($cached) {
15397:         return $landing;
15398:     }
15399:     my $dom = &host_domain($lonid);
15400:     if ($dom ne '') {
15401:         my $cachetime = 60*60*24;
15402:         my %domconfig =
15403:             &get_dom('configuration',['login'],$dom);
15404:         if (ref($domconfig{'login'}) eq 'HASH') {
15405:             if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
15406:                 if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
15407:                     $landing = 1;
15408:                 }
15409:             }
15410:         }
15411:         return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
15412:     }
15413:     return;
15414: }
15415: 
15416: # ------------------------------------------------------------- Declutters URLs
15417: 
15418: sub declutter {
15419:     my $thisfn=shift;
15420:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
15421:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
15422:         $thisfn=~s{^/home/httpd/html}{};
15423:     }
15424:     $thisfn=~s/^\///;
15425:     $thisfn=~s|^adm/wrapper/||;
15426:     $thisfn=~s|^adm/coursedocs/showdoc/||;
15427:     $thisfn=~s/^res\///;
15428:     $thisfn=~s/^priv\///;
15429:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
15430:         $thisfn=~s/\?.+$//;
15431:     }
15432:     return $thisfn;
15433: }
15434: 
15435: # ------------------------------------------------------------- Clutter up URLs
15436: 
15437: sub clutter {
15438:     my $thisfn='/'.&declutter(shift);
15439:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
15440: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
15441:        $thisfn='/res'.$thisfn; 
15442:     }
15443:     if ($thisfn !~m|^/adm|) {
15444: 	if ($thisfn =~ m|^/ext/|) {
15445: 	    $thisfn='/adm/wrapper'.$thisfn;
15446: 	} else {
15447: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
15448: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
15449: 	    if ($embstyle eq 'ssi'
15450: 		|| ($embstyle eq 'hdn')
15451: 		|| ($embstyle eq 'rat')
15452: 		|| ($embstyle eq 'prv')
15453: 		|| ($embstyle eq 'ign')) {
15454: 		#do nothing with these
15455: 	    } elsif (($embstyle eq 'img') 
15456: 		|| ($embstyle eq 'emb')
15457: 		|| ($embstyle eq 'wrp')) {
15458: 		$thisfn='/adm/wrapper'.$thisfn;
15459: 	    } elsif ($embstyle eq 'unk'
15460: 		     && $thisfn!~/\.(sequence|page)$/) {
15461: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
15462: 	    } else {
15463: #		&logthis("Got a blank emb style");
15464: 	    }
15465: 	}
15466:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
15467:         $thisfn='/adm/wrapper'.$thisfn;
15468:     }
15469:     return $thisfn;
15470: }
15471: 
15472: sub clutter_with_no_wrapper {
15473:     my $uri = &clutter(shift);
15474:     if ($uri =~ m-^/adm/-) {
15475: 	$uri =~ s-^/adm/wrapper/-/-;
15476: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
15477:     }
15478:     return $uri;
15479: }
15480: 
15481: sub freeze_escape {
15482:     my ($value)=@_;
15483:     if (ref($value)) {
15484: 	$value=&nfreeze($value);
15485: 	return '__FROZEN__'.&escape($value);
15486:     }
15487:     return &escape($value);
15488: }
15489: 
15490: 
15491: sub thaw_unescape {
15492:     my ($value)=@_;
15493:     if ($value =~ /^__FROZEN__/) {
15494: 	substr($value,0,10,undef);
15495: 	$value=&unescape($value);
15496: 	return &thaw($value);
15497:     }
15498:     return &unescape($value);
15499: }
15500: 
15501: sub correct_line_ends {
15502:     my ($result)=@_;
15503:     $$result =~s/\r\n/\n/mg;
15504:     $$result =~s/\r/\n/mg;
15505: }
15506: # ================================================================ Main Program
15507: 
15508: sub goodbye {
15509:    &logthis("Starting Shut down");
15510: #not converted to using infrastruture and probably shouldn't be
15511:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
15512: #converted
15513: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
15514:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
15515: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
15516: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
15517: #1.1 only
15518: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
15519: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
15520: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
15521: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
15522:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
15523:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
15524:    &logthis(sprintf("%-20s is %s",'hits',$hits));
15525:    &flushcourselogs();
15526:    &logthis("Shutting down");
15527: }
15528: 
15529: sub get_dns {
15530:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
15531:     if (!$ignore_cache) {
15532: 	my ($content,$cached)=
15533: 	    &is_cached_new('dns',$url);
15534: 	if ($cached) {
15535: 	    &$func($content,$hashref);
15536: 	    return;
15537: 	}
15538:     }
15539: 
15540:     my %alldns;
15541:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
15542:         foreach my $dns (<$config>) {
15543: 	    next if ($dns !~ /^\^(\S*)/x);
15544:             my $line = $1;
15545:             my ($host,$protocol) = split(/:/,$line);
15546:             if ($protocol ne 'https') {
15547:                 $protocol = 'http';
15548:             }
15549: 	    $alldns{$host} = $protocol;
15550:         }
15551:         close($config);
15552:     }
15553:     while (%alldns) {
15554: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
15555:         my ($contents,@content);
15556:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15557:             my $command = (split('/',$url))[3];
15558:             my ($dir,$file) = &parse_getdns_url($command,$url);
15559:             delete($alldns{$dns});
15560:             next if (($dir eq '') || ($file eq ''));
15561:             if (open(my $config,'<',"$dir/$file")) {
15562:                 @content = <$config>;
15563:                 close($config);
15564:             }
15565:             if ($url eq '/adm/dns/loncapaCRL') {
15566:                 $contents = join('',@content);
15567:             }
15568:         } else {
15569: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15570:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
15571:             delete($alldns{$dns});
15572: 	    next if ($response->is_error());
15573:             if ($url eq '/adm/dns/loncapaCRL') {
15574:                 $contents = $response->content;
15575:             } else {
15576:                 @content = split("\n",$response->content);
15577:             }
15578:         }
15579:         if ($url eq '/adm/dns/loncapaCRL') {
15580:             return &$func($contents);
15581:         } else {
15582: 	    unless ($nocache) {
15583: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
15584: 	    }
15585: 	    &$func(\@content,$hashref);
15586:             return;
15587:         }
15588:     }
15589:     my $which = (split('/',$url,4))[3];
15590:     if ($which eq 'loncapaCRL') {
15591:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
15592:         if (-e $diskfile) {
15593:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
15594:         } else {
15595:             &logthis("unable to contact DNS, no on disk file $diskfile available");
15596:         }
15597:     } else {
15598:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
15599:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15600:             my @content = <$config>;
15601:             close($config);
15602:             &$func(\@content,$hashref);
15603:         }
15604:     }
15605:     return;
15606: }
15607: 
15608: # ------------------------------------------------------Get DNS checksums file
15609: sub parse_dns_checksums_tab {
15610:     my ($lines,$hashref) = @_;
15611:     my $lonhost = $perlvar{'lonHostID'};
15612:     my $machine_dom = &host_domain($lonhost);
15613:     my $loncaparev = &get_server_loncaparev($machine_dom);
15614:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15615:     my $webconfdir = '/etc/httpd/conf';
15616:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15617:         $webconfdir = '/etc/apache2';
15618:     } elsif ($distro =~ /^sles(\d+)$/) {
15619:         if ($1 >= 10) {
15620:             $webconfdir = '/etc/apache2';
15621:         }
15622:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15623:         if ($1 >= 10.0) {
15624:             $webconfdir = '/etc/apache2';
15625:         }
15626:     }
15627:     my ($release,$timestamp) = split(/\-/,$loncaparev);
15628:     my (%chksum,%revnum);
15629:     if (ref($lines) eq 'ARRAY') {
15630:         chomp(@{$lines});
15631:         my $version = shift(@{$lines});
15632:         if ($version eq $release) {  
15633:             foreach my $line (@{$lines}) {
15634:                 my ($file,$version,$shasum) = split(/,/,$line);
15635:                 if ($file =~ m{^/etc/httpd/conf}) {
15636:                     if ($webconfdir eq '/etc/apache2') {
15637:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15638:                     }
15639:                 }
15640:                 $chksum{$file} = $shasum;
15641:                 $revnum{$file} = $version;
15642:             }
15643:             if (ref($hashref) eq 'HASH') {
15644:                 %{$hashref} = (
15645:                                 sums     => \%chksum,
15646:                                 versions => \%revnum,
15647:                               );
15648:             }
15649:         }
15650:     }
15651:     return;
15652: }
15653: 
15654: sub fetch_dns_checksums {
15655:     my %checksums;
15656:     my $machine_dom = &host_domain($perlvar{'lonHostID'});
15657:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
15658:     my ($release,$timestamp) = split(/\-/,$loncaparev);
15659:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
15660:              \%checksums);
15661:     return \%checksums;
15662: }
15663: 
15664: sub fetch_crl_pemfile {
15665:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
15666: }
15667: 
15668: sub save_crl_pem {
15669:     my ($content) = @_;
15670:     my ($msg,$hadchanges);
15671:     if ($content ne '') {
15672:         my $now = time;
15673:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
15674:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
15675:         if (open(my $fh,'>',"$tmpcrl")) {
15676:             print $fh $content;
15677:             close($fh);
15678:             if (-e $lonca) {
15679:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
15680:                     my $check = <PIPE>;
15681:                     close(PIPE);
15682:                     chomp($check);
15683:                     if ($check eq 'verify OK') {
15684:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
15685:                         my $backup;
15686:                         if (-e $dest) {
15687:                             if (&File::Copy::move($dest,"$dest.bak")) {
15688:                                 $backup = 'ok';
15689:                             }
15690:                         }
15691:                         if (&File::Copy::move($tmpcrl,$dest)) {
15692:                             $msg = 'ok';
15693:                             if ($backup) {
15694:                                 my (%oldnums,%newnums);
15695:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
15696:                                     while (<PIPE>) {
15697:                                         $oldnums{(split(/:/))[1]} = 1;
15698:                                     }
15699:                                     close(PIPE);
15700:                                 }
15701:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
15702:                                     while(<PIPE>) {
15703:                                         $newnums{(split(/:/))[1]} = 1;
15704:                                     }
15705:                                     close(PIPE);
15706:                                 }
15707:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
15708:                                     unless (exists($oldnums{$key})) {
15709:                                         $hadchanges = 1;
15710:                                         last;
15711:                                     }
15712:                                 }
15713:                                 unless ($hadchanges) {
15714:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
15715:                                         unless (exists($newnums{$key})) {
15716:                                             $hadchanges = 1;
15717:                                             last;
15718:                                         }
15719:                                     }
15720:                                 }
15721:                             }
15722:                         }
15723:                     } else {
15724:                         unlink($tmpcrl);
15725:                     }
15726:                 } else {
15727:                     unlink($tmpcrl);
15728:                 }
15729:             } else {
15730:                 unlink($tmpcrl);
15731:             }
15732:         }
15733:     }
15734:     return ($msg,$hadchanges);
15735: }
15736: 
15737: sub parse_getdns_url {
15738:     my ($command,$url) = @_;
15739:     my $dir = $perlvar{'lonTabDir'};
15740:     my $file;
15741:     if ($command eq 'hosts') {
15742:         $file = 'dns_hosts.tab';
15743:     } elsif ($command eq 'domain') {
15744:         $file = 'dns_domain.tab';
15745:     } elsif ($command eq 'checksums') {
15746:         my $version = (split('/',$url))[4];
15747:         $file = "dns_checksums/$version.tab",
15748:     } elsif ($command eq 'loncapaCRL') {
15749:         $dir = $perlvar{'lonCertificateDirectory'};
15750:         $file = $perlvar{'lonnetCertRevocationList'};
15751:     }
15752:     return ($dir,$file);
15753: }
15754: 
15755: # ------------------------------------------------------------ Read domain file
15756: {
15757:     my $loaded;
15758:     my %domain;
15759: 
15760:     sub parse_domain_tab {
15761: 	my ($lines) = @_;
15762: 	foreach my $line (@$lines) {
15763: 	    next if ($line =~ /^(\#|\s*$ )/x);
15764: 
15765: 	    chomp($line);
15766: 	    my ($name,@elements) = split(/:/,$line,9);
15767: 	    my %this_domain;
15768: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
15769: 			       'lang_def', 'city', 'longi', 'lati',
15770: 			       'primary') {
15771: 		$this_domain{$field} = shift(@elements);
15772: 	    }
15773: 	    $domain{$name} = \%this_domain;
15774: 	}
15775:     }
15776: 
15777:     sub reset_domain_info {
15778: 	undef($loaded);
15779: 	undef(%domain);
15780:     }
15781: 
15782:     sub load_domain_tab {
15783: 	my ($ignore_cache,$nocache) = @_;
15784: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
15785: 	my $fh;
15786: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
15787: 	    my @lines = <$fh>;
15788: 	    &parse_domain_tab(\@lines);
15789: 	}
15790: 	close($fh);
15791: 	$loaded = 1;
15792:     }
15793: 
15794:     sub domain {
15795: 	&load_domain_tab() if (!$loaded);
15796: 
15797: 	my ($name,$what) = @_;
15798: 	return if ( !exists($domain{$name}) );
15799: 
15800: 	if (!$what) {
15801: 	    return $domain{$name}{'description'};
15802: 	}
15803: 	return $domain{$name}{$what};
15804:     }
15805: 
15806:     sub domain_info {
15807:         &load_domain_tab() if (!$loaded);
15808:         return %domain;
15809:     }
15810: 
15811: }
15812: 
15813: 
15814: # ------------------------------------------------------------- Read hosts file
15815: {
15816:     my %hostname;
15817:     my %hostdom;
15818:     my %libserv;
15819:     my $loaded;
15820:     my %name_to_host;
15821:     my %internetdom;
15822:     my %LC_dns_serv;
15823: 
15824:     sub parse_hosts_tab {
15825: 	my ($file) = @_;
15826: 	foreach my $configline (@$file) {
15827: 	    next if ($configline =~ /^(\#|\s*$ )/x);
15828:             chomp($configline);
15829: 	    if ($configline =~ /^\^/) {
15830:                 if ($configline =~ /^\^([\w.\-]+)/) {
15831:                     $LC_dns_serv{$1} = 1;
15832:                 }
15833:                 next;
15834:             }
15835: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
15836: 	    $name=~s/\s//g;
15837: 	    if ($id && $domain && $role && $name) {
15838:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15839:                     my $curr = $hostname{$id};
15840:                     my $skip;
15841:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
15842:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15843:                             $skip = 1;
15844:                         } else {
15845:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15846:                         }
15847:                     }
15848:                     unless ($skip) {
15849:                         push(@{$name_to_host{$name}},$id);
15850:                     }
15851:                 } else {
15852:                     push(@{$name_to_host{$name}},$id);
15853:                 }
15854: 		$hostname{$id}=$name;
15855: 		$hostdom{$id}=$domain;
15856: 		if ($role eq 'library') { $libserv{$id}=$name; }
15857:                 if (defined($protocol)) {
15858:                     if ($protocol eq 'https') {
15859:                         $protocol{$id} = $protocol;
15860:                     } else {
15861:                         $protocol{$id} = 'http'; 
15862:                     }
15863:                 } else {
15864:                     $protocol{$id} = 'http';
15865:                 }
15866:                 if (defined($intdom)) {
15867:                     $internetdom{$id} = $intdom;
15868:                 }
15869: 	    }
15870: 	}
15871:     }
15872:     
15873:     sub reset_hosts_info {
15874: 	&purge_remembered();
15875: 	&reset_domain_info();
15876: 	&reset_hosts_ip_info();
15877:         undef(%internetdom);
15878: 	undef(%name_to_host);
15879: 	undef(%hostname);
15880: 	undef(%hostdom);
15881: 	undef(%libserv);
15882: 	undef($loaded);
15883:     }
15884: 
15885:     sub load_hosts_tab {
15886: 	my ($ignore_cache,$nocache) = @_;
15887: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
15888: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
15889: 	my @config = <$config>;
15890: 	&parse_hosts_tab(\@config);
15891: 	close($config);
15892: 	$loaded=1;
15893:     }
15894: 
15895:     sub hostname {
15896: 	&load_hosts_tab() if (!$loaded);
15897: 
15898: 	my ($lonid) = @_;
15899: 	return $hostname{$lonid};
15900:     }
15901: 
15902:     sub all_hostnames {
15903: 	&load_hosts_tab() if (!$loaded);
15904: 
15905: 	return %hostname;
15906:     }
15907: 
15908:     sub all_names {
15909:         my ($ignore_cache,$nocache) = @_;
15910: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
15911: 
15912: 	return %name_to_host;
15913:     }
15914: 
15915:     sub all_host_domain {
15916:         &load_hosts_tab() if (!$loaded);
15917:         return %hostdom;
15918:     }
15919: 
15920:     sub all_host_intdom {
15921:         &load_hosts_tab() if (!$loaded);
15922:         return %internetdom;
15923:     }
15924: 
15925:     sub is_library {
15926: 	&load_hosts_tab() if (!$loaded);
15927: 
15928: 	return exists($libserv{$_[0]});
15929:     }
15930: 
15931:     sub all_library {
15932: 	&load_hosts_tab() if (!$loaded);
15933: 
15934: 	return %libserv;
15935:     }
15936: 
15937:     sub unique_library {
15938: 	#2x reverse removes all hostnames that appear more than once
15939:         my %unique = reverse &all_library();
15940:         return reverse %unique;
15941:     }
15942: 
15943:     sub get_servers {
15944: 	&load_hosts_tab() if (!$loaded);
15945: 
15946: 	my ($domain,$type) = @_;
15947: 	my %possible_hosts = ($type eq 'library') ? %libserv
15948: 	                                          : %hostname;
15949: 	my %result;
15950: 	if (ref($domain) eq 'ARRAY') {
15951: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15952: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
15953: 		    $result{$host} = $hostname;
15954: 		}
15955: 	    }
15956: 	} else {
15957: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15958: 		if ($hostdom{$host} eq $domain) {
15959: 		    $result{$host} = $hostname;
15960: 		}
15961: 	    }
15962: 	}
15963: 	return %result;
15964:     }
15965: 
15966:     sub get_unique_servers {
15967:         my %unique = reverse &get_servers(@_);
15968: 	return reverse %unique;
15969:     }
15970: 
15971:     sub host_domain {
15972: 	&load_hosts_tab() if (!$loaded);
15973: 
15974: 	my ($lonid) = @_;
15975: 	return $hostdom{$lonid};
15976:     }
15977: 
15978:     sub all_domains {
15979: 	&load_hosts_tab() if (!$loaded);
15980: 
15981: 	my %seen;
15982: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
15983: 	return @uniq;
15984:     }
15985: 
15986:     sub internet_dom {
15987:         &load_hosts_tab() if (!$loaded);
15988: 
15989:         my ($lonid) = @_;
15990:         return $internetdom{$lonid};
15991:     }
15992: 
15993:     sub is_LC_dns {
15994:         &load_hosts_tab() if (!$loaded);
15995: 
15996:         my ($hostname) = @_;
15997:         return exists($LC_dns_serv{$hostname});
15998:     }
15999: 
16000: }
16001: 
16002: { 
16003:     my %iphost;
16004:     my %name_to_ip;
16005:     my %lonid_to_ip;
16006: 
16007:     sub get_hosts_from_ip {
16008: 	my ($ip) = @_;
16009: 	my %iphosts = &get_iphost();
16010: 	if (ref($iphosts{$ip})) {
16011: 	    return @{$iphosts{$ip}};
16012: 	}
16013: 	return;
16014:     }
16015:     
16016:     sub reset_hosts_ip_info {
16017: 	undef(%iphost);
16018: 	undef(%name_to_ip);
16019: 	undef(%lonid_to_ip);
16020:     }
16021: 
16022:     sub get_host_ip {
16023: 	my ($lonid) = @_;
16024: 	if (exists($lonid_to_ip{$lonid})) {
16025: 	    return $lonid_to_ip{$lonid};
16026: 	}
16027: 	my $name=&hostname($lonid);
16028:    	my $ip = gethostbyname($name);
16029: 	return if (!$ip || length($ip) ne 4);
16030: 	$ip=inet_ntoa($ip);
16031: 	$name_to_ip{$name}   = $ip;
16032: 	$lonid_to_ip{$lonid} = $ip;
16033: 	return $ip;
16034:     }
16035:     
16036:     sub get_iphost {
16037: 	my ($ignore_cache,$nocache) = @_;
16038: 
16039: 	if (!$ignore_cache) {
16040: 	    if (%iphost) {
16041: 		return %iphost;
16042: 	    }
16043: 	    my ($ip_info,$cached)=
16044: 		&is_cached_new('iphost','iphost');
16045: 	    if ($cached) {
16046: 		%iphost      = %{$ip_info->[0]};
16047: 		%name_to_ip  = %{$ip_info->[1]};
16048: 		%lonid_to_ip = %{$ip_info->[2]};
16049: 		return %iphost;
16050: 	    }
16051: 	}
16052: 
16053: 	# get yesterday's info for fallback
16054: 	my %old_name_to_ip;
16055: 	my ($ip_info,$cached)=
16056: 	    &is_cached_new('iphost','iphost');
16057: 	if ($cached) {
16058: 	    %old_name_to_ip = %{$ip_info->[1]};
16059: 	}
16060: 
16061: 	my %name_to_host = &all_names($ignore_cache,$nocache);
16062: 	foreach my $name (keys(%name_to_host)) {
16063: 	    my $ip;
16064: 	    if (!exists($name_to_ip{$name})) {
16065: 		$ip = gethostbyname($name);
16066: 		if (!$ip || length($ip) ne 4) {
16067: 		    if (defined($old_name_to_ip{$name})) {
16068: 			$ip = $old_name_to_ip{$name};
16069: 			&logthis("Can't find $name defaulting to old $ip");
16070: 		    } else {
16071: 			&logthis("Name $name no IP found");
16072: 			next;
16073: 		    }
16074: 		} else {
16075: 		    $ip=inet_ntoa($ip);
16076: 		}
16077: 		$name_to_ip{$name} = $ip;
16078: 	    } else {
16079: 		$ip = $name_to_ip{$name};
16080: 	    }
16081: 	    foreach my $id (@{ $name_to_host{$name} }) {
16082: 		$lonid_to_ip{$id} = $ip;
16083: 	    }
16084: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
16085: 	}
16086:         unless ($nocache) {
16087: 	    &do_cache_new('iphost','iphost',
16088: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
16089: 		          48*60*60);
16090:         }
16091: 
16092: 	return %iphost;
16093:     }
16094: 
16095:     #
16096:     #  Given a DNS returns the loncapa host name for that DNS 
16097:     # 
16098:     sub host_from_dns {
16099:         my ($dns) = @_;
16100:         my @hosts;
16101:         my $ip;
16102: 
16103:         if (exists($name_to_ip{$dns})) {
16104:             $ip = $name_to_ip{$dns};
16105:         }
16106:         if (!$ip) {
16107:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
16108:             if (length($ip) == 4) { 
16109: 	        $ip   = &IO::Socket::inet_ntoa($ip);
16110:             }
16111:         }
16112:         if ($ip) {
16113: 	    @hosts = get_hosts_from_ip($ip);
16114: 	    return $hosts[0];
16115:         }
16116:         return undef;
16117:     }
16118: 
16119:     sub get_internet_names {
16120:         my ($lonid) = @_;
16121:         return if ($lonid eq '');
16122:         my ($idnref,$cached)=
16123:             &is_cached_new('internetnames',$lonid);
16124:         if ($cached) {
16125:             return $idnref;
16126:         }
16127:         my $ip = &get_host_ip($lonid);
16128:         my @hosts = &get_hosts_from_ip($ip);
16129:         my %iphost = &get_iphost();
16130:         my (@idns,%seen);
16131:         foreach my $id (@hosts) {
16132:             my $dom = &host_domain($id);
16133:             my $prim_id = &domain($dom,'primary');
16134:             my $prim_ip = &get_host_ip($prim_id);
16135:             next if ($seen{$prim_ip});
16136:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
16137:                 foreach my $id (@{$iphost{$prim_ip}}) {
16138:                     my $intdom = &internet_dom($id);
16139:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
16140:                         push(@idns,$intdom);
16141:                     }
16142:                 }
16143:             }
16144:             $seen{$prim_ip} = 1;
16145:         }
16146:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
16147:     }
16148: 
16149: }
16150: 
16151: sub all_loncaparevs {
16152:     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);
16153: }
16154: 
16155: # ---------------------------------------------------------- Read loncaparev table
16156: {
16157:     sub load_loncaparevs { 
16158:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
16159:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
16160:                 while (my $configline=<$config>) {
16161:                     chomp($configline);
16162:                     my ($hostid,$loncaparev)=split(/:/,$configline);
16163:                     $loncaparevs{$hostid}=$loncaparev;
16164:                 }
16165:                 close($config);
16166:             }
16167:         }
16168:     }
16169: }
16170: 
16171: # ---------------------------------------------------------- Read serverhostID table
16172: {
16173:     sub load_serverhomeIDs {
16174:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
16175:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
16176:                 while (my $configline=<$config>) {
16177:                     chomp($configline);
16178:                     my ($name,$id)=split(/:/,$configline);
16179:                     $serverhomeIDs{$name}=$id;
16180:                 }
16181:                 close($config);
16182:             }
16183:         }
16184:     }
16185: }
16186: 
16187: 
16188: BEGIN {
16189: 
16190: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
16191:     unless ($readit) {
16192: {
16193:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
16194:     %perlvar = (%perlvar,%{$configvars});
16195: }
16196: 
16197: 
16198: # ------------------------------------------------------ Read spare server file
16199: {
16200:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
16201: 
16202:     while (my $configline=<$config>) {
16203:        chomp($configline);
16204:        if ($configline) {
16205: 	   my ($host,$type) = split(':',$configline,2);
16206: 	   if (!defined($type) || $type eq '') { $type = 'default' };
16207: 	   push(@{ $spareid{$type} }, $host);
16208:        }
16209:     }
16210:     close($config);
16211: }
16212: # ------------------------------------------------------------ Read permissions
16213: {
16214:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
16215: 
16216:     while (my $configline=<$config>) {
16217: 	chomp($configline);
16218: 	if ($configline) {
16219: 	    my ($role,$perm)=split(/ /,$configline);
16220: 	    if ($perm ne '') { $pr{$role}=$perm; }
16221: 	}
16222:     }
16223:     close($config);
16224: }
16225: 
16226: # -------------------------------------------- Read plain texts for permissions
16227: {
16228:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
16229: 
16230:     while (my $configline=<$config>) {
16231: 	chomp($configline);
16232: 	if ($configline) {
16233: 	    my ($short,@plain)=split(/:/,$configline);
16234:             %{$prp{$short}} = ();
16235: 	    if (@plain > 0) {
16236:                 $prp{$short}{'std'} = $plain[0];
16237:                 for (my $i=1; $i<@plain; $i++) {
16238:                     $prp{$short}{'alt'.$i} = $plain[$i];  
16239:                 }
16240:             }
16241: 	}
16242:     }
16243:     close($config);
16244: }
16245: 
16246: # ---------------------------------------------------------- Read package table
16247: {
16248:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
16249: 
16250:     while (my $configline=<$config>) {
16251: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
16252: 	chomp($configline);
16253: 	my ($short,$plain)=split(/:/,$configline);
16254: 	my ($pack,$name)=split(/\&/,$short);
16255: 	if ($plain ne '') {
16256: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
16257: 	    $packagetab{$short}=$plain; 
16258: 	}
16259:     }
16260:     close($config);
16261: }
16262: 
16263: # ---------------------------------------------------------- Read loncaparev table
16264: 
16265: &load_loncaparevs();
16266: 
16267: # ---------------------------------------------------------- Read serverhostID table
16268: 
16269: &load_serverhomeIDs();
16270: 
16271: # ---------------------------------------------------------- Read releaseslist XML
16272: {
16273:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
16274:     if (-e $file) {
16275:         my $parser = HTML::LCParser->new($file);
16276:         while (my $token = $parser->get_token()) {
16277:             if ($token->[0] eq 'S') {
16278:                 my $item = $token->[1];
16279:                 my $name = $token->[2]{'name'};
16280:                 my $value = $token->[2]{'value'};
16281:                 my $valuematch = $token->[2]{'valuematch'};
16282:                 my $namematch = $token->[2]{'namematch'};
16283:                 if ($item eq 'parameter') {
16284:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
16285:                         my $release = $parser->get_text();
16286:                         $release =~ s/(^\s*|\s*$ )//gx;
16287:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
16288:                     }
16289:                 } elsif ($item ne '' && $name ne '') {
16290:                     my $release = $parser->get_text();
16291:                     $release =~ s/(^\s*|\s*$ )//gx;
16292:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
16293:                 }
16294:             }
16295:         }
16296:     }
16297: }
16298: 
16299: # ---------------------------------------------------------- Read managers table
16300: {
16301:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
16302:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
16303:             while (my $configline=<$config>) {
16304:                 chomp($configline);
16305:                 next if ($configline =~ /^\#/);
16306:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
16307:                     $managerstab{$configline} = 1;
16308:                 }
16309:             }
16310:             close($config);
16311:         }
16312:     }
16313: }
16314: 
16315: # ------------- set up temporary directory
16316: {
16317:     $tmpdir = LONCAPA::tempdir();
16318: 
16319: }
16320: 
16321: # ------------- set default texengine (domain default overrides this)
16322: {
16323:     $deftex = LONCAPA::texengine();
16324: }
16325: 
16326: # ------------- set default minimum length for passwords for internal auth users
16327: {
16328:     $passwdmin = LONCAPA::passwd_min();
16329: }
16330: 
16331: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
16332: 				'compress_threshold'=> 20_000,
16333:  			        });
16334: 
16335: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
16336: $dumpcount=0;
16337: $locknum=0;
16338: 
16339: &logtouch();
16340: &logthis('<font color="yellow">INFO: Read configuration</font>');
16341: $readit=1;
16342:     {
16343: 	use integer;
16344: 	my $test=(2**32)+1;
16345: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
16346: 	&logthis(" Detected 64bit platform ($_64bit)");
16347:     }
16348: }
16349: }
16350: 
16351: 1;
16352: __END__
16353: 
16354: =pod
16355: 
16356: =head1 NAME
16357: 
16358: Apache::lonnet - Subroutines to ask questions about things in the network.
16359: 
16360: =head1 SYNOPSIS
16361: 
16362: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
16363: 
16364:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
16365: 
16366: Common parameters:
16367: 
16368: =over 4
16369: 
16370: =item *
16371: 
16372: $uname : an internal username (if $cname expecting a course Id specifically)
16373: 
16374: =item *
16375: 
16376: $udom : a domain (if $cdom expecting a course's domain specifically)
16377: 
16378: =item *
16379: 
16380: $symb : a resource instance identifier
16381: 
16382: =item *
16383: 
16384: $namespace : the name of a .db file that contains the data needed or
16385: being set.
16386: 
16387: =back
16388: 
16389: =head1 OVERVIEW
16390: 
16391: lonnet provides subroutines which interact with the
16392: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
16393: about classes, users, and resources.
16394: 
16395: For many of these objects you can also use this to store data about
16396: them or modify them in various ways.
16397: 
16398: =head2 Symbs
16399: 
16400: To identify a specific instance of a resource, LON-CAPA uses symbols
16401: or "symbs"X<symb>. These identifiers are built from the URL of the
16402: map, the resource number of the resource in the map, and the URL of
16403: the resource itself. The latter is somewhat redundant, but might help
16404: if maps change.
16405: 
16406: An example is
16407: 
16408:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
16409: 
16410: The respective map entry is
16411: 
16412:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
16413:   title="Problem 2">
16414:  </resource>
16415: 
16416: Symbs are used by the random number generator, as well as to store and
16417: restore data specific to a certain instance of for example a problem.
16418: 
16419: =head2 Storing And Retrieving Data
16420: 
16421: X<store()>X<cstore()>X<restore()>Three of the most important functions
16422: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
16423: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
16424: is is the non-critical message twin of cstore. These functions are for
16425: handlers to store a perl hash to a user's permanent data space in an
16426: easy manner, and to retrieve it again on another call. It is expected
16427: that a handler would use this once at the beginning to retrieve data,
16428: and then again once at the end to send only the new data back.
16429: 
16430: The data is stored in the user's data directory on the user's
16431: homeserver under the ID of the course.
16432: 
16433: The hash that is returned by restore will have all of the previous
16434: value for all of the elements of the hash.
16435: 
16436: Example:
16437: 
16438:  #creating a hash
16439:  my %hash;
16440:  $hash{'foo'}='bar';
16441: 
16442:  #storing it
16443:  &Apache::lonnet::cstore(\%hash);
16444: 
16445:  #changing a value
16446:  $hash{'foo'}='notbar';
16447: 
16448:  #adding a new value
16449:  $hash{'bar'}='foo';
16450:  &Apache::lonnet::cstore(\%hash);
16451: 
16452:  #retrieving the hash
16453:  my %history=&Apache::lonnet::restore();
16454: 
16455:  #print the hash
16456:  foreach my $key (sort(keys(%history))) {
16457:    print("\%history{$key} = $history{$key}");
16458:  }
16459: 
16460: Will print out:
16461: 
16462:  %history{1:foo} = bar
16463:  %history{1:keys} = foo:timestamp
16464:  %history{1:timestamp} = 990455579
16465:  %history{2:bar} = foo
16466:  %history{2:foo} = notbar
16467:  %history{2:keys} = foo:bar:timestamp
16468:  %history{2:timestamp} = 990455580
16469:  %history{bar} = foo
16470:  %history{foo} = notbar
16471:  %history{timestamp} = 990455580
16472:  %history{version} = 2
16473: 
16474: Note that the special hash entries C<keys>, C<version> and
16475: C<timestamp> were added to the hash. C<version> will be equal to the
16476: total number of versions of the data that have been stored. The
16477: C<timestamp> attribute will be the UNIX time the hash was
16478: stored. C<keys> is available in every historical section to list which
16479: keys were added or changed at a specific historical revision of a
16480: hash.
16481: 
16482: B<Warning>: do not store the hash that restore returns directly. This
16483: will cause a mess since it will restore the historical keys as if the
16484: were new keys. I.E. 1:foo will become 1:1:foo etc.
16485: 
16486: Calling convention:
16487: 
16488:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
16489:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
16490: 
16491: For more detailed information, see lonnet specific documentation.
16492: 
16493: =head1 RETURN MESSAGES
16494: 
16495: =over 4
16496: 
16497: =item * B<con_lost>: unable to contact remote host
16498: 
16499: =item * B<con_delayed>: unable to contact remote host, message will be delivered
16500: when the connection is brought back up
16501: 
16502: =item * B<con_failed>: unable to contact remote host and unable to save message
16503: for later delivery
16504: 
16505: =item * B<error:>: an error a occurred, a description of the error follows the :
16506: 
16507: =item * B<no_such_host>: unable to fund a host associated with the user/domain
16508: that was requested
16509: 
16510: =back
16511: 
16512: =head1 PUBLIC SUBROUTINES
16513: 
16514: =head2 Session Environment Functions
16515: 
16516: =over 4
16517: 
16518: =item * 
16519: X<appenv()>
16520: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
16521: the user envirnoment file, and will be restored for each access this
16522: user makes during this session, also modifies the %env for the current
16523: process. Optional rolesarrayref - if defined contains a reference to an array
16524: of roles which are exempt from the restriction on modifying user.role entries 
16525: in the user's environment.db and in %env.    
16526: 
16527: =item *
16528: X<delenv()>
16529: B<delenv($delthis,$regexp)>: removes all items from the session
16530: environment file that begin with $delthis. If the 
16531: optional second arg - $regexp - is true, $delthis is treated as a 
16532: regular expression, otherwise \Q$delthis\E is used. 
16533: The values are also deleted from the current processes %env.
16534: 
16535: =item * get_env_multiple($name) 
16536: 
16537: gets $name from the %env hash, it seemlessly handles the cases where multiple
16538: values may be defined and end up as an array ref.
16539: 
16540: returns an array of values
16541: 
16542: =back
16543: 
16544: =head2 User Information
16545: 
16546: =over 4
16547: 
16548: =item *
16549: X<queryauthenticate()>
16550: B<queryauthenticate($uname,$udom)>: try to determine user's current 
16551: authentication scheme
16552: 
16553: =item *
16554: X<authenticate()>
16555: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
16556: authenticate user from domain's lib servers (first use the current
16557: one). C<$upass> should be the users password.
16558: $checkdefauth is optional (value is 1 if a check should be made to
16559:    authenticate user using default authentication method, and allow
16560:    account creation if username does not have account in the domain).
16561: $clientcancheckhost is optional (value is 1 if checking whether the
16562:    server can host will occur on the client side in lonauth.pm).   
16563: 
16564: =item *
16565: X<homeserver()>
16566: B<homeserver($uname,$udom)>: find the server which has
16567: the user's directory and files (there must be only one), this caches
16568: the answer, and also caches if there is a borken connection.
16569: 
16570: =item *
16571: X<idget()>
16572: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
16573: a list of student/employee IDs or clicker IDs
16574: (student/employee IDs are a unique resource in a domain, there must be 
16575: only 1 ID per username, and only 1 username per ID in a specific domain).
16576: clickerIDs are not necessarily unique, as students might share clickers.
16577: (returns hash: id=>name,id=>name)
16578: 
16579: =item *
16580: X<idrget()>
16581: B<idrget($udom,@unames)>: find the IDs behind a list of
16582: usernames (returns hash: name=>id,name=>id)
16583: 
16584: =item *
16585: X<idput()>
16586: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
16587: names and associated student/employee IDs or clicker IDs.
16588: 
16589: =item *
16590: X<iddel()>
16591: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
16592: student/employee ID or clicker ID username look-ups from domain.
16593: The homeserver ($uhome) and namespace ($namespace) are optional.
16594: If no $uhome is provided, it will be determined usig &homeserver()
16595: for each user.  If no $namespace is provided, the default is ids.
16596: 
16597: =item *
16598: X<updateclickers()>
16599: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
16600: clicker ID-to-username look-ups in clickers.db on library server.
16601: Permitted actions are add or del (i.e., add or delete). The 
16602: clickers.db contains clickerID as keys (escaped), and each corresponding
16603: value is an escaped comma-separated list of usernames (for whom the
16604: library server is the homeserver), who registered that particular ID.
16605: If $critical is true, the update will be sent via &critical, otherwise
16606: &reply() will be used.
16607: 
16608: =item *
16609: X<rolesinit()>
16610: B<rolesinit($udom,$username)>: get user privileges.
16611: returns user role, first access and timer interval hashes
16612: 
16613: =item *
16614: X<privileged()>
16615: B<privileged($username,$domain)>: returns a true if user has a
16616: privileged and active role (i.e. su or dc), false otherwise.
16617: 
16618: =item *
16619: X<getsection()>
16620: B<getsection($udom,$uname,$cname)>: finds the section of student in the
16621: course $cname, return section name/number or '' for "not in course"
16622: and '-1' for "no section"
16623: 
16624: =item *
16625: X<userenvironment()>
16626: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
16627: passed in @what from the requested user's environment, returns a hash
16628: 
16629: =item * 
16630: X<userlog_query()>
16631: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
16632: activity.log file. %filters defines filters applied when parsing the
16633: log file. These can be start or end timestamps, or the type of action
16634: - log to look for Login or Logout events, check for Checkin or
16635: Checkout, role for role selection. The response is in the form
16636: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
16637: escaped strings of the action recorded in the activity.log file.
16638: 
16639: =back
16640: 
16641: =head2 User Roles
16642: 
16643: =over 4
16644: 
16645: =item *
16646: 
16647: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
16648: returns codes for allowed actions.
16649: 
16650: The first argument is required, all others are optional.
16651: 
16652: $priv is the privilege being checked.
16653: $uri contains additional information about what is being checked for access (e.g.,
16654: URL, course ID etc.). 
16655: $symb is the unique resource instance identifier in a course; if needed,
16656: but not provided, it will be retrieved via a call to &symbread(). 
16657: $role is the role for which a priv is being checked (only used if priv is evb). 
16658: $clientip is the user's IP address (only used when checking for access to portfolio 
16659: files).
16660: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
16661: prevents recursive calls to &allowed.
16662: 
16663:  F: full access
16664:  U,I,K: authentication modes (cxx only)
16665:  '': forbidden
16666:  1: user needs to choose course
16667:  2: browse allowed
16668:  A: passphrase authentication needed
16669:  B: access temporarily blocked because of a blocking event in a course.
16670:  D: access blocked because access is required via session initiated via deep-link 
16671: 
16672: =item *
16673: 
16674: constructaccess($url,$setpriv) : check for access to construction space URL
16675: 
16676: See if the owner domain and name in the URL match those in the
16677: expected environment.  If so, return three element list
16678: ($ownername,$ownerdomain,$ownerhome).
16679: 
16680: Otherwise return the null string.
16681: 
16682: If second argument 'setpriv' is true, it assigns the privileges,
16683: and returns the same three element list, unless the owner has
16684: blocked "ad hoc" Domain Coordinator access to the Author Space,
16685: in which case the null string is returned.
16686: 
16687: =item *
16688: 
16689: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16690: define a custom role rolename set privileges in format of lonTabs/roles.tab
16691: for system, domain, and course level. $uname and $udom are optional (current
16692: user's username and domain will be used when either of $uname or $udom are absent.
16693: 
16694: =item *
16695: 
16696: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
16697: (rolesplain.tab); plain text explanation of a user role term.
16698: $type is Course (default) or Community.
16699: If $forcedefault evaluates to true, text returned will be default 
16700: text for $type. Otherwise, if this is a course, the text returned 
16701: will be a custom name for the role (if defined in the course's 
16702: environment).  If no custom name is defined the default is returned.
16703:    
16704: =item *
16705: 
16706: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
16707: All arguments are optional. Returns a hash of a roles, either for
16708: co-author/assistant author roles for a user's Construction Space
16709: (default), or if $context is 'userroles', roles for the user himself,
16710: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16711: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16712: For each key, value is set to colon-separated start and end times for
16713: the role.  If no username and domain are specified, will default to
16714: current user/domain. Types, roles, and roledoms are references to arrays
16715: of role statuses (active, future or previous), roles 
16716: (e.g., cc,in, st etc.) and domains of the roles which can be used
16717: to restrict the list of roles reported. If no array ref is 
16718: provided for types, will default to return only active roles.
16719: 
16720: =item *
16721: 
16722: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16723: user: $uname:$udom has a role in the course: $cdom_$cnum. 
16724: 
16725: Additional optional arguments are: $type (if role checking is to be restricted 
16726: to certain user status types -- previous (expired roles), active (currently
16727: available roles) or future (roles available in the future), and
16728: $hideprivileged -- if true will not report course roles for users who
16729: have active Domain Coordinator role in course's domain or in additional
16730: domains (specified in 'Domains to check for privileged users' in course
16731: environment -- set via:  Course Settings -> Classlists and staff listing).
16732: 
16733: =item *
16734: 
16735: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16736: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16737: $possdomains and $possroles are optional array refs -- to domains to check and
16738: roles to check.  If $possdomains is not specified, a dump will be done of the
16739: users' roles.db to check for a dc or su role in any domain. This can be
16740: time consuming if &privileged is called repeatedly (e.g., when displaying a
16741: classlist), so in such cases, supplying a $possdomains array is preferred, as
16742: this then allows &privileged_by_domain() to be used, which caches the identity
16743: of privileged users, eliminating the need for repeated calls to &dump().
16744: 
16745: =item *
16746: 
16747: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16748: where the outer hash keys are domains specified in the $possdomains array ref,
16749: next inner hash keys are privileged roles specified in the $roles array ref,
16750: and the innermost hash contains key = value pairs for username:domain = end:start
16751: for active or future "privileged" users with that role in that domain. To avoid
16752: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16753: innerhash are cached using priv_$role and $dom as the identifiers.
16754: 
16755: =back
16756: 
16757: =head2 User Modification
16758: 
16759: =over 4
16760: 
16761: =item *
16762: 
16763: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
16764: user for the level given by URL.  Optional start and end dates (leave empty
16765: string or zero for "no date")
16766: 
16767: =item *
16768: 
16769: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16770: change a users, password, possible return values are: ok,
16771: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16772: refused
16773: 
16774: =item *
16775: 
16776: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
16777: 
16778: =item *
16779: 
16780: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16781:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
16782: 
16783: will update user information (firstname,middlename,lastname,generation,
16784: permanentemail), and if forceid is true, student/employee ID also.
16785: A user's institutional affiliation(s) can also be updated.
16786: User information fields will not be overwritten with empty entries 
16787: unless the field is included in the $candelete array reference.
16788: This array is included when a single user is modified via "Manage Users",
16789: or when Autoupdate.pl is run by cron in a domain.
16790: 
16791: =item *
16792: 
16793: modifystudent
16794: 
16795: modify a student's enrollment and identification information.
16796: The course id is resolved based on the current user's environment.  
16797: This means the invoking user must be a course coordinator or otherwise
16798: associated with a course.
16799: 
16800: This call is essentially a wrapper for lonnet::modifyuser and
16801: lonnet::modify_student_enrollment
16802: 
16803: Inputs: 
16804: 
16805: =over 4
16806: 
16807: =item B<$udom> Student's loncapa domain
16808: 
16809: =item B<$uname> Student's loncapa login name
16810: 
16811: =item B<$uid> Student/Employee ID
16812: 
16813: =item B<$umode> Student's authentication mode
16814: 
16815: =item B<$upass> Student's password
16816: 
16817: =item B<$first> Student's first name
16818: 
16819: =item B<$middle> Student's middle name
16820: 
16821: =item B<$last> Student's last name
16822: 
16823: =item B<$gene> Student's generation
16824: 
16825: =item B<$usec> Student's section in course
16826: 
16827: =item B<$end> Unix time of the roles expiration
16828: 
16829: =item B<$start> Unix time of the roles start date
16830: 
16831: =item B<$forceid> If defined, allow $uid to be changed
16832: 
16833: =item B<$desiredhome> server to use as home server for student
16834: 
16835: =item B<$email> Student's permanent e-mail address
16836: 
16837: =item B<$type> Type of enrollment (auto or manual)
16838: 
16839: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
16840: 
16841: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
16842: 
16843: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
16844: 
16845: =item B<$context> role change context (shown in User Management Logs display in a course)
16846: 
16847: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16848: 
16849: =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.
16850: 
16851: =back
16852: 
16853: =item *
16854: 
16855: modify_student_enrollment
16856: 
16857: Change a student's enrollment status in a class.  The environment variable
16858: 'role.request.course' must be defined for this function to proceed.
16859: 
16860: Inputs:
16861: 
16862: =over 4
16863: 
16864: =item $udom, student's domain
16865: 
16866: =item $uname, student's name
16867: 
16868: =item $uid, student's user id
16869: 
16870: =item $first, student's first name
16871: 
16872: =item $middle
16873: 
16874: =item $last
16875: 
16876: =item $gene
16877: 
16878: =item $usec
16879: 
16880: =item $end
16881: 
16882: =item $start
16883: 
16884: =item $type
16885: 
16886: =item $locktype
16887: 
16888: =item $cid
16889: 
16890: =item $selfenroll
16891: 
16892: =item $context
16893: 
16894: =item $credits, number of credits student will earn from this class
16895: 
16896: =item $instsec, institutional course section code for student
16897: 
16898: =back
16899: 
16900: 
16901: =item *
16902: 
16903: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16904: custom role; give a custom role to a user for the level given by URL.  Specify
16905: name and domain of role author, and role name
16906: 
16907: =item *
16908: 
16909: revokerole($udom,$uname,$url,$role) : revoke a role for url
16910: 
16911: =item *
16912: 
16913: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16914: 
16915: =back
16916: 
16917: =head2 Course Infomation
16918: 
16919: =over 4
16920: 
16921: =item *
16922: 
16923: coursedescription($courseid,$options) : returns a hash of information about the
16924: specified course id, including all environment settings for the
16925: course, the description of the course will be in the hash under the
16926: key 'description'
16927: 
16928: $options is an optional parameter that if supplied is a hash reference that controls
16929: what how this function works.  It has the following key/values:
16930: 
16931: =over 4
16932: 
16933: =item freshen_cache
16934: 
16935: If defined, and the environment cache for the course is valid, it is 
16936: returned in the returned hash.
16937: 
16938: =item one_time
16939: 
16940: If defined, the last cache time is set to _now_
16941: 
16942: =item user
16943: 
16944: If defined, the supplied username is used instead of the current user.
16945: 
16946: 
16947: =back
16948: 
16949: =item *
16950: 
16951: resdata($name,$domain,$type,@which) : request for current parameter
16952: setting for a specific $type, where $type is either 'course' or 'user',
16953: @what should be a list of parameters to ask about. This routine caches
16954: answers for 10 minutes.
16955: 
16956: =item *
16957: 
16958: get_courseresdata($courseid, $domain) : dump the entire course resource
16959: data base, returning a hash that is keyed by the resource name and has
16960: values that are the resource value.  I believe that the timestamps and
16961: versions are also returned.
16962: 
16963: =back
16964: 
16965: =head2 Course Modification
16966: 
16967: =over 4
16968: 
16969: =item *
16970: 
16971: writecoursepref($courseid,%prefs) : write preferences (environment
16972: database) for a course
16973: 
16974: =item *
16975: 
16976: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16977: 
16978: =item *
16979: 
16980: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
16981: 
16982: =item *
16983: 
16984: is_course($courseid), is_course($cdom, $cnum)
16985: 
16986: Accepts either a combined $courseid (in the form of domain_courseid) or the
16987: two component version $cdom, $cnum. It checks if the specified course exists.
16988: 
16989: Returns:
16990:     undef if the course doesn't exist, otherwise
16991:     in scalar context the combined courseid.
16992:     in list context the two components of the course identifier, domain and 
16993:     courseid.    
16994: 
16995: =back
16996: 
16997: =head2 Bubblesheet Configuration
16998: 
16999: =over 4
17000: 
17001: =item *
17002: 
17003: get_scantron_config($which)
17004: 
17005: $which - the name of the configuration to parse from the file.
17006: 
17007: Parses and returns the bubblesheet configuration line selected as a
17008: hash of configuration file fields.
17009: 
17010: 
17011: Returns:
17012:     If the named configuration is not in the file, an empty
17013:     hash is returned.
17014: 
17015:     a hash with the fields
17016:       name         - internal name for the this configuration setup
17017:       description  - text to display to operator that describes this config
17018:       CODElocation - if 0 or the string 'none'
17019:                           - no CODE exists for this config
17020:                      if -1 || the string 'letter'
17021:                           - a CODE exists for this config and is
17022:                             a string of letters
17023:                      Unsupported value (but planned for future support)
17024:                           if a positive integer
17025:                                - The CODE exists as the first n items from
17026:                                  the question section of the form
17027:                           if the string 'number'
17028:                                - The CODE exists for this config and is
17029:                                  a string of numbers
17030:       CODEstart   - (only matter if a CODE exists) column in the line where
17031:                      the CODE starts
17032:       CODElength  - length of the CODE
17033:       IDstart     - column where the student/employee ID starts
17034:       IDlength    - length of the student/employee ID info
17035:       Qstart      - column where the information from the bubbled
17036:                     'questions' start
17037:       Qlength     - number of columns comprising a single bubble line from
17038:                     the sheet. (usually either 1 or 10)
17039:       Qon         - either a single character representing the character used
17040:                     to signal a bubble was chosen in the positional setup, or
17041:                     the string 'letter' if the letter of the chosen bubble is
17042:                     in the final, or 'number' if a number representing the
17043:                     chosen bubble is in the file (1->A 0->J)
17044:       Qoff        - the character used to represent that a bubble was
17045:                     left blank
17046:       PaperID     - if the scanning process generates a unique number for each
17047:                     sheet scanned the column that this ID number starts in
17048:       PaperIDlength - number of columns that comprise the unique ID number
17049:                       for the sheet of paper
17050:       FirstName   - column that the first name starts in
17051:       FirstNameLength - number of columns that the first name spans
17052:       LastName    - column that the last name starts in
17053:       LastNameLength - number of columns that the last name spans
17054:       BubblesPerRow - number of bubbles available in each row used to
17055:                       bubble an answer. (If not specified, 10 assumed).
17056: 
17057: 
17058: =item *
17059: 
17060: get_scantronformat_file($cdom)
17061: 
17062: $cdom - the course's domain (optional); if not supplied, uses
17063: domain for current $env{'request.course.id'}.
17064: 
17065: Returns an array containing lines from the scantron format file for
17066: the domain of the course.
17067: 
17068: If a url for a custom.tab file is listed in domain's configuration.db,
17069: lines are from this file.
17070: 
17071: Otherwise, if a default.tab has been published in RES space by the
17072: domainconfig user, lines are from this file.
17073: 
17074: Otherwise, fall back to getting lines from the legacy file on the
17075: local server:  /home/httpd/lonTabs/default_scantronformat.tab
17076: 
17077: =back
17078: 
17079: =head2 Resource Subroutines
17080: 
17081: =over 4
17082: 
17083: =item *
17084: 
17085: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
17086: 
17087: =item *
17088: 
17089: repcopy($filename) : subscribes to the requested file, and attempts to
17090: replicate from the owning library server, Might return
17091: 'unavailable', 'not_found', 'forbidden', 'ok', or
17092: 'bad_request', also attempts to grab the metadata for the
17093: resource. Expects the local filesystem pathname
17094: (/home/httpd/html/res/....)
17095: 
17096: =back
17097: 
17098: =head2 Resource Information
17099: 
17100: =over 4
17101: 
17102: =item *
17103: 
17104: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
17105: and returns the value of a variety of different possible values,
17106: $varname should be a request string, and the other parameters can be
17107: used to specify who and what one is asking about. Ordinarily, $cid 
17108: does not need to be specified, as it is retrived from 
17109: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
17110: within lonuserstate::loadmap() when initializing a course, before
17111: $env{'request.course.id'} has been set, so it needs to be provided
17112: in that one case.
17113: 
17114: Possible values for $varname are environment.lastname (or other item
17115: from the envirnment hash), user.name (or someother aspect about the
17116: user), resource.0.maxtries (or some other part and parameter of a
17117: resource)
17118: 
17119: =item *
17120: 
17121: directcondval($number) : get current value of a condition; reads from a state
17122: string
17123: 
17124: =item *
17125: 
17126: condval($condidx) : value of condition index based on state
17127: 
17128: =item *
17129: 
17130: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
17131: resource's metadata, $what should be either a specific key, or either
17132: 'keys' (to get a list of possible keys) or 'packages' to get a list of
17133: packages that this resource currently uses, the last 3 arguments are 
17134: only used internally for recursive metadata.
17135: 
17136: the toolsymb is only used where the uri is for an external tool (for which
17137: the uri as well as the symb are guaranteed to be unique).
17138: 
17139: this function automatically caches all requests except any made recursively
17140: to retrieve a list of metadata keys for an imported library file ($liburi is 
17141: defined).
17142: 
17143: =item *
17144: 
17145: metadata_query($query,$custom,$customshow) : make a metadata query against the
17146: network of library servers; returns file handle of where SQL and regex results
17147: will be stored for query
17148: 
17149: =item *
17150: 
17151: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
17152: return symbolic list entry (all arguments optional). 
17153: 
17154: Args: filename is the filename (including path) for the file for which a symb 
17155: is required; donotrecurse, if true will prevent calls to allowed() being made 
17156: to check access status if more than one resource was found in the bighash 
17157: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
17158: a randompick); ignorecachednull, if true will prevent a symb of '' being 
17159: returned if $env{$cache_str} is defined as ''; checkforblock if true will
17160: cause possible symbs to be checked to determine if they are subject to content
17161: blocking, if so they will not be included as possible symbs; possibles is a
17162: ref to a hash, which, as a side effect, will be populated with all possible 
17163: symbs (content blocking not tested).
17164:  
17165: returns the data handle
17166: 
17167: =item *
17168: 
17169: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
17170: and is a possible symb for the URL in $thisfn, and if is an encrypted
17171: resource that the user accessed using /enc/ returns a 1 on success, 0
17172: on failure, user must be in a course, as it assumes the existence of
17173: the course initial hash, and uses $env('request.course.id'}.  The third
17174: arg is an optional reference to a scalar.  If this arg is passed in the 
17175: call to symbverify, it will be set to 1 if the symb has been set to be 
17176: encrypted; otherwise it will be null.  
17177: 
17178: =item *
17179: 
17180: symbclean($symb) : removes versions numbers from a symb, returns the
17181: cleaned symb
17182: 
17183: =item *
17184: 
17185: is_on_map($uri) : checks if the $uri is somewhere on the current
17186: course map, user must be in a course for it to work.
17187: 
17188: =item *
17189: 
17190: numval($salt) : return random seed value (addend for rndseed)
17191: 
17192: =item *
17193: 
17194: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
17195: a random seed, all arguments are optional, if they aren't sent it uses the
17196: environment to derive them. Note: if symb isn't sent and it can't get one
17197: from &symbread it will use the current time as its return value
17198: 
17199: =item *
17200: 
17201: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
17202: unfakeable, receipt
17203: 
17204: =item *
17205: 
17206: receipt() : API to ireceipt working off of env values; given out to users
17207: 
17208: =item *
17209: 
17210: countacc($url) : count the number of accesses to a given URL
17211: 
17212: =item *
17213: 
17214: 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
17215: 
17216: =item *
17217: 
17218: 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)
17219: 
17220: =item *
17221: 
17222: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
17223: 
17224: =item *
17225: 
17226: devalidate($symb) : devalidate temporary spreadsheet calculations,
17227: forcing spreadsheet to reevaluate the resource scores next time.
17228: 
17229: =item * 
17230: 
17231: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
17232: when viewing in course context.
17233: 
17234:  input: six args -- filename (decluttered), course number, course domain,
17235:                     url, symb (if registered) and group (if this is a 
17236:                     group item -- e.g., bulletin board, group page etc.).
17237: 
17238:  output: array of five scalars --
17239:          $cfile -- url for file editing if editable on current server
17240:          $home -- homeserver of resource (i.e., for author if published,
17241:                                           or course if uploaded.).
17242:          $switchserver --  1 if server switch will be needed.
17243:          $forceedit -- 1 if icon/link should be to go to edit mode 
17244:          $forceview -- 1 if icon/link should be to go to view mode
17245: 
17246: =item *
17247: 
17248: is_course_upload($file,$cnum,$cdom)
17249: 
17250: Used in course context to determine if current file was uploaded to 
17251: the course (i.e., would be found in /userfiles/docs on the course's 
17252: homeserver.
17253: 
17254:   input: 3 args -- filename (decluttered), course number and course domain.
17255:   output: boolean -- 1 if file was uploaded.
17256: 
17257: =back
17258: 
17259: =head2 Storing/Retreiving Data
17260: 
17261: =over 4
17262: 
17263: =item *
17264: 
17265: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
17266: permanently for this url; hashref needs to be given and should be a \%hashname;
17267: the remaining args aren't required and if they aren't passed or are '' they will
17268: be derived from the env (with the exception of $laststore, which is an 
17269: optional arg used when a user's submission is stored in grading).
17270: $laststore is $version=$timestamp, where $version is the most recent version
17271: number retrieved for the corresponding $symb in the $namespace db file, and
17272: $timestamp is the timestamp for that transaction (UNIX time).
17273: $laststore is currently only passed when cstore() is called by 
17274: structuretags::finalize_storage().
17275: 
17276: =item *
17277: 
17278: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
17279: but uses critical subroutine
17280: 
17281: =item *
17282: 
17283: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
17284: all args are optional
17285: 
17286: =item *
17287: 
17288: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
17289: dumps the complete (or key matching regexp) namespace into a hash
17290: ($udom, $uname, $regexp, $range are optional) for a namespace that is
17291: normally &store()ed into
17292: 
17293: $range should be either an integer '100' (give me the first 100
17294:                                            matching records)
17295:               or be  two integers sperated by a - with no spaces
17296:                  '30-50' (give me the 30th through the 50th matching
17297:                           records)
17298: 
17299: 
17300: =item *
17301: 
17302: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
17303: replaces a &store() version of data with a replacement set of data
17304: for a particular resource in a namespace passed in the $storehash hash 
17305: reference. If $tolog is true, the transaction is logged in the courselog
17306: with an action=PUTSTORE.
17307: 
17308: =item *
17309: 
17310: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
17311: works very similar to store/cstore, but all data is stored in a
17312: temporary location and can be reset using tmpreset, $storehash should
17313: be a hash reference, returns nothing on success
17314: 
17315: =item *
17316: 
17317: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
17318: similar to restore, but all data is stored in a temporary location and
17319: can be reset using tmpreset. Returns a hash of values on success,
17320: error string otherwise.
17321: 
17322: =item *
17323: 
17324: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
17325: deltes all keys for $symb form the temporary storage hash.
17326: 
17327: =item *
17328: 
17329: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
17330: reference filled in from namesp ($udom and $uname are optional)
17331: 
17332: =item *
17333: 
17334: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
17335: namesp ($udom and $uname are optional)
17336: 
17337: =item *
17338: 
17339: dump($namespace,$udom,$uname,$regexp,$range) : 
17340: dumps the complete (or key matching regexp) namespace into a hash
17341: ($udom, $uname, $regexp, $range are optional)
17342: 
17343: $range should be either an integer '100' (give me the first 100
17344:                                            matching records)
17345:               or be  two integers sperated by a - with no spaces
17346:                  '30-50' (give me the 30th through the 50th matching
17347:                           records)
17348: =item *
17349: 
17350: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
17351: $store can be a scalar, an array reference, or if the amount to be 
17352: incremented is > 1, a hash reference.
17353: 
17354: ($udom and $uname are optional)
17355: 
17356: =item *
17357: 
17358: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
17359: ($udom and $uname are optional)
17360: 
17361: =item *
17362: 
17363: cput($namespace,$storehash,$udom,$uname) : critical put
17364: ($udom and $uname are optional)
17365: 
17366: =item *
17367: 
17368: newput($namespace,$storehash,$udom,$uname) :
17369: 
17370: Attempts to store the items in the $storehash, but only if they don't
17371: currently exist, if this succeeds you can be certain that you have 
17372: successfully created a new key value pair in the $namespace db.
17373: 
17374: 
17375: Args:
17376:  $namespace: name of database to store values to
17377:  $storehash: hashref to store to the db
17378:  $udom: (optional) domain of user containing the db
17379:  $uname: (optional) name of user caontaining the db
17380: 
17381: Returns:
17382:  'ok' -> succeeded in storing all keys of $storehash
17383:  'key_exists: <key>' -> failed to anything out of $storehash, as at
17384:                         least <key> already existed in the db (other
17385:                         requested keys may also already exist)
17386:  'error: <msg>' -> unable to tie the DB or other error occurred
17387:  'con_lost' -> unable to contact request server
17388:  'refused' -> action was not allowed by remote machine
17389: 
17390: 
17391: =item *
17392: 
17393: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
17394: reference filled in from namesp (encrypts the return communication)
17395: ($udom and $uname are optional)
17396: 
17397: =item *
17398: 
17399: log($udom,$name,$home,$message) : write to permanent log for user; use
17400: critical subroutine
17401: 
17402: =item *
17403: 
17404: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
17405: array reference filled in from namespace found in domain level on either
17406: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
17407: 
17408: =item *
17409: 
17410: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
17411: domain level either on specified domain server ($uhome) or primary domain 
17412: server ($udom and $uhome are optional)
17413: 
17414: =item * 
17415: 
17416: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
17417: for: authentication, language, quotas, timezone, date locale, and portal URL in
17418: the target domain.
17419: 
17420: May also include additional key => value pairs for the following groups:
17421: 
17422: =over
17423: 
17424: =item
17425: disk quotas (MB allocated by default to portfolios and authoring spaces).
17426: 
17427: =over
17428: 
17429: =item defaultquota, authorquota
17430: 
17431: =back
17432: 
17433: =item
17434: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
17435: portfolio for users).
17436: 
17437: =over
17438: 
17439: =item
17440: aboutme, blog, webdav, portfolio
17441: 
17442: =back
17443: 
17444: =item
17445: requestcourses: ability to request courses, and how requests are processed.
17446: 
17447: =over
17448: 
17449: =item
17450: official, unofficial, community, textbook, placement
17451: 
17452: =back
17453: 
17454: =item
17455: inststatus: types of institutional affiliation, and order in which they are displayed.
17456: 
17457: =over
17458: 
17459: =item
17460: inststatustypes, inststatusorder, inststatusguest
17461: 
17462: =back
17463: 
17464: =item
17465: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
17466: for course's uploaded content.
17467: 
17468: =over
17469: 
17470: =item
17471: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
17472: communityquota, textbookquota, placementquota
17473: 
17474: =back
17475: 
17476: =item
17477: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
17478: on your servers.
17479: 
17480: =over
17481: 
17482: =item 
17483: remotesessions, hostedsessions
17484: 
17485: =back
17486: 
17487: =back
17488: 
17489: In cases where a domain coordinator has never used the "Set Domain Configuration"
17490: utility to create a configuration.db file on a domain's primary library server 
17491: only the following domain defaults: auth_def, auth_arg_def, lang_def
17492: -- corresponding values are authentication type (internal, krb4, krb5,
17493: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
17494: will be available. Values are retrieved from cache (if current), unless the
17495: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
17496: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
17497: 
17498: Typical usage:
17499: 
17500: %domdefaults = &get_domain_defaults($target_domain);
17501: 
17502: =back
17503: 
17504: =head2 Network Status Functions
17505: 
17506: =over 4
17507: 
17508: =item *
17509: 
17510: dirlist() : return directory list based on URI (first arg).
17511: 
17512: Inputs: 1 required, 5 optional.
17513: 
17514: =over
17515: 
17516: =item 
17517: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
17518: 
17519: =item
17520: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
17521: 
17522: =item
17523: $username -  username of user/course to be listed. Extracted from $uri if absent. 
17524: 
17525: =item
17526: $getpropath - boolean: 1 if prepend path using &propath(). 
17527: 
17528: =item
17529: $getuserdir - boolean: 1 if prepend path for "userfiles".
17530: 
17531: =item 
17532: $alternateRoot - path to prepend in place of path from $uri.
17533: 
17534: =back
17535: 
17536: Returns: Array of up to two items.
17537: 
17538: =over
17539: 
17540: a reference to an array of files/subdirectories
17541: 
17542: =over
17543: 
17544: Each element in the array of files/subdirectories is a & separated list of
17545: item name and the result of running stat on the item.  If dirlist was requested
17546: for a file instead of a directory, the item name will be ''. For a directory 
17547: listing, if the item is a metadata file, the element will end &N&M 
17548: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
17549: default copyright set (1).  
17550: 
17551: =back
17552: 
17553: a scalar containing error condition (if encountered).
17554: 
17555: =over
17556: 
17557: =item 
17558: no_host (no homeserver identified for $username:$domain).
17559: 
17560: =item 
17561: no_such_host (server contacted for listing not identified as valid host).
17562: 
17563: =item 
17564: con_lost (connection to remote server failed).
17565: 
17566: =item 
17567: refused (invalid $username:$domain received on lond side).
17568: 
17569: =item 
17570: no_such_dir (directory at specified path on lond side does not exist). 
17571: 
17572: =item 
17573: empty (directory at specified path on lond side is empty).
17574: 
17575: =over
17576: 
17577: This is currently not encountered because the &ls3, &ls2, 
17578: &ls (_handler) routines on the lond side do not filter out
17579: . and .. from a directory listing. 
17580: 
17581: =back
17582: 
17583: =back
17584: 
17585: =back
17586: 
17587: =item *
17588: 
17589: spareserver() : find server with least workload from spare.tab
17590: 
17591: 
17592: =item *
17593: 
17594: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
17595: if there is no corresponding loncapa host.
17596: 
17597: =back
17598: 
17599: 
17600: =head2 Apache Request
17601: 
17602: =over 4
17603: 
17604: =item *
17605: 
17606: ssi($url,%hash) : server side include, does a complete request cycle on url to
17607: localhost, posts hash
17608: 
17609: =back
17610: 
17611: =head2 Data to String to Data
17612: 
17613: =over 4
17614: 
17615: =item *
17616: 
17617: hash2str(%hash) : convert a hash into a string complete with escaping and '='
17618: and '&' separators, supports elements that are arrayrefs and hashrefs
17619: 
17620: =item *
17621: 
17622: hashref2str($hashref) : convert a hashref into a string complete with
17623: escaping and '=' and '&' separators, supports elements that are
17624: arrayrefs and hashrefs
17625: 
17626: =item *
17627: 
17628: arrayref2str($arrayref) : convert an arrayref into a string complete
17629: with escaping and '&' separators, supports elements that are arrayrefs
17630: and hashrefs
17631: 
17632: =item *
17633: 
17634: str2hash($string) : convert string to hash using unescaping and
17635: splitting on '=' and '&', supports elements that are arrayrefs and
17636: hashrefs
17637: 
17638: =item *
17639: 
17640: str2array($string) : convert string to hash using unescaping and
17641: splitting on '&', supports elements that are arrayrefs and hashrefs
17642: 
17643: =back
17644: 
17645: =head2 Logging Routines
17646: 
17647: 
17648: These routines allow one to make log messages in the lonnet.log and
17649: lonnet.perm logfiles.
17650: 
17651: =over 4
17652: 
17653: =item *
17654: 
17655: logtouch() : make sure the logfile, lonnet.log, exists
17656: 
17657: =item *
17658: 
17659: logthis() : append message to the normal lonnet.log file, it gets
17660: preiodically rolled over and deleted.
17661: 
17662: =item *
17663: 
17664: logperm() : append a permanent message to lonnet.perm.log, this log
17665: file never gets deleted by any automated portion of the system, only
17666: messages of critical importance should go in here.
17667: 
17668: 
17669: =back
17670: 
17671: =head2 General File Helper Routines
17672: 
17673: =over 4
17674: 
17675: =item *
17676: 
17677: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17678: (a) files in /uploaded
17679:   (i) If a local copy of the file exists - 
17680:       compares modification date of local copy with last-modified date for 
17681:       definitive version stored on home server for course. If local copy is 
17682:       stale, requests a new version from the home server and stores it. 
17683:       If the original has been removed from the home server, then local copy 
17684:       is unlinked.
17685:   (ii) If local copy does not exist -
17686:       requests the file from the home server and stores it. 
17687:   
17688:   If $caller is 'uploadrep':  
17689:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17690:     for request for files originally uploaded via DOCS. 
17691:      - returns 'ok' if fresh local copy now available, -1 otherwise.
17692:   
17693:   Otherwise:
17694:      This indicates a call from the content generation phase of the request.
17695:      -  returns the entire contents of the file or -1.
17696:      
17697: (b) files in /res
17698:    - returns the entire contents of a file or -1; 
17699:    it properly subscribes to and replicates the file if neccessary.
17700: 
17701: 
17702: =item *
17703: 
17704: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17705:                   reference
17706: 
17707: returns either a stat() list of data about the file or an empty list
17708: if the file doesn't exist or couldn't find out about it (connection
17709: problems or user unknown)
17710: 
17711: =item *
17712: 
17713: filelocation($dir,$file) : returns file system location of a file
17714: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17715: directory that relative $file lookups are to looked in ($dir of /a/dir
17716: and a file of ../bob will become /a/bob)
17717: 
17718: =item *
17719: 
17720: hreflocation($dir,$file) : returns file system location or a URL; same as
17721: filelocation except for hrefs
17722: 
17723: =item *
17724: 
17725: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17726: also removes beginning /home/httpd/html unless /priv/ follows it.
17727: 
17728: =back
17729: 
17730: =head2 Usererfile file routines (/uploaded*)
17731: 
17732: =over 4
17733: 
17734: =item *
17735: 
17736: userfileupload(): main rotine for putting a file in a user or course's
17737:                   filespace, arguments are,
17738: 
17739:  formname - required - this is the name of the element in $env where the
17740:            filename, and the contents of the file to create/modifed exist
17741:            the filename is in $env{'form.'.$formname.'.filename'} and the
17742:            contents of the file is located in $env{'form.'.$formname}
17743:  context - if coursedoc, store the file in the course of the active role
17744:              of the current user; 
17745:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17746:            if 'canceloverwrite': delete file in tmp/overwrites directory
17747:  subdir - required - subdirectory to put the file in under ../userfiles/
17748:          if undefined, it will be placed in "unknown"
17749: 
17750:  (This routine calls clean_filename() to remove any dangerous
17751:  characters from the filename, and then calls finuserfileupload() to
17752:  complete the transaction)
17753: 
17754:  returns either the url of the uploaded file (/uploaded/....) if successful
17755:  and /adm/notfound.html if unsuccessful
17756: 
17757: =item *
17758: 
17759: clean_filename(): routine for cleaing a filename up for storage in
17760:                  userfile space, argument is:
17761: 
17762:  filename - proposed filename
17763: 
17764: returns: the new clean filename
17765: 
17766: =item *
17767: 
17768: finishuserfileupload(): routine that creates and sends the file to
17769: userspace, probably shouldn't be called directly
17770: 
17771:   docuname: username or courseid of destination for the file
17772:   docudom: domain of user/course of destination for the file
17773:   formname: same as for userfileupload()
17774:   fname: filename (including subdirectories) for the file
17775:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
17776:           if hashref, and context is scantron, will convert csv format to standard format
17777:   allfiles: reference to hash used to store objects found by parser
17778:   codebase: reference to hash used for codebases of java objects found by parser
17779:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17780:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
17781:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
17782:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
17783:   context: if 'overwrite', will move the uploaded file from its temporary location to
17784:             userfiles to facilitate overwriting a previously uploaded file with same name.
17785:   mimetype: reference to scalar to accommodate mime type determined
17786:             from File::MMagic if $parser = parse.
17787: 
17788:  returns either the url of the uploaded file (/uploaded/....) if successful
17789:  and /adm/notfound.html if unsuccessful (or an error message if context 
17790:  was 'overwrite').
17791:  
17792: 
17793: =item *
17794: 
17795: renameuserfile(): renames an existing userfile to a new name
17796: 
17797:   Args:
17798:    docuname: username or courseid of destination for the file
17799:    docudom: domain of user/course of destination for the file
17800:    old: current file name (including any subdirs under userfiles)
17801:    new: desired file name (including any subdirs under userfiles)
17802: 
17803: =item *
17804: 
17805: mkdiruserfile(): creates a directory is a userfiles dir
17806: 
17807:   Args:
17808:    docuname: username or courseid of destination for the file
17809:    docudom: domain of user/course of destination for the file
17810:    dir: dir to create (including any subdirs under userfiles)
17811: 
17812: =item *
17813: 
17814: removeuserfile(): removes a file that exists in userfiles
17815: 
17816:   Args:
17817:    docuname: username or courseid of destination for the file
17818:    docudom: domain of user/course of destination for the file
17819:    fname: filname to delete (including any subdirs under userfiles)
17820: 
17821: =item *
17822: 
17823: removeuploadedurl(): convience function for removeuserfile()
17824: 
17825:   Args:
17826:    url:  a full /uploaded/... url to delete
17827: 
17828: =item * 
17829: 
17830: get_portfile_permissions():
17831:   Args:
17832:     domain: domain of user or course contain the portfolio files
17833:     user: name of user or num of course contain the portfolio files
17834:   Returns:
17835:     hashref of a dump of the proper file_permissions.db
17836:    
17837: 
17838: =item * 
17839: 
17840: get_access_controls():
17841: 
17842: Args:
17843:   current_permissions: the hash ref returned from get_portfile_permissions()
17844:   group: (optional) the group you want the files associated with
17845:   file: (optional) the file you want access info on
17846: 
17847: Returns:
17848:     a hash (keys are file names) of hashes containing
17849:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17850:         values are XML containing access control settings (see below) 
17851: 
17852: Internal notes:
17853: 
17854:  access controls are stored in file_permissions.db as key=value pairs.
17855:     key -> path to file/file_name\0uniqueID:scope_end_start
17856:         where scope -> public,guest,course,group,domains or users.
17857:               end -> UNIX time for end of access (0 -> no end date)
17858:               start -> UNIX time for start of access
17859: 
17860:     value -> XML description of access control
17861:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17862:             <start></start>
17863:             <end></end>
17864: 
17865:             <password></password>  for scope type = guest
17866: 
17867:             <domain></domain>     for scope type = course or group
17868:             <number></number>
17869:             <roles id="">
17870:              <role></role>
17871:              <access></access>
17872:              <section></section>
17873:              <group></group>
17874:             </roles>
17875: 
17876:             <dom></dom>         for scope type = domains
17877: 
17878:             <users>             for scope type = users
17879:              <user>
17880:               <uname></uname>
17881:               <udom></udom>
17882:              </user>
17883:             </users>
17884:            </scope> 
17885:               
17886:  Access data is also aggregated for each file in an additional key=value pair:
17887:  key -> path to file/file_name\0accesscontrol 
17888:  value -> reference to hash
17889:           hash contains key = value pairs
17890:           where key = uniqueID:scope_end_start
17891:                 value = UNIX time record was last updated
17892: 
17893:           Used to improve speed of look-ups of access controls for each file.  
17894:  
17895:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17896: 
17897: =item *
17898: 
17899: modify_access_controls():
17900: 
17901: Modifies access controls for a portfolio file
17902: Args
17903: 1. file name
17904: 2. reference to hash of required changes,
17905: 3. domain
17906: 4. username
17907:   where domain,username are the domain of the portfolio owner 
17908:   (either a user or a course) 
17909: 
17910: Returns:
17911: 1. result of additions or updates ('ok' or 'error', with error message). 
17912: 2. result of deletions ('ok' or 'error', with error message).
17913: 3. reference to hash of any new or updated access controls.
17914: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17915:    key = integer (inbound ID)
17916:    value = uniqueID
17917: 
17918: =item *
17919: 
17920: get_timebased_id():
17921: 
17922: Attempts to get a unique timestamp-based suffix for use with items added to a 
17923: course via the Course Editor (e.g., folders, composite pages, 
17924: group bulletin boards).
17925: 
17926: Args: (first three required; six others optional)
17927: 
17928: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17929:    docssequence, or name of group
17930: 
17931: 2. keyid (alphanumeric): name of temporary locking key in hash,
17932:    e.g., num, boardids
17933: 
17934: 3. namespace: name of gdbm file used to store suffixes already assigned;  
17935:    file will be named nohist_namespace.db
17936: 
17937: 4. cdom: domain of course; default is current course domain from %env
17938: 
17939: 5. cnum: course number; default is current course number from %env
17940: 
17941: 6. idtype: set to concat if an additional digit is to be appended to the 
17942:    unix timestamp to form the suffix, if the plain timestamp is already
17943:    in use.  Default is to not do this, but simply increment the unix 
17944:    timestamp by 1 until a unique key is obtained.
17945: 
17946: 7. who: holder of locking key; defaults to user:domain for user.
17947: 
17948: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
17949:    retrying); default is 3.
17950: 
17951: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
17952: 
17953: Returns:
17954: 
17955: 1. suffix obtained (numeric)
17956: 
17957: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17958: 
17959: 3. error: contains (localized) error message if an error occurred.
17960: 
17961: 
17962: =back
17963: 
17964: =head2 HTTP Helper Routines
17965: 
17966: =over 4
17967: 
17968: =item *
17969: 
17970: escape() : unpack non-word characters into CGI-compatible hex codes
17971: 
17972: =item *
17973: 
17974: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17975: 
17976: =back
17977: 
17978: =head1 PRIVATE SUBROUTINES
17979: 
17980: =head2 Underlying communication routines (Shouldn't call)
17981: 
17982: =over 4
17983: 
17984: =item *
17985: 
17986: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17987: 
17988: =item *
17989: 
17990: reply() : uses subreply to send a message to remote machine, logs all failures
17991: 
17992: =item *
17993: 
17994: critical() : passes a critical message to another server; if cannot
17995: get through then place message in connection buffer directory and
17996: returns con_delayed, if incapable of saving message, returns
17997: con_failed
17998: 
17999: =item *
18000: 
18001: reconlonc() : tries to reconnect lonc client processes.
18002: 
18003: =back
18004: 
18005: =head2 Resource Access Logging
18006: 
18007: =over 4
18008: 
18009: =item *
18010: 
18011: flushcourselogs() : flush (save) buffer logs and access logs
18012: 
18013: =item *
18014: 
18015: courselog($what) : save message for course in hash
18016: 
18017: =item *
18018: 
18019: courseacclog($what) : save message for course using &courselog().  Perform
18020: special processing for specific resource types (problems, exams, quizzes, etc).
18021: 
18022: =item *
18023: 
18024: goodbye() : flush course logs and log shutting down; it is called in srm.conf
18025: as a PerlChildExitHandler
18026: 
18027: =back
18028: 
18029: =head2 Other
18030: 
18031: =over 4
18032: 
18033: =item *
18034: 
18035: symblist($mapname,%newhash) : update symbolic storage links
18036: 
18037: =back
18038: 
18039: =cut
18040: 

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