File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1539: download - view: text, annotated - select for diffs
Fri Jun 6 20:36:56 2025 UTC (4 weeks, 1 day ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- When encrypturl is in effect extra effort may be needed to get the symb.
  Also try to avoid sending an empty string in the signed LTI launch payload
  for resource_link_title and resource_link_id, because for some reason, the
  ProctorU LTI 1.1 Provider requires something other than no value for them.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1539 2025/06/06 20:36:56 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:     if ($loncaparevs{$lonid} =~ /^(\d+\.\d+\.[\w.]+)-\d+$/) {
  192:         print $client (join(':',$hostname,$lonid,$1,&machine_ids($hostname))."\n");
  193:     } else {
  194:         print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
  195:     }
  196:     my $result = <$client>;
  197:     chomp($result);
  198:     return 1 if ($result eq 'done');
  199:     return 0;
  200: }
  201: 
  202: sub get_server_timezone {
  203:     my ($cnum,$cdom) = @_;
  204:     my $home=&homeserver($cnum,$cdom);
  205:     if ($home ne 'no_host') {
  206:         my $cachetime = 24*3600;
  207:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  208:         if (defined($cached)) {
  209:             return $timezone;
  210:         } else {
  211:             my $timezone = &reply('servertimezone',$home);
  212:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  213:         }
  214:     }
  215: }
  216: 
  217: sub get_server_distarch {
  218:     my ($lonhost,$ignore_cache) = @_;
  219:     if (defined($lonhost)) {
  220:         if (!defined(&hostname($lonhost))) {
  221:             return;
  222:         }
  223:         my $cachetime = 12*3600;
  224:         if (!$ignore_cache) {
  225:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  226:             if (defined($cached)) {
  227:                 return $distarch;
  228:             }
  229:         }
  230:         my $rep = &reply('serverdistarch',$lonhost);
  231:         unless ($rep eq 'unknown_cmd' || $rep eq 'no_such_host' ||
  232:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  233:                 $rep eq '') {
  234:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  235:         }
  236:     }
  237:     return;
  238: }
  239: 
  240: sub get_servercerts_info {
  241:     my ($lonhost,$hostname,$context) = @_;
  242:     return if ($lonhost eq '');
  243:     if ($hostname eq '') {
  244:         $hostname = &hostname($lonhost);
  245:     }
  246:     return if ($hostname eq '');
  247:     my ($rep,$uselocal);
  248:     if ($context eq 'install') {
  249:         $uselocal = 1;
  250:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  251:         $uselocal = 1;
  252:     }
  253:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  254:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  255:         if ($distro eq '') {
  256:             $uselocal = 0;
  257:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  258:             if ($1 < 6) {
  259:                 $uselocal = 0;
  260:             }
  261:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  262:             if ($1 < 12) {
  263:                 $uselocal = 0;
  264:             }
  265:         }
  266:     }
  267:     if ($uselocal) {
  268:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  269:     } else {
  270:         $rep=&reply('servercerts',$lonhost);
  271:     }
  272:     my ($result,%returnhash);
  273:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  274:         ($rep eq 'unknown_cmd')) {
  275:         $result = $rep;
  276:     } else {
  277:         $result = 'ok';
  278:         my @pairs=split(/\&/,$rep);
  279:         foreach my $item (@pairs) {
  280:             my ($key,$value)=split(/=/,$item,2);
  281:             my $what = &unescape($key);
  282:             $returnhash{$what}=&thaw_unescape($value);
  283:         }
  284:     }
  285:     return ($result,\%returnhash);
  286: }
  287: 
  288: sub get_server_loncaparev {
  289:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  290:     if (defined($lonhost)) {
  291:         if (!defined(&hostname($lonhost))) {
  292:             undef($lonhost);
  293:         }
  294:     }
  295:     if (!defined($lonhost)) {
  296:         if (defined(&domain($dom,'primary'))) {
  297:             $lonhost=&domain($dom,'primary');
  298:             if ($lonhost eq 'no_host') {
  299:                 undef($lonhost);
  300:             }
  301:         }
  302:     }
  303:     if (defined($lonhost)) {
  304:         my $cachetime = 12*3600;
  305:         if (!$ignore_cache) {
  306:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  307:             if (defined($cached)) {
  308:                 return $loncaparev;
  309:             }
  310:         }
  311:         my ($answer,$loncaparev);
  312:         my @ids=&current_machine_ids();
  313:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  314:             $answer = $perlvar{'lonVersion'};
  315:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  316:                 $loncaparev = $1;
  317:             }
  318:         } else {
  319:             $answer = &reply('serverloncaparev',$lonhost);
  320:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  321:                 if ($caller eq 'loncron') {
  322:                     my $hostname = &hostname($lonhost);
  323:                     my $protocol = $protocol{$lonhost};
  324:                     $protocol = 'http' if ($protocol ne 'https');
  325:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  326:                     my $request=new HTTP::Request('GET',$url);
  327:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  328:                     unless ($response->is_error()) {
  329:                         my $content = $response->content;
  330:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  331:                             $loncaparev = $1;
  332:                         }
  333:                     }
  334:                 } else {
  335:                     $loncaparev = $loncaparevs{$lonhost};
  336:                 }
  337:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  338:                 $loncaparev = $1;
  339:             }
  340:         }
  341:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  342:     }
  343: }
  344: 
  345: sub get_server_homeID {
  346:     my ($hostname,$ignore_cache,$caller) = @_;
  347:     unless ($ignore_cache) {
  348:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  349:         if (defined($cached)) {
  350:             return $serverhomeID;
  351:         }
  352:     }
  353:     my $cachetime = 12*3600;
  354:     my $serverhomeID;
  355:     if ($caller eq 'loncron') { 
  356:         my @machine_ids = &machine_ids($hostname);
  357:         foreach my $id (@machine_ids) {
  358:             my $response = &reply('serverhomeID',$id);
  359:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  360:                 $serverhomeID = $response;
  361:                 last;
  362:             }
  363:         }
  364:         if ($serverhomeID eq '') {
  365:             $serverhomeID = $machine_ids[-1];
  366:         }
  367:     } else {
  368:         $serverhomeID = $serverhomeIDs{$hostname};
  369:     }
  370:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  371: }
  372: 
  373: sub get_remote_globals {
  374:     my ($lonhost,$whathash,$ignore_cache) = @_;
  375:     my ($result,%returnhash,%whatneeded);
  376:     if (ref($whathash) eq 'HASH') {
  377:         foreach my $what (sort(keys(%{$whathash}))) {
  378:             my $hashid = $lonhost.'-'.$what;
  379:             my ($response,$cached);
  380:             unless ($ignore_cache) {
  381:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  382:             }
  383:             if (defined($cached)) {
  384:                 $returnhash{$what} = $response;
  385:             } else {
  386:                 $whatneeded{$what} = 1;
  387:             }
  388:         }
  389:         if (keys(%whatneeded) == 0) {
  390:             $result = 'ok';
  391:         } else {
  392:             my $requested = &freeze_escape(\%whatneeded);
  393:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  394:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  395:                 ($rep eq 'unknown_cmd')) {
  396:                 $result = $rep;
  397:             } else {
  398:                 $result = 'ok';
  399:                 my @pairs=split(/\&/,$rep);
  400:                 foreach my $item (@pairs) {
  401:                     my ($key,$value)=split(/=/,$item,2);
  402:                     my $what = &unescape($key);
  403:                     my $hashid = $lonhost.'-'.$what;
  404:                     $returnhash{$what}=&thaw_unescape($value);
  405:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  406:                 }
  407:             }
  408:         }
  409:     }
  410:     return ($result,\%returnhash);
  411: }
  412: 
  413: sub remote_devalidate_cache {
  414:     my ($lonhost,$cachekeys) = @_;
  415:     my $items;
  416:     return unless (ref($cachekeys) eq 'ARRAY');
  417:     my $cachestr = join('&',@{$cachekeys});
  418:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  419:     return $response;
  420: }
  421: 
  422: sub sign_lti {
  423:     my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
  424:     my $chome;
  425:     if (&domain($cdom) ne '') {
  426:         if ($crsdef) {
  427:             $chome = &homeserver($cnum,$cdom);
  428:         } else {
  429:             $chome = &domain($cdom,'primary');
  430:         }
  431:     }
  432:     if ($cdom && $chome && ($chome ne 'no_host')) {
  433:         if ((ref($paramsref) eq 'HASH') &&
  434:             (ref($inforef) eq 'HASH')) {
  435:             my $rep;
  436:             if (grep { $_ eq $chome } &current_machine_ids()) {
  437:                 # domain information is hosted on this machine
  438:                 $rep =
  439:                     &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
  440:                                                      $context,$url,$ltinum,$keynum,
  441:                                                      $perlvar{'lonVersion'},
  442:                                                      $paramsref,$inforef);
  443:                 if (ref($rep) eq 'HASH') {
  444:                     return ('ok',$rep);
  445:                 }
  446:             } else {
  447:                 my ($escurl,$params,$info);
  448:                 $escurl = &escape($url);
  449:                 if (ref($paramsref) eq 'HASH') {
  450:                     $params = &freeze_escape($paramsref);
  451:                 }
  452:                 if (ref($inforef) eq 'HASH') {
  453:                     $info = &freeze_escape($inforef);
  454:                 }
  455:                 $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
  456:             }
  457:             if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
  458:                 return ();
  459:             } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
  460:                      ($inforef->{'respfmt'} eq 'to_authorization_header')) {
  461:                 return ('ok',$rep);
  462:             } else {
  463:                 my %returnhash;
  464:                 foreach my $item (split(/\&/,$rep)) {
  465:                     my ($name,$value)=split(/\=/,$item);
  466:                     $returnhash{&unescape($name)}=&thaw_unescape($value);
  467:                 }
  468:                 return('ok',\%returnhash);
  469:             }
  470:         } else {
  471:             return ();
  472:         }
  473:     } else {
  474:         return ();
  475:         &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
  476:     }
  477: }
  478: 
  479: # -------------------------------------------------- Non-critical communication
  480: sub subreply {
  481:     my ($cmd,$server)=@_;
  482:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  483:     #
  484:     #  With loncnew process trimming, there's a timing hole between lonc server
  485:     #  process exit and the master server picking up the listen on the AF_UNIX
  486:     #  socket.  In that time interval, a lock file will exist:
  487: 
  488:     my $lockfile=$peerfile.".lock";
  489:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  490: 	sleep(0.1);
  491:     }
  492:     # At this point, either a loncnew parent is listening or an old lonc
  493:     # or loncnew child is listening so we can connect or everything's dead.
  494:     #
  495:     #   We'll give the connection a few tries before abandoning it.  If
  496:     #   connection is not possible, we'll con_lost back to the client.
  497:     #   
  498:     my $client;
  499:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  500: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  501: 				      Type    => SOCK_STREAM,
  502: 				      Timeout => 10);
  503: 	if ($client) {
  504: 	    last;		# Connected!
  505: 	} else {
  506: 	    &create_connection(&hostname($server),$server);
  507: 	}
  508:         sleep(0.1);	# Try again later if failed connection.
  509:     }
  510:     my $answer;
  511:     if ($client) {
  512: 	print $client "sethost:$server:$cmd\n";
  513: 	$answer=<$client>;
  514: 	if (!$answer) { $answer="con_lost"; }
  515: 	chomp($answer);
  516:     } else {
  517: 	$answer = 'con_lost';	# Failed connection.
  518:     }
  519:     return $answer;
  520: }
  521: 
  522: sub reply {
  523:     my ($cmd,$server)=@_;
  524:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  525:     my $answer=subreply($cmd,$server);
  526:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  527:         my $logged = $cmd;
  528:         if ($cmd =~ /^encrypt:([^:]+):/) {
  529:             my $subcmd = $1;
  530:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  531:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  532:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
  533:                 ($subcmd eq 'put')) {
  534:                 (undef,undef,my @rest) = split(/:/,$cmd);
  535:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  536:                     splice(@rest,2,1,'Hidden');
  537:                 } elsif ($subcmd eq 'passwd') {
  538:                     splice(@rest,2,2,('Hidden','Hidden'));
  539:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  540:                          ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
  541:                     splice(@rest,3,1,'Hidden');
  542:                 }
  543:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  544:             }
  545:         }
  546:         &logthis("<font color=\"blue\">WARNING:".
  547:                  " $logged to $server returned $answer</font>");
  548:     }
  549:     return $answer;
  550: }
  551: 
  552: # ----------------------------------------------------------- Send USR1 to lonc
  553: 
  554: sub reconlonc {
  555:     my ($lonid) = @_;
  556:     if ($lonid) {
  557:         my $hostname = &hostname($lonid);
  558: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  559: 	if ($hostname && -e $peerfile) {
  560: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  561: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  562: 					     Type    => SOCK_STREAM,
  563: 					     Timeout => 10);
  564: 	    if ($client) {
  565: 		print $client ("reset_retries\n");
  566: 		my $answer=<$client>;
  567: 		#reset just this one.
  568: 	    }
  569: 	}
  570: 	return;
  571:     }
  572: 
  573:     &logthis("Trying to reconnect lonc");
  574:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  575:     if (open(my $fh,"<",$loncfile)) {
  576: 	my $loncpid=<$fh>;
  577:         chomp($loncpid);
  578:         if (kill 0 => $loncpid) {
  579: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  580:             kill USR1 => $loncpid;
  581:             sleep 1;
  582:         } else {
  583: 	    &logthis(
  584:                "<font color=\"blue\">WARNING:".
  585:                " lonc at pid $loncpid not responding, giving up</font>");
  586:         }
  587:     } else {
  588: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  589:     }
  590: }
  591: 
  592: # ------------------------------------------------------ Critical communication
  593: 
  594: sub critical {
  595:     my ($cmd,$server)=@_;
  596:     unless (&hostname($server)) {
  597:         &logthis("<font color=\"blue\">WARNING:".
  598:                " Critical message to unknown server ($server)</font>");
  599:         return 'no_such_host';
  600:     }
  601:     my $answer=reply($cmd,$server);
  602:     if ($answer eq 'con_lost') {
  603: 	&reconlonc($server);
  604: 	my $answer=reply($cmd,$server);
  605:         if ($answer eq 'con_lost') {
  606:             my $now=time;
  607:             my $middlename=$cmd;
  608:             $middlename=substr($middlename,0,16);
  609:             $middlename=~s/\W//g;
  610:             my $dfilename=
  611:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  612:             $dumpcount++;
  613:             {
  614: 		my $dfh;
  615: 		if (open($dfh,">",$dfilename)) {
  616: 		    print $dfh "$cmd\n"; 
  617: 		    close($dfh);
  618: 		}
  619:             }
  620:             sleep 1;
  621:             my $wcmd='';
  622:             {
  623: 		my $dfh;
  624: 		if (open($dfh,"<",$dfilename)) {
  625: 		    $wcmd=<$dfh>; 
  626: 		    close($dfh);
  627: 		}
  628:             }
  629:             chomp($wcmd);
  630:             if ($wcmd eq $cmd) {
  631: 		&logthis("<font color=\"blue\">WARNING: ".
  632:                          "Connection buffer $dfilename: $cmd</font>");
  633:                 &logperm("D:$server:$cmd");
  634: 	        return 'con_delayed';
  635:             } else {
  636:                 &logthis("<font color=\"red\">CRITICAL:"
  637:                         ." Critical connection failed: $server $cmd</font>");
  638:                 &logperm("F:$server:$cmd");
  639:                 return 'con_failed';
  640:             }
  641:         }
  642:     }
  643:     return $answer;
  644: }
  645: 
  646: # ------------------------------------------- check if return value is an error
  647: 
  648: sub error {
  649:     my ($result) = @_;
  650:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  651: 	if ($2 == 2) { return undef; }
  652: 	return $1;
  653:     }
  654:     return undef;
  655: }
  656: 
  657: sub convert_and_load_session_env {
  658:     my ($lonidsdir,$handle)=@_;
  659:     my @profile;
  660:     {
  661: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  662: 	if (!$opened) {
  663: 	    return 0;
  664: 	}
  665: 	flock($idf,LOCK_SH);
  666: 	@profile=<$idf>;
  667: 	close($idf);
  668:     }
  669:     my %temp_env;
  670:     foreach my $line (@profile) {
  671: 	if ($line !~ m/=/) {
  672: 	    return 0;
  673: 	}
  674: 	chomp($line);
  675: 	my ($envname,$envvalue)=split(/=/,$line,2);
  676: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  677:     }
  678:     unlink("$lonidsdir/$handle.id");
  679:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  680: 	    0640)) {
  681: 	%disk_env = %temp_env;
  682: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  683: 	untie(%disk_env);
  684:     }
  685:     return 1;
  686: }
  687: 
  688: # ------------------------------------------- Transfer profile into environment
  689: my $env_loaded;
  690: sub transfer_profile_to_env {
  691:     my ($lonidsdir,$handle,$force_transfer) = @_;
  692:     if (!$force_transfer && $env_loaded) { return; } 
  693: 
  694:     if (!defined($lonidsdir)) {
  695: 	$lonidsdir = $perlvar{'lonIDsDir'};
  696:     }
  697:     if (!defined($handle)) {
  698:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  699:     }
  700: 
  701:     my $convert;
  702:     {
  703:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  704: 	if (!$opened) {
  705: 	    return;
  706: 	}
  707: 	flock($idf,LOCK_SH);
  708: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  709: 		&GDBM_READER(),0640)) {
  710: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  711: 	    untie(%disk_env);
  712: 	} else {
  713: 	    $convert = 1;
  714: 	}
  715:     }
  716:     if ($convert) {
  717: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  718: 	    &logthis("Failed to load session, or convert session.");
  719: 	}
  720:     }
  721: 
  722:     my %remove;
  723:     while ( my $envname = each(%env) ) {
  724:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  725:             if ($time < time-300) {
  726:                 $remove{$key}++;
  727:             }
  728:         }
  729:     }
  730: 
  731:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  732:     $env_loaded=1;
  733:     foreach my $expired_key (keys(%remove)) {
  734:         &delenv($expired_key);
  735:     }
  736: }
  737: 
  738: # ---------------------------------------------------- Check for valid session 
  739: sub check_for_valid_session {
  740:     my ($r,$name,$userhashref,$domref) = @_;
  741:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  742:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  743:     if ($name eq 'lonDAV') {
  744:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  745:     } else {
  746:         $lonidsdir=$r->dir_config('lonIDsDir');
  747:         if ($name eq '') {
  748:             $name = 'lonID';
  749:         }
  750:     }
  751:     if ($name eq 'lonID') {
  752:         $secure = 'lonSID';
  753:         $linkname = 'lonLinkID';
  754:         $pubname = 'lonPubID';
  755:         if (exists($cookies{$secure})) {
  756:             $lonid=$cookies{$secure};
  757:         } elsif (exists($cookies{$name})) {
  758:             $lonid=$cookies{$name};
  759:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  760:             $lonid=$cookies{$linkname};
  761:         } elsif (exists($cookies{$pubname})) {
  762:             $lonid=$cookies{$pubname};
  763:         }
  764:     } else {
  765:         $lonid=$cookies{$name};
  766:     }
  767:     return undef if (!$lonid);
  768: 
  769:     my $handle=&LONCAPA::clean_handle($lonid->value);
  770:     if (-l "$lonidsdir/$handle.id") {
  771:         my $link = readlink("$lonidsdir/$handle.id");
  772:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  773:             $handle = $1;
  774:         }
  775:     }
  776:     if (!-e "$lonidsdir/$handle.id") {
  777:         if ((ref($domref)) && ($name eq 'lonID') && 
  778:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  779:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  780:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  781:                 $$domref = $possudom;
  782:             }
  783:         }
  784:         return undef;
  785:     }
  786: 
  787:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  788:     return undef if (!$opened);
  789: 
  790:     flock($idf,LOCK_SH);
  791:     my %disk_env;
  792:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  793: 	    &GDBM_READER(),0640)) {
  794: 	return undef;	
  795:     }
  796: 
  797:     if (!defined($disk_env{'user.name'})
  798: 	|| !defined($disk_env{'user.domain'})) {
  799:         untie(%disk_env);
  800: 	return undef;
  801:     }
  802: 
  803:     if (ref($userhashref) eq 'HASH') {
  804:         $userhashref->{'name'} = $disk_env{'user.name'};
  805:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  806:         if ($disk_env{'request.role'}) {
  807:             $userhashref->{'role'} = $disk_env{'request.role'};
  808:         }
  809:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  810:         if ($userhashref->{'lti'}) {
  811:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  812:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  813:         }
  814:     }
  815:     untie(%disk_env);
  816: 
  817:     return $handle;
  818: }
  819: 
  820: sub timed_flock {
  821:     my ($file,$lock_type) = @_;
  822:     my $failed=0;
  823:     eval {
  824: 	local $SIG{__DIE__}='DEFAULT';
  825: 	local $SIG{ALRM}=sub {
  826: 	    $failed=1;
  827: 	    die("failed lock");
  828: 	};
  829: 	alarm(13);
  830: 	flock($file,$lock_type);
  831: 	alarm(0);
  832:     };
  833:     if ($failed) {
  834: 	return undef;
  835:     } else {
  836: 	return 1;
  837:     }
  838: }
  839: 
  840: sub get_sessionfile_vars {
  841:     my ($handle,$lonidsdir,$storearr) = @_;
  842:     my %returnhash;
  843:     unless (ref($storearr) eq 'ARRAY') {
  844:         return %returnhash;
  845:     }
  846:     if (-l "$lonidsdir/$handle.id") {
  847:         my $link = readlink("$lonidsdir/$handle.id");
  848:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  849:             $handle = $1;
  850:         }
  851:     }
  852:     if ((-e "$lonidsdir/$handle.id") &&
  853:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  854:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  855:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  856:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  857:                 flock($idf,LOCK_SH);
  858:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  859:                         &GDBM_READER(),0640)) {
  860:                     foreach my $item (@{$storearr}) {
  861:                         $returnhash{$item} = $disk_env{$item};
  862:                     }
  863:                     untie(%disk_env);
  864:                 }
  865:             }
  866:         }
  867:     }
  868:     return %returnhash;
  869: }
  870: 
  871: # ---------------------------------------------------------- Append Environment
  872: 
  873: sub appenv {
  874:     my ($newenv,$roles) = @_;
  875:     if (ref($newenv) eq 'HASH') {
  876:         foreach my $key (keys(%{$newenv})) {
  877:             my $refused = 0;
  878: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  879:                 $refused = 1;
  880:                 if (ref($roles) eq 'ARRAY') {
  881:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  882:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  883:                         $refused = 0;
  884:                     }
  885:                 }
  886:             }
  887:             if ($refused) {
  888:                 &logthis("<font color=\"blue\">WARNING: ".
  889:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  890:                          .'</font>');
  891: 	        delete($newenv->{$key});
  892:             } else {
  893:                 $env{$key}=$newenv->{$key};
  894:             }
  895:         }
  896:         my $lonids = $perlvar{'lonIDsDir'};
  897:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  898:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  899:             if ($opened
  900: 	        && &timed_flock($env_file,LOCK_EX)
  901: 	        &&
  902: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  903: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  904: 	        while (my ($key,$value) = each(%{$newenv})) {
  905: 	            $disk_env{$key} = $value;
  906: 	        }
  907: 	        untie(%disk_env);
  908:             }
  909:         }
  910:     }
  911:     return 'ok';
  912: }
  913: # ----------------------------------------------------- Delete from Environment
  914: 
  915: sub delenv {
  916:     my ($delthis,$regexp,$roles) = @_;
  917:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  918:         my $refused = 1;
  919:         if (ref($roles) eq 'ARRAY') {
  920:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  921:             if (grep(/^\Q$role\E$/,@{$roles})) {
  922:                 $refused = 0;
  923:             }
  924:         }
  925:         if ($refused) {
  926:             &logthis("<font color=\"blue\">WARNING: ".
  927:                      "Attempt to delete from environment ".$delthis);
  928:             return 'error';
  929:         }
  930:     }
  931:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  932:     if ($opened
  933: 	&& &timed_flock($env_file,LOCK_EX)
  934: 	&&
  935: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  936: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  937: 	foreach my $key (keys(%disk_env)) {
  938: 	    if ($regexp) {
  939:                 if ($key=~/^$delthis/) {
  940:                     delete($env{$key});
  941:                     delete($disk_env{$key});
  942:                 } 
  943:             } else {
  944:                 if ($key=~/^\Q$delthis\E/) {
  945: 		    delete($env{$key});
  946: 		    delete($disk_env{$key});
  947: 	        }
  948:             }
  949: 	}
  950: 	untie(%disk_env);
  951:     }
  952:     return 'ok';
  953: }
  954: 
  955: sub get_env_multiple {
  956:     my ($name) = @_;
  957:     my @values;
  958:     if (defined($env{$name})) {
  959:         # exists is it an array
  960:         if (ref($env{$name})) {
  961:             @values=@{ $env{$name} };
  962:         } else {
  963:             $values[0]=$env{$name};
  964:         }
  965:     }
  966:     return(@values);
  967: }
  968: 
  969: # ------------------------------------------------------------------- Locking
  970: 
  971: sub set_lock {
  972:     my ($text)=@_;
  973:     $locknum++;
  974:     my $id=$$.'-'.$locknum;
  975:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  976:              'session.lock.'.$id => $text});
  977:     return $id;
  978: }
  979: 
  980: sub get_locks {
  981:     my $num=0;
  982:     my %texts=();
  983:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  984:        if ($lock=~/\w/) {
  985:           $num++;
  986:           $texts{$lock}=$env{'session.lock.'.$lock};
  987:        }
  988:    }
  989:    return ($num,%texts);
  990: }
  991: 
  992: sub remove_lock {
  993:     my ($id)=@_;
  994:     my $newlocks='';
  995:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  996:        if (($lock=~/\w/) && ($lock ne $id)) {
  997:           $newlocks.=','.$lock;
  998:        }
  999:     }
 1000:     &appenv({'session.locks' => $newlocks});
 1001:     &delenv('session.lock.'.$id);
 1002: }
 1003: 
 1004: sub remove_all_locks {
 1005:     my $activelocks=$env{'session.locks'};
 1006:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
 1007:        if ($lock=~/\w/) {
 1008:           &remove_lock($lock);
 1009:        }
 1010:     }
 1011: }
 1012: 
 1013: 
 1014: # ------------------------------------------ Find out current server userload
 1015: sub userload {
 1016:     my $numusers=0;
 1017:     {
 1018: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
 1019: 	my $filename;
 1020: 	my $curtime=time;
 1021: 	while ($filename=readdir(LONIDS)) {
 1022: 	    next if ($filename eq '.' || $filename eq '..');
 1023: 	    next if ($filename =~ /publicuser_\d+\.id/);
 1024:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
 1025: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
 1026: 	    if ($curtime-$mtime < 1800) { $numusers++; }
 1027: 	}
 1028: 	closedir(LONIDS);
 1029:     }
 1030:     my $userloadpercent=0;
 1031:     my $maxuserload=$perlvar{'lonUserLoadLim'};
 1032:     if ($maxuserload) {
 1033: 	$userloadpercent=100*$numusers/$maxuserload;
 1034:     }
 1035:     $userloadpercent=sprintf("%.2f",$userloadpercent);
 1036:     return $userloadpercent;
 1037: }
 1038: 
 1039: # ------------------------------ Find server with least workload from spare.tab
 1040: 
 1041: sub spareserver {
 1042:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
 1043:     my $spare_server;
 1044:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
 1045:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
 1046:                                                      :  $userloadpercent;
 1047:     my ($uint_dom,$remotesessions);
 1048:     if (($udom ne '') && (&domain($udom) ne '')) {
 1049:         my $uprimary_id = &domain($udom,'primary');
 1050:         $uint_dom = &internet_dom($uprimary_id);
 1051:         my %udomdefaults = &get_domain_defaults($udom);
 1052:         $remotesessions = $udomdefaults{'remotesessions'};
 1053:     }
 1054:     my $spareshash = &this_host_spares($udom);
 1055:     if (ref($spareshash) eq 'HASH') {
 1056:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1057:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1058:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
 1059:                                              $try_server));
 1060: 	        ($spare_server, $lowest_load) =
 1061: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
 1062:             }
 1063:         }
 1064: 
 1065:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1066: 
 1067:         if (!$found_server) {
 1068:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1069: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1070:                     next unless (&spare_can_host($udom,$uint_dom,
 1071:                                                  $remotesessions,$try_server));
 1072: 	            ($spare_server, $lowest_load) =
 1073: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1074:                 }
 1075: 	    }
 1076:         }
 1077:     }
 1078: 
 1079:     if (!$want_server_name) {
 1080:         if (defined($spare_server)) {
 1081:             my $hostname = &hostname($spare_server);
 1082:             if (defined($hostname)) {
 1083:                 my $protocol = 'http';
 1084:                 if ($protocol{$spare_server} eq 'https') {
 1085:                     $protocol = $protocol{$spare_server};
 1086:                 }
 1087:                 my $alias = &use_proxy_alias($r,$spare_server);
 1088:                 $hostname = $alias if ($alias ne '');
 1089: 	        $spare_server = $protocol.'://'.$hostname;
 1090:             }
 1091:         }
 1092:     }
 1093:     return $spare_server;
 1094: }
 1095: 
 1096: sub compare_server_load {
 1097:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1098: 
 1099:     if ($required) {
 1100:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1101:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1102:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1103:         if (($major eq '' && $minor eq '') ||
 1104:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1105:             return ($spare_server,$lowest_load);
 1106:         }
 1107:     }
 1108: 
 1109:     my $loadans     = &reply('load',    $try_server);
 1110:     my $userloadans = &reply('userload',$try_server);
 1111: 
 1112:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1113: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1114:     }
 1115: 
 1116:     my $load;
 1117:     if ($loadans =~ /\d/) {
 1118: 	if ($userloadans =~ /\d/) {
 1119: 	    #both are numbers, pick the bigger one
 1120: 	    $load = ($loadans > $userloadans) ? $loadans 
 1121: 		                              : $userloadans;
 1122: 	} else {
 1123: 	    $load = $loadans;
 1124: 	}
 1125:     } else {
 1126: 	$load = $userloadans;
 1127:     }
 1128: 
 1129:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1130: 	$spare_server = $try_server;
 1131: 	$lowest_load  = $load;
 1132:     }
 1133:     return ($spare_server,$lowest_load);
 1134: }
 1135: 
 1136: # --------------------------- ask offload servers if user already has a session
 1137: sub find_existing_session {
 1138:     my ($udom,$uname) = @_;
 1139:     my $spareshash = &this_host_spares($udom);
 1140:     if (ref($spareshash) eq 'HASH') {
 1141:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1142:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1143:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1144:             }
 1145:         }
 1146:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1147:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1148:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1149:             }
 1150:         }
 1151:     }
 1152:     return;
 1153: }
 1154: 
 1155: sub delusersession {
 1156:     my ($lonid,$udom,$uname) = @_;
 1157:     my $uprimary_id = &domain($udom,'primary');
 1158:     my $uintdom = &internet_dom($uprimary_id);
 1159:     my $intdom = &internet_dom($lonid);
 1160:     my $serverhomedom = &host_domain($lonid);
 1161:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1162:         return &reply(join(':','delusersession',
 1163:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1164:     }
 1165:     return;
 1166: }
 1167: 
 1168: # check if user's browser sent load balancer cookie and server still has session
 1169: # and is not overloaded.
 1170: sub check_for_balancer_cookie {
 1171:     my ($r,$update_mtime) = @_;
 1172:     my ($otherserver,$cookie);
 1173:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1174:     if (exists($cookies{'balanceID'})) {
 1175:         my $balid = $cookies{'balanceID'};
 1176:         $cookie=&LONCAPA::clean_handle($balid->value);
 1177:         my $balancedir=$r->dir_config('lonBalanceDir');
 1178:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1179:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1180:                 my ($possudom,$possuname) = ($1,$2);
 1181:                 my $has_session = 0;
 1182:                 if ((&domain($possudom) ne '') &&
 1183:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1184:                     my $try_server;
 1185:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1186:                     if ($opened) {
 1187:                         flock($idf,LOCK_SH);
 1188:                         while (my $line = <$idf>) {
 1189:                             chomp($line);
 1190:                             if (&hostname($line) ne '') {
 1191:                                 $try_server = $line;
 1192:                                 last;
 1193:                             }
 1194:                         }
 1195:                         close($idf);
 1196:                         if (($try_server) &&
 1197:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1198:                             my $lowest_load = 30000;
 1199:                             ($otherserver,$lowest_load) =
 1200:                                 &compare_server_load($try_server,undef,$lowest_load);
 1201:                             if ($otherserver ne '' && $lowest_load < 100) {
 1202:                                 $has_session = 1;
 1203:                             } else {
 1204:                                 undef($otherserver);
 1205:                             }
 1206:                         }
 1207:                     }
 1208:                 }
 1209:                 if ($has_session) {
 1210:                     if ($update_mtime) {
 1211:                         my $atime = my $mtime = time;
 1212:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1213:                     }
 1214:                 } else {
 1215:                     unlink("$balancedir/$cookie.id");
 1216:                 }
 1217:             }
 1218:         }
 1219:     }
 1220:     return ($otherserver,$cookie);
 1221: }
 1222: 
 1223: sub updatebalcookie {
 1224:     my ($cookie,$balancer,$lastentry)=@_;
 1225:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1226:         my ($udom,$uname) = ($1,$2);
 1227:         my $uprimary_id = &domain($udom,'primary');
 1228:         my $uintdom = &internet_dom($uprimary_id);
 1229:         my $intdom = &internet_dom($balancer);
 1230:         my $serverhomedom = &host_domain($balancer);
 1231:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1232:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1233:         }
 1234:     }
 1235:     return;
 1236: }
 1237: 
 1238: sub delbalcookie {
 1239:     my ($cookie,$balancer) =@_;
 1240:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1241:         my ($udom,$uname) = ($1,$2);
 1242:         my $uprimary_id = &domain($udom,'primary');
 1243:         my $uintdom = &internet_dom($uprimary_id);
 1244:         my $intdom = &internet_dom($balancer);
 1245:         my $serverhomedom = &host_domain($balancer);
 1246:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1247:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1248:         }
 1249:     }
 1250: }
 1251: 
 1252: # -------------------------------- ask if server already has a session for user
 1253: sub has_user_session {
 1254:     my ($lonid,$udom,$uname) = @_;
 1255:     my $result = &reply(join(':','userhassession',
 1256: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1257:     return 1 if ($result eq 'ok');
 1258: 
 1259:     return 0;
 1260: }
 1261: 
 1262: # --------- determine least loaded server in a user's domain which allows login
 1263: 
 1264: sub choose_server {
 1265:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1266:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1267:     my %servers = &get_servers($udom);
 1268:     my $lowest_load = 30000;
 1269:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1270:     if ($skiploadbal) {
 1271:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1272:         unless (defined($cached)) {
 1273:             my $cachetime = 60*60*24;
 1274:             my %domconfig =
 1275:                 &get_dom('configuration',['loadbalancing'],$udom);
 1276:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1277:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1278:                                            $cachetime);
 1279:             }
 1280:         }
 1281:     }
 1282:     foreach my $lonhost (keys(%servers)) {
 1283:         if ($skiploadbal) {
 1284:             if (ref($balancers) eq 'HASH') {
 1285:                 next if (exists($balancers->{$lonhost}));
 1286:             }
 1287:         }
 1288:         my $loginvia;
 1289:         if ($checkloginvia) {
 1290:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1291:             if ($loginvia) {
 1292:                 my ($server,$path) = split(/:/,$loginvia);
 1293:                 ($login_host, $lowest_load) =
 1294:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1295:                 if ($login_host eq $server) {
 1296:                     $portal_path = $path;
 1297:                     $isredirect = 1;
 1298:                 }
 1299:             } else {
 1300:                 ($login_host, $lowest_load) =
 1301:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1302:                 if ($login_host eq $lonhost) {
 1303:                     $portal_path = '';
 1304:                     $isredirect = ''; 
 1305:                 }
 1306:             }
 1307:         } else {
 1308:             ($login_host, $lowest_load) =
 1309:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1310:         }
 1311:     }
 1312:     if ($login_host ne '') {
 1313:         $hostname = &hostname($login_host);
 1314:     }
 1315:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1316: }
 1317: 
 1318: sub get_course_sessions {
 1319:     my ($cnum,$cdom,$lastactivity) = @_;
 1320:     my %servers = &internet_dom_servers($cdom);
 1321:     my %returnhash;
 1322:     foreach my $server (sort(keys(%servers))) {
 1323:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1324:         my @pairs=split(/\&/,$rep);
 1325:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1326:             foreach my $item (@pairs) {
 1327:                 my ($key,$value)=split(/=/,$item,2);
 1328:                 $key = &unescape($key);
 1329:                 next if ($key =~ /^error: 2 /);
 1330:                 if (exists($returnhash{$key})) {
 1331:                     next if ($value < $returnhash{$key});
 1332:                 }
 1333:                 $returnhash{$key}=$value;
 1334:             }
 1335:         }
 1336:     }
 1337:     return %returnhash;
 1338: }
 1339: 
 1340: # --------------------------------------------- Try to change a user's password
 1341: 
 1342: sub changepass {
 1343:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1344:     $currentpass = &escape($currentpass);
 1345:     $newpass     = &escape($newpass);
 1346:     my $lonhost = $perlvar{'lonHostID'};
 1347:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1348: 		       $server);
 1349:     if (! $answer) {
 1350: 	&logthis("No reply on password change request to $server ".
 1351: 		 "by $uname in domain $udom.");
 1352:     } elsif ($answer =~ "^ok") {
 1353:         &logthis("$uname in $udom successfully changed their password ".
 1354: 		 "on $server.");
 1355:     } elsif ($answer =~ "^pwchange_failure") {
 1356: 	&logthis("$uname in $udom was unable to change their password ".
 1357: 		 "on $server.  The action was blocked by either lcpasswd ".
 1358: 		 "or pwchange");
 1359:     } elsif ($answer =~ "^non_authorized") {
 1360:         &logthis("$uname in $udom did not get their password correct when ".
 1361: 		 "attempting to change it on $server.");
 1362:     } elsif ($answer =~ "^auth_mode_error") {
 1363:         &logthis("$uname in $udom attempted to change their password despite ".
 1364: 		 "not being locally or internally authenticated on $server.");
 1365:     } elsif ($answer =~ "^unknown_user") {
 1366:         &logthis("$uname in $udom attempted to change their password ".
 1367: 		 "on $server but were unable to because $server is not ".
 1368: 		 "their home server.");
 1369:     } elsif ($answer =~ "^refused") {
 1370: 	&logthis("$server refused to change $uname in $udom password because ".
 1371: 		 "it was sent an unencrypted request to change the password.");
 1372:     } elsif ($answer =~ "invalid_client") {
 1373:         &logthis("$server refused to change $uname in $udom password because ".
 1374:                  "it was a reset by e-mail originating from an invalid server.");
 1375:     } elsif ($answer =~ "^prioruse") {
 1376:        &logthis("$server refused to change $uname in $udom password because ".
 1377:                 "the password had been used before");
 1378:     }
 1379:     return $answer;
 1380: }
 1381: 
 1382: # ----------------------- Try to determine user's current authentication scheme
 1383: 
 1384: sub queryauthenticate {
 1385:     my ($uname,$udom)=@_;
 1386:     my $uhome=&homeserver($uname,$udom);
 1387:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1388: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1389: 	return 'no_host';
 1390:     }
 1391:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1392:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1393: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1394:     }
 1395:     return $answer;
 1396: }
 1397: 
 1398: # --------- Try to authenticate user from domain's lib servers (first this one)
 1399: 
 1400: sub authenticate {
 1401:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1402:     $upass=&escape($upass);
 1403:     $uname= &LONCAPA::clean_username($uname);
 1404:     my $uhome=&homeserver($uname,$udom,1);
 1405:     my $newhome;
 1406:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1407: # Maybe the machine was offline and only re-appeared again recently?
 1408:         &reconlonc();
 1409: # One more
 1410: 	$uhome=&homeserver($uname,$udom,1);
 1411:         if (($uhome eq 'no_host') && $checkdefauth) {
 1412:             if (defined(&domain($udom,'primary'))) {
 1413:                 $newhome=&domain($udom,'primary');
 1414:             }
 1415:             if ($newhome ne '') {
 1416:                 $uhome = $newhome;
 1417:             }
 1418:         }
 1419: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1420: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1421: 	    return 'no_host';
 1422:         }
 1423:     }
 1424:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1425:     if ($answer eq 'authorized') {
 1426:         if ($newhome) {
 1427:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1428:             return 'no_account_on_host'; 
 1429:         } else {
 1430:             &logthis("User $uname at $udom authorized by $uhome");
 1431:             return $uhome;
 1432:         }
 1433:     }
 1434:     if ($answer eq 'non_authorized') {
 1435: 	&logthis("User $uname at $udom rejected by $uhome");
 1436: 	return 'no_host';
 1437:     }
 1438:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1439:     return 'no_host';
 1440: }
 1441: 
 1442: sub can_switchserver {
 1443:     my ($udom,$home) = @_;
 1444:     my ($canswitch,@intdoms);
 1445:     my $internet_names = &get_internet_names($home);
 1446:     if (ref($internet_names) eq 'ARRAY') {
 1447:         @intdoms = @{$internet_names};
 1448:     }
 1449:     my $uint_dom = &internet_dom(&domain($udom,'primary'));
 1450:     if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1451:         $canswitch = 1;
 1452:     } else {
 1453:          my $serverhomeID = &get_server_homeID(&hostname($home));
 1454:          my $serverhomedom = &host_domain($serverhomeID);
 1455:          my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1456:          my %udomdefaults = &get_domain_defaults($udom);
 1457:          my $remoterev = &get_server_loncaparev('',$home);
 1458:          $canswitch = &can_host_session($udom,$home,$remoterev,
 1459:                                         $udomdefaults{'remotesessions'},
 1460:                                         $defdomdefaults{'hostedsessions'});
 1461:     }
 1462:     return $canswitch;
 1463: }
 1464: 
 1465: sub can_host_session {
 1466:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1467:     my $canhost = 1;
 1468:     my $host_idn = &internet_dom($lonhost);
 1469:     if (ref($remotesessions) eq 'HASH') {
 1470:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1471:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1472:                 $canhost = 0;
 1473:             } else {
 1474:                 $canhost = 1;
 1475:             }
 1476:         }
 1477:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1478:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1479:                 $canhost = 1;
 1480:             } else {
 1481:                 $canhost = 0;
 1482:             }
 1483:         }
 1484:         if ($canhost) {
 1485:             if ($remotesessions->{'version'} ne '') {
 1486:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1487:                 if ($reqmajor ne '' && $reqminor ne '') {
 1488:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1489:                         my $major = $1;
 1490:                         my $minor = $2;
 1491:                         if (($major < $reqmajor ) ||
 1492:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1493:                             $canhost = 0;
 1494:                         }
 1495:                     } else {
 1496:                         $canhost = 0;
 1497:                     }
 1498:                 }
 1499:             }
 1500:         }
 1501:     }
 1502:     if ($canhost) {
 1503:         if (ref($hostedsessions) eq 'HASH') {
 1504:             my $uprimary_id = &domain($udom,'primary');
 1505:             my $uint_dom = &internet_dom($uprimary_id);
 1506:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1507:                 if (($uint_dom ne '') && 
 1508:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1509:                     $canhost = 0;
 1510:                 } else {
 1511:                     $canhost = 1;
 1512:                 }
 1513:             }
 1514:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1515:                 if (($uint_dom ne '') && 
 1516:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1517:                     $canhost = 1;
 1518:                 } else {
 1519:                     $canhost = 0;
 1520:                 }
 1521:             }
 1522:         }
 1523:     }
 1524:     return $canhost;
 1525: }
 1526: 
 1527: sub spare_can_host {
 1528:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1529:     my $canhost=1;
 1530:     my $try_server_hostname = &hostname($try_server);
 1531:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1532:     my $serverhomedom = &host_domain($serverhomeID);
 1533:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1534:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1535:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1536:             $canhost = 0;
 1537:         }
 1538:     }
 1539:     if ($canhost) {
 1540:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1541:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1542:                 unless (&shared_institution($udom,$try_server)) {
 1543:                     $canhost = 0;
 1544:                 }
 1545:             }
 1546:         }
 1547:     }
 1548:     if (($canhost) && ($uint_dom)) {
 1549:         my @intdoms;
 1550:         my $internet_names = &get_internet_names($try_server);
 1551:         if (ref($internet_names) eq 'ARRAY') {
 1552:             @intdoms = @{$internet_names};
 1553:         }
 1554:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1555:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1556:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1557:                                          $remotesessions,
 1558:                                          $defdomdefaults{'hostedsessions'});
 1559:         }
 1560:     }
 1561:     return $canhost;
 1562: }
 1563: 
 1564: sub this_host_spares {
 1565:     my ($dom) = @_;
 1566:     my ($dom_in_use,$lonhost_in_use,$result);
 1567:     my @hosts = &current_machine_ids();
 1568:     foreach my $lonhost (@hosts) {
 1569:         if (&host_domain($lonhost) eq $dom) {
 1570:             $dom_in_use = $dom;
 1571:             $lonhost_in_use = $lonhost;
 1572:             last;
 1573:         }
 1574:     }
 1575:     if ($dom_in_use ne '') {
 1576:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1577:     }
 1578:     if (ref($result) ne 'HASH') {
 1579:         $lonhost_in_use = $perlvar{'lonHostID'};
 1580:         $dom_in_use = &host_domain($lonhost_in_use);
 1581:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1582:         if (ref($result) ne 'HASH') {
 1583:             $result = \%spareid;
 1584:         }
 1585:     }
 1586:     return $result;
 1587: }
 1588: 
 1589: sub spares_for_offload  {
 1590:     my ($dom_in_use,$lonhost_in_use) = @_;
 1591:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1592:     if (defined($cached)) {
 1593:         return $result;
 1594:     } else {
 1595:         my $cachetime = 60*60*24;
 1596:         my %domconfig =
 1597:             &get_dom('configuration',['usersessions'],$dom_in_use);
 1598:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1599:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1600:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1601:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1602:                 }
 1603:             }
 1604:         }
 1605:     }
 1606:     return;
 1607: }
 1608: 
 1609: sub get_lonbalancer_config {
 1610:     my ($servers) = @_;
 1611:     my ($currbalancer,$currtargets);
 1612:     if (ref($servers) eq 'HASH') {
 1613:         foreach my $server (keys(%{$servers})) {
 1614:             my %what = (
 1615:                          spareid => 1,
 1616:                          perlvar => 1,
 1617:                        );
 1618:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1619:             if ($result eq 'ok') {
 1620:                 if (ref($returnhash) eq 'HASH') {
 1621:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1622:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1623:                             $currbalancer = $server;
 1624:                             $currtargets = {};
 1625:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1626:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1627:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1628:                                 }
 1629:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1630:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1631:                                 }
 1632:                             }
 1633:                             last;
 1634:                         }
 1635:                     }
 1636:                 }
 1637:             }
 1638:         }
 1639:     }
 1640:     return ($currbalancer,$currtargets);
 1641: }
 1642: 
 1643: sub check_loadbalancing {
 1644:     my ($uname,$udom,$caller) = @_;
 1645:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1646:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1647:     my $lonhost = $perlvar{'lonHostID'};
 1648:     my @hosts = &current_machine_ids();
 1649:     my $uprimary_id = &domain($udom,'primary');
 1650:     my $uintdom = &internet_dom($uprimary_id);
 1651:     my $intdom = &internet_dom($lonhost);
 1652:     my $serverhomedom = &host_domain($lonhost);
 1653:     my $domneedscache;
 1654:     my $cachetime = 60*60*24;
 1655: 
 1656:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1657:         $dom_in_use = $udom;
 1658:         $homeintdom = 1;
 1659:     } else {
 1660:         $dom_in_use = $serverhomedom;
 1661:     }
 1662:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1663:     unless (defined($cached)) {
 1664:         my %domconfig =
 1665:             &get_dom('configuration',['loadbalancing'],$dom_in_use);
 1666:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1667:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1668:         } else {
 1669:             $domneedscache = $dom_in_use;
 1670:         }
 1671:     }
 1672:     if (ref($result) eq 'HASH') {
 1673:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1674:             &check_balancer_result($result,@hosts);
 1675:         if ($is_balancer) {
 1676:             if (ref($currrules) eq 'HASH') {
 1677:                 if ($homeintdom) {
 1678:                     if ($uname ne '') {
 1679:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1680:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1681:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1682:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1683:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1684:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1685:                             }
 1686:                         }
 1687:                         if ($rule_in_effect eq '') {
 1688:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1689:                             if ($userenv{'inststatus'} ne '') {
 1690:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1691:                                 my ($othertitle,$usertypes,$types) =
 1692:                                     &Apache::loncommon::sorted_inst_types($udom);
 1693:                                 if (ref($types) eq 'ARRAY') {
 1694:                                     foreach my $type (@{$types}) {
 1695:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1696:                                             if (exists($currrules->{$type})) {
 1697:                                                 $rule_in_effect = $currrules->{$type};
 1698:                                             }
 1699:                                         }
 1700:                                     }
 1701:                                 }
 1702:                             } else {
 1703:                                 if (exists($currrules->{'default'})) {
 1704:                                     $rule_in_effect = $currrules->{'default'};
 1705:                                 }
 1706:                             }
 1707:                         }
 1708:                     } else {
 1709:                         if (exists($currrules->{'default'})) {
 1710:                             $rule_in_effect = $currrules->{'default'};
 1711:                         }
 1712:                     }
 1713:                 } else {
 1714:                     if ($currrules->{'_LC_external'} ne '') {
 1715:                         $rule_in_effect = $currrules->{'_LC_external'};
 1716:                     }
 1717:                 }
 1718:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1719:                                                        $uname,$udom);
 1720:             }
 1721:         }
 1722:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1723:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1724:         unless (defined($cached)) {
 1725:             my %domconfig =
 1726:                 &get_dom('configuration',['loadbalancing'],$serverhomedom);
 1727:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1728:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1729:             } else {
 1730:                 $domneedscache = $serverhomedom;
 1731:             }
 1732:         }
 1733:         if (ref($result) eq 'HASH') {
 1734:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1735:                 &check_balancer_result($result,@hosts);
 1736:             if ($is_balancer) {
 1737:                 if (ref($currrules) eq 'HASH') {
 1738:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1739:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1740:                     }
 1741:                 }
 1742:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1743:                                                        $uname,$udom);
 1744:             }
 1745:         } else {
 1746:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1747:                 $is_balancer = 1;
 1748:                 $offloadto = &this_host_spares($dom_in_use);
 1749:             }
 1750:             unless (defined($cached)) {
 1751:                 $domneedscache = $serverhomedom;
 1752:             }
 1753:         }
 1754:     } else {
 1755:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1756:             $is_balancer = 1;
 1757:             $offloadto = &this_host_spares($dom_in_use);
 1758:         }
 1759:         unless (defined($cached)) {
 1760:             $domneedscache = $serverhomedom;
 1761:         }
 1762:     }
 1763:     if ($domneedscache) {
 1764:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1765:     }
 1766:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1767:         my $lowest_load = 30000;
 1768:         if (ref($offloadto) eq 'HASH') {
 1769:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1770:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1771:                     ($otherserver,$lowest_load) =
 1772:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1773:                 }
 1774:             }
 1775:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1776: 
 1777:             if (!$found_server) {
 1778:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1779:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1780:                         ($otherserver,$lowest_load) =
 1781:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1782:                     }
 1783:                 }
 1784:             }
 1785:         } elsif (ref($offloadto) eq 'ARRAY') {
 1786:             if (@{$offloadto} == 1) {
 1787:                 $otherserver = $offloadto->[0];
 1788:             } elsif (@{$offloadto} > 1) {
 1789:                 foreach my $try_server (@{$offloadto}) {
 1790:                     ($otherserver,$lowest_load) =
 1791:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1792:                 }
 1793:             }
 1794:         }
 1795:         unless ($caller eq 'login') {
 1796:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1797:                 $is_balancer = 0;
 1798:                 if ($uname ne '' && $udom ne '') {
 1799:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1800:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1801:                                  'user.loadbalcheck.time' => time});
 1802:                     }
 1803:                 }
 1804:             }
 1805:         }
 1806:     }
 1807:     if (($is_balancer) && (!$homeintdom)) {
 1808:         undef($setcookie);
 1809:     }
 1810:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1811: }
 1812: 
 1813: sub check_balancer_result {
 1814:     my ($result,@hosts) = @_;
 1815:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1816:     if (ref($result) eq 'HASH') {
 1817:         if ($result->{'lonhost'} ne '') {
 1818:             my $currbalancer = $result->{'lonhost'};
 1819:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1820:                 $is_balancer = 1;
 1821:                 $currtargets = $result->{'targets'};
 1822:                 $currrules = $result->{'rules'};
 1823:             }
 1824:             $dom_balancers = $currbalancer;
 1825:         } else {
 1826:             if (keys(%{$result})) {
 1827:                 foreach my $key (keys(%{$result})) {
 1828:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1829:                         (ref($result->{$key}) eq 'HASH')) {
 1830:                         $is_balancer = 1;
 1831:                         $currrules = $result->{$key}{'rules'};
 1832:                         $currtargets = $result->{$key}{'targets'};
 1833:                         $setcookie = $result->{$key}{'cookie'};
 1834:                         last;
 1835:                     }
 1836:                 }
 1837:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1838:             }
 1839:         }
 1840:     }
 1841:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1842: }
 1843: 
 1844: sub get_loadbalancer_targets {
 1845:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1846:     my $offloadto;
 1847:     if ($rule_in_effect eq 'none') {
 1848:         return [$perlvar{'lonHostID'}];
 1849:     } elsif ($rule_in_effect eq '') {
 1850:         $offloadto = $currtargets;
 1851:     } else {
 1852:         if ($rule_in_effect eq 'homeserver') {
 1853:             my $homeserver = &homeserver($uname,$udom);
 1854:             if ($homeserver ne 'no_host') {
 1855:                 $offloadto = [$homeserver];
 1856:             }
 1857:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1858:             my %domconfig =
 1859:                 &get_dom('configuration',['loadbalancing'],$udom);
 1860:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1861:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1862:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1863:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1864:                     }
 1865:                 }
 1866:             } else {
 1867:                 my %servers = &internet_dom_servers($udom);
 1868:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1869:                 if (&hostname($remotebalancer) ne '') {
 1870:                     $offloadto = [$remotebalancer];
 1871:                 }
 1872:             }
 1873:         } elsif (&hostname($rule_in_effect) ne '') {
 1874:             $offloadto = [$rule_in_effect];
 1875:         }
 1876:     }
 1877:     return $offloadto;
 1878: }
 1879: 
 1880: sub internet_dom_servers {
 1881:     my ($dom) = @_;
 1882:     my (%uniqservers,%servers);
 1883:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1884:     my @machinedoms = &machine_domains($primaryserver);
 1885:     foreach my $mdom (@machinedoms) {
 1886:         my %currservers = %servers;
 1887:         my %server = &get_servers($mdom);
 1888:         %servers = (%currservers,%server);
 1889:     }
 1890:     my %by_hostname;
 1891:     foreach my $id (keys(%servers)) {
 1892:         push(@{$by_hostname{$servers{$id}}},$id);
 1893:     }
 1894:     foreach my $hostname (sort(keys(%by_hostname))) {
 1895:         if (@{$by_hostname{$hostname}} > 1) {
 1896:             my $match = 0;
 1897:             foreach my $id (@{$by_hostname{$hostname}}) {
 1898:                 if (&host_domain($id) eq $dom) {
 1899:                     $uniqservers{$id} = $hostname;
 1900:                     $match = 1;
 1901:                 }
 1902:             }
 1903:             unless ($match) {
 1904:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1905:             }
 1906:         } else {
 1907:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1908:         }
 1909:     }
 1910:     return %uniqservers;
 1911: }
 1912: 
 1913: sub trusted_domains {
 1914:     my ($cmdtype,$calldom) = @_;
 1915:     my ($trusted,$untrusted);
 1916:     if (&domain($calldom) eq '') {
 1917:         return ($trusted,$untrusted);
 1918:     }
 1919:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1920:         return ($trusted,$untrusted);
 1921:     }
 1922:     my $callprimary = &domain($calldom,'primary');
 1923:     my $intcalldom = &internet_dom($callprimary);
 1924:     if ($intcalldom eq '') {
 1925:         return ($trusted,$untrusted);
 1926:     }
 1927: 
 1928:     my ($trustconfig,$cached)=&is_cached_new('trust',$calldom);
 1929:     unless (defined($cached)) {
 1930:         my %domconfig = &get_dom('configuration',['trust'],$calldom);
 1931:         &do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1932:         $trustconfig = $domconfig{'trust'};
 1933:     }
 1934:     if (ref($trustconfig)) {
 1935:         my (%possexc,%possinc,@allexc,@allinc); 
 1936:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1937:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1938:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1939:             }
 1940:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1941:                 $possinc{$intcalldom} = 1;
 1942:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1943:             }
 1944:         }
 1945:         if (keys(%possexc)) {
 1946:             if (keys(%possinc)) {
 1947:                 foreach my $key (sort(keys(%possexc))) {
 1948:                     next if ($key eq $intcalldom);
 1949:                     unless ($possinc{$key}) {
 1950:                         push(@allexc,$key);
 1951:                     }
 1952:                 }
 1953:             } else {
 1954:                 @allexc = sort(keys(%possexc));
 1955:             }
 1956:         }
 1957:         if (keys(%possinc)) {
 1958:             $possinc{$intcalldom} = 1;
 1959:             @allinc = sort(keys(%possinc));
 1960:         }
 1961:         if ((@allexc > 0) || (@allinc > 0)) {
 1962:             my %doms_by_intdom;
 1963:             my %allintdoms = &all_host_intdom();
 1964:             my %alldoms = &all_host_domain();
 1965:             foreach my $key (%allintdoms) {
 1966:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1967:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1968:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1969:                     }
 1970:                 } else {
 1971:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1972:                 }
 1973:             }
 1974:             foreach my $exc (@allexc) {
 1975:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1976:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1977:                 }
 1978:             }
 1979:             foreach my $inc (@allinc) {
 1980:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1981:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1982:                 }
 1983:             }
 1984:         }
 1985:     }
 1986:     return ($trusted,$untrusted);
 1987: }
 1988: 
 1989: sub will_trust {
 1990:     my ($cmdtype,$domain,$possdom) = @_;
 1991:     return 1 if ($domain eq $possdom);
 1992:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1993:     my $willtrust; 
 1994:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1995:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1996:             $willtrust = 1;
 1997:         }
 1998:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1999:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 2000:             $willtrust = 1;
 2001:         }
 2002:     } else {
 2003:         $willtrust = 1;
 2004:     }
 2005:     return $willtrust;
 2006: }
 2007: 
 2008: # ---------------------- Find the homebase for a user from domain's lib servers
 2009: 
 2010: my %homecache;
 2011: sub homeserver {
 2012:     my ($uname,$udom,$ignoreBadCache)=@_;
 2013:     my $index="$uname:$udom";
 2014: 
 2015:     if (exists($homecache{$index})) { return $homecache{$index}; }
 2016: 
 2017:     my %servers = &get_servers($udom,'library');
 2018:     foreach my $tryserver (keys(%servers)) {
 2019:         next if ($ignoreBadCache ne 'true' && 
 2020: 		 exists($badServerCache{$tryserver}));
 2021: 
 2022: 	my $answer=reply("home:$udom:$uname",$tryserver);
 2023: 	if ($answer eq 'found') {
 2024: 	    delete($badServerCache{$tryserver}); 
 2025: 	    return $homecache{$index}=$tryserver;
 2026: 	} elsif ($answer eq 'no_host') {
 2027: 	    $badServerCache{$tryserver}=1;
 2028: 	}
 2029:     }    
 2030:     return 'no_host';
 2031: }
 2032: 
 2033: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 2034: 
 2035: sub idget {
 2036:     my ($udom,$idsref,$namespace)=@_;
 2037:     my %returnhash=();
 2038:     my @ids=(); 
 2039:     if (ref($idsref) eq 'ARRAY') {
 2040:         @ids = @{$idsref};
 2041:     } else {
 2042:         return %returnhash; 
 2043:     }
 2044:     if ($namespace eq '') {
 2045:         $namespace = 'ids';
 2046:     }
 2047:     
 2048:     my %servers = &get_servers($udom,'library');
 2049:     foreach my $tryserver (keys(%servers)) {
 2050: 	my $idlist=join('&', map { &escape($_); } @ids);
 2051: 	if ($namespace eq 'ids') {
 2052: 	    $idlist=~tr/A-Z/a-z/;
 2053: 	}
 2054: 	my $reply;
 2055: 	if ($namespace eq 'ids') {
 2056: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 2057: 	} else {
 2058: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 2059: 	}
 2060: 	my @answer=();
 2061: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 2062: 	    @answer=split(/\&/,$reply);
 2063: 	}                    ;
 2064: 	my $i;
 2065: 	for ($i=0;$i<=$#ids;$i++) {
 2066: 	    if ($answer[$i]) {
 2067: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 2068: 	    }
 2069: 	}
 2070:     }
 2071:     return %returnhash;
 2072: }
 2073: 
 2074: # ------------------------------------- Find the IDs behind a list of usernames
 2075: 
 2076: sub idrget {
 2077:     my ($udom,@unames)=@_;
 2078:     my %returnhash=();
 2079:     foreach my $uname (@unames) {
 2080:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 2081:     }
 2082:     return %returnhash;
 2083: }
 2084: 
 2085: # Store away a list of names and associated student/employee IDs or clicker IDs
 2086: 
 2087: sub idput {
 2088:     my ($udom,$idsref,$uhom,$namespace)=@_;
 2089:     my %servers=();
 2090:     my %ids=();
 2091:     my %byid = ();
 2092:     if (ref($idsref) eq 'HASH') {
 2093:         %ids=%{$idsref};
 2094:     }
 2095:     if ($namespace eq '') {
 2096:         $namespace = 'ids'; 
 2097:     }
 2098:     foreach my $uname (keys(%ids)) {
 2099: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2100:         if ($uhom eq '') {
 2101:             $uhom=&homeserver($uname,$udom);
 2102:         }
 2103:         if ($uhom ne 'no_host') {
 2104:             my $esc_unam=&escape($uname);
 2105:             if ($namespace eq 'ids') {
 2106:                 my $id=&escape($ids{$uname});
 2107:                 $id=~tr/A-Z/a-z/;
 2108:                 my $esc_unam=&escape($uname);
 2109:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2110:             } else {
 2111:                 my @currids = split(/,/,$ids{$uname});
 2112:                 foreach my $id (@currids) {
 2113:                     $byid{$uhom}{$id} .= $uname.',';
 2114:                 }
 2115:             }
 2116:         }
 2117:     }
 2118:     if ($namespace eq 'clickers') {
 2119:         foreach my $server (keys(%byid)) {
 2120:             if (ref($byid{$server}) eq 'HASH') {
 2121:                 foreach my $id (keys(%{$byid{$server}})) {
 2122:                     $byid{$server} =~ s/,$//;
 2123:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2124:                 }
 2125:             }
 2126:         }
 2127:     }
 2128:     foreach my $server (keys(%servers)) {
 2129:         $servers{$server} =~ s/\&$//;
 2130:         if ($namespace eq 'ids') {     
 2131:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2132:         } else {
 2133:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2134:         }
 2135:     }
 2136: }
 2137: 
 2138: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2139: 
 2140: sub iddel {
 2141:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2142:     my %result=();
 2143:     my %ids=();
 2144:     my %byid = ();
 2145:     if (ref($idshashref) eq 'HASH') {
 2146:         %ids=%{$idshashref};
 2147:     } else {
 2148:         return %result;
 2149:     }
 2150:     if ($namespace eq '') {
 2151:         $namespace = 'ids';
 2152:     }
 2153:     my %servers=();
 2154:     while (my ($id,$unamestr) = each(%ids)) {
 2155:         if ($namespace eq 'ids') {
 2156:             my $uhom = $uhome;
 2157:             if ($uhom eq '') { 
 2158:                 $uhom=&homeserver($unamestr,$udom);
 2159:             }
 2160:             if ($uhom ne 'no_host') {
 2161:                 $servers{$uhom}.='&'.&escape($id);
 2162:             }
 2163:          } else {
 2164:             my @curritems = split(/,/,$ids{$id});
 2165:             foreach my $uname (@curritems) {
 2166:                 my $uhom = $uhome;
 2167:                 if ($uhom eq '') {
 2168:                     $uhom=&homeserver($uname,$udom);
 2169:                 }
 2170:                 if ($uhom ne 'no_host') { 
 2171:                     $byid{$uhom}{$id} .= $uname.',';
 2172:                 }
 2173:             }
 2174:         }
 2175:     }
 2176:     if ($namespace eq 'clickers') {
 2177:         foreach my $server (keys(%byid)) {
 2178:             if (ref($byid{$server}) eq 'HASH') {
 2179:                 foreach my $id (keys(%{$byid{$server}})) {
 2180:                     $byid{$server}{$id} =~ s/,$//;
 2181:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2182:                 }
 2183:             }
 2184:         }
 2185:     }
 2186:     foreach my $server (keys(%servers)) {
 2187:         $servers{$server} =~ s/\&$//;
 2188:         if ($namespace eq 'ids') {
 2189:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2190:         } elsif ($namespace eq 'clickers') {
 2191:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2192:         }
 2193:     }
 2194:     return %result;
 2195: }
 2196: 
 2197: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2198: 
 2199: sub updateclickers {
 2200:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2201:     my %clickers;
 2202:     if (ref($idshashref) eq 'HASH') {
 2203:         %clickers=%{$idshashref};
 2204:     } else {
 2205:         return;
 2206:     }
 2207:     my $items='';
 2208:     foreach my $item (keys(%clickers)) {
 2209:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2210:     }
 2211:     $items=~s/\&$//;
 2212:     my $request = "updateclickers:$udom:$action:$items";
 2213:     if ($critical) {
 2214:         return &critical($request,$uhome);
 2215:     } else {
 2216:         return &reply($request,$uhome);
 2217:     }
 2218: }
 2219: 
 2220: # ------------------------------dump from db file owned by domainconfig user
 2221: sub dump_dom {
 2222:     my ($namespace, $udom, $regexp) = @_;
 2223: 
 2224:     $udom ||= $env{'user.domain'};
 2225: 
 2226:     return () unless $udom;
 2227: 
 2228:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2229: }
 2230: 
 2231: # ------------------------------------------ get items from domain db files   
 2232: 
 2233: sub get_dom {
 2234:     my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
 2235:     return if ($udom eq 'public');
 2236:     my $items='';
 2237:     foreach my $item (@$storearr) {
 2238:         $items.=&escape($item).'&';
 2239:     }
 2240:     $items=~s/\&$//;
 2241:     if (!$udom) {
 2242:         $udom=$env{'user.domain'};
 2243:         return if ($udom eq 'public');
 2244:         if (defined(&domain($udom,'primary'))) {
 2245:             $uhome=&domain($udom,'primary');
 2246:         } else {
 2247:             undef($uhome);
 2248:         }
 2249:     } else {
 2250:         if (!$uhome) {
 2251:             if (defined(&domain($udom,'primary'))) {
 2252:                 $uhome=&domain($udom,'primary');
 2253:             }
 2254:         }
 2255:     }
 2256:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2257:         my $rep;
 2258:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2259:             # domain information is hosted on this machine
 2260:             $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
 2261:         } else {
 2262:             if ($encrypt) {
 2263:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2264:             } else {
 2265:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2266:             }
 2267:         }
 2268:         my %returnhash;
 2269:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2270:             return %returnhash;
 2271:         }
 2272:         my @pairs=split(/\&/,$rep);
 2273:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2274:             return @pairs;
 2275:         }
 2276:         my $i=0;
 2277:         foreach my $item (@$storearr) {
 2278:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2279:             $i++;
 2280:         }
 2281:         return %returnhash;
 2282:     } else {
 2283:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2284:     }
 2285: }
 2286: 
 2287: # -------------------------------------------- put items in domain db files 
 2288: 
 2289: sub put_dom {
 2290:     my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
 2291:     if (!$udom) {
 2292:         $udom=$env{'user.domain'};
 2293:         if (defined(&domain($udom,'primary'))) {
 2294:             $uhome=&domain($udom,'primary');
 2295:         } else {
 2296:             undef($uhome);
 2297:         }
 2298:     } else {
 2299:         if (!$uhome) {
 2300:             if (defined(&domain($udom,'primary'))) {
 2301:                 $uhome=&domain($udom,'primary');
 2302:             }
 2303:         }
 2304:     } 
 2305:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2306:         my $items='';
 2307:         foreach my $item (keys(%$storehash)) {
 2308:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2309:         }
 2310:         $items=~s/\&$//;
 2311:         if ($encrypt) {
 2312:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2313:         } else {
 2314:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2315:         }
 2316:     } else {
 2317:         &logthis("put_dom failed - no homeserver and/or domain");
 2318:     }
 2319: }
 2320: 
 2321: # --------------------- newput for items in db file owned by domainconfig user
 2322: sub newput_dom {
 2323:     my ($namespace,$storehash,$udom) = @_;
 2324:     my $result;
 2325:     if (!$udom) {
 2326:         $udom=$env{'user.domain'};
 2327:     }
 2328:     if ($udom) {
 2329:         my $uname = &get_domainconfiguser($udom);
 2330:         $result = &newput($namespace,$storehash,$udom,$uname);
 2331:     }
 2332:     return $result;
 2333: }
 2334: 
 2335: # --------------------- delete for items in db file owned by domainconfig user
 2336: sub del_dom {
 2337:     my ($namespace,$storearr,$udom)=@_;
 2338:     if (ref($storearr) eq 'ARRAY') {
 2339:         if (!$udom) {
 2340:             $udom=$env{'user.domain'};
 2341:         }
 2342:         if ($udom) {
 2343:             my $uname = &get_domainconfiguser($udom); 
 2344:             return &del($namespace,$storearr,$udom,$uname);
 2345:         }
 2346:     }
 2347: }
 2348: 
 2349: sub store_dom {
 2350:     my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
 2351:     $$storehash{'ip'}=&get_requestor_ip();
 2352:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2353:     my $namevalue='';
 2354:     foreach my $key (keys(%{$storehash})) {
 2355:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 2356:     }
 2357:     $namevalue=~s/\&$//;
 2358:     if (grep { $_ eq $home } current_machine_ids()) {
 2359:         return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
 2360:     } else {
 2361:         if ($namespace eq 'private') {
 2362:             return 'refused';
 2363:         } elsif ($encrypt) {
 2364:             return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
 2365:         } else {
 2366:             return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
 2367:         }
 2368:     }
 2369: }
 2370: 
 2371: sub restore_dom {
 2372:     my ($id,$namespace,$dom,$home,$encrypt) = @_;
 2373:     my $answer;
 2374:     if (grep { $_ eq $home } current_machine_ids()) {
 2375:         $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
 2376:     } elsif ($namespace ne 'private') {
 2377:         if ($encrypt) {
 2378:             $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
 2379:         } else {
 2380:             $answer=&reply("restoredom:$dom:$namespace:$id",$home);
 2381:         }
 2382:     }
 2383:     my %returnhash=();
 2384:     unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') || 
 2385:             ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
 2386:         foreach my $line (split(/\&/,$answer)) {
 2387:             my ($name,$value)=split(/\=/,$line);
 2388:             $returnhash{&unescape($name)}=&thaw_unescape($value);
 2389:         }
 2390:         my $version;
 2391:         for ($version=1;$version<=$returnhash{'version'};$version++) {
 2392:             foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 2393:                 $returnhash{$item}=$returnhash{$version.':'.$item};
 2394:             }
 2395:         }
 2396:     }
 2397:     return %returnhash;
 2398: }
 2399: 
 2400: # ----------------------------------construct domainconfig user for a domain 
 2401: sub get_domainconfiguser {
 2402:     my ($udom) = @_;
 2403:     return $udom.'-domainconfig';
 2404: }
 2405: 
 2406: sub retrieve_inst_usertypes {
 2407:     my ($udom) = @_;
 2408:     my (%returnhash,@order);
 2409:     my %domdefs = &get_domain_defaults($udom);
 2410:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2411:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2412:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2413:     } else {
 2414:         if (defined(&domain($udom,'primary'))) {
 2415:             my $uhome=&domain($udom,'primary');
 2416:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2417:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2418:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2419:                 return (\%returnhash,\@order);
 2420:             }
 2421:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2422:             my @pairs=split(/\&/,$hashitems);
 2423:             foreach my $item (@pairs) {
 2424:                 my ($key,$value)=split(/=/,$item,2);
 2425:                 $key = &unescape($key);
 2426:                 next if ($key =~ /^error: 2 /);
 2427:                 $returnhash{$key}=&thaw_unescape($value);
 2428:             }
 2429:             my @esc_order = split(/\&/,$orderitems);
 2430:             foreach my $item (@esc_order) {
 2431:                 push(@order,&unescape($item));
 2432:             }
 2433:         } else {
 2434:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2435:         }
 2436:         return (\%returnhash,\@order);
 2437:     }
 2438: }
 2439: 
 2440: sub is_domainimage {
 2441:     my ($url) = @_;
 2442:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
 2443:         if (&domain($1) ne '') {
 2444:             return '1';
 2445:         }
 2446:     }
 2447:     return;
 2448: }
 2449: 
 2450: sub inst_directory_query {
 2451:     my ($srch) = @_;
 2452:     my $udom = $srch->{'srchdomain'};
 2453:     my %results;
 2454:     my $homeserver = &domain($udom,'primary');
 2455:     my $outcome;
 2456:     if ($homeserver ne '') {
 2457:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2458:             if ($srch->{'srchby'} eq 'email') {
 2459:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2460:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2461:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2462:                     (($major == 2) && ($minor < 12))) {
 2463:                     return;
 2464:                 }
 2465:             }
 2466:         }
 2467: 	my $queryid=&reply("querysend:instdirsearch:".
 2468: 			   &escape($srch->{'srchby'}).':'.
 2469: 			   &escape($srch->{'srchterm'}).':'.
 2470: 			   &escape($srch->{'srchtype'}),$homeserver);
 2471: 	my $host=&hostname($homeserver);
 2472: 	if ($queryid !~/^\Q$host\E\_/) {
 2473: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2474: 	    return;
 2475: 	}
 2476: 	my $response = &get_query_reply($queryid);
 2477: 	my $maxtries = 5;
 2478: 	my $tries = 1;
 2479: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2480: 	    $response = &get_query_reply($queryid);
 2481: 	    $tries ++;
 2482: 	}
 2483: 
 2484:         if (!&error($response) && $response ne 'refused') {
 2485:             if ($response eq 'unavailable') {
 2486:                 $outcome = $response;
 2487:             } else {
 2488:                 $outcome = 'ok';
 2489:                 my @matches = split(/\n/,$response);
 2490:                 foreach my $match (@matches) {
 2491:                     my ($key,$value) = split(/=/,$match);
 2492:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2493:                 }
 2494:             }
 2495:         }
 2496:     }
 2497:     return ($outcome,%results);
 2498: }
 2499: 
 2500: sub usersearch {
 2501:     my ($srch) = @_;
 2502:     my $dom = $srch->{'srchdomain'};
 2503:     my %results;
 2504:     my %libserv = &all_library();
 2505:     my $query = 'usersearch';
 2506:     foreach my $tryserver (keys(%libserv)) {
 2507:         if (&host_domain($tryserver) eq $dom) {
 2508:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2509:                 if ($srch->{'srchby'} eq 'email') {
 2510:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2511:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2512:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2513:                              (($major == 2) && ($minor < 12)));
 2514:                 }
 2515:             }
 2516:             my $host=&hostname($tryserver);
 2517:             my $queryid=
 2518:                 &reply("querysend:".&escape($query).':'.
 2519:                        &escape($srch->{'srchby'}).':'.
 2520:                        &escape($srch->{'srchtype'}).':'.
 2521:                        &escape($srch->{'srchterm'}),$tryserver);
 2522:             if ($queryid !~/^\Q$host\E\_/) {
 2523:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2524:                 next;
 2525:             }
 2526:             my $reply = &get_query_reply($queryid);
 2527:             my $maxtries = 1;
 2528:             my $tries = 1;
 2529:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2530:                 $reply = &get_query_reply($queryid);
 2531:                 $tries ++;
 2532:             }
 2533:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2534:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2535:             } else {
 2536:                 my @matches;
 2537:                 if ($reply =~ /\n/) {
 2538:                     @matches = split(/\n/,$reply);
 2539:                 } else {
 2540:                     @matches = split(/\&/,$reply);
 2541:                 }
 2542:                 foreach my $match (@matches) {
 2543:                     my ($uname,$udom,%userhash);
 2544:                     foreach my $entry (split(/:/,$match)) {
 2545:                         my ($key,$value) =
 2546:                             map {&unescape($_);} split(/=/,$entry);
 2547:                         $userhash{$key} = $value;
 2548:                         if ($key eq 'username') {
 2549:                             $uname = $value;
 2550:                         } elsif ($key eq 'domain') {
 2551:                             $udom = $value;
 2552:                         }
 2553:                     }
 2554:                     $results{$uname.':'.$udom} = \%userhash;
 2555:                 }
 2556:             }
 2557:         }
 2558:     }
 2559:     return %results;
 2560: }
 2561: 
 2562: sub get_instuser {
 2563:     my ($udom,$uname,$id) = @_;
 2564:     my $homeserver = &domain($udom,'primary');
 2565:     my ($outcome,%results);
 2566:     if ($homeserver ne '') {
 2567:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2568:                            &escape($id).':'.&escape($udom),$homeserver);
 2569:         my $host=&hostname($homeserver);
 2570:         if ($queryid !~/^\Q$host\E\_/) {
 2571:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2572:             return;
 2573:         }
 2574:         my $response = &get_query_reply($queryid);
 2575:         my $maxtries = 5;
 2576:         my $tries = 1;
 2577:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2578:             $response = &get_query_reply($queryid);
 2579:             $tries ++;
 2580:         }
 2581:         if (!&error($response) && $response ne 'refused') {
 2582:             if ($response eq 'unavailable') {
 2583:                 $outcome = $response;
 2584:             } else {
 2585:                 $outcome = 'ok';
 2586:                 my @matches = split(/\n/,$response);
 2587:                 foreach my $match (@matches) {
 2588:                     my ($key,$value) = split(/=/,$match);
 2589:                     $results{&unescape($key)} = &thaw_unescape($value);
 2590:                 }
 2591:             }
 2592:         }
 2593:     }
 2594:     my %userinfo;
 2595:     if (ref($results{$uname}) eq 'HASH') {
 2596:         %userinfo = %{$results{$uname}};
 2597:     } 
 2598:     return ($outcome,%userinfo);
 2599: }
 2600: 
 2601: sub get_multiple_instusers {
 2602:     my ($udom,$users,$caller) = @_;
 2603:     my ($outcome,$results);
 2604:     if (ref($users) eq 'HASH') {
 2605:         my $count = keys(%{$users}); 
 2606:         my $requested = &freeze_escape($users);
 2607:         my $homeserver = &domain($udom,'primary');
 2608:         if ($homeserver ne '') {
 2609:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2610:             my $host=&hostname($homeserver);
 2611:             if ($queryid !~/^\Q$host\E\_/) {
 2612:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2613:                          ' for host: '.$homeserver.'in domain '.$udom);
 2614:                 return ($outcome,$results);
 2615:             }
 2616:             my $response = &get_query_reply($queryid);
 2617:             my $maxtries = 5;
 2618:             if ($count > 100) {
 2619:                 $maxtries = 1+int($count/20);
 2620:             }
 2621:             my $tries = 1;
 2622:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2623:                 $response = &get_query_reply($queryid);
 2624:                 $tries ++;
 2625:             }
 2626:             if ($response eq '') {
 2627:                 $results = {};
 2628:                 foreach my $key (keys(%{$users})) {
 2629:                     my ($uname,$id);
 2630:                     if ($caller eq 'id') {
 2631:                         $id = $key;
 2632:                     } else {
 2633:                         $uname = $key;
 2634:                     }
 2635:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2636:                     $outcome = $resp;
 2637:                     if ($resp eq 'ok') {
 2638:                         %{$results} = (%{$results}, %info);
 2639:                     } else {
 2640:                         last;
 2641:                     }
 2642:                 }
 2643:             } elsif(!&error($response) && ($response ne 'refused')) {
 2644:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2645:                     $outcome = $response;
 2646:                 } else {
 2647:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2648:                     if ($outcome eq 'ok') {
 2649:                         $results = &thaw_unescape($userdata); 
 2650:                     }
 2651:                 }
 2652:             }
 2653:         }
 2654:     }
 2655:     return ($outcome,$results);
 2656: }
 2657: 
 2658: sub inst_rulecheck {
 2659:     my ($udom,$uname,$id,$item,$rules) = @_;
 2660:     my %returnhash;
 2661:     if ($udom ne '') {
 2662:         if (ref($rules) eq 'ARRAY') {
 2663:             @{$rules} = map {&escape($_);} (@{$rules});
 2664:             my $rulestr = join(':',@{$rules});
 2665:             my $homeserver=&domain($udom,'primary');
 2666:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2667:                 my $response;
 2668:                 if ($item eq 'username') {                
 2669:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2670:                                               ':'.&escape($uname).':'.$rulestr,
 2671:                                               $homeserver));
 2672:                 } elsif ($item eq 'id') {
 2673:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2674:                                               ':'.&escape($id).':'.$rulestr,
 2675:                                               $homeserver));
 2676:                 } elsif ($item eq 'selfcreate') {
 2677:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2678:                                                &escape($udom).':'.&escape($uname).
 2679:                                               ':'.$rulestr,$homeserver));
 2680:                 } elsif ($item eq 'unamemap') {
 2681:                     $response=&unescape(&reply('instunamemapcheck:'.
 2682:                                                &escape($udom).':'.&escape($uname).
 2683:                                               ':'.$rulestr,$homeserver));
 2684:                 }
 2685:                 if ($response ne 'refused') {
 2686:                     my @pairs=split(/\&/,$response);
 2687:                     foreach my $item (@pairs) {
 2688:                         my ($key,$value)=split(/=/,$item,2);
 2689:                         $key = &unescape($key);
 2690:                         next if ($key =~ /^error: 2 /);
 2691:                         $returnhash{$key}=&thaw_unescape($value);
 2692:                     }
 2693:                 }
 2694:             }
 2695:         }
 2696:     }
 2697:     return %returnhash;
 2698: }
 2699: 
 2700: sub inst_userrules {
 2701:     my ($udom,$check) = @_;
 2702:     my (%ruleshash,@ruleorder);
 2703:     if ($udom ne '') {
 2704:         my $homeserver=&domain($udom,'primary');
 2705:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2706:             my $response;
 2707:             if ($check eq 'id') {
 2708:                 $response=&reply('instidrules:'.&escape($udom),
 2709:                                  $homeserver);
 2710:             } elsif ($check eq 'email') {
 2711:                 $response=&reply('instemailrules:'.&escape($udom),
 2712:                                  $homeserver);
 2713:             } elsif ($check eq 'unamemap') {
 2714:                 $response=&reply('unamemaprules:'.&escape($udom),
 2715:                                  $homeserver); 
 2716:             } else {
 2717:                 $response=&reply('instuserrules:'.&escape($udom),
 2718:                                  $homeserver);
 2719:             }
 2720:             if (($response ne 'refused') && ($response ne 'error') && 
 2721:                 ($response ne 'unknown_cmd') && 
 2722:                 ($response ne 'no_such_host')) {
 2723:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2724:                 my @pairs=split(/\&/,$hashitems);
 2725:                 foreach my $item (@pairs) {
 2726:                     my ($key,$value)=split(/=/,$item,2);
 2727:                     $key = &unescape($key);
 2728:                     next if ($key =~ /^error: 2 /);
 2729:                     $ruleshash{$key}=&thaw_unescape($value);
 2730:                 }
 2731:                 my @esc_order = split(/\&/,$orderitems);
 2732:                 foreach my $item (@esc_order) {
 2733:                     push(@ruleorder,&unescape($item));
 2734:                 }
 2735:             }
 2736:         }
 2737:     }
 2738:     return (\%ruleshash,\@ruleorder);
 2739: }
 2740: 
 2741: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2742: 
 2743: sub get_domain_defaults {
 2744:     my ($domain,$ignore_cache) = @_;
 2745:     return if (($domain eq '') || ($domain eq 'public'));
 2746:     my $cachetime = 60*60*24;
 2747:     unless ($ignore_cache) {
 2748:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2749:         if (defined($cached)) {
 2750:             if (ref($result) eq 'HASH') {
 2751:                 return %{$result};
 2752:             }
 2753:         }
 2754:     }
 2755:     my %domdefaults;
 2756:     my %domconfig =
 2757:          &get_dom('configuration',['defaults','quotas',
 2758:                                   'requestcourses','inststatus',
 2759:                                   'coursedefaults','usersessions',
 2760:                                   'requestauthor','authordefaults',
 2761:                                   'selfenrollment','coursecategories',
 2762:                                   'ssl','autoenroll','trust',
 2763:                                   'helpsettings','wafproxy',
 2764:                                   'ltisec','toolsec','privacy'],$domain);
 2765:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2766:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2767:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2768:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2769:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2770:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2771:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2772:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2773:         $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
 2774:         $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
 2775:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2776:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2777:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2778:         $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
 2779:     } else {
 2780:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2781:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2782:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2783:     }
 2784:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2785:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2786:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2787:         } else {
 2788:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2789:         }
 2790:         my @usertools = ('aboutme','blog','webdav','portfolio','portaccess');
 2791:         foreach my $item (@usertools) {
 2792:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2793:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2794:             }
 2795:         }
 2796:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2797:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2798:         }
 2799:     }
 2800:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2801:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2802:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2803:         }
 2804:     }
 2805:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2806:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2807:     }
 2808:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
 2809:         foreach my $item ('nocodemirror','copyright','sourceavail','domcoordacc','editors','archive') {
 2810:             if ($item eq 'editors') {
 2811:                 if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
 2812:                     $domdefaults{$item} = join(',',@{$domconfig{'authordefaults'}{'editors'}});
 2813:                 }
 2814:             } else {
 2815:                 $domdefaults{$item} = $domconfig{'authordefaults'}{$item};
 2816:             }
 2817:         }
 2818:     }
 2819:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2820:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2821:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2822:         }
 2823:     }
 2824:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2825:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2826:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2827:         $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
 2828:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2829:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2830:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2831:         }
 2832:         if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
 2833:             $domdefaults{'crseditors'}=join(',',@{$domconfig{'coursedefaults'}{'crseditors'}});
 2834:         }
 2835:         foreach my $type (@coursetypes) {
 2836:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2837:                 unless ($type eq 'community') {
 2838:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2839:                 }
 2840:             }
 2841:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2842:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2843:             }
 2844:             if (ref($domconfig{'coursedefaults'}{'coursequota'}) eq 'HASH') {
 2845:                 $domdefaults{$type.'coursequota'} = $domconfig{'coursedefaults'}{'coursequota'}{$type};
 2846:             }
 2847:             if ($domdefaults{'postsubmit'} eq 'on') {
 2848:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2849:                     $domdefaults{$type.'postsubtimeout'} = 
 2850:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2851:                 }
 2852:             }
 2853:             if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
 2854:                 $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
 2855:             } else {
 2856:                 $domdefaults{$type.'domexttool'} = 1;
 2857:             }
 2858:             if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
 2859:                 $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
 2860:             } else {
 2861:                 $domdefaults{$type.'exttool'} = 0;
 2862:             }
 2863:             if (ref($domconfig{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
 2864:                 $domdefaults{$type.'crsauthor'} = $domconfig{'coursedefaults'}{'crsauthor'}{$type};
 2865:             } else {
 2866:                 $domdefaults{$type.'crsauthor'} = 1;
 2867:             }
 2868:         }
 2869:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2870:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2871:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2872:                 if (@clonecodes) {
 2873:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2874:                 }
 2875:             }
 2876:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2877:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2878:         }
 2879:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2880:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2881:         }
 2882:         if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
 2883:             $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
 2884:         }
 2885:     }
 2886:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2887:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2888:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2889:         }
 2890:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2891:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2892:         }
 2893:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2894:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2895:         }
 2896:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2897:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2898:         }
 2899:     }
 2900:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2901:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2902:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2903:                             'approval','limit');
 2904:             foreach my $type (@coursetypes) {
 2905:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2906:                     my @mgrdc = ();
 2907:                     foreach my $item (@settings) {
 2908:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2909:                             push(@mgrdc,$item);
 2910:                         }
 2911:                     }
 2912:                     if (@mgrdc) {
 2913:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2914:                     }
 2915:                 }
 2916:             }
 2917:         }
 2918:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2919:             foreach my $type (@coursetypes) {
 2920:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2921:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2922:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2923:                     }
 2924:                 }
 2925:             }
 2926:         }
 2927:     }
 2928:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2929:         $domdefaults{'catauth'} = 'std';
 2930:         $domdefaults{'catunauth'} = 'std';
 2931:         if ($domconfig{'coursecategories'}{'auth'}) {
 2932:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2933:         }
 2934:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2935:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2936:         }
 2937:     }
 2938:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2939:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2940:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2941:         }
 2942:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2943:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2944:         }
 2945:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2946:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2947:         }
 2948:     }
 2949:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2950:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2951:         foreach my $prefix (@prefixes) {
 2952:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2953:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2954:             }
 2955:         }
 2956:     }
 2957:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2958:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2959:         $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
 2960:     }
 2961:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2962:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2963:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2964:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2965:         }
 2966:     }
 2967:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2968:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
 2969:             if ($domconfig{'wafproxy'}{$item}) {
 2970:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2971:             }
 2972:         }
 2973:     }
 2974:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
 2975:         if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
 2976:             $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
 2977:             $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
 2978:             $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
 2979:         }
 2980:         if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
 2981:             if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
 2982:                 $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
 2983:             }
 2984:         }
 2985:         if (ref($domconfig{'ltisec'}{'suggested'}) eq 'HASH') {
 2986:             my %suggestions = %{$domconfig{'ltisec'}{'suggested'}};
 2987:             foreach my $item (keys(%{$domconfig{'ltisec'}{'suggested'}})) {
 2988:                 unless (ref($domconfig{'ltisec'}{'suggested'}{$item}) eq 'HASH') {
 2989:                     delete($suggestions{$item});
 2990:                 }
 2991:             }
 2992:             if (keys(%suggestions)) {
 2993:                 $domdefaults{'linkprotsuggested'} = \%suggestions;
 2994:             }
 2995:         }
 2996:     }
 2997:     if (ref($domconfig{'toolsec'}) eq 'HASH') {
 2998:         if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
 2999:             $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
 3000:             $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
 3001:         }
 3002:         if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
 3003:             if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
 3004:                 $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
 3005:             }
 3006:         }
 3007:     }
 3008:     if (ref($domconfig{'privacy'}) eq 'HASH') {
 3009:         if (ref($domconfig{'privacy'}{'approval'}) eq 'HASH') {
 3010:             foreach my $domtype ('instdom','extdom') {
 3011:                 if (ref($domconfig{'privacy'}{'approval'}{$domtype}) eq 'HASH') {
 3012:                     foreach my $roletype ('domain','author','course','community') {
 3013:                         if ($domconfig{'privacy'}{'approval'}{$domtype}{$roletype} eq 'user') {
 3014:                             $domdefaults{'userapprovals'} = 1;
 3015:                             last;
 3016:                         }
 3017:                     }
 3018:                 }
 3019:                 last if ($domdefaults{'userapprovals'});
 3020:             }
 3021:         }
 3022:         if (ref($domconfig{'privacy'}{'othdom'}) eq 'HASH') {
 3023:             $domdefaults{'privacyothdom'} = $domconfig{'privacy'}{'othdom'};
 3024:         }
 3025:     }
 3026:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 3027:     return %domdefaults;
 3028: }
 3029: 
 3030: sub get_dom_cats {
 3031:     my ($dom) = @_;
 3032:     return unless (&domain($dom));
 3033:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 3034:     unless (defined($cached)) {
 3035:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 3036:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 3037:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 3038:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 3039:             } else {
 3040:                 $cats = {};
 3041:             }
 3042:         } else {
 3043:             $cats = {};
 3044:         }
 3045:         &do_cache_new('cats',$dom,$cats,3600);
 3046:     }
 3047:     return $cats;
 3048: }
 3049: 
 3050: sub get_dom_instcats {
 3051:     my ($dom) = @_;
 3052:     return unless (&domain($dom));
 3053:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 3054:     unless (defined($cached)) {
 3055:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 3056:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 3057:         if ($totcodes > 0) {
 3058:             my $caller = 'global';
 3059:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 3060:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 3061:                 $instcats = {
 3062:                                 totcodes => $totcodes,
 3063:                                 codes => \%codes,
 3064:                                 codetitles => \@codetitles,
 3065:                                 cat_titles => \%cat_titles,
 3066:                                 cat_order => \%cat_order,
 3067:                             };
 3068:                 &do_cache_new('instcats',$dom,$instcats,3600);
 3069:             }
 3070:         }
 3071:     }
 3072:     return $instcats;
 3073: }
 3074: 
 3075: sub retrieve_instcodes {
 3076:     my ($coursecodes,$dom) = @_;
 3077:     my $totcodes;
 3078:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 3079:     foreach my $course (keys(%courses)) {
 3080:         if (ref($courses{$course}) eq 'HASH') {
 3081:             if ($courses{$course}{'inst_code'} ne '') {
 3082:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 3083:                 $totcodes ++;
 3084:             }
 3085:         }
 3086:     }
 3087:     return $totcodes;
 3088: }
 3089: 
 3090: sub course_portal_url {
 3091:     my ($cnum,$cdom,$r) = @_;
 3092:     my $chome = &homeserver($cnum,$cdom);
 3093:     my $hostname = &hostname($chome);
 3094:     my $protocol = $protocol{$chome};
 3095:     $protocol = 'http' if ($protocol ne 'https');
 3096:     my %domdefaults = &get_domain_defaults($cdom);
 3097:     my $firsturl;
 3098:     if ($domdefaults{'portal_def'}) {
 3099:         $firsturl = $domdefaults{'portal_def'};
 3100:     } else {
 3101:         my $alias = &use_proxy_alias($r,$chome);
 3102:         $hostname = $alias if ($alias ne '');
 3103:         $firsturl = $protocol.'://'.$hostname;
 3104:     }
 3105:     return $firsturl;
 3106: }
 3107: 
 3108: sub url_prefix {
 3109:     my ($r,$dom,$home,$context) = @_;
 3110:     my $prefix;
 3111:     my %domdefs = &get_domain_defaults($dom);
 3112:     if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
 3113:         if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
 3114:             $prefix = $1;
 3115:         }
 3116:     }
 3117:     if ($prefix eq '') {
 3118:         my $hostname = &hostname($home);
 3119:         my $protocol = $protocol{$home};
 3120:         $protocol = 'http' if ($protocol{$home} ne 'https');
 3121:         my $alias = &use_proxy_alias($r,$home);
 3122:         $hostname = $alias if ($alias ne '');
 3123:         $prefix = $protocol.'://'.$hostname;
 3124:     }
 3125:     return $prefix;
 3126: }
 3127: 
 3128: # --------------------------------------------- Get domain config for passwords
 3129: 
 3130: sub get_passwdconf {
 3131:     my ($dom) = @_;
 3132:     my (%passwdconf,$gotconf,$lookup);
 3133:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 3134:     if (defined($cached)) {
 3135:         if (ref($result) eq 'HASH') {
 3136:             %passwdconf = %{$result};
 3137:             $gotconf = 1;
 3138:         }
 3139:     }
 3140:     unless ($gotconf) {
 3141:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 3142:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 3143:             %passwdconf = %{$domconfig{'passwords'}};
 3144:         }
 3145:         my $cachetime = 24*60*60;
 3146:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 3147:     }
 3148:     return %passwdconf;
 3149: }
 3150: 
 3151: # --------------------------------------------------- Assign a key to a student
 3152: 
 3153: sub assign_access_key {
 3154: #
 3155: # a valid key looks like uname:udom#comments
 3156: # comments are being appended
 3157: #
 3158:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 3159:     $kdom=
 3160:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 3161:     $knum=
 3162:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 3163:     $cdom=
 3164:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3165:     $cnum=
 3166:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3167:     $udom=$env{'user.name'} unless (defined($udom));
 3168:     $uname=$env{'user.domain'} unless (defined($uname));
 3169:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 3170:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 3171:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 3172:                                                   # assigned to this person
 3173:                                                   # - this should not happen,
 3174:                                                   # unless something went wrong
 3175:                                                   # the first time around
 3176: # ready to assign
 3177:         $logentry=$1.'; '.$logentry;
 3178:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 3179:                                                  $kdom,$knum) eq 'ok') {
 3180: # key now belongs to user
 3181: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 3182:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 3183:                 &appenv({'environment.'.$envkey => $ckey});
 3184:                 return 'ok';
 3185:             } else {
 3186:                 return 
 3187:   'error: Count not permanently assign key, will need to be re-entered later.';
 3188: 	    }
 3189:         } else {
 3190:             return 'error: Could not assign key, try again later.';
 3191:         }
 3192:     } elsif (!$existing{$ckey}) {
 3193: # the key does not exist
 3194: 	return 'error: The key does not exist';
 3195:     } else {
 3196: # the key is somebody else's
 3197: 	return 'error: The key is already in use';
 3198:     }
 3199: }
 3200: 
 3201: # ------------------------------------------ put an additional comment on a key
 3202: 
 3203: sub comment_access_key {
 3204: #
 3205: # a valid key looks like uname:udom#comments
 3206: # comments are being appended
 3207: #
 3208:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 3209:     $cdom=
 3210:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3211:     $cnum=
 3212:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3213:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3214:     if ($existing{$ckey}) {
 3215:         $existing{$ckey}.='; '.$logentry;
 3216: # ready to assign
 3217:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 3218:                                                  $cdom,$cnum) eq 'ok') {
 3219: 	    return 'ok';
 3220:         } else {
 3221: 	    return 'error: Count not store comment.';
 3222:         }
 3223:     } else {
 3224: # the key does not exist
 3225: 	return 'error: The key does not exist';
 3226:     }
 3227: }
 3228: 
 3229: # ------------------------------------------------------ Generate a set of keys
 3230: 
 3231: sub generate_access_keys {
 3232:     my ($number,$cdom,$cnum,$logentry)=@_;
 3233:     $cdom=
 3234:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3235:     $cnum=
 3236:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3237:     unless (&allowed('mky',$cdom)) { return 0; }
 3238:     unless (($cdom) && ($cnum)) { return 0; }
 3239:     if ($number>10000) { return 0; }
 3240:     sleep(2); # make sure don't get same seed twice
 3241:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 3242:     my $total=0;
 3243:     for (my $i=1;$i<=$number;$i++) {
 3244:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 3245:                   sprintf("%lx",int(100000*rand)).'-'.
 3246:                   sprintf("%lx",int(100000*rand));
 3247:        $newkey=~s/1/g/g; # folks mix up 1 and l
 3248:        $newkey=~s/0/h/g; # and also 0 and O
 3249:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 3250:        if ($existing{$newkey}) {
 3251:            $i--;
 3252:        } else {
 3253: 	  if (&put('accesskeys',
 3254:               { $newkey => '# generated '.localtime().
 3255:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 3256:                            '; '.$logentry },
 3257: 		   $cdom,$cnum) eq 'ok') {
 3258:               $total++;
 3259: 	  }
 3260:        }
 3261:     }
 3262:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3263:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3264:     return $total;
 3265: }
 3266: 
 3267: # ------------------------------------------------------- Validate an accesskey
 3268: 
 3269: sub validate_access_key {
 3270:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3271:     $cdom=
 3272:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3273:     $cnum=
 3274:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3275:     $udom=$env{'user.domain'} unless (defined($udom));
 3276:     $uname=$env{'user.name'} unless (defined($uname));
 3277:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3278:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3279: }
 3280: 
 3281: # ------------------------------------- Find the section of student in a course
 3282: sub devalidate_getsection_cache {
 3283:     my ($udom,$unam,$courseid)=@_;
 3284:     my $hashid="$udom:$unam:$courseid";
 3285:     &devalidate_cache_new('getsection',$hashid);
 3286: }
 3287: 
 3288: sub courseid_to_courseurl {
 3289:     my ($courseid) = @_;
 3290:     #already url style courseid
 3291:     return $courseid if ($courseid =~ m{^/});
 3292: 
 3293:     if (exists($env{'course.'.$courseid.'.num'})) {
 3294: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3295: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3296: 	return "/$cdom/$cnum";
 3297:     }
 3298: 
 3299:     my %courseinfo=&coursedescription($courseid);
 3300:     if (exists($courseinfo{'num'})) {
 3301: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3302:     }
 3303: 
 3304:     return undef;
 3305: }
 3306: 
 3307: sub getsection {
 3308:     my ($udom,$unam,$courseid)=@_;
 3309:     my $cachetime=1800;
 3310: 
 3311:     my $hashid="$udom:$unam:$courseid";
 3312:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3313:     if (defined($cached)) { return $result; }
 3314: 
 3315:     my %Pending; 
 3316:     my %Expired;
 3317:     #
 3318:     # Each role can either have not started yet (pending), be active, 
 3319:     #    or have expired.
 3320:     #
 3321:     # If there is an active role, we are done.
 3322:     #
 3323:     # If there is more than one role which has not started yet, 
 3324:     #     choose the one which will start sooner
 3325:     # If there is one role which has not started yet, return it.
 3326:     #
 3327:     # If there is more than one expired role, choose the one which ended last.
 3328:     # If there is a role which has expired, return it.
 3329:     #
 3330:     $courseid = &courseid_to_courseurl($courseid);
 3331:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3332:     foreach my $key (keys(%roleshash)) {
 3333:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3334:         my $section=$1;
 3335:         if ($key eq $courseid.'_st') { $section=''; }
 3336:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3337:         my $now=time;
 3338:         if (defined($end) && $end && ($now > $end)) {
 3339:             $Expired{$end}=$section;
 3340:             next;
 3341:         }
 3342:         if (defined($start) && $start && ($now < $start)) {
 3343:             $Pending{$start}=$section;
 3344:             next;
 3345:         }
 3346:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3347:     }
 3348:     #
 3349:     # Presumedly there will be few matching roles from the above
 3350:     # loop and the sorting time will be negligible.
 3351:     if (scalar(keys(%Pending))) {
 3352:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3353:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3354:     } 
 3355:     if (scalar(keys(%Expired))) {
 3356:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3357:         my $time = pop(@sorted);
 3358:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3359:     }
 3360:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3361: }
 3362: 
 3363: sub save_cache {
 3364:     &purge_remembered();
 3365:     #&Apache::loncommon::validate_page();
 3366:     undef(%env);
 3367:     undef($env_loaded);
 3368: }
 3369: 
 3370: my $to_remember=-1;
 3371: my %remembered;
 3372: my %accessed;
 3373: my $kicks=0;
 3374: my $hits=0;
 3375: sub make_key {
 3376:     my ($name,$id) = @_;
 3377:     if (length($id) > 65 
 3378: 	&& length(&escape($id)) > 200) {
 3379: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3380:     }
 3381:     return &escape($name.':'.$id);
 3382: }
 3383: 
 3384: sub devalidate_cache_new {
 3385:     my ($name,$id,$debug) = @_;
 3386:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3387:     my $remembered_id=$name.':'.$id;
 3388:     $id=&make_key($name,$id);
 3389:     $memcache->delete($id);
 3390:     delete($remembered{$remembered_id});
 3391:     delete($accessed{$remembered_id});
 3392: }
 3393: 
 3394: sub is_cached_new {
 3395:     my ($name,$id,$debug) = @_;
 3396:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3397:     if (exists($remembered{$remembered_id})) {
 3398: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3399: 	$accessed{$remembered_id}=[&gettimeofday()];
 3400: 	$hits++;
 3401: 	return ($remembered{$remembered_id},1);
 3402:     }
 3403:     $id=&make_key($name,$id);
 3404:     my $value = $memcache->get($id);
 3405:     if (!(defined($value))) {
 3406: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3407: 	return (undef,undef);
 3408:     }
 3409:     if ($value eq '__undef__') {
 3410: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3411: 	$value=undef;
 3412:     }
 3413:     &make_room($remembered_id,$value,$debug);
 3414:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3415:     return ($value,1);
 3416: }
 3417: 
 3418: sub do_cache_new {
 3419:     my ($name,$id,$value,$time,$debug) = @_;
 3420:     my $remembered_id=$name.':'.$id;
 3421:     $id=&make_key($name,$id);
 3422:     my $setvalue=$value;
 3423:     if (!defined($setvalue)) {
 3424: 	$setvalue='__undef__';
 3425:     }
 3426:     if (!defined($time) ) {
 3427: 	$time=600;
 3428:     }
 3429:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3430:     my $result = $memcache->set($id,$setvalue,$time);
 3431:     if (! $result) {
 3432: 	&logthis("caching of id -> $id  failed");
 3433: 	$memcache->disconnect_all();
 3434:     }
 3435:     # need to make a copy of $value
 3436:     &make_room($remembered_id,$value,$debug);
 3437:     return $value;
 3438: }
 3439: 
 3440: sub make_room {
 3441:     my ($remembered_id,$value,$debug)=@_;
 3442: 
 3443:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3444:                                     : $value;
 3445:     if ($to_remember<0) { return; }
 3446:     $accessed{$remembered_id}=[&gettimeofday()];
 3447:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3448:     my $to_kick;
 3449:     my $max_time=0;
 3450:     foreach my $other (keys(%accessed)) {
 3451: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3452: 	    $to_kick=$other;
 3453: 	    $max_time=&tv_interval($accessed{$other});
 3454: 	}
 3455:     }
 3456:     delete($remembered{$to_kick});
 3457:     delete($accessed{$to_kick});
 3458:     $kicks++;
 3459:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3460:     return;
 3461: }
 3462: 
 3463: sub purge_remembered {
 3464:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3465:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3466:     undef(%remembered);
 3467:     undef(%accessed);
 3468: }
 3469: # ------------------------------------- Read an entry from a user's environment
 3470: 
 3471: sub userenvironment {
 3472:     my ($udom,$unam,@what)=@_;
 3473:     my $items;
 3474:     foreach my $item (@what) {
 3475:         $items.=&escape($item).'&';
 3476:     }
 3477:     $items=~s/\&$//;
 3478:     my %returnhash=();
 3479:     my $uhome = &homeserver($unam,$udom);
 3480:     unless ($uhome eq 'no_host') {
 3481:         my @answer=split(/\&/, 
 3482:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3483:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3484:             return %returnhash;
 3485:         }
 3486:         my $i;
 3487:         for ($i=0;$i<=$#what;$i++) {
 3488: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3489:         }
 3490:     }
 3491:     return %returnhash;
 3492: }
 3493: 
 3494: # ---------------------------------------------------------- Get a studentphoto
 3495: sub studentphoto {
 3496:     my ($udom,$unam,$ext) = @_;
 3497:     my $home=&homeserver($unam,$udom);
 3498:     if (defined($env{'request.course.id'})) {
 3499:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3500:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3501:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3502:             } else {
 3503:                 my ($result,$perm_reqd)=
 3504: 		    &auto_photo_permission($unam,$udom);
 3505:                 if ($result eq 'ok') {
 3506:                     if (!($perm_reqd eq 'yes')) {
 3507:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3508:                     }
 3509:                 }
 3510:             }
 3511:         }
 3512:     } else {
 3513:         my ($result,$perm_reqd) = 
 3514: 	    &auto_photo_permission($unam,$udom);
 3515:         if ($result eq 'ok') {
 3516:             if (!($perm_reqd eq 'yes')) {
 3517:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3518:             }
 3519:         }
 3520:     }
 3521:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3522: }
 3523: 
 3524: sub retrievestudentphoto {
 3525:     my ($udom,$unam,$ext,$type) = @_;
 3526:     my $home=&homeserver($unam,$udom);
 3527:     my $ret=&reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3528:     if ($ret eq 'ok') {
 3529:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3530:         if ($type eq 'thumbnail') {
 3531:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3532:         }
 3533:         my $tokenurl=&tokenwrapper($url);
 3534:         return $tokenurl;
 3535:     } else {
 3536:         if ($type eq 'thumbnail') {
 3537:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3538:         } else { 
 3539:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3540:         }
 3541:     }
 3542: }
 3543: 
 3544: # -------------------------------------------------------------------- New chat
 3545: 
 3546: sub chatsend {
 3547:     my ($newentry,$anon,$group)=@_;
 3548:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3549:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3550:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3551:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3552: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3553: 		   &escape($newentry)).':'.$group,$chome);
 3554: }
 3555: 
 3556: # ------------------------------------------ Find current version of a resource
 3557: 
 3558: sub getversion {
 3559:     my $fname=&clutter(shift);
 3560:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3561:     return &currentversion(&filelocation('',$fname));
 3562: }
 3563: 
 3564: sub currentversion {
 3565:     my $fname=shift;
 3566:     my $author=$fname;
 3567:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3568:     my ($udom,$uname)=split(/\//,$author);
 3569:     my $home=&homeserver($uname,$udom);
 3570:     if ($home eq 'no_host') { 
 3571:         return -1; 
 3572:     }
 3573:     my $answer=&reply("currentversion:$fname",$home);
 3574:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3575: 	return -1;
 3576:     }
 3577:     return $answer;
 3578: }
 3579: 
 3580: #
 3581: # Return special version number of resource if set by override, empty otherwise
 3582: #
 3583: sub usedversion {
 3584:     my $fname=shift;
 3585:     unless ($fname) { $fname=$env{'request.uri'}; }
 3586:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3587:     if ($urlversion) { return $urlversion; }
 3588:     return '';
 3589: }
 3590: 
 3591: # ----------------------------- Subscribe to a resource, return URL if possible
 3592: 
 3593: sub subscribe {
 3594:     my $fname=shift;
 3595:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3596:     $fname=~s/[\n\r]//g;
 3597:     my $author=$fname;
 3598:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3599:     my ($udom,$uname)=split(/\//,$author);
 3600:     my $home=homeserver($uname,$udom);
 3601:     if ($home eq 'no_host') {
 3602:         return 'not_found';
 3603:     }
 3604:     my $answer=reply("sub:$fname",$home);
 3605:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3606: 	$answer.=' by '.$home;
 3607:     }
 3608:     return $answer;
 3609: }
 3610:     
 3611: # -------------------------------------------------------------- Replicate file
 3612: 
 3613: sub repcopy {
 3614:     my $filename=shift;
 3615:     $filename=~s/\/+/\//g;
 3616:     my $londocroot = $perlvar{'lonDocRoot'};
 3617:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3618:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3619:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3620: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3621: 	return &repcopy_userfile($filename);
 3622:     }
 3623:     $filename=~s/[\n\r]//g;
 3624:     my $transname="$filename.in.transfer";
 3625: # FIXME: this should flock
 3626:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3627:     my $remoteurl=subscribe($filename);
 3628:     if ($remoteurl =~ /^con_lost by/) {
 3629: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3630:            return 'unavailable';
 3631:     } elsif ($remoteurl eq 'not_found') {
 3632: 	   #&logthis("Subscribe returned not_found: $filename");
 3633: 	   return 'not_found';
 3634:     } elsif ($remoteurl =~ /^rejected by/) {
 3635: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3636:            return 'forbidden';
 3637:     } elsif ($remoteurl eq 'directory') {
 3638:            return 'ok';
 3639:     } else {
 3640:         my $author=$filename;
 3641:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3642:         my ($udom,$uname)=split(/\//,$author);
 3643:         my $home=homeserver($uname,$udom);
 3644:         unless ($home eq $perlvar{'lonHostID'}) {
 3645:            my @parts=split(/\//,$filename);
 3646:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3647:            if ($path ne "$londocroot/res") {
 3648:                &logthis("Malconfiguration for replication: $filename");
 3649: 	       return 'bad_request';
 3650:            }
 3651:            my $count;
 3652:            for ($count=5;$count<$#parts;$count++) {
 3653:                $path.="/$parts[$count]";
 3654:                if ((-e $path)!=1) {
 3655: 		   mkdir($path,0777);
 3656:                }
 3657:            }
 3658:            my $request=new HTTP::Request('GET',"$remoteurl");
 3659:            my $response;
 3660:            if ($remoteurl =~ m{/raw/}) {
 3661:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3662:            } else {
 3663:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3664:            }
 3665:            if ($response->is_error()) {
 3666: 	       unlink($transname);
 3667:                my $message=$response->status_line;
 3668:                &logthis("<font color=\"blue\">WARNING:"
 3669:                        ." LWP get: $message: $filename</font>");
 3670:                return 'unavailable';
 3671:            } else {
 3672: 	       if ($remoteurl!~/\.meta$/) {
 3673:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3674:                   my $mresponse;
 3675:                   if ($remoteurl =~ m{/raw/}) {
 3676:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3677:                   } else {
 3678:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3679:                   }
 3680:                   if ($mresponse->is_error()) {
 3681: 		      unlink($filename.'.meta');
 3682:                       &logthis(
 3683:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3684:                   }
 3685: 	       }
 3686:                rename($transname,$filename);
 3687:                return 'ok';
 3688:            }
 3689:        }
 3690:     }
 3691: }
 3692: 
 3693: # ------------------------------------------------- Unsubscribe from a resource
 3694: 
 3695: sub unsubscribe {
 3696:     my ($fname) = @_;
 3697:     my $answer;
 3698:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3699:     $fname=~s/[\n\r]//g;
 3700:     my $author=$fname;
 3701:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3702:     my ($udom,$uname)=split(/\//,$author);
 3703:     my $home=homeserver($uname,$udom);
 3704:     if ($home eq 'no_host') {
 3705:         $answer = 'no_host';
 3706:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3707:         $answer = 'home';
 3708:     } else {
 3709:         my $defdom = $perlvar{'lonDefDomain'};
 3710:         if (&will_trust('content',$defdom,$udom)) {
 3711:             $answer = reply("unsub:$fname",$home);
 3712:         } else {
 3713:             $answer = 'untrusted';
 3714:         }
 3715:     }
 3716:     return $answer;
 3717: }
 3718: 
 3719: # ------------------------------------------------ Get server side include body
 3720: sub ssi_body {
 3721:     my ($filelink,%form)=@_;
 3722:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3723:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3724:     }
 3725:     my $output='';
 3726:     my $response;
 3727:     if ($filelink=~/^https?\:/) {
 3728:        ($output,$response)=&externalssi($filelink);
 3729:     } else {
 3730:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3731:        $filelink .= 'inhibitmenu=yes';
 3732:        ($output,$response)=&ssi($filelink,%form);
 3733:     }
 3734:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3735:     $output=~s/^.*?\<body[^\>]*\>//si;
 3736:     $output=~s/\<\/body\s*\>.*?$//si;
 3737:     $output=~s{\Q<div class="LC_landmark" role="main">\E}{<div>}gs;
 3738:     if (wantarray) {
 3739:         return ($output, $response);
 3740:     } else {
 3741:         return $output;
 3742:     }
 3743: }
 3744: 
 3745: # --------------------------------------------------------- Server Side Include
 3746: 
 3747: sub absolute_url {
 3748:     my ($host_name,$unalias,$keep_proto) = @_;
 3749:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3750:     if ($host_name eq '') {
 3751: 	$host_name = $ENV{'SERVER_NAME'};
 3752:     }
 3753:     if ($unalias) {
 3754:         my $alias = &get_proxy_alias();
 3755:         if ($alias eq $host_name) {
 3756:             my $lonhost = $perlvar{'lonHostID'};
 3757:             my $hostname = &hostname($lonhost);
 3758:             my $lcproto; 
 3759:             if (($keep_proto) || ($hostname eq '')) {
 3760:                 $lcproto = $protocol;
 3761:             } else {
 3762:                 $lcproto = $protocol{$lonhost};
 3763:                 $lcproto = 'http' if ($lcproto ne 'https');
 3764:                 $lcproto .= '://';
 3765:             }
 3766:             unless ($hostname eq '') {
 3767:                 return $lcproto.$hostname;
 3768:             }
 3769:         }
 3770:     }
 3771:     return $protocol.$host_name;
 3772: }
 3773: 
 3774: #
 3775: #   Server side include.
 3776: # Parameters:
 3777: #  fn     Possibly encrypted resource name/id.
 3778: #  form   Hash that describes how the rendering should be done
 3779: #         and other things.
 3780: # Returns:
 3781: #   Scalar context: The content of the response.
 3782: #   Array context:  2 element list of the content and the full response object.
 3783: #     
 3784: sub ssi {
 3785: 
 3786:     my ($fn,%form)=@_;
 3787:     my ($host,$request,$response);
 3788:     $host = &absolute_url('',1);
 3789: 
 3790:     $form{'no_update_last_known'}=1;
 3791:     &Apache::lonenc::check_encrypt(\$fn);
 3792:     if (%form) {
 3793:       $request=new HTTP::Request('POST',$host.$fn);
 3794:       $request->content(join('&',map { 
 3795:             my $name = escape($_);
 3796:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3797:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3798:             : &escape($form{$_}) );    
 3799:         } keys(%form)));
 3800:     } else {
 3801:       $request=new HTTP::Request('GET',$host.$fn);
 3802:     }
 3803: 
 3804:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3805:     my $lonhost = $perlvar{'lonHostID'};
 3806:     my $islocal;
 3807:     if (($env{'request.course.id'}) &&
 3808:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3809:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3810:         ($form{'grade_symb'} ne '') &&
 3811:         (&allowed('mgr',$env{'request.course.id'}.
 3812:                         ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3813:         $islocal = 1;
 3814:     }
 3815:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3816:                                              '','','',$islocal);
 3817: 
 3818:     if (wantarray) {
 3819: 	return ($response->content, $response);
 3820:     } else {
 3821: 	return $response->content;
 3822:     }
 3823: }
 3824: 
 3825: sub externalssi {
 3826:     my ($url)=@_;
 3827:     my $request=new HTTP::Request('GET',$url);
 3828:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3829:     if (wantarray) {
 3830:         return ($response->content, $response);
 3831:     } else {
 3832:         return $response->content;
 3833:     }
 3834: }
 3835: 
 3836: 
 3837: # If the local copy of a replicated resource is outdated, trigger a  
 3838: # connection from the homeserver to flush the delayed queue. If no update 
 3839: # happens, remove local copies of outdated resource (and corresponding
 3840: # metadata file).
 3841: 
 3842: sub remove_stale_resfile {
 3843:     my ($url) = @_;
 3844:     my $removed;
 3845:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3846:         my $audom = $1;
 3847:         my $auname = $2;
 3848:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3849:             my $homeserver = &homeserver($auname,$audom);
 3850:             unless (($homeserver eq 'no_host') ||
 3851:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3852:                 my $fname = &filelocation('',$url);
 3853:                 if (-e $fname) {
 3854:                     my $hostname = &hostname($homeserver);
 3855:                     if ($hostname) {
 3856:                         my $protocol = $protocol{$homeserver};
 3857:                         $protocol = 'http' if ($protocol ne 'https');
 3858:                         my $uri = &declutter($url);
 3859:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3860:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3861:                         if ($response->is_success()) {
 3862:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3863:                             my $locmodtime = (stat($fname))[9];
 3864:                             if ($locmodtime < $remmodtime) {
 3865:                                 my $stale;
 3866:                                 my $answer = &reply('pong',$homeserver);
 3867:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3868:                                     sleep(0.2);
 3869:                                     $locmodtime = (stat($fname))[9];
 3870:                                     if ($locmodtime < $remmodtime) {
 3871:                                         my $posstransfer = $fname.'.in.transfer';
 3872:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3873:                                             $removed = 1;
 3874:                                         } else {
 3875:                                             $stale = 1;
 3876:                                         }
 3877:                                     } else {
 3878:                                         $removed = 1;
 3879:                                     }
 3880:                                 } else {
 3881:                                     $stale = 1;
 3882:                                 }
 3883:                                 if ($stale) {
 3884:                                     if (unlink($fname)) {
 3885:                                         if ($uri!~/\.meta$/) {
 3886:                                             if (-e $fname.'.meta') {
 3887:                                                 unlink($fname.'.meta');
 3888:                                             }
 3889:                                         }
 3890:                                         my $unsubresult = &unsubscribe($fname);
 3891:                                         unless ($unsubresult eq 'ok') {
 3892:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3893:                                         }
 3894:                                         $removed = 1;
 3895:                                     }
 3896:                                 }
 3897:                             }
 3898:                         }
 3899:                     }
 3900:                 }
 3901:             }
 3902:         }
 3903:     }
 3904:     return $removed;
 3905: }
 3906: 
 3907: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3908: 
 3909: sub allowuploaded {
 3910:     my ($srcurl,$url)=@_;
 3911:     $url=&clutter(&declutter($url));
 3912:     my $dir=$url;
 3913:     $dir=~s/\/[^\/]+$//;
 3914:     my %httpref=();
 3915:     my $httpurl=&hreflocation('',$url);
 3916:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3917:     &Apache::lonnet::appenv(\%httpref);
 3918: }
 3919: 
 3920: #
 3921: # Determine if the current user should be able to edit a particular resource,
 3922: # when viewing in course context.
 3923: # (a) When viewing resource used to determine if "Edit" item is included in 
 3924: #     Functions.
 3925: # (b) When displaying folder contents in course editor, used to determine if
 3926: #     "Edit" link will be displayed alongside resource.
 3927: #
 3928: #  input: six args -- filename (decluttered), course number, course domain,
 3929: #                   url, symb (if registered) and group (if this is a group
 3930: #                   item -- e.g., bulletin board, group page etc.).
 3931: #  output: array of five scalars -- 
 3932: #          $cfile -- url for file editing if editable on current server
 3933: #          $home -- homeserver of resource (i.e., for author if published,
 3934: #                                           or course if uploaded.).
 3935: #          $switchserver --  1 if server switch will be needed.
 3936: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3937: #          $forceview -- 1 if icon/link should be to go to view mode
 3938: #
 3939: 
 3940: sub can_edit_resource {
 3941:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3942:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3943: #
 3944: # For aboutme pages user can only edit his/her own.
 3945: #
 3946:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3947:         my ($sdom,$sname) = ($1,$2);
 3948:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3949:             $home = $env{'user.home'};
 3950:             $cfile = $resurl;
 3951:             if ($env{'form.forceedit'}) {
 3952:                 $forceview = 1;
 3953:             } else {
 3954:                 $forceedit = 1;
 3955:             }
 3956:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3957:         } else {
 3958:             return;
 3959:         }
 3960:     }
 3961: 
 3962: #
 3963: # For /adm/viewcoauthors can only edit if author or co-author who is manager.
 3964: #
 3965: 
 3966:     if (($resurl eq '/adm/viewcoauthors') && ($cnum ne '') && ($cdom ne '')) {
 3967:         if (((&allowed('cca',"$cdom/$cnum")) ||
 3968:              (&allowed('caa',"$cdom/$cnum"))) ||
 3969:              ((&allowed('vca',"$cdom/$cnum") ||
 3970:                &allowed('vaa',"$cdom/$cnum")) &&
 3971:               ($env{"environment.internal.manager./$cdom/$cnum"}))) {
 3972:             $home = $env{'user.home'};
 3973:             $cfile = $resurl;
 3974:             if ($env{'form.forceedit'}) {
 3975:                 $forceview = 1;
 3976:             } else {
 3977:                 $forceedit = 1;
 3978:             }
 3979:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3980:         } else {
 3981:             return;
 3982:         }
 3983:     }
 3984: 
 3985:     if ($env{'request.course.id'}) {
 3986:         my $crsedit = &allowed('mdc',$env{'request.course.id'});
 3987:         if ($group ne '') {
 3988: # if this is a group homepage or group bulletin board, check group privs
 3989:             my $allowed = 0;
 3990:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3991:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3992:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3993:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3994:                     $allowed = 1;
 3995:                 }
 3996:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3997:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3998:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3999:                     $allowed = 1;
 4000:                 }
 4001:             }
 4002:             if ($allowed) {
 4003:                 $home=&homeserver($cnum,$cdom);
 4004:                 if ($env{'form.forceedit'}) {
 4005:                     $forceview = 1;
 4006:                 } else {
 4007:                     $forceedit = 1;
 4008:                 }
 4009:                 $cfile = $resurl;
 4010:             } else {
 4011:                 return;
 4012:             }
 4013:         } else {
 4014:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 4015:                 unless (&allowed('opa',$env{'request.course.id'})) {
 4016:                     return;
 4017:                 }
 4018:             } elsif (!$crsedit) {
 4019:                 if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
 4020: #
 4021: # No edit allowed where CC has switched to student role.
 4022: #
 4023:                     return;
 4024:                 } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
 4025:                          ($resurl =~ m{^/res/lib/templates/})) {
 4026:                     return;
 4027:                 }
 4028:             }
 4029:         }
 4030:     }
 4031: 
 4032:     if ($file ne '') {
 4033:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 4034:             if (&is_course_upload($file,$cnum,$cdom)) {
 4035:                 $uploaded = 1;
 4036:                 $incourse = 1;
 4037:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 4038:                     $cfile = &hreflocation('',$file);
 4039:                     if ($env{'form.forceedit'}) {
 4040:                         $forceview = 1;
 4041:                     } else {
 4042:                         $forceedit = 1;
 4043:                     }
 4044:                 }
 4045:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 4046:                 $incourse = 1;
 4047:                 if ($env{'form.forceedit'}) {
 4048:                     $forceview = 1;
 4049:                 } else {
 4050:                     $forceedit = 1;
 4051:                 }
 4052:                 $cfile = $resurl;
 4053:             } elsif (($resurl ne '') && (&is_on_map($resurl))) {
 4054:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 4055:                     $incourse = 1;
 4056:                     if ($env{'form.forceedit'}) {
 4057:                         $forceview = 1;
 4058:                     } else {
 4059:                         $forceedit = 1;
 4060:                     }
 4061:                     $cfile = $resurl;
 4062:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 4063:                     $incourse = 1;
 4064:                     $cfile = $resurl.'/smpedit';
 4065:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 4066:                     $incourse = 1;
 4067:                     if ($env{'form.forceedit'}) {
 4068:                         $forceview = 1;
 4069:                     } else {
 4070:                         $forceedit = 1;
 4071:                     }
 4072:                     $cfile = $resurl;
 4073:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 4074:                     my ($map,$id,$res) = &decode_symb($symb);
 4075:                     if ($map =~ /\.page$/) {
 4076:                         $incourse = 1;
 4077:                         if ($env{'form.forceedit'}) {
 4078:                             $forceview = 1;
 4079:                             $cfile = $map;
 4080:                         } else {
 4081:                             $forceedit = 1;
 4082:                             $cfile =  '/adm/wrapper'.$resurl;
 4083:                         }
 4084:                     }
 4085:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 4086:                     $incourse = 1;
 4087:                     if ($env{'form.forceedit'}) {
 4088:                         $forceview = 1;
 4089:                     } else {
 4090:                         $forceedit = 1;
 4091:                     }
 4092:                     $cfile = $resurl;
 4093:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 4094:                     $incourse = 1;
 4095:                     if ($env{'form.forceedit'}) {
 4096:                         $forceview = 1;
 4097:                     } else {
 4098:                         $forceedit = 1;
 4099:                     }
 4100:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 4101:                 }
 4102:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 4103:                 my $template = '/res/lib/templates/simpleproblem.problem';
 4104:                 if (&is_on_map($template)) { 
 4105:                     $incourse = 1;
 4106:                     $forceview = 1;
 4107:                     $cfile = $template;
 4108:                 }
 4109:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 4110:                 $incourse = 1;
 4111:                 if ($env{'form.forceedit'}) {
 4112:                     $forceview = 1;
 4113:                 } else {
 4114:                     $forceedit = 1;
 4115:                 }
 4116:                 $cfile = $resurl;
 4117:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 4118:                 $incourse = 1;
 4119:                 if ($env{'form.forceedit'}) {
 4120:                     $forceview = 1;
 4121:                 } else {
 4122:                     $forceedit = 1;
 4123:                 }
 4124:                 $cfile = $resurl;
 4125:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 4126:                 $incourse = 1;
 4127:                 $forceview = 1;
 4128:                 if ($symb) {
 4129:                     my ($map,$id,$res)=&decode_symb($symb);
 4130:                     $env{'request.symb'} = $symb;
 4131:                     $cfile = &clutter($res);
 4132:                 } else {
 4133:                     $cfile = $env{'form.suppurl'};
 4134:                     my $escfile = &unescape($cfile);
 4135:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 4136:                         $cfile = '/adm/wrapper'.$escfile;
 4137:                     } else {
 4138:                         $escfile =~ s{^http://}{};
 4139:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 4140:                     }
 4141:                 }
 4142:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 4143:                 if ($env{'form.forceedit'}) {
 4144:                     $forceview = 1;
 4145:                 } else {
 4146:                     $forceedit = 1;
 4147:                 }
 4148:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 4149:             }
 4150:         }
 4151:         if ($uploaded || $incourse) {
 4152:             $home=&homeserver($cnum,$cdom);
 4153:         } elsif ($file !~ m{/$}) {
 4154:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 4155:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 4156:             # Check that the user has permission to edit this resource
 4157:             my $setpriv = 1;
 4158:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 4159:             if (defined($cfudom)) {
 4160:                 $home=&homeserver($cfuname,$cfudom);
 4161:                 $cfile=$file;
 4162:             }
 4163:         }
 4164:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 4165:             (($home ne '') && ($home ne 'no_host'))) {
 4166:             my @ids=&current_machine_ids();
 4167:             unless (grep(/^\Q$home\E$/,@ids)) {
 4168:                 $switchserver=1;
 4169:             }
 4170:         }
 4171:     }
 4172:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 4173: }
 4174: 
 4175: sub is_course_upload {
 4176:     my ($file,$cnum,$cdom) = @_;
 4177:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 4178:     $uploadpath =~ s{^\/}{};
 4179:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 4180:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 4181:         return 1;
 4182:     }
 4183:     return;
 4184: }
 4185: 
 4186: sub in_course {
 4187:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 4188:     if ($hideprivileged) {
 4189:         my $skipuser;
 4190:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 4191:         my @possdoms = ($cdom);  
 4192:         if ($coursehash{'checkforpriv'}) { 
 4193:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 4194:         }
 4195:         if (&privileged($uname,$udom,\@possdoms)) {
 4196:             $skipuser = 1;
 4197:             if ($coursehash{'nothideprivileged'}) {
 4198:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 4199:                     my $user;
 4200:                     if ($item =~ /:/) {
 4201:                         $user = $item;
 4202:                     } else {
 4203:                         $user = join(':',split(/[\@]/,$item));
 4204:                     }
 4205:                     if ($user eq $uname.':'.$udom) {
 4206:                         undef($skipuser);
 4207:                         last;
 4208:                     }
 4209:                 }
 4210:             }
 4211:             if ($skipuser) {
 4212:                 return 0;
 4213:             }
 4214:         }
 4215:     }
 4216:     $type ||= 'any';
 4217:     if (!defined($cdom) || !defined($cnum)) {
 4218:         my $cid  = $env{'request.course.id'};
 4219:         $cdom = $env{'course.'.$cid.'.domain'};
 4220:         $cnum = $env{'course.'.$cid.'.num'};
 4221:     }
 4222:     my $typesref;
 4223:     if (($type eq 'any') || ($type eq 'all')) {
 4224:         $typesref = ['active','previous','future'];
 4225:     } elsif ($type eq 'previous' || $type eq 'future') {
 4226:         $typesref = [$type];
 4227:     }
 4228:     my %roles = &get_my_roles($uname,$udom,'userroles',
 4229:                               $typesref,undef,[$cdom]);
 4230:     my ($tmp) = keys(%roles);
 4231:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 4232:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 4233:     if (@course_roles > 0) {
 4234:         return 1;
 4235:     }
 4236:     return 0;
 4237: }
 4238: 
 4239: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 4240: # input: action, courseID, current domain, intended
 4241: #        path to file, source of file, instruction to parse file for objects,
 4242: #        ref to hash for embedded objects,
 4243: #        ref to hash for codebase of java objects.
 4244: #        reference to scalar to accommodate mime type determined
 4245: #          from File::MMagic if $parser = parse.
 4246: #
 4247: # output: url to file (if action was uploaddoc), 
 4248: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 4249: #
 4250: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 4251: # course.
 4252: #
 4253: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4254: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 4255: #          course's home server.
 4256: #
 4257: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 4258: #          be copied from $source (current location) to 
 4259: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4260: #         and will then be copied to
 4261: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 4262: #         course's home server.
 4263: #
 4264: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4265: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 4266: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4267: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 4268: #         in course's home server.
 4269: #
 4270: 
 4271: sub process_coursefile {
 4272:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 4273:         $mimetype)=@_;
 4274:     my $fetchresult;
 4275:     my $home=&homeserver($docuname,$docudom);
 4276:     if ($action eq 'propagate') {
 4277:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4278: 			     $home);
 4279:     } else {
 4280:         my $fpath = '';
 4281:         my $fname = $file;
 4282:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4283:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4284:         my $filepath = &build_filepath($fpath);
 4285:         if ($action eq 'copy') {
 4286:             if ($source eq '') {
 4287:                 $fetchresult = 'no source file';
 4288:                 return $fetchresult;
 4289:             } else {
 4290:                 my $destination = $filepath.'/'.$fname;
 4291:                 rename($source,$destination);
 4292:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4293:                                  $home);
 4294:             }
 4295:         } elsif ($action eq 'uploaddoc') {
 4296:             open(my $fh,'>',$filepath.'/'.$fname);
 4297:             print $fh $env{'form.'.$source};
 4298:             close($fh);
 4299:             if ($parser eq 'parse') {
 4300:                 my $mm = new File::MMagic;
 4301:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4302:                 if ($type eq 'text/html') {
 4303:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4304:                     unless ($parse_result eq 'ok') {
 4305:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4306:                     }
 4307:                 }
 4308:                 if (ref($mimetype)) {
 4309:                     $$mimetype = $type;
 4310:                 } 
 4311:             }
 4312:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4313:                                  $home);
 4314:             if ($fetchresult eq 'ok') {
 4315:                 return '/uploaded/'.$fpath.'/'.$fname;
 4316:             } else {
 4317:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4318:                         ' to host '.$home.': '.$fetchresult);
 4319:                 return '/adm/notfound.html';
 4320:             }
 4321:         }
 4322:     }
 4323:     unless ( $fetchresult eq 'ok') {
 4324:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4325:              ' to host '.$home.': '.$fetchresult);
 4326:     }
 4327:     return $fetchresult;
 4328: }
 4329: 
 4330: sub build_filepath {
 4331:     my ($fpath) = @_;
 4332:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4333:     unless ($fpath eq '') {
 4334:         my @parts=split('/',$fpath);
 4335:         foreach my $part (@parts) {
 4336:             $filepath.= '/'.$part;
 4337:             if ((-e $filepath)!=1) {
 4338:                 mkdir($filepath,0777);
 4339:             }
 4340:         }
 4341:     }
 4342:     return $filepath;
 4343: }
 4344: 
 4345: sub store_edited_file {
 4346:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4347:     my $file = $primary_url;
 4348:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4349:     my $fpath = '';
 4350:     my $fname = $file;
 4351:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4352:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4353:     my $filepath = &build_filepath($fpath);
 4354:     open(my $fh,'>',$filepath.'/'.$fname);
 4355:     print $fh $content;
 4356:     close($fh);
 4357:     my $home=&homeserver($docuname,$docudom);
 4358:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4359: 			  $home);
 4360:     if ($$fetchresult eq 'ok') {
 4361:         return '/uploaded/'.$fpath.'/'.$fname;
 4362:     } else {
 4363:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4364: 		 ' to host '.$home.': '.$$fetchresult);
 4365:         return '/adm/notfound.html';
 4366:     }
 4367: }
 4368: 
 4369: sub clean_filename {
 4370:     my ($fname,$args)=@_;
 4371: # Replace Windows backslashes by forward slashes
 4372:     $fname=~s/\\/\//g;
 4373:     if (!$args->{'keep_path'}) {
 4374:         # Get rid of everything but the actual filename
 4375: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4376:     }
 4377: # Replace spaces by underscores
 4378:     $fname=~s/\s+/\_/g;
 4379: # Transliterate non-ascii text to ascii
 4380:     my $lang = &Apache::lonlocal::current_language();
 4381:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4382: # Replace all other weird characters by nothing
 4383:     $fname=~s{[^/\w\.\-]}{}g;
 4384: # Replace all .\d. sequences with _\d. so they no longer look like version
 4385: # numbers
 4386:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4387: # Replace three or more adjacent underscores with one for consistency 
 4388: # with loncfile::filename_check() so complete url can be extracted by
 4389: # lonnet::decode_symb()
 4390:     $fname=~s/_{3,}/_/g;
 4391:     return $fname;
 4392: }
 4393: 
 4394: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4395: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4396: # image with the same aspect ratio as the original, but with dimensions which do 
 4397: # not exceed $resizewidth and $resizeheight.
 4398:  
 4399: sub resizeImage {
 4400:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4401:     my $ima = Image::Magick->new;
 4402:     my $resized;
 4403:     if (-e $img_path) {
 4404:         $ima->Read($img_path);
 4405:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4406:             my $width = $ima->Get('width');
 4407:             my $height = $ima->Get('height');
 4408:             if ($width > $resizewidth) {
 4409: 	        my $factor = $width/$resizewidth;
 4410:                 my $newheight = $height/$factor;
 4411:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4412:                 $resized = 1;
 4413:             }
 4414:         }
 4415:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4416:             my $width = $ima->Get('width');
 4417:             my $height = $ima->Get('height');
 4418:             if ($height > $resizeheight) {
 4419:                 my $factor = $height/$resizeheight;
 4420:                 my $newwidth = $width/$factor;
 4421:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4422:                 $resized = 1;
 4423:             }
 4424:         }
 4425:         if ($resized) {
 4426:             $ima->Write($img_path);
 4427:         }
 4428:     }
 4429:     return;
 4430: }
 4431: 
 4432: # --------------- Take an uploaded file and put it into the userfiles directory
 4433: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4434: #                    the desired filename is in $env{"form.$formname.filename"}
 4435: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4436: #                                    canceloverwrite, scantron, toollogo  or ''.
 4437: #                   if 'coursedoc': upload to the current course
 4438: #                   if 'existingfile': write file to tmp/overwrites directory 
 4439: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4440: #                   $context is passed as argument to &finishuserfileupload
 4441: #        $subdir - directory in userfile to store the file into
 4442: #        $parser - instruction to parse file for objects ($parser = parse) or
 4443: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4444: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4445: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4446: #        $allfiles - reference to hash for embedded objects
 4447: #        $codebase - reference to hash for codebase of java objects
 4448: #        $destuname - username for permanent storage of uploaded file
 4449: #        $destudom - domain for permanaent storage of uploaded file
 4450: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4451: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4452: #        $resizewidth - width (pixels) to which to resize uploaded image
 4453: #        $resizeheight - height (pixels) to which to resize uploaded image
 4454: #        $mimetype - reference to scalar to accommodate mime type determined
 4455: #                    from File::MMagic.
 4456: # 
 4457: # output: url of file in userspace, or error: <message> 
 4458: #             or /adm/notfound.html if failure to upload occurse
 4459: 
 4460: sub userfileupload {
 4461:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4462:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4463:     if (!defined($subdir)) { $subdir='unknown'; }
 4464:     my $fname=$env{'form.'.$formname.'.filename'};
 4465:     $fname=&clean_filename($fname);
 4466:     # See if there is anything left
 4467:     unless ($fname) { return 'error: no uploaded file'; }
 4468:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4469:     if ($fname =~ /^\./) {
 4470:         my ($s,$usec) = &gettimeofday();
 4471:         while (length($usec) < 6) {
 4472:             $usec = '0'.$usec;
 4473:         }
 4474:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4475:     }
 4476:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4477:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4478:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4479:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4480:         my $now = time;
 4481:         my $filepath;
 4482:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4483:              $filepath = 'tmp/helprequests/'.$now;
 4484:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4485:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4486:                          '_'.$env{'user.domain'}.'/pending';
 4487:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4488:             my ($docuname,$docudom);
 4489:             if ($destudom =~ /^$match_domain$/) {
 4490:                 $docudom = $destudom;
 4491:             } else {
 4492:                 $docudom = $env{'user.domain'};
 4493:             }
 4494:             if ($destuname =~ /^$match_username$/) {
 4495:                 $docuname = $destuname;
 4496:             } else {
 4497:                 $docuname = $env{'user.name'};
 4498:             }
 4499:             if (exists($env{'form.group'})) {
 4500:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4501:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4502:             }
 4503:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4504:             if ($context eq 'canceloverwrite') {
 4505:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4506:                 if (-e  $tempfile) {
 4507:                     my @info = stat($tempfile);
 4508:                     if ($info[9] eq $env{'form.timestamp'}) {
 4509:                         unlink($tempfile);
 4510:                     }
 4511:                 }
 4512:                 return;
 4513:             }
 4514:         }
 4515:         # Create the directory if not present
 4516:         my @parts=split(/\//,$filepath);
 4517:         my $fullpath = $perlvar{'lonDaemons'};
 4518:         for (my $i=0;$i<@parts;$i++) {
 4519:             $fullpath .= '/'.$parts[$i];
 4520:             if ((-e $fullpath)!=1) {
 4521:                 mkdir($fullpath,0777);
 4522:             }
 4523:         }
 4524:         open(my $fh,'>',$fullpath.'/'.$fname);
 4525:         print $fh $env{'form.'.$formname};
 4526:         close($fh);
 4527:         if ($context eq 'existingfile') {
 4528:             my @info = stat($fullpath.'/'.$fname);
 4529:             return ($fullpath.'/'.$fname,$info[9]);
 4530:         } else {
 4531:             return $fullpath.'/'.$fname;
 4532:         }
 4533:     }
 4534:     if ($subdir eq 'scantron') {
 4535:         $fname = 'scantron_orig_'.$fname;
 4536:     } else {
 4537:         $fname="$subdir/$fname";
 4538:     }
 4539:     if ($context eq 'coursedoc') {
 4540: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4541: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4542:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4543:             return &finishuserfileupload($docuname,$docudom,
 4544: 					 $formname,$fname,$parser,$allfiles,
 4545: 					 $codebase,$thumbwidth,$thumbheight,
 4546:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4547:         } else {
 4548:             if ($env{'form.folder'}) {
 4549:                 $fname=$env{'form.folder'}.'/'.$fname;
 4550:             }
 4551:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4552: 				       $fname,$formname,$parser,
 4553: 				       $allfiles,$codebase,$mimetype);
 4554:         }
 4555:     } elsif (defined($destuname)) {
 4556:         my $docuname=$destuname;
 4557:         my $docudom=$destudom;
 4558: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4559: 				     $parser,$allfiles,$codebase,
 4560:                                      $thumbwidth,$thumbheight,
 4561:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4562:     } else {
 4563:         my $docuname=$env{'user.name'};
 4564:         my $docudom=$env{'user.domain'};
 4565:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4566:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4567:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4568:         }
 4569: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4570: 				     $parser,$allfiles,$codebase,
 4571:                                      $thumbwidth,$thumbheight,
 4572:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4573:     }
 4574: }
 4575: 
 4576: sub finishuserfileupload {
 4577:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4578:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4579:     my $path=$docudom.'/'.$docuname.'/';
 4580:     my $filepath=$perlvar{'lonDocRoot'};
 4581:   
 4582:     my ($fnamepath,$file,$fetchthumb);
 4583:     $file=$fname;
 4584:     if ($fname=~m|/|) {
 4585:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4586: 	$path.=$fnamepath.'/';
 4587:     }
 4588:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4589:     my $count;
 4590:     for ($count=4;$count<=$#parts;$count++) {
 4591:         $filepath.="/$parts[$count]";
 4592:         if ((-e $filepath)!=1) {
 4593: 	    mkdir($filepath,0777);
 4594:         }
 4595:     }
 4596: 
 4597: # Save the file
 4598:     {
 4599: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4600: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4601: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4602: 	    return '/adm/notfound.html';
 4603: 	}
 4604:         if ($context eq 'overwrite') {
 4605:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4606:             my $target = $filepath.'/'.$file;
 4607:             if (-e $source) {
 4608:                 my @info = stat($source);
 4609:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4610:                     unless (&File::Copy::move($source,$target)) {
 4611:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4612:                         return "Moving from $source failed";
 4613:                     }
 4614:                 } else {
 4615:                     return "Temporary file: $source had unexpected date/time for last modification";
 4616:                 }
 4617:             } else {
 4618:                 return "Temporary file: $source missing";
 4619:             }
 4620:         } elsif (!print FH ($env{'form.'.$formname})) {
 4621: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4622: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4623: 	    return '/adm/notfound.html';
 4624: 	}
 4625: 	close(FH);
 4626:         if ($resizewidth && $resizeheight) {
 4627:             my $mm = new File::MMagic;
 4628:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4629:             if ($mime_type =~ m{^image/}) {
 4630: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4631:             }  
 4632: 	}
 4633:     }
 4634:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4635:         if (ref($mimetype)) {
 4636:             if ($$mimetype eq '') {
 4637:                 my $mm = new File::MMagic;
 4638:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4639:                 $$mimetype = $type;
 4640:             }
 4641:         }
 4642:     }
 4643:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4644:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4645:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4646:                                                        $allfiles,$codebase);
 4647:             unless ($parse_result eq 'ok') {
 4648:                 &logthis('Failed to parse '.$filepath.$file.
 4649: 	   	         ' for embedded media: '.$parse_result); 
 4650:             }
 4651:         }
 4652:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4653:         my $format = $env{'form.scantron_format'};
 4654:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4655:     }
 4656:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4657:         my $input = $filepath.'/'.$file;
 4658:         my $output = $filepath.'/'.'tn-'.$file;
 4659:         my $makethumb; 
 4660:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4661:         if ($context eq 'toollogo') {
 4662:             my ($fullwidth,$fullheight) = &check_dimensions($input);
 4663:             if ($fullwidth ne '' && $fullheight ne '') {
 4664:                 if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 4665:                     $makethumb = 1;
 4666:                 }
 4667:             }
 4668:         } else {
 4669:             $makethumb = 1;
 4670:         }
 4671:         if ($makethumb) {
 4672:             my @args = ('convert','-sample',$thumbsize,$input,$output);
 4673:             system({$args[0]} @args);
 4674:             if (-e $filepath.'/'.'tn-'.$file) {
 4675:                 $fetchthumb  = 1; 
 4676:             }
 4677:         }
 4678:     }
 4679:  
 4680: # Notify homeserver to grep it
 4681: #
 4682:     my $docuhome=&homeserver($docuname,$docudom);	
 4683:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4684:     if ($fetchresult eq 'ok') {
 4685:         if ($fetchthumb) {
 4686:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4687:             if ($thumbresult ne 'ok') {
 4688:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4689:                          $docuhome.': '.$thumbresult);
 4690:             }
 4691:         }
 4692: #
 4693: # Return the URL to it
 4694:         return '/uploaded/'.$path.$file;
 4695:     } else {
 4696:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4697: 		 ': '.$fetchresult);
 4698:         return '/adm/notfound.html';
 4699:     }
 4700: }
 4701: 
 4702: sub extract_embedded_items {
 4703:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4704:     my @state = ();
 4705:     my (%lastids,%related,%shockwave,%flashvars);
 4706:     my %javafiles = (
 4707:                       codebase => '',
 4708:                       code => '',
 4709:                       archive => ''
 4710:                     );
 4711:     my %mediafiles = (
 4712:                       src => '',
 4713:                       movie => '',
 4714:                      );
 4715:     my $p;
 4716:     if ($content) {
 4717:         $p = HTML::LCParser->new($content);
 4718:     } else {
 4719:         $p = HTML::LCParser->new($fullpath);
 4720:     }
 4721:     while (my $t=$p->get_token()) {
 4722: 	if ($t->[0] eq 'S') {
 4723: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4724: 	    push(@state, $tagname);
 4725:             if (lc($tagname) eq 'allow') {
 4726:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4727:             }
 4728: 	    if (lc($tagname) eq 'img') {
 4729: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4730: 	    }
 4731: 	    if (lc($tagname) eq 'a') {
 4732:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4733:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4734:                 }
 4735: 	    }
 4736:             if (lc($tagname) eq 'script') {
 4737:                 my $src;
 4738:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4739:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4740:                 } else {
 4741:                     if ($attr->{'src'} ne '') {
 4742:                         $src = $attr->{'src'};
 4743:                         &add_filetype($allfiles,$src,'src');
 4744:                     }
 4745:                 }
 4746:                 my $text = $p->get_trimmed_text();
 4747:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4748:                     my @swfargs = split(/,/,$1);
 4749:                     foreach my $item (@swfargs) {
 4750:                         $item =~ s/["']//g;
 4751:                         $item =~ s/^\s+//;
 4752:                         $item =~ s/\s+$//;
 4753:                     }
 4754:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4755:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4756:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4757:                         } else {
 4758:                             $related{$swfargs[0]} = [$swfargs[2]];
 4759:                         }
 4760:                     }
 4761:                 }
 4762:             }
 4763:             if (lc($tagname) eq 'link') {
 4764:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4765:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4766:                 }
 4767:             }
 4768: 	    if (lc($tagname) eq 'object' ||
 4769: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4770: 		foreach my $item (keys(%javafiles)) {
 4771: 		    $javafiles{$item} = '';
 4772: 		}
 4773:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4774:                     $lastids{lc($tagname)} = $attr->{'id'};
 4775:                 }
 4776: 	    }
 4777: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4778: 		my $name = lc($attr->{'name'});
 4779: 		foreach my $item (keys(%javafiles)) {
 4780: 		    if ($name eq $item) {
 4781: 			$javafiles{$item} = $attr->{'value'};
 4782: 			last;
 4783: 		    }
 4784: 		}
 4785:                 my $pathfrom;
 4786: 		foreach my $item (keys(%mediafiles)) {
 4787: 		    if ($name eq $item) {
 4788:                         $pathfrom = $attr->{'value'};
 4789:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4790: 			&add_filetype($allfiles,$pathfrom,$name);
 4791: 			last;
 4792: 		    }
 4793: 		}
 4794:                 if ($name eq 'flashvars') {
 4795:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4796:                 }
 4797:                 if ($pathfrom ne '') {
 4798:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4799:                                          $pathfrom);
 4800:                 }
 4801: 	    }
 4802: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4803: 		foreach my $item (keys(%javafiles)) {
 4804: 		    if ($attr->{$item}) {
 4805: 			$javafiles{$item} = $attr->{$item};
 4806: 			last;
 4807: 		    }
 4808: 		}
 4809: 		foreach my $item (keys(%mediafiles)) {
 4810: 		    if ($attr->{$item}) {
 4811: 			&add_filetype($allfiles,$attr->{$item},$item);
 4812: 			last;
 4813: 		    }
 4814: 		}
 4815:                 if (lc($tagname) eq 'embed') {
 4816:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4817:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4818:                                              $attr->{'src'});
 4819:                     }
 4820:                 }
 4821: 	    }
 4822:             if (lc($tagname) eq 'iframe') {
 4823:                 my $src = $attr->{'src'} ;
 4824:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4825:                     &add_filetype($allfiles,$src,'src');
 4826:                 } elsif ($src =~ m{^/}) {
 4827:                     if ($env{'request.course.id'}) {
 4828:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4829:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4830:                         my $url = &hreflocation('',$fullpath);
 4831:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4832:                             my $relpath = $1;
 4833:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4834:                                 &add_filetype($allfiles,$1,'src');
 4835:                             }
 4836:                         }
 4837:                     }
 4838:                 }
 4839:             }
 4840:             if ($t->[4] =~ m{/>$}) {
 4841:                 pop(@state);
 4842:             }
 4843: 	} elsif ($t->[0] eq 'E') {
 4844: 	    my ($tagname) = ($t->[1]);
 4845: 	    if ($javafiles{'codebase'} ne '') {
 4846: 		$javafiles{'codebase'} .= '/';
 4847: 	    }  
 4848: 	    if (lc($tagname) eq 'applet' ||
 4849: 		lc($tagname) eq 'object' ||
 4850: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4851: 		) {
 4852: 		foreach my $item (keys(%javafiles)) {
 4853: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4854: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4855: 			&add_filetype($allfiles,$file,$item);
 4856: 		    }
 4857: 		}
 4858: 	    } 
 4859: 	    pop @state;
 4860: 	}
 4861:     }
 4862:     foreach my $id (sort(keys(%flashvars))) {
 4863:         if ($shockwave{$id} ne '') {
 4864:             my @pairs = split(/\&/,$flashvars{$id});
 4865:             foreach my $pair (@pairs) {
 4866:                 my ($key,$value) = split(/\=/,$pair);
 4867:                 if ($key eq 'thumb') {
 4868:                     &add_filetype($allfiles,$value,$key);
 4869:                 } elsif ($key eq 'content') {
 4870:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4871:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4872:                     if ($ext ne '') {
 4873:                         &add_filetype($allfiles,$path.$value,$ext);
 4874:                     }
 4875:                 }
 4876:             }
 4877:         }
 4878:     }
 4879:     return 'ok';
 4880: }
 4881: 
 4882: sub add_filetype {
 4883:     my ($allfiles,$file,$type)=@_;
 4884:     if (exists($allfiles->{$file})) {
 4885: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4886: 	    push(@{$allfiles->{$file}}, &escape($type));
 4887: 	}
 4888:     } else {
 4889: 	@{$allfiles->{$file}} = (&escape($type));
 4890:     }
 4891: }
 4892: 
 4893: sub embedded_dependency {
 4894:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4895:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4896:         if (($identifier ne '') &&
 4897:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4898:             ($pathfrom ne '')) {
 4899:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4900:             foreach my $dep (@{$related->{$identifier}}) {
 4901:                 &add_filetype($allfiles,$path.$dep,'object');
 4902:             }
 4903:         }
 4904:     }
 4905:     return;
 4906: }
 4907: 
 4908: sub check_dimensions {
 4909:     my ($inputfile) = @_;
 4910:     my ($fullwidth,$fullheight);
 4911:     if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
 4912:         my $mm = new File::MMagic;
 4913:         my $mime_type = $mm->checktype_filename($inputfile);
 4914:         if ($mime_type =~ m{^image/}) {
 4915:             if (open(PIPE,"identify $inputfile 2>&1 |")) {
 4916:                 my $imageinfo = <PIPE>;
 4917:                 if (!close(PIPE)) {
 4918:                     &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 4919:                 }
 4920:                 chomp($imageinfo);
 4921:                 my ($fullsize) =
 4922:                     ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 4923:                 if ($fullsize) {
 4924:                     ($fullwidth,$fullheight) = split(/x/,$fullsize);
 4925:                 }
 4926:             }
 4927:         }
 4928:     }
 4929:     return ($fullwidth,$fullheight);
 4930: }
 4931: 
 4932: sub bubblesheet_converter {
 4933:     my ($cdom,$fullpath,$config,$format) = @_;
 4934:     if ((&domain($cdom) ne '') &&
 4935:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4936:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4937:         my (%csvcols,%csvoptions);
 4938:         if (ref($config->{'fields'}) eq 'HASH') {  
 4939:             %csvcols = %{$config->{'fields'}};
 4940:         }
 4941:         if (ref($config->{'options'}) eq 'HASH') {
 4942:             %csvoptions = %{$config->{'options'}};
 4943:         }
 4944:         my %csvbynum = reverse(%csvcols);
 4945:         my %scantronconf = &get_scantron_config($format,$cdom);
 4946:         if (keys(%scantronconf)) {
 4947:             my %bynum = (
 4948:                           $scantronconf{CODEstart} => 'CODEstart',
 4949:                           $scantronconf{IDstart}   => 'IDstart',
 4950:                           $scantronconf{PaperID}   => 'PaperID',
 4951:                           $scantronconf{FirstName} => 'FirstName',
 4952:                           $scantronconf{LastName}  => 'LastName',
 4953:                           $scantronconf{Qstart}    => 'Qstart',
 4954:                         );
 4955:             my @ordered;
 4956:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4957:                 push(@ordered,$bynum{$item});
 4958:             }
 4959:             my %mapstart = (
 4960:                               CODEstart => 'CODE',
 4961:                               IDstart   => 'ID',
 4962:                               PaperID   => 'PaperID',
 4963:                               FirstName => 'FirstName',
 4964:                               LastName  => 'LastName',
 4965:                               Qstart    => 'FirstQuestion',
 4966:                            );
 4967:             my %maplength = (
 4968:                               CODEstart => 'CODElength',
 4969:                               IDstart   => 'IDlength',
 4970:                               PaperID   => 'PaperIDlength',
 4971:                               FirstName => 'FirstNamelength',
 4972:                               LastName  => 'LastNamelength',
 4973:             );
 4974:             if (open(my $fh,'<',$fullpath)) {
 4975:                 my $output;
 4976:                 my %lettdig = &letter_to_digits();
 4977:                 my %diglett = reverse(%lettdig);
 4978:                 my $numletts = scalar(keys(%lettdig));
 4979:                 my $num = 0;
 4980:                 while (my $line=<$fh>) {
 4981:                     $num ++;
 4982:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4983:                     $line =~ s{[\r\n]+$}{};
 4984:                     my %found;
 4985:                     my @values = split(/,/,$line,-1);
 4986:                     my ($qstart,$record);
 4987:                     for (my $i=0; $i<@values; $i++) {
 4988:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4989:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4990:                             if ($values[$i] eq '') {
 4991:                                 $values[$i] = $scantronconf{'Qoff'};
 4992:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4993:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4994:                                     $values[$i] = $lettdig{uc($values[$i])};
 4995:                                 }
 4996:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4997:                                 if ($values[$i] =~ /^[0-9]$/) {
 4998:                                     $values[$i] = $diglett{$values[$i]};
 4999:                                 }
 5000:                             } else {
 5001:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 5002:                                     my $digit;
 5003:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 5004:                                         $digit = $lettdig{uc($values[$i])}-1;
 5005:                                         if ($values[$i] eq 'J') {
 5006:                                             $digit += $numletts;
 5007:                                         }
 5008:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 5009:                                         $digit = $values[$i]-1;
 5010:                                         if ($values[$i] eq '0') {
 5011:                                             $digit += $numletts;
 5012:                                         }
 5013:                                     }
 5014:                                     my $qval='';
 5015:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 5016:                                         if ($j == $digit) {
 5017:                                             $qval .= $scantronconf{'Qon'};
 5018:                                         } else {
 5019:                                             $qval .= $scantronconf{'Qoff'};
 5020:                                         }
 5021:                                     }
 5022:                                     $values[$i] = $qval;
 5023:                                 }
 5024:                             }
 5025:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 5026:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 5027:                             }
 5028:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 5029:                             if ($numblank > 0) {
 5030:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 5031:                             }
 5032:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 5033:                                 $qstart = $i;
 5034:                                 $found{$csvbynum{$i}} = $values[$i];
 5035:                             } else {
 5036:                                 $found{'FirstQuestion'} .= $values[$i];
 5037:                             }
 5038:                         } elsif (exists($csvbynum{$i})) {
 5039:                             if ($csvoptions{'rem'}) {
 5040:                                 $values[$i] =~ s/^\s+//;
 5041:                             }
 5042:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 5043:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 5044:                                     $values[$i] = '0'.$values[$i];
 5045:                                 }
 5046:                             }
 5047:                             $found{$csvbynum{$i}} = $values[$i];
 5048:                         }
 5049:                     }
 5050:                     foreach my $item (@ordered) {
 5051:                         my $currlength = 1+length($record);
 5052:                         my $numspaces = $scantronconf{$item} - $currlength;
 5053:                         if ($numspaces > 0) {
 5054:                             $record .= (' ' x $numspaces);
 5055:                         }
 5056:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 5057:                             unless ($item eq 'Qstart') {
 5058:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 5059:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 5060:                                 }
 5061:                             }
 5062:                             $record .= $found{$mapstart{$item}};
 5063:                         }
 5064:                     }
 5065:                     $output .= "$record\n";
 5066:                 }
 5067:                 close($fh);
 5068:                 if ($output) {
 5069:                     if (open(my $fh,'>',$fullpath)) {
 5070:                         print $fh $output;
 5071:                         close($fh);
 5072:                     }
 5073:                 }
 5074:             }
 5075:         }
 5076:         return;
 5077:     }
 5078: }
 5079: 
 5080: sub letter_to_digits {
 5081:     my %lettdig = (
 5082:                     A => 1,
 5083:                     B => 2,
 5084:                     C => 3,
 5085:                     D => 4,
 5086:                     E => 5,
 5087:                     F => 6,
 5088:                     G => 7,
 5089:                     H => 8,
 5090:                     I => 9,
 5091:                     J => 0,
 5092:                   );
 5093:     return %lettdig;
 5094: }
 5095: 
 5096: sub get_scantron_config {
 5097:     my ($which,$cdom) = @_;
 5098:     my @lines = &get_scantronformat_file($cdom);
 5099:     my %config;
 5100:     #FIXME probably should move to XML it has already gotten a bit much now
 5101:     foreach my $line (@lines) {
 5102:         my ($name,$descrip)=split(/:/,$line);
 5103:         if ($name ne $which ) { next; }
 5104:         chomp($line);
 5105:         my @config=split(/:/,$line);
 5106:         $config{'name'}=$config[0];
 5107:         $config{'description'}=$config[1];
 5108:         $config{'CODElocation'}=$config[2];
 5109:         $config{'CODEstart'}=$config[3];
 5110:         $config{'CODElength'}=$config[4];
 5111:         $config{'IDstart'}=$config[5];
 5112:         $config{'IDlength'}=$config[6];
 5113:         $config{'Qstart'}=$config[7];
 5114:         $config{'Qlength'}=$config[8];
 5115:         $config{'Qoff'}=$config[9];
 5116:         $config{'Qon'}=$config[10];
 5117:         $config{'PaperID'}=$config[11];
 5118:         $config{'PaperIDlength'}=$config[12];
 5119:         $config{'FirstName'}=$config[13];
 5120:         $config{'FirstNamelength'}=$config[14];
 5121:         $config{'LastName'}=$config[15];
 5122:         $config{'LastNamelength'}=$config[16];
 5123:         $config{'BubblesPerRow'}=$config[17];
 5124:         last;
 5125:     }
 5126:     return %config;
 5127: }
 5128: 
 5129: sub get_scantronformat_file {
 5130:     my ($cdom) = @_;
 5131:     if ($cdom eq '') {
 5132:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5133:     }
 5134:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 5135:     my $gottab = 0;
 5136:     my @lines;
 5137:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 5138:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 5139:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 5140:             if ($formatfile ne '-1') {
 5141:                 @lines = split("\n",$formatfile,-1);
 5142:                 $gottab = 1;
 5143:             }
 5144:         }
 5145:     }
 5146:     if (!$gottab) {
 5147:         my $confname = $cdom.'-domainconfig';
 5148:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 5149:         my $formatfile = &getfile($default);
 5150:         if ($formatfile ne '-1') {
 5151:             @lines = split("\n",$formatfile,-1);
 5152:             $gottab = 1;
 5153:         }
 5154:     }
 5155:     if (!$gottab) {
 5156:         my @domains = &current_machine_domains();
 5157:         if (grep(/^\Q$cdom\E$/,@domains)) {
 5158:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 5159:                 @lines = <$fh>;
 5160:                 close($fh);
 5161:             }
 5162:         } else {
 5163:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 5164:                 @lines = <$fh>;
 5165:                 close($fh);
 5166:             }
 5167:         }
 5168:         chomp(@lines);
 5169:     }
 5170:     return @lines;
 5171: }
 5172: 
 5173: sub removeuploadedurl {
 5174:     my ($url)=@_;	
 5175:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 5176:     return &removeuserfile($uname,$udom,$fname);
 5177: }
 5178: 
 5179: sub removeuserfile {
 5180:     my ($docuname,$docudom,$fname)=@_;
 5181:     my $home=&homeserver($docuname,$docudom);    
 5182:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 5183:     if ($result eq 'ok') {	
 5184:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 5185:             my $metafile = $fname.'.meta';
 5186:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 5187: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 5188:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 5189:             my $sqlresult = 
 5190:                 &update_portfolio_table($docuname,$docudom,$file,
 5191:                                         'portfolio_metadata',$group,
 5192:                                         'delete');
 5193:         }
 5194:     }
 5195:     return $result;
 5196: }
 5197: 
 5198: sub mkdiruserfile {
 5199:     my ($docuname,$docudom,$dir)=@_;
 5200:     my $home=&homeserver($docuname,$docudom);
 5201:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 5202: }
 5203: 
 5204: sub renameuserfile {
 5205:     my ($docuname,$docudom,$old,$new)=@_;
 5206:     my $home=&homeserver($docuname,$docudom);
 5207:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 5208:                         &escape("$old").':'.&escape("$new"),$home);
 5209:     if ($result eq 'ok') {
 5210:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 5211:             my $oldmeta = $old.'.meta';
 5212:             my $newmeta = $new.'.meta';
 5213:             my $metaresult = 
 5214:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 5215: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 5216:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 5217:             my $sqlresult = 
 5218:                 &update_portfolio_table($docuname,$docudom,$file,
 5219:                                         'portfolio_metadata',$group,
 5220:                                         'delete');
 5221:         }
 5222:     }
 5223:     return $result;
 5224: }
 5225: 
 5226: # ------------------------------------------------------------------------- Log
 5227: 
 5228: sub log {
 5229:     my ($dom,$nam,$hom,$what)=@_;
 5230:     return critical("log:$dom:$nam:$what",$hom);
 5231: }
 5232: 
 5233: # ------------------------------------------------------------------ Course Log
 5234: #
 5235: # This routine flushes several buffers of non-mission-critical nature
 5236: #
 5237: 
 5238: sub flushcourselogs {
 5239:     &logthis('Flushing log buffers');
 5240: #
 5241: # course logs
 5242: # This is a log of all transactions in a course, which can be used
 5243: # for data mining purposes
 5244: #
 5245: # It also collects the courseid database, which lists last transaction
 5246: # times and course titles for all courseids
 5247: #
 5248:     my %courseidbuffer=();
 5249:     foreach my $crsid (keys(%courselogs)) {
 5250:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 5251: 		          &escape($courselogs{$crsid}),
 5252: 		          $coursehombuf{$crsid}) eq 'ok') {
 5253: 	    delete $courselogs{$crsid};
 5254:         } else {
 5255:             &logthis('Failed to flush log buffer for '.$crsid);
 5256:             if (length($courselogs{$crsid})>40000) {
 5257:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 5258:                         " exceeded maximum size, deleting.</font>");
 5259:                delete $courselogs{$crsid};
 5260:             }
 5261:         }
 5262:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 5263:             'description' => $coursedescrbuf{$crsid},
 5264:             'inst_code'    => $courseinstcodebuf{$crsid},
 5265:             'type'        => $coursetypebuf{$crsid},
 5266:             'owner'       => $courseownerbuf{$crsid},
 5267:         };
 5268:     }
 5269: #
 5270: # Write course id database (reverse lookup) to homeserver of courses 
 5271: # Is used in pickcourse
 5272: #
 5273:     foreach my $crs_home (keys(%courseidbuffer)) {
 5274:         my $response = &courseidput(&host_domain($crs_home),
 5275:                                     $courseidbuffer{$crs_home},
 5276:                                     $crs_home,'timeonly');
 5277:     }
 5278: #
 5279: # File accesses
 5280: # Writes to the dynamic metadata of resources to get hit counts, etc.
 5281: #
 5282:     foreach my $entry (keys(%accesshash)) {
 5283:         if ($entry =~ /___count$/) {
 5284:             my ($dom,$name);
 5285:             ($dom,$name,undef)=
 5286: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 5287:             if (! defined($dom) || $dom eq '' || 
 5288:                 ! defined($name) || $name eq '') {
 5289:                 my $cid = $env{'request.course.id'};
 5290: #
 5291: # FIXME 11/29/2021
 5292: # Typo in rev. 1.458 (2003/12/09)??
 5293: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
 5294: #
 5295: # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
 5296: # $dom and $name will always be null, so the &inc() call will default to storing this data
 5297: # in a nohist_accesscount.db file for the user rather than the course.
 5298: #
 5299: # That said there is a lot of noise in the data being stored.
 5300: # So counts for prtspool/  and adm/ etc. are recorded.
 5301: #
 5302: # A review of which items ending '___count' are written to %accesshash should likely be 
 5303: # made before deciding whether to set these to 'course.' instead of 'request.'
 5304: #
 5305: # Under the current scheme each user receives a nohist_accesscount.db file listing 
 5306: # accesses for things which are not published resources, regardless of course, and
 5307: # there is not a nohist_accesscount.db file in a course, which might log accesses from
 5308: # anyone in the course for things which are not published resources.
 5309: #
 5310: # For an author, nohist_accesscount.db ends up having records for other items
 5311: # mixed up with the legitimate access counts for the author's published resources.
 5312: #
 5313:                 $dom  = $env{'request.'.$cid.'.domain'};
 5314:                 $name = $env{'request.'.$cid.'.num'};
 5315:             }
 5316:             my $value = $accesshash{$entry};
 5317:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 5318:             my %temphash=($url => $value);
 5319:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 5320:             if ($result eq 'ok') {
 5321:                 delete $accesshash{$entry};
 5322:             }
 5323:         } else {
 5324:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 5325:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 5326:             my %temphash=($entry => $accesshash{$entry});
 5327:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 5328:                 delete $accesshash{$entry};
 5329:             }
 5330:         }
 5331:     }
 5332: #
 5333: # Roles
 5334: # Reverse lookup of user roles for course faculty/staff and co-authorship
 5335: #
 5336:     foreach my $entry (keys(%userrolehash)) {
 5337:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 5338: 	    split(/\:/,$entry);
 5339:         if (&put('nohist_userroles',
 5340:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 5341:                 $rudom,$runame) eq 'ok') {
 5342: 	    delete $userrolehash{$entry};
 5343:         }
 5344:     }
 5345: #
 5346: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 5347: #
 5348:     my %domrolebuffer = ();
 5349:     foreach my $entry (keys(%domainrolehash)) {
 5350:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 5351:         if ($domrolebuffer{$rudom}) {
 5352:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5353:                       '='.&escape($domainrolehash{$entry});
 5354:         } else {
 5355:             $domrolebuffer{$rudom}.=&escape($entry).
 5356:                       '='.&escape($domainrolehash{$entry});
 5357:         }
 5358:         delete $domainrolehash{$entry};
 5359:     }
 5360:     foreach my $dom (keys(%domrolebuffer)) {
 5361: 	my %servers;
 5362: 	if (defined(&domain($dom,'primary'))) {
 5363: 	    my $primary=&domain($dom,'primary');
 5364: 	    my $hostname=&hostname($primary);
 5365: 	    $servers{$primary} = $hostname;
 5366: 	} else { 
 5367: 	    %servers = &get_servers($dom,'library');
 5368: 	}
 5369: 	foreach my $tryserver (keys(%servers)) {
 5370: 	    if (&reply('domroleput:'.$dom.':'.
 5371: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5372: 		last;
 5373: 	    } else {  
 5374: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5375: 	    }
 5376:         }
 5377:     }
 5378:     $dumpcount++;
 5379: }
 5380: 
 5381: sub courselog {
 5382:     my $what=shift;
 5383:     $what=time.':'.$what;
 5384:     unless ($env{'request.course.id'}) { return ''; }
 5385:     $coursedombuf{$env{'request.course.id'}}=
 5386:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5387:     $coursenumbuf{$env{'request.course.id'}}=
 5388:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5389:     $coursehombuf{$env{'request.course.id'}}=
 5390:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5391:     $coursedescrbuf{$env{'request.course.id'}}=
 5392:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5393:     $courseinstcodebuf{$env{'request.course.id'}}=
 5394:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5395:     $courseownerbuf{$env{'request.course.id'}}=
 5396:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5397:     $coursetypebuf{$env{'request.course.id'}}=
 5398:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5399:     if (defined $courselogs{$env{'request.course.id'}}) {
 5400: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5401:     } else {
 5402: 	$courselogs{$env{'request.course.id'}}.=$what;
 5403:     }
 5404:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5405: 	&flushcourselogs();
 5406:     }
 5407: }
 5408: 
 5409: sub courseacclog {
 5410:     my $fnsymb=shift;
 5411:     unless ($env{'request.course.id'}) { return ''; }
 5412:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5413:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5414:         $what.=':POST';
 5415:         # FIXME: Probably ought to escape things....
 5416: 	foreach my $key (keys(%env)) {
 5417:             if ($key=~/^form\.(.*)/) {
 5418:                 my $formitem = $1;
 5419:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5420:                     $what.=':'.$formitem.'='.$env{$key};
 5421:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5422:                     if ($formitem eq 'proctorpassword') {
 5423:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5424:                     } else {
 5425:                         $what.=':'.$formitem.'='.$env{$key};
 5426:                     }
 5427:                 }
 5428:             }
 5429:         }
 5430:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5431:         # FIXME: We should not be depending on a form parameter that someone
 5432:         # editing lonsearchcat.pm might change in the future.
 5433:         if ($env{'form.phase'} eq 'course_search') {
 5434:             $what.= ':POST';
 5435:             # FIXME: Probably ought to escape things....
 5436:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5437:                                  'crsdiscuss') {
 5438:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5439:             }
 5440:         }
 5441:     }
 5442:     &courselog($what);
 5443: }
 5444: 
 5445: sub countacc {
 5446:     my $url=&declutter(shift);
 5447:     return if (! defined($url) || $url eq '');
 5448:     unless ($env{'request.course.id'}) { return ''; }
 5449: #
 5450: # Mark that this url was used in this course
 5451: #
 5452:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5453: #
 5454: # Increase the access count for this resource in this child process
 5455: #
 5456:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5457:     $accesshash{$key}++;
 5458: }
 5459: 
 5460: sub linklog {
 5461:     my ($from,$to)=@_;
 5462:     $from=&declutter($from);
 5463:     $to=&declutter($to);
 5464:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5465:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5466: }
 5467: 
 5468: sub statslog {
 5469:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5470:     if ($users<2) { return; }
 5471:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5472:             'course'       => $env{'request.course.id'},
 5473:             'sections'     => '"all"',
 5474:             'num_students' => $users,
 5475:             'part'         => $part,
 5476:             'symb'         => $symb,
 5477:             'mean_tries'   => $av_attempts,
 5478:             'deg_of_diff'  => $degdiff});
 5479:     foreach my $key (keys(%dynstore)) {
 5480:         $accesshash{$key}=$dynstore{$key};
 5481:     }
 5482: }
 5483:   
 5484: sub userrolelog {
 5485:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5486:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5487:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5488:        $userrolehash
 5489:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5490:                     =$tend.':'.$tstart;
 5491:     }
 5492:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5493:        $userrolehash
 5494:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5495:                     =$tend.':'.$tstart;
 5496:     }
 5497:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5498:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5499:        $domainrolehash
 5500:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5501:                     = $tend.':'.$tstart;
 5502:     }
 5503: }
 5504: 
 5505: sub courserolelog {
 5506:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,
 5507:         $context,$othdomby,$requester)=@_;
 5508:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5509:         my $cdom = $1;
 5510:         my $cnum = $2;
 5511:         my $sec = $3;
 5512:         my $namespace = 'rolelog';
 5513:         my %storehash = (
 5514:                            role    => $trole,
 5515:                            start   => $tstart,
 5516:                            end     => $tend,
 5517:                            selfenroll => $selfenroll,
 5518:                            context    => $context,
 5519:                         );
 5520:         if ($othdomby) {
 5521:             if ($othdomby eq 'othdombydc') {
 5522:                 $storehash{'approval'} = 'domain';
 5523:             } elsif ($othdomby eq 'othdombyuser') {
 5524:                 $storehash{'approval'} = 'user'; 
 5525:             }
 5526:             if ($requester ne '') {
 5527:                 $storehash{'requester'} = $requester;
 5528:             }
 5529:         }
 5530:         if ($trole eq 'gr') {
 5531:             $namespace = 'groupslog';
 5532:             $storehash{'group'} = $sec;
 5533:         } else {
 5534:             $storehash{'section'} = $sec;
 5535:             my ($curruserdomstr,$newuserdomstr);
 5536:             if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'})) {
 5537:                 $curruserdomstr = $env{'course.'.$env{'request.course.id'}.'.internal.userdomains'};
 5538:             } else {
 5539:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
 5540:                 $curruserdomstr = $courseinfo{'internal.userdomains'};
 5541:             }
 5542:             if ($curruserdomstr ne '') {
 5543:                 my @udoms = split(/,/,$curruserdomstr);
 5544:                 unless (grep(/^\Q$domain\E/,@udoms)) {
 5545:                     push(@udoms,$domain);
 5546:                     $newuserdomstr = join(',',sort(@udoms));
 5547:                 }
 5548:             } else {
 5549:                 $newuserdomstr = $domain;
 5550:             }
 5551:             if ($newuserdomstr ne '') {
 5552:                 my $putresult = &put('environment',{ 'internal.userdomains' => $newuserdomstr },
 5553:                                      $cdom,$cnum);
 5554:                 if ($putresult eq 'ok') {
 5555:                     unless (($selfenroll) || ($context eq 'selfenroll')) { 
 5556:                         if (($context eq 'createcourse') || ($context eq 'requestcourses') ||
 5557:                             ($context eq 'automated') || ($context eq 'domain')) {
 5558:                             $env{'course.'.$cdom.'_'.$cnum.'.internal.userdomains'} = $newuserdomstr;
 5559:                         } elsif ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
 5560:                             &appenv({'course.'.$cdom.'_'.$cnum.'.internal.userdomains' => $newuserdomstr});
 5561:                         }
 5562:                     }
 5563:                 }
 5564:             }
 5565:         }
 5566:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5567:                    $domain,$cnum,$cdom);
 5568:         if (($trole ne 'st') || ($sec ne '')) {
 5569:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5570:         }
 5571:     }
 5572:     return;
 5573: }
 5574: 
 5575: sub domainrolelog {
 5576:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,
 5577:         $context,$othdomby,$requester)=@_;
 5578:     if ($area =~ m{^/($match_domain)/$}) {
 5579:         my $cdom = $1;
 5580:         my $domconfiguser = &get_domainconfiguser($cdom);
 5581:         my $namespace = 'rolelog';
 5582:         my %storehash = (
 5583:                            role    => $trole,
 5584:                            start   => $tstart,
 5585:                            end     => $tend,
 5586:                            context => $context,
 5587:                         );
 5588:         if ($othdomby) {
 5589:             if ($othdomby eq 'othdombydc') {
 5590:                 $storehash{'approval'} = 'domain';
 5591:             } elsif ($othdomby eq 'othdombyuser') {
 5592:                 $storehash{'approval'} = 'user';
 5593:             }
 5594:             if ($requester ne '') {
 5595:                 $storehash{'requester'} = $requester;
 5596:             }
 5597:         }
 5598:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5599:                    $domain,$domconfiguser,$cdom);
 5600:     }
 5601:     return;
 5602: 
 5603: }
 5604: 
 5605: sub coauthorrolelog {
 5606:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,
 5607:         $context,$othdomby,$requester)=@_;
 5608:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5609:         my $audom = $1;
 5610:         my $auname = $2;
 5611:         my $namespace = 'rolelog';
 5612:         my %storehash = (
 5613:                            role    => $trole,
 5614:                            start   => $tstart,
 5615:                            end     => $tend,
 5616:                            context => $context,
 5617:                         );
 5618:         if ($othdomby) {
 5619:             if ($othdomby eq 'othdombydc') {
 5620:                 $storehash{'approval'} = 'domain';
 5621:             } elsif ($othdomby eq 'othdombyuser') {
 5622:                 $storehash{'approval'} = 'user';
 5623:             }
 5624:             if ($requester ne '') {
 5625:                 $storehash{'requester'} = $requester;
 5626:             }
 5627:         }
 5628:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5629:                    $domain,$auname,$audom);
 5630:     }
 5631:     return;
 5632: }
 5633: 
 5634: sub authorarchivelog {
 5635:     my ($hashref,$size,$filesdest,$action) = @_;
 5636:     my $lonprtdir = $Apache::lonnet::perlvar{'lonPrtDir'};
 5637:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 5638:     $filesdest =~ s{^\Q$lonprtdir/\E}{};
 5639:     if ($filesdest =~ m{^($match_username)_($match_domain)_archive_(\d+_\d+_\d+(|[.\w]+))$}) {
 5640:         my ($auname,$audom,$id) = ($1,$2,$3);
 5641:         if (ref($hashref) eq 'HASH') {
 5642:             my $namespace = 'archivelog';
 5643:             my $dir;
 5644:             if ($hashref->{dir} =~ m{^\Q$londocroot/priv/$audom/$auname\E(.*)$}) {
 5645:                 $dir = $1;
 5646:             }
 5647:             my $delflag = 0;
 5648:             my %storehash = (
 5649:                               id      => $id,
 5650:                               dir     => $dir,
 5651:                               files   => $hashref->{numfiles},
 5652:                               subdirs => $hashref->{numdirs},
 5653:                               bytes   => $hashref->{bytes},
 5654:                               size    => $size,
 5655:                               action  => $action,
 5656:                             );
 5657:             if ($action eq 'delete') {
 5658:                 $delflag = 1;
 5659:             }
 5660:             &write_log('author',$namespace,\%storehash,$delflag,$auname,
 5661:                        $audom,$auname,$audom);
 5662:         }
 5663:     }
 5664:     return;
 5665: }
 5666: 
 5667: sub get_course_adv_roles {
 5668:     my ($cid,$codes) = @_;
 5669:     $cid=$env{'request.course.id'} unless (defined($cid));
 5670:     my %coursehash=&coursedescription($cid);
 5671:     my $crstype = &Apache::loncommon::course_type($cid);
 5672:     my %nothide=();
 5673:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5674:         if ($user !~ /:/) {
 5675: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5676:         } else {
 5677:             $nothide{$user}=1;
 5678:         }
 5679:     }
 5680:     my @possdoms = ($coursehash{'domain'});
 5681:     if ($coursehash{'checkforpriv'}) {
 5682:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5683:     }
 5684:     my %returnhash=();
 5685:     my %dumphash=
 5686:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5687:     my $now=time;
 5688:     my %privileged;
 5689:     foreach my $entry (keys(%dumphash)) {
 5690: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5691:         if (($tstart) && ($tstart<0)) { next; }
 5692:         if (($tend) && ($tend<$now)) { next; }
 5693:         if (($tstart) && ($now<$tstart)) { next; }
 5694:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5695: 	if ($username eq '' || $domain eq '') { next; }
 5696:         if ((&privileged($username,$domain,\@possdoms)) &&
 5697:             (!$nothide{$username.':'.$domain})) { next; }
 5698: 	if ($role eq 'cr') { next; }
 5699:         if ($codes) {
 5700:             if ($section) { $role .= ':'.$section; }
 5701:             if ($returnhash{$role}) {
 5702:                 $returnhash{$role}.=','.$username.':'.$domain;
 5703:             } else {
 5704:                 $returnhash{$role}=$username.':'.$domain;
 5705:             }
 5706:         } else {
 5707:             my $key=&plaintext($role,$crstype);
 5708:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5709:             if ($returnhash{$key}) {
 5710: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5711:             } else {
 5712:                 $returnhash{$key}=$username.':'.$domain;
 5713:             }
 5714:         }
 5715:     }
 5716:     return %returnhash;
 5717: }
 5718: 
 5719: sub get_my_roles {
 5720:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5721:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5722:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5723:     my (%dumphash,%nothide);
 5724:     if ($context eq 'userroles') {
 5725:         %dumphash = &dump('roles',$udom,$uname);
 5726:     } else {
 5727:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5728:         if ($hidepriv) {
 5729:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5730:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5731:                 if ($user !~ /:/) {
 5732:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5733:                 } else {
 5734:                     $nothide{$user} = 1;
 5735:                 }
 5736:             }
 5737:         }
 5738:     }
 5739:     my %returnhash=();
 5740:     my $now=time;
 5741:     my %privileged;
 5742:     foreach my $entry (keys(%dumphash)) {
 5743:         my ($role,$tend,$tstart);
 5744:         if ($context eq 'userroles') {
 5745:             next if ($entry =~ /^rolesdef/);
 5746: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5747:         } else {
 5748:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5749:         }
 5750:         if (($tstart) && ($tstart<0)) { next; }
 5751:         my $status = 'active';
 5752:         if (($tend) && ($tend<=$now)) {
 5753:             $status = 'previous';
 5754:         } 
 5755:         if (($tstart) && ($now<$tstart)) {
 5756:             $status = 'future';
 5757:         }
 5758:         if (ref($types) eq 'ARRAY') {
 5759:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5760:                 next;
 5761:             } 
 5762:         } else {
 5763:             if ($status ne 'active') {
 5764:                 next;
 5765:             }
 5766:         }
 5767:         my ($rolecode,$username,$domain,$section,$area);
 5768:         if ($context eq 'userroles') {
 5769:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5770:             (undef,$domain,$username,$section) = split(/\//,$area);
 5771:         } else {
 5772:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5773:         }
 5774:         if (ref($roledoms) eq 'ARRAY') {
 5775:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5776:                 next;
 5777:             }
 5778:         }
 5779:         if (ref($roles) eq 'ARRAY') {
 5780:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5781:                 if ($role =~ /^cr\//) {
 5782:                     if (!grep(/^cr$/,@{$roles})) {
 5783:                         next;
 5784:                     }
 5785:                 } elsif ($role =~ /^gr\//) {
 5786:                     if (!grep(/^gr$/,@{$roles})) {
 5787:                         next;
 5788:                     }
 5789:                 } else {
 5790:                     next;
 5791:                 }
 5792:             }
 5793:         }
 5794:         if ($hidepriv) {
 5795:             my @privroles = ('dc','su');
 5796:             if ($context eq 'userroles') {
 5797:                 next if (grep(/^\Q$role\E$/,@privroles));
 5798:             } else {
 5799:                 my $possdoms = [$domain];
 5800:                 if (ref($roledoms) eq 'ARRAY') {
 5801:                    push(@{$possdoms},@{$roledoms}); 
 5802:                 }
 5803:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5804:                     if (!$nothide{$username.':'.$domain}) {
 5805:                         next;
 5806:                     }
 5807:                 }
 5808:             }
 5809:         }
 5810:         if ($withsec) {
 5811:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5812:                 $tstart.':'.$tend;
 5813:         } else {
 5814:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5815:         }
 5816:     }
 5817:     return %returnhash;
 5818: }
 5819: 
 5820: sub get_all_adhocroles {
 5821:     my ($dom) = @_;
 5822:     my @roles_by_num = ();
 5823:     my %domdefaults = &get_domain_defaults($dom);
 5824:     my (%description,%access_in_dom,%access_info);
 5825:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5826:         my $count = 0;
 5827:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5828:         my %ordered;
 5829:         foreach my $role (sort(keys(%domcurrent))) {
 5830:             my ($order,$desc,$access_in_dom);
 5831:             if (ref($domcurrent{$role}) eq 'HASH') {
 5832:                 $order = $domcurrent{$role}{'order'};
 5833:                 $desc = $domcurrent{$role}{'desc'};
 5834:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5835:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5836:             }
 5837:             if ($order eq '') {
 5838:                 $order = $count;
 5839:             }
 5840:             $ordered{$order} = $role;
 5841:             if ($desc ne '') {
 5842:                 $description{$role} = $desc;
 5843:             } else {
 5844:                 $description{$role}= $role;
 5845:             }
 5846:             $count++;
 5847:         }
 5848:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5849:             push(@roles_by_num,$ordered{$item});
 5850:         }
 5851:     }
 5852:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5853: }
 5854: 
 5855: sub get_my_adhocroles {
 5856:     my ($cid,$checkreg) = @_;
 5857:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5858:     if ($env{'request.course.id'} eq $cid) {
 5859:         $cdom = $env{'course.'.$cid.'.domain'};
 5860:         $cnum = $env{'course.'.$cid.'.num'};
 5861:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5862:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5863:         $cdom = $1;
 5864:         $cnum = $2;
 5865:         %info = &get('environment',['internal.coursecode'],
 5866:                      $cdom,$cnum);
 5867:     }
 5868:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5869:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5870:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5871:         if ($rosterhash{$user} ne '') {
 5872:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5873:             return ([],{}) if ($type eq 'auto');
 5874:         }
 5875:     }
 5876:     if (($cdom ne '') && ($cnum ne ''))  {
 5877:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5878:             my $then=$env{'user.login.time'};
 5879:             my $update=$env{'user.update.time'};
 5880:             if (!$update) {
 5881:                 $update = $then;
 5882:             }
 5883:             my @liveroles;
 5884:             foreach my $role ('dh','da') {
 5885:                 if ($env{"user.role.$role./$cdom/"}) {
 5886:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5887:                     my $limit = $update;
 5888:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5889:                         $limit = $then;
 5890:                     }
 5891:                     my $activerole = 1;
 5892:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5893:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5894:                     if ($activerole) {
 5895:                         push(@liveroles,$role);
 5896:                     }
 5897:                 }
 5898:             }
 5899:             if (@liveroles) {
 5900:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5901:                     my ($accessref,$accessinfo,%access_in_dom);
 5902:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5903:                     if (ref($roles_by_num) eq 'ARRAY') {
 5904:                         if (@{$roles_by_num}) {
 5905:                             my %settings;
 5906:                             if ($env{'request.course.id'} eq $cid) {
 5907:                                 foreach my $envkey (keys(%env)) {
 5908:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5909:                                         $settings{$1} = $env{$envkey};
 5910:                                     }
 5911:                                 }
 5912:                             } else {
 5913:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5914:                             }
 5915:                             my %setincrs;
 5916:                             if ($settings{'internal.adhocaccess'}) {
 5917:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5918:                             }
 5919:                             my @statuses;
 5920:                             if ($env{'environment.inststatus'}) {
 5921:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5922:                             }
 5923:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5924:                             if (ref($accessref) eq 'HASH') {
 5925:                                 %access_in_dom = %{$accessref};
 5926:                             }
 5927:                             foreach my $role (@{$roles_by_num}) {
 5928:                                 my ($curraccess,@okstatus,@personnel);
 5929:                                 if ($setincrs{$role}) {
 5930:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5931:                                     if ($curraccess eq 'status') {
 5932:                                         @okstatus = split(/\&/,$rest);
 5933:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5934:                                         @personnel = split(/\&/,$rest);
 5935:                                     }
 5936:                                 } else {
 5937:                                     $curraccess = $access_in_dom{$role};
 5938:                                     if (ref($accessinfo) eq 'HASH') {
 5939:                                         if ($curraccess eq 'status') {
 5940:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5941:                                                 @okstatus = @{$accessinfo->{$role}};
 5942:                                             }
 5943:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5944:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5945:                                                 @personnel = @{$accessinfo->{$role}};
 5946:                                             }
 5947:                                         }
 5948:                                     }
 5949:                                 }
 5950:                                 if ($curraccess eq 'none') {
 5951:                                     next;
 5952:                                 } elsif ($curraccess eq 'all') {
 5953:                                     push(@possroles,$role);
 5954:                                 } elsif ($curraccess eq 'dh') {
 5955:                                     if (grep(/^dh$/,@liveroles)) {
 5956:                                         push(@possroles,$role);
 5957:                                     } else {
 5958:                                         next;
 5959:                                     }
 5960:                                 } elsif ($curraccess eq 'da') {
 5961:                                     if (grep(/^da$/,@liveroles)) {
 5962:                                         push(@possroles,$role);
 5963:                                     } else {
 5964:                                         next;
 5965:                                     }
 5966:                                 } elsif ($curraccess eq 'status') {
 5967:                                     if (@okstatus) {
 5968:                                         if (!@statuses) {
 5969:                                             if (grep(/^default$/,@okstatus)) {
 5970:                                                 push(@possroles,$role);
 5971:                                             }
 5972:                                         } else {
 5973:                                             foreach my $status (@okstatus) {
 5974:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5975:                                                     push(@possroles,$role);
 5976:                                                     last;
 5977:                                                 }
 5978:                                             }
 5979:                                         }
 5980:                                     }
 5981:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5982:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5983:                                         if ($curraccess eq 'exc') {
 5984:                                             push(@possroles,$role);
 5985:                                         }
 5986:                                     } elsif ($curraccess eq 'inc') {
 5987:                                         push(@possroles,$role);
 5988:                                     }
 5989:                                 }
 5990:                             }
 5991:                         }
 5992:                     }
 5993:                 }
 5994:             }
 5995:         }
 5996:     }
 5997:     unless (ref($description) eq 'HASH') {
 5998:         if (ref($roles_by_num) eq 'ARRAY') {
 5999:             my %desc;
 6000:             map { $desc{$_} = $_; } (@{$roles_by_num});
 6001:             $description = \%desc;
 6002:         } else {
 6003:             $description = {};
 6004:         }
 6005:     }
 6006:     return (\@possroles,$description);
 6007: }
 6008: 
 6009: # ----------------------------------------------------- Frontpage Announcements
 6010: #
 6011: #
 6012: 
 6013: sub postannounce {
 6014:     my ($server,$text)=@_;
 6015:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 6016:     unless ($text=~/\w/) { $text=''; }
 6017:     return &reply('setannounce:'.&escape($text),$server);
 6018: }
 6019: 
 6020: sub getannounce {
 6021: 
 6022:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 6023: 	my $announcement='';
 6024: 	while (my $line = <$fh>) { $announcement .= $line; }
 6025: 	close($fh);
 6026: 	if ($announcement=~/\w/) { 
 6027: 	    return 
 6028:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 6029:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 6030: 	} else {
 6031: 	    return '';
 6032: 	}
 6033:     } else {
 6034: 	return '';
 6035:     }
 6036: }
 6037: 
 6038: # ---------------------------------------------------------- Course ID routines
 6039: # Deal with domain's nohist_courseid.db files
 6040: #
 6041: 
 6042: sub courseidput {
 6043:     my ($domain,$storehash,$coursehome,$caller) = @_;
 6044:     return unless (ref($storehash) eq 'HASH');
 6045:     my $outcome;
 6046:     if ($caller eq 'timeonly') {
 6047:         my $cids = '';
 6048:         foreach my $item (keys(%$storehash)) {
 6049:             $cids.=&escape($item).'&';
 6050:         }
 6051:         $cids=~s/\&$//;
 6052:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 6053:                           $coursehome);       
 6054:     } else {
 6055:         my $items = '';
 6056:         foreach my $item (keys(%$storehash)) {
 6057:             $items.= &escape($item).'='.
 6058:                      &freeze_escape($$storehash{$item}).'&';
 6059:         }
 6060:         $items=~s/\&$//;
 6061:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 6062:                           $coursehome);
 6063:     }
 6064:     if ($outcome eq 'unknown_cmd') {
 6065:         my $what;
 6066:         foreach my $cid (keys(%$storehash)) {
 6067:             $what .= &escape($cid).'=';
 6068:             foreach my $item ('description','inst_code','owner','type') {
 6069:                 $what .= &escape($storehash->{$cid}{$item}).':';
 6070:             }
 6071:             $what =~ s/\:$/&/;
 6072:         }
 6073:         $what =~ s/\&$//;  
 6074:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 6075:     } else {
 6076:         return $outcome;
 6077:     }
 6078: }
 6079: 
 6080: sub courseiddump {
 6081:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 6082:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 6083:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 6084:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 6085:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 6086:     my $as_hash = 1;
 6087:     my %returnhash;
 6088:     if (!$domfilter) { $domfilter=''; }
 6089:     my %libserv = &all_library();
 6090:     foreach my $tryserver (keys(%libserv)) {
 6091:         if ( (  $hostidflag == 1 
 6092: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 6093: 	     || (!defined($hostidflag)) ) {
 6094: 
 6095: 	    if (($domfilter eq '') ||
 6096: 		(&host_domain($tryserver) eq $domfilter)) {
 6097:                 my $rep;
 6098:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 6099:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 6100:                         join(":", (&host_domain($tryserver), $sincefilter, 
 6101:                                 &escape($descfilter), &escape($instcodefilter), 
 6102:                                 &escape($ownerfilter), &escape($coursefilter),
 6103:                                 &escape($typefilter), &escape($regexp_ok), 
 6104:                                 $as_hash, &escape($selfenrollonly), 
 6105:                                 &escape($catfilter), $showhidden, $caller, 
 6106:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 6107:                                 &escape($createdbefore), &escape($createdafter), 
 6108:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 6109:                                 $reqcrsdom,&escape($reqinstcode))));
 6110:                 } else {
 6111:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 6112:                              $sincefilter.':'.&escape($descfilter).':'.
 6113:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 6114:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 6115:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 6116:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 6117:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 6118:                              &escape($cc_clone).':'.$cloneonly.':'.
 6119:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 6120:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 6121:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 6122:                 }
 6123:                      
 6124:                 my @pairs=split(/\&/,$rep);
 6125:                 foreach my $item (@pairs) {
 6126:                     my ($key,$value)=split(/\=/,$item,2);
 6127:                     $key = &unescape($key);
 6128:                     next if ($key =~ /^error: 2 /);
 6129:                     my $result = &thaw_unescape($value);
 6130:                     if (ref($result) eq 'HASH') {
 6131:                         $returnhash{$key}=$result;
 6132:                     } else {
 6133:                         my @responses = split(/:/,$value);
 6134:                         my @items = ('description','inst_code','owner','type');
 6135:                         for (my $i=0; $i<@responses; $i++) {
 6136:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 6137:                         }
 6138:                     }
 6139:                 }
 6140:             }
 6141:         }
 6142:     }
 6143:     return %returnhash;
 6144: }
 6145: 
 6146: sub courselastaccess {
 6147:     my ($cdom,$cnum,$hostidref) = @_;
 6148:     my %returnhash;
 6149:     if ($cdom && $cnum) {
 6150:         my $chome = &homeserver($cnum,$cdom);
 6151:         if ($chome ne 'no_host') {
 6152:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 6153:             &extract_lastaccess(\%returnhash,$rep);
 6154:         }
 6155:     } else {
 6156:         if (!$cdom) { $cdom=''; }
 6157:         my %libserv = &all_library();
 6158:         foreach my $tryserver (keys(%libserv)) {
 6159:             if (ref($hostidref) eq 'ARRAY') {
 6160:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 6161:             } 
 6162:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 6163:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 6164:                 &extract_lastaccess(\%returnhash,$rep);
 6165:             }
 6166:         }
 6167:     }
 6168:     return %returnhash;
 6169: }
 6170: 
 6171: sub extract_lastaccess {
 6172:     my ($returnhash,$rep) = @_;
 6173:     if (ref($returnhash) eq 'HASH') {
 6174:         unless ($rep eq 'unknown_cmd' || $rep eq 'no_such_host' || 
 6175:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 6176:                  $rep eq '') {
 6177:             my @pairs=split(/\&/,$rep);
 6178:             foreach my $item (@pairs) {
 6179:                 my ($key,$value)=split(/\=/,$item,2);
 6180:                 $key = &unescape($key);
 6181:                 next if ($key =~ /^error: 2 /);
 6182:                 $returnhash->{$key} = &thaw_unescape($value);
 6183:             }
 6184:         }
 6185:     }
 6186:     return;
 6187: }
 6188: 
 6189: # ---------------------------------------------------------- DC e-mail
 6190: 
 6191: sub dcmailput {
 6192:     my ($domain,$msgid,$message,$server)=@_;
 6193:     my $status = &critical(
 6194:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 6195:        &escape($message),$server);
 6196:     return $status;
 6197: }
 6198: 
 6199: sub dcmaildump {
 6200:     my ($dom,$startdate,$enddate,$senders) = @_;
 6201:     my %returnhash=();
 6202: 
 6203:     if (defined(&domain($dom,'primary'))) {
 6204:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 6205:                                                          &escape($enddate).':';
 6206: 	my @esc_senders=map { &escape($_)} @$senders;
 6207: 	$cmd.=&escape(join('&',@esc_senders));
 6208: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 6209:             my ($key,$value) = split(/\=/,$line,2);
 6210:             if (($key) && ($value)) {
 6211:                 $returnhash{&unescape($key)} = &unescape($value);
 6212:             }
 6213:         }
 6214:     }
 6215:     return %returnhash;
 6216: }
 6217: # ---------------------------------------------------------- Domain roles
 6218: 
 6219: sub get_domain_roles {
 6220:     my ($dom,$roles,$startdate,$enddate)=@_;
 6221:     if ((!defined($startdate)) || ($startdate eq '')) {
 6222:         $startdate = '.';
 6223:     }
 6224:     if ((!defined($enddate)) || ($enddate eq '')) {
 6225:         $enddate = '.';
 6226:     }
 6227:     my $rolelist;
 6228:     if (ref($roles) eq 'ARRAY') {
 6229:         $rolelist = join('&',@{$roles});
 6230:     }
 6231:     my %personnel = ();
 6232: 
 6233:     my %servers = &get_servers($dom,'library');
 6234:     foreach my $tryserver (keys(%servers)) {
 6235: 	%{$personnel{$tryserver}}=();
 6236: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 6237: 					    &escape($startdate).':'.
 6238: 					    &escape($enddate).':'.
 6239: 					    &escape($rolelist), $tryserver))) {
 6240: 	    my ($key,$value) = split(/\=/,$line,2);
 6241: 	    if (($key) && ($value)) {
 6242: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 6243: 	    }
 6244: 	}
 6245:     }
 6246:     return %personnel;
 6247: }
 6248: 
 6249: sub get_active_domroles {
 6250:     my ($dom,$roles) = @_;
 6251:     return () unless (ref($roles) eq 'ARRAY');
 6252:     my $now = time;
 6253:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 6254:     my %domroles;
 6255:     foreach my $server (keys(%dompersonnel)) {
 6256:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 6257:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 6258:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 6259:         }
 6260:     }
 6261:     return %domroles;
 6262: }
 6263: 
 6264: # ----------------------------------------------------------- Interval timing 
 6265: 
 6266: {
 6267: # Caches needed for speedup of navmaps
 6268: # We don't want to cache this for very long at all (5 seconds at most)
 6269: # 
 6270: # The user for whom we cache
 6271: my $cachedkey='';
 6272: # The cached times for this user
 6273: my %cachedtimes=();
 6274: # When this was last done
 6275: my $cachedtime='';
 6276: 
 6277: sub load_all_first_access {
 6278:     my ($uname,$udom,$ignorecache)=@_;
 6279:     if (($cachedkey eq $uname.':'.$udom) &&
 6280:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 6281:         (!$ignorecache)) {
 6282:         return;
 6283:     }
 6284:     $cachedtime=time;
 6285:     $cachedkey=$uname.':'.$udom;
 6286:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 6287: }
 6288: 
 6289: sub get_first_access {
 6290:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 6291:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 6292:     if ($argsymb) { $symb=$argsymb; }
 6293:     my ($map,$id,$res)=&decode_symb($symb);
 6294:     if ($argmap) { $map = $argmap; }
 6295:     if ($type eq 'course') {
 6296: 	$res='course';
 6297:     } elsif ($type eq 'map') {
 6298: 	$res=&symbread($map);
 6299:     } else {
 6300: 	$res=$symb;
 6301:     }
 6302:     &load_all_first_access($uname,$udom,$ignorecache);
 6303:     return $cachedtimes{"$courseid\0$res"};
 6304: }
 6305: 
 6306: sub set_first_access {
 6307:     my ($type,$interval)=@_;
 6308:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 6309:     my ($map,$id,$res)=&decode_symb($symb);
 6310:     if ($type eq 'course') {
 6311: 	$res='course';
 6312:     } elsif ($type eq 'map') {
 6313: 	$res=&symbread($map);
 6314:     } else {
 6315: 	$res=$symb;
 6316:     }
 6317:     $cachedkey='';
 6318:     my $firstaccess=&get_first_access($type,$symb,$map);
 6319:     if ($firstaccess) {
 6320:         &logthis("First access time already set ($firstaccess) when attempting ".
 6321:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 6322:                  "in $courseid");
 6323:         return 'already_set';
 6324:     } else {
 6325:         my $start = time;
 6326: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 6327:                           $udom,$uname);
 6328:         if ($putres eq 'ok') {
 6329:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 6330:                  $udom,$uname); 
 6331:             &appenv(
 6332:                      {
 6333:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 6334:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 6335:                      }
 6336:                   );
 6337:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 6338:                 $cachedtimes{"$courseid\0$res"} = $start;
 6339:             }
 6340:         } elsif ($putres ne 'refused') {
 6341:             &logthis("Result: $putres when attempting to set first access time ".
 6342:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 6343:         }
 6344:         return $putres;
 6345:     }
 6346:     return 'already_set';
 6347: }
 6348: }
 6349: 
 6350: # --------------------------------------------- Set Expire Date for Spreadsheet
 6351: 
 6352: sub expirespread {
 6353:     my ($uname,$udom,$stype,$usymb)=@_;
 6354:     my $cid=$env{'request.course.id'}; 
 6355:     if ($cid) {
 6356:        my $now=time;
 6357:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 6358:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 6359:                             $env{'course.'.$cid.'.num'}.
 6360: 	        	    ':nohist_expirationdates:'.
 6361:                             &escape($key).'='.$now,
 6362:                             $env{'course.'.$cid.'.home'})
 6363:     }
 6364:     return 'ok';
 6365: }
 6366: 
 6367: # ----------------------------------------------------- Devalidate Spreadsheets
 6368: 
 6369: sub devalidate {
 6370:     my ($symb,$uname,$udom)=@_;
 6371:     my $cid=$env{'request.course.id'}; 
 6372:     if ($cid) {
 6373:         # delete the stored spreadsheets for
 6374:         # - the student level sheet of this user in course's homespace
 6375:         # - the assessment level sheet for this resource 
 6376:         #   for this user in user's homespace
 6377: 	# - current conditional state info
 6378: 	my $key=$uname.':'.$udom.':';
 6379:         my $status=
 6380: 	    &del('nohist_calculatedsheets',
 6381: 		 [$key.'studentcalc:'],
 6382: 		 $env{'course.'.$cid.'.domain'},
 6383: 		 $env{'course.'.$cid.'.num'})
 6384: 		.' '.
 6385: 	    &del('nohist_calculatedsheets_'.$cid,
 6386: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 6387:         unless ($status eq 'ok ok') {
 6388:            &logthis('Could not devalidate spreadsheet '.
 6389:                     $uname.' at '.$udom.' for '.
 6390: 		    $symb.': '.$status);
 6391:         }
 6392: 	&delenv('user.state.'.$cid);
 6393:     }
 6394: }
 6395: 
 6396: sub get_scalar {
 6397:     my ($string,$end) = @_;
 6398:     my $value;
 6399:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 6400: 	$value = $1;
 6401:     } elsif ($$string =~ s/^([^&]*?)&//) {
 6402: 	$value = $1;
 6403:     }
 6404:     return &unescape($value);
 6405: }
 6406: 
 6407: sub array2str {
 6408:   my (@array) = @_;
 6409:   my $result=&arrayref2str(\@array);
 6410:   $result=~s/^__ARRAY_REF__//;
 6411:   $result=~s/__END_ARRAY_REF__$//;
 6412:   return $result;
 6413: }
 6414: 
 6415: sub arrayref2str {
 6416:   my ($arrayref) = @_;
 6417:   my $result='__ARRAY_REF__';
 6418:   foreach my $elem (@$arrayref) {
 6419:     if(ref($elem) eq 'ARRAY') {
 6420:       $result.=&arrayref2str($elem).'&';
 6421:     } elsif(ref($elem) eq 'HASH') {
 6422:       $result.=&hashref2str($elem).'&';
 6423:     } elsif(ref($elem)) {
 6424:       #print("Got a ref of ".(ref($elem))." skipping.");
 6425:     } else {
 6426:       $result.=&escape($elem).'&';
 6427:     }
 6428:   }
 6429:   $result=~s/\&$//;
 6430:   $result .= '__END_ARRAY_REF__';
 6431:   return $result;
 6432: }
 6433: 
 6434: sub hash2str {
 6435:   my (%hash) = @_;
 6436:   my $result=&hashref2str(\%hash);
 6437:   $result=~s/^__HASH_REF__//;
 6438:   $result=~s/__END_HASH_REF__$//;
 6439:   return $result;
 6440: }
 6441: 
 6442: sub hashref2str {
 6443:   my ($hashref)=@_;
 6444:   my $result='__HASH_REF__';
 6445:   foreach my $key (sort(keys(%$hashref))) {
 6446:     if (ref($key) eq 'ARRAY') {
 6447:       $result.=&arrayref2str($key).'=';
 6448:     } elsif (ref($key) eq 'HASH') {
 6449:       $result.=&hashref2str($key).'=';
 6450:     } elsif (ref($key)) {
 6451:       $result.='=';
 6452:       #print("Got a ref of ".(ref($key))." skipping.");
 6453:     } else {
 6454: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6455:     }
 6456: 
 6457:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6458:       $result.=&arrayref2str($hashref->{$key}).'&';
 6459:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6460:       $result.=&hashref2str($hashref->{$key}).'&';
 6461:     } elsif(ref($hashref->{$key})) {
 6462:        $result.='&';
 6463:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6464:     } else {
 6465:       $result.=&escape($hashref->{$key}).'&';
 6466:     }
 6467:   }
 6468:   $result=~s/\&$//;
 6469:   $result .= '__END_HASH_REF__';
 6470:   return $result;
 6471: }
 6472: 
 6473: sub str2hash {
 6474:     my ($string)=@_;
 6475:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6476:     return %$hash;
 6477: }
 6478: 
 6479: sub str2hashref {
 6480:   my ($string) = @_;
 6481: 
 6482:   my %hash;
 6483: 
 6484:   if($string !~ /^__HASH_REF__/) {
 6485:       if (! ($string eq '' || !defined($string))) {
 6486: 	  $hash{'error'}='Not hash reference';
 6487:       }
 6488:       return (\%hash, $string);
 6489:   }
 6490: 
 6491:   $string =~ s/^__HASH_REF__//;
 6492: 
 6493:   while($string !~ /^__END_HASH_REF__/) {
 6494:       #key
 6495:       my $key='';
 6496:       if($string =~ /^__HASH_REF__/) {
 6497:           ($key, $string)=&str2hashref($string);
 6498:           if(defined($key->{'error'})) {
 6499:               $hash{'error'}='Bad data';
 6500:               return (\%hash, $string);
 6501:           }
 6502:       } elsif($string =~ /^__ARRAY_REF__/) {
 6503:           ($key, $string)=&str2arrayref($string);
 6504:           if($key->[0] eq 'Array reference error') {
 6505:               $hash{'error'}='Bad data';
 6506:               return (\%hash, $string);
 6507:           }
 6508:       } else {
 6509:           $string =~ s/^(.*?)=//;
 6510: 	  $key=&unescape($1);
 6511:       }
 6512:       $string =~ s/^=//;
 6513: 
 6514:       #value
 6515:       my $value='';
 6516:       if($string =~ /^__HASH_REF__/) {
 6517:           ($value, $string)=&str2hashref($string);
 6518:           if(defined($value->{'error'})) {
 6519:               $hash{'error'}='Bad data';
 6520:               return (\%hash, $string);
 6521:           }
 6522:       } elsif($string =~ /^__ARRAY_REF__/) {
 6523:           ($value, $string)=&str2arrayref($string);
 6524:           if($value->[0] eq 'Array reference error') {
 6525:               $hash{'error'}='Bad data';
 6526:               return (\%hash, $string);
 6527:           }
 6528:       } else {
 6529: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6530:       }
 6531:       $string =~ s/^&//;
 6532: 
 6533:       $hash{$key}=$value;
 6534:   }
 6535: 
 6536:   $string =~ s/^__END_HASH_REF__//;
 6537: 
 6538:   return (\%hash, $string);
 6539: }
 6540: 
 6541: sub str2array {
 6542:     my ($string)=@_;
 6543:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6544:     return @$array;
 6545: }
 6546: 
 6547: sub str2arrayref {
 6548:   my ($string) = @_;
 6549:   my @array;
 6550: 
 6551:   if($string !~ /^__ARRAY_REF__/) {
 6552:       if (! ($string eq '' || !defined($string))) {
 6553: 	  $array[0]='Array reference error';
 6554:       }
 6555:       return (\@array, $string);
 6556:   }
 6557: 
 6558:   $string =~ s/^__ARRAY_REF__//;
 6559: 
 6560:   while($string !~ /^__END_ARRAY_REF__/) {
 6561:       my $value='';
 6562:       if($string =~ /^__HASH_REF__/) {
 6563:           ($value, $string)=&str2hashref($string);
 6564:           if(defined($value->{'error'})) {
 6565:               $array[0] ='Array reference error';
 6566:               return (\@array, $string);
 6567:           }
 6568:       } elsif($string =~ /^__ARRAY_REF__/) {
 6569:           ($value, $string)=&str2arrayref($string);
 6570:           if($value->[0] eq 'Array reference error') {
 6571:               $array[0] ='Array reference error';
 6572:               return (\@array, $string);
 6573:           }
 6574:       } else {
 6575: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6576:       }
 6577:       $string =~ s/^&//;
 6578: 
 6579:       push(@array, $value);
 6580:   }
 6581: 
 6582:   $string =~ s/^__END_ARRAY_REF__//;
 6583: 
 6584:   return (\@array, $string);
 6585: }
 6586: 
 6587: # -------------------------------------------------------------------Temp Store
 6588: 
 6589: sub tmpreset {
 6590:   my ($symb,$namespace,$domain,$stuname) = @_;
 6591:   if (!$symb) {
 6592:     $symb=&symbread();
 6593:     if (!$symb) { $symb= $env{'request.url'}; }
 6594:   }
 6595:   $symb=escape($symb);
 6596: 
 6597:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6598:   $namespace=~s/\//\_/g;
 6599:   $namespace=~s/\W//g;
 6600: 
 6601:   if (!$domain) { $domain=$env{'user.domain'}; }
 6602:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6603:   if ($domain eq 'public' && $stuname eq 'public') {
 6604:       $stuname=&get_requestor_ip();
 6605:   }
 6606:   my $path=LONCAPA::tempdir();
 6607:   my %hash;
 6608:   if (tie(%hash,'GDBM_File',
 6609: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6610: 	  &GDBM_WRCREAT(),0640)) {
 6611:     foreach my $key (keys(%hash)) {
 6612:       if ($key=~ /:$symb/) {
 6613: 	delete($hash{$key});
 6614:       }
 6615:     }
 6616:   }
 6617: }
 6618: 
 6619: sub tmpstore {
 6620:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6621: 
 6622:   if (!$symb) {
 6623:     $symb=&symbread();
 6624:     if (!$symb) { $symb= $env{'request.url'}; }
 6625:   }
 6626:   $symb=escape($symb);
 6627: 
 6628:   if (!$namespace) {
 6629:     # I don't think we would ever want to store this for a course.
 6630:     # it seems this will only be used if we don't have a course.
 6631:     #$namespace=$env{'request.course.id'};
 6632:     #if (!$namespace) {
 6633:       $namespace=$env{'request.state'};
 6634:     #}
 6635:   }
 6636:   $namespace=~s/\//\_/g;
 6637:   $namespace=~s/\W//g;
 6638:   if (!$domain) { $domain=$env{'user.domain'}; }
 6639:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6640:   if ($domain eq 'public' && $stuname eq 'public') {
 6641:       $stuname=&get_requestor_ip();
 6642:   }
 6643:   my $now=time;
 6644:   my %hash;
 6645:   my $path=LONCAPA::tempdir();
 6646:   if (tie(%hash,'GDBM_File',
 6647: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6648: 	  &GDBM_WRCREAT(),0640)) {
 6649:     $hash{"version:$symb"}++;
 6650:     my $version=$hash{"version:$symb"};
 6651:     my $allkeys=''; 
 6652:     foreach my $key (keys(%$storehash)) {
 6653:       $allkeys.=$key.':';
 6654:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6655:     }
 6656:     $hash{"$version:$symb:timestamp"}=$now;
 6657:     $allkeys.='timestamp';
 6658:     $hash{"$version:keys:$symb"}=$allkeys;
 6659:     if (untie(%hash)) {
 6660:       return 'ok';
 6661:     } else {
 6662:       return "error:$!";
 6663:     }
 6664:   } else {
 6665:     return "error:$!";
 6666:   }
 6667: }
 6668: 
 6669: # -----------------------------------------------------------------Temp Restore
 6670: 
 6671: sub tmprestore {
 6672:   my ($symb,$namespace,$domain,$stuname) = @_;
 6673: 
 6674:   if (!$symb) {
 6675:     $symb=&symbread();
 6676:     if (!$symb) { $symb= $env{'request.url'}; }
 6677:   }
 6678:   $symb=escape($symb);
 6679: 
 6680:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6681: 
 6682:   if (!$domain) { $domain=$env{'user.domain'}; }
 6683:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6684:   if ($domain eq 'public' && $stuname eq 'public') {
 6685:       $stuname=&get_requestor_ip();
 6686:   }
 6687:   my %returnhash;
 6688:   $namespace=~s/\//\_/g;
 6689:   $namespace=~s/\W//g;
 6690:   my %hash;
 6691:   my $path=LONCAPA::tempdir();
 6692:   if (tie(%hash,'GDBM_File',
 6693: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6694: 	  &GDBM_READER(),0640)) {
 6695:     my $version=$hash{"version:$symb"};
 6696:     $returnhash{'version'}=$version;
 6697:     my $scope;
 6698:     for ($scope=1;$scope<=$version;$scope++) {
 6699:       my $vkeys=$hash{"$scope:keys:$symb"};
 6700:       my @keys=split(/:/,$vkeys);
 6701:       my $key;
 6702:       $returnhash{"$scope:keys"}=$vkeys;
 6703:       foreach $key (@keys) {
 6704: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6705: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6706:       }
 6707:     }
 6708:     if (!(untie(%hash))) {
 6709:       return "error:$!";
 6710:     }
 6711:   } else {
 6712:     return "error:$!";
 6713:   }
 6714:   return %returnhash;
 6715: }
 6716: 
 6717: # ----------------------------------------------------------------------- Store
 6718: 
 6719: sub store {
 6720:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6721:     my $home='';
 6722: 
 6723:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6724: 
 6725:     $symb=&symbclean($symb);
 6726:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6727: 
 6728:     if (!$domain) { $domain=$env{'user.domain'}; }
 6729:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6730: 
 6731:     &devalidate($symb,$stuname,$domain);
 6732: 
 6733:     $symb=escape($symb);
 6734:     if (!$namespace) { 
 6735:        unless ($namespace=$env{'request.course.id'}) { 
 6736:           return ''; 
 6737:        } 
 6738:     }
 6739:     if (!$home) { $home=$env{'user.home'}; }
 6740: 
 6741:     $$storehash{'ip'}=&get_requestor_ip();
 6742:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6743: 
 6744:     my $namevalue='';
 6745:     foreach my $key (keys(%$storehash)) {
 6746:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6747:     }
 6748:     $namevalue=~s/\&$//;
 6749:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6750:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6751: }
 6752: 
 6753: # -------------------------------------------------------------- Critical Store
 6754: 
 6755: sub cstore {
 6756:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6757:     my $home='';
 6758: 
 6759:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6760: 
 6761:     unless (($symb eq '_feedback') || ($symb eq '_discussion')) {
 6762:         $symb=&symbclean($symb);
 6763:     }
 6764:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6765: 
 6766:     if (!$domain) { $domain=$env{'user.domain'}; }
 6767:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6768: 
 6769:     unless (($symb eq '_feedback') || ($symb eq '_discussion')) {
 6770:         &devalidate($symb,$stuname,$domain);
 6771:     }
 6772: 
 6773:     $symb=escape($symb);
 6774:     if (!$namespace) { 
 6775:        unless ($namespace=$env{'request.course.id'}) { 
 6776:           return ''; 
 6777:        } 
 6778:     }
 6779:     if (!$home) { $home=$env{'user.home'}; }
 6780: 
 6781:     $$storehash{'ip'} = &get_requestor_ip();
 6782:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6783: 
 6784:     my $namevalue='';
 6785:     foreach my $key (keys(%$storehash)) {
 6786:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6787:     }
 6788:     $namevalue=~s/\&$//;
 6789:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6790:     return critical
 6791:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6792: }
 6793: 
 6794: # --------------------------------------------------------------------- Restore
 6795: 
 6796: sub restore {
 6797:     my ($symb,$namespace,$domain,$stuname) = @_;
 6798:     my $home='';
 6799: 
 6800:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6801: 
 6802:     if (!$symb) {
 6803:         return if ($namespace eq 'courserequests');
 6804:         unless ($symb=escape(&symbread())) { return ''; }
 6805:     } else {
 6806:         unless ($namespace eq 'courserequests') {
 6807:             $symb=&escape(&symbclean($symb));
 6808:         }
 6809:     }
 6810:     if (!$namespace) { 
 6811:        unless ($namespace=$env{'request.course.id'}) { 
 6812:           return ''; 
 6813:        } 
 6814:     }
 6815:     if (!$domain) { $domain=$env{'user.domain'}; }
 6816:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6817:     if (!$home) { $home=$env{'user.home'}; }
 6818:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6819: 
 6820:     my %returnhash=();
 6821:     foreach my $line (split(/\&/,$answer)) {
 6822: 	my ($name,$value)=split(/\=/,$line);
 6823:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6824:     }
 6825:     my $version;
 6826:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6827:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6828:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6829:        }
 6830:     }
 6831:     return %returnhash;
 6832: }
 6833: 
 6834: # ---------------------------------------------------------- Course Description
 6835: #
 6836: #  
 6837: 
 6838: sub coursedescription {
 6839:     my ($courseid,$args)=@_;
 6840:     $courseid=~s/^\///;
 6841:     $courseid=~s/\_/\//g;
 6842:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6843:     my $chome=&homeserver($cnum,$cdomain);
 6844:     my $normalid=$cdomain.'_'.$cnum;
 6845:     # need to always cache even if we get errors otherwise we keep 
 6846:     # trying and trying and trying to get the course description.
 6847:     my %envhash=();
 6848:     my %returnhash=();
 6849:     
 6850:     my $expiretime=600;
 6851:     if ($env{'request.course.id'} eq $normalid) {
 6852: 	$expiretime=120;
 6853:     }
 6854: 
 6855:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6856:     if (!$args->{'freshen_cache'}
 6857: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6858: 	foreach my $key (keys(%env)) {
 6859: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6860: 	    my ($setting) = $1;
 6861: 	    $returnhash{$setting} = $env{$key};
 6862: 	}
 6863: 	return %returnhash;
 6864:     }
 6865: 
 6866:     # get the data again
 6867: 
 6868:     if (!$args->{'one_time'}) {
 6869: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6870:     }
 6871: 
 6872:     if ($chome ne 'no_host') {
 6873:        %returnhash=&dump('environment',$cdomain,$cnum);
 6874:        if (!exists($returnhash{'con_lost'})) {
 6875: 	   my $username = $env{'user.name'}; # Defult username
 6876: 	   if(defined $args->{'user'}) {
 6877: 	       $username = $args->{'user'};
 6878: 	   }
 6879:            $returnhash{'home'}= $chome;
 6880: 	   $returnhash{'domain'} = $cdomain;
 6881: 	   $returnhash{'num'} = $cnum;
 6882:            if (!defined($returnhash{'type'})) {
 6883:                $returnhash{'type'} = 'Course';
 6884:            }
 6885:            while (my ($name,$value) = each %returnhash) {
 6886:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6887:            }
 6888:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6889:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6890: 	       $username.'_'.$cdomain.'_'.$cnum;
 6891:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6892:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6893:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6894:        }
 6895:     }
 6896:     if (!$args->{'one_time'}) {
 6897: 	&appenv(\%envhash);
 6898:     }
 6899:     return %returnhash;
 6900: }
 6901: 
 6902: sub update_released_required {
 6903:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6904:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6905:         $cid = $env{'request.course.id'};
 6906:         $cdom = $env{'course.'.$cid.'.domain'};
 6907:         $cnum = $env{'course.'.$cid.'.num'};
 6908:         $chome = $env{'course.'.$cid.'.home'};
 6909:     }
 6910:     if ($needsrelease) {
 6911:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6912:         my $needsupdate;
 6913:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6914:             $needsupdate = 1;
 6915:         } else {
 6916:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6917:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6918:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6919:                 $needsupdate = 1;
 6920:             }
 6921:         }
 6922:         if ($needsupdate) {
 6923:             my %needshash = (
 6924:                              'internal.releaserequired' => $needsrelease,
 6925:                             );
 6926:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6927:             if ($putresult eq 'ok') {
 6928:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6929:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6930:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6931:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6932:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6933:                 }
 6934:             }
 6935:         }
 6936:     }
 6937:     return;
 6938: }
 6939: 
 6940: # -------------------------------------------------See if a user is privileged
 6941: 
 6942: sub privileged {
 6943:     my ($username,$domain,$possdomains,$possroles)=@_;
 6944:     my $now = time;
 6945:     my $roles;
 6946:     if (ref($possroles) eq 'ARRAY') {
 6947:         $roles = $possroles; 
 6948:     } else {
 6949:         $roles = ['dc','su'];
 6950:     }
 6951:     if (ref($possdomains) eq 'ARRAY') {
 6952:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6953:         foreach my $dom (@{$possdomains}) {
 6954:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6955:                 (ref($privileged{$dom}) eq 'HASH')) {
 6956:                 foreach my $role (@{$roles}) {
 6957:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6958:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6959:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6960:                             return 1 unless (($end && $end < $now) ||
 6961:                                              ($start && $start > $now));
 6962:                         }
 6963:                     }
 6964:                 }
 6965:             }
 6966:         }
 6967:     } else {
 6968:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6969:         my $now = time;
 6970: 
 6971:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6972:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6973:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6974:                 return 1 unless ($tend && $tend < $now) 
 6975:                         or ($tstart && $tstart > $now);
 6976:             }
 6977:         }
 6978:     }
 6979:     return 0;
 6980: }
 6981: 
 6982: sub privileged_by_domain {
 6983:     my ($domains,$roles) = @_;
 6984:     my %privileged = ();
 6985:     my $cachetime = 60*60*24;
 6986:     my $now = time;
 6987:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6988:         return %privileged;
 6989:     }
 6990:     foreach my $dom (@{$domains}) {
 6991:         next if (ref($privileged{$dom}) eq 'HASH');
 6992:         my $needroles;
 6993:         foreach my $role (@{$roles}) {
 6994:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6995:             if (defined($cached)) {
 6996:                 if (ref($result) eq 'HASH') {
 6997:                     $privileged{$dom}{$role} = $result;
 6998:                 }
 6999:             } else {
 7000:                 $needroles = 1;
 7001:             }
 7002:         }
 7003:         if ($needroles) {
 7004:             my %dompersonnel = &get_domain_roles($dom,$roles);
 7005:             $privileged{$dom} = {};
 7006:             foreach my $server (keys(%dompersonnel)) {
 7007:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 7008:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 7009:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 7010:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 7011:                         next if ($end && $end < $now);
 7012:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 7013:                             $dompersonnel{$server}{$item};
 7014:                     }
 7015:                 }
 7016:             }
 7017:             if (ref($privileged{$dom}) eq 'HASH') {
 7018:                 foreach my $role (@{$roles}) {
 7019:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 7020:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 7021:                     } else {
 7022:                         my %hash = ();
 7023:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 7024:                     }
 7025:                 }
 7026:             }
 7027:         }
 7028:     }
 7029:     return %privileged;
 7030: }
 7031: 
 7032: # -------------------------------------------------------- Get user privileges
 7033: 
 7034: sub rolesinit {
 7035:     my ($domain, $username) = @_;
 7036:     my %userroles = ('user.login.time' => time);
 7037:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 7038: 
 7039:     # firstaccess and timerinterval are related to timed maps/resources. 
 7040:     # also, blocking can be triggered by an activating timer
 7041:     # it's saved in the user's %env.
 7042:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 7043:     my %timerinterval = &dump('timerinterval', $domain, $username);
 7044:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 7045:         %timerintchk, %timerintenv, %coauthorenv);
 7046: 
 7047:     foreach my $key (keys(%firstaccess)) {
 7048:         my ($cid, $rest) = split(/\0/, $key);
 7049:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 7050:     }
 7051: 
 7052:     foreach my $key (keys(%timerinterval)) {
 7053:         my ($cid,$rest) = split(/\0/,$key);
 7054:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 7055:     }
 7056: 
 7057:     my %allroles=();
 7058:     my %allgroups=();
 7059:     my %gotcoauconfig=();
 7060:     my %domdefaults=();
 7061: 
 7062:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 7063:         my $role = $rolesdump{$area};
 7064:         $area =~ s/\_\w\w$//;
 7065: 
 7066:         my ($trole, $tend, $tstart, $group_privs);
 7067: 
 7068:         if ($role =~ /^cr/) {
 7069:         # Custom role, defined by a user 
 7070:         # e.g., user.role.cr/msu/smith/mynewrole
 7071:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 7072:                 $trole = $1;
 7073:                 ($tend, $tstart) = split('_', $2);
 7074:             } else {
 7075:                 $trole = $role;
 7076:             }
 7077:         } elsif ($role =~ m|^gr/|) {
 7078:         # Role of member in a group, defined within a course/community
 7079:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 7080:             ($trole, $tend, $tstart) = split(/_/, $role);
 7081:             next if $tstart eq '-1';
 7082:             ($trole, $group_privs) = split(/\//, $trole);
 7083:             $group_privs = &unescape($group_privs);
 7084:         } else {
 7085:         # Just a normal role, defined in roles.tab
 7086:             ($trole, $tend, $tstart) = split(/_/,$role);
 7087:         }
 7088: 
 7089:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 7090:                  $username);
 7091:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 7092: 
 7093:         # role expired or not available yet?
 7094:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 7095:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 7096: 
 7097:         next if $area eq '' or $trole eq '';
 7098: 
 7099:         my $spec = "$trole.$area";
 7100:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 7101: 
 7102:         if ($trole =~ /^cr\//) {
 7103:         # Custom role, defined by a user
 7104:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7105:         } elsif ($trole eq 'gr') {
 7106:         # Role of a member in a group, defined within a course/community
 7107:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 7108:             next;
 7109:         } else {
 7110:         # Normal role, defined in roles.tab
 7111:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7112:             if (($trole eq 'ca') || ($trole eq 'aa')) {
 7113:                 (undef,my ($audom,$auname)) = split(/\//,$area);
 7114:                 unless ($gotcoauconfig{$area}) {
 7115:                     my @ca_settings = ('authoreditors','coauthorlist','coauthoroptin');
 7116:                     my %info = &userenvironment($audom,$auname,@ca_settings);
 7117:                     $gotcoauconfig{$area} = 1;
 7118:                     foreach my $item (@ca_settings) {
 7119:                         if (exists($info{$item})) {
 7120:                             my $name = $item;
 7121:                             if ($item eq 'authoreditors') {
 7122:                                 $name = 'editors';
 7123:                                 unless ($info{'authoreditors'}) {
 7124:                                     my %domdefs;
 7125:                                     if (ref($domdefaults{$audom}) eq 'HASH') {
 7126:                                         %domdefs = %{$domdefaults{$audom}};
 7127:                                     } else {
 7128:                                         %domdefs = &get_domain_defaults($audom);
 7129:                                         $domdefaults{$audom} = \%domdefs;
 7130:                                     }
 7131:                                     if ($domdefs{$name} ne '') {
 7132:                                         $info{'authoreditors'} = $domdefs{$name};
 7133:                                     } else {
 7134:                                         $info{'authoreditors'} = 'edit,xml';
 7135:                                     }
 7136:                                 }
 7137:                             }
 7138:                             $coauthorenv{"environment.internal.$name.$area"} = $info{$item};
 7139:                         }
 7140:                     }
 7141:                 }
 7142:             }
 7143:         }
 7144: 
 7145:         my $cid = $tdomain.'_'.$trest;
 7146:         unless ($firstaccchk{$cid}) {
 7147:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 7148:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 7149:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 7150:                         $coursetimerstarts{$cid}{$item}; 
 7151:                 }
 7152:             }
 7153:             $firstaccchk{$cid} = 1;
 7154:         }
 7155:         unless ($timerintchk{$cid}) {
 7156:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 7157:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 7158:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 7159:                        $coursetimerintervals{$cid}{$item};
 7160:                 }
 7161:             }
 7162:             $timerintchk{$cid} = 1;
 7163:         }
 7164:     }
 7165: 
 7166:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 7167:                                                           \%allroles, \%allgroups);
 7168:     $env{'user.adv'} = $userroles{'user.adv'};
 7169:     $env{'user.rar'} = $userroles{'user.rar'};
 7170: 
 7171:     return (\%userroles,\%firstaccenv,\%timerintenv,\%coauthorenv);
 7172: }
 7173: 
 7174: sub set_arearole {
 7175:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 7176:     unless ($nolog) {
 7177: # log the associated role with the area
 7178:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 7179:     }
 7180:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 7181: }
 7182: 
 7183: sub custom_roleprivs {
 7184:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 7185:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 7186:     my $homsvr = &homeserver($rauthor,$rdomain);
 7187:     if (&hostname($homsvr) ne '') {
 7188:         my ($rdummy,$roledef)=
 7189:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 7190:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 7191:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 7192:             if (defined($syspriv)) {
 7193:                 if ($trest =~ /^$match_community$/) {
 7194:                     $syspriv =~ s/bre\&S//; 
 7195:                 }
 7196:                 $$allroles{'cm./'}.=':'.$syspriv;
 7197:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 7198:             }
 7199:             if ($tdomain ne '') {
 7200:                 if (defined($dompriv)) {
 7201:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 7202:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 7203:                 }
 7204:                 if (($trest ne '') && (defined($coursepriv))) {
 7205:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 7206:                         my $rolename = $1;
 7207:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 7208:                     }
 7209:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 7210:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 7211:                 }
 7212:             }
 7213:         }
 7214:     }
 7215: }
 7216: 
 7217: sub course_adhocrole_privs {
 7218:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 7219:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 7220:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 7221:         my (%currprivs,%storeprivs);
 7222:         foreach my $item (split(/:/,$coursepriv)) {
 7223:             my ($priv,$restrict) = split(/\&/,$item);
 7224:             $currprivs{$priv} = $restrict;
 7225:         }
 7226:         my (%possadd,%possremove,%full);
 7227:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 7228:             my ($priv,$restrict)=split(/\&/,$item);
 7229:             $full{$priv} = $restrict;
 7230:         }
 7231:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 7232:             next if ($item eq '');
 7233:             my ($rule,$rest) = split(/=/,$item);
 7234:             next unless (($rule eq 'off') || ($rule eq 'on'));
 7235:             foreach my $priv (split(/:/,$rest)) {
 7236:                 if ($priv ne '') {
 7237:                     if ($rule eq 'off') {
 7238:                         $possremove{$priv} = 1;
 7239:                     } else {
 7240:                         $possadd{$priv} = 1;
 7241:                     }
 7242:                 }
 7243:             }
 7244:         }
 7245:         foreach my $priv (sort(keys(%full))) {
 7246:             if (exists($currprivs{$priv})) {
 7247:                 unless (exists($possremove{$priv})) {
 7248:                     $storeprivs{$priv} = $currprivs{$priv};
 7249:                 }
 7250:             } elsif (exists($possadd{$priv})) {
 7251:                 $storeprivs{$priv} = $full{$priv};
 7252:             }
 7253:         }
 7254:         $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 7255:     }
 7256:     return $coursepriv;
 7257: }
 7258: 
 7259: sub group_roleprivs {
 7260:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 7261:     my $access = 1;
 7262:     my $now = time;
 7263:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 7264:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 7265:     if ($access) {
 7266:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 7267:         $$allgroups{$course}{$group} .=':'.$group_privs;
 7268:     }
 7269: }
 7270: 
 7271: sub standard_roleprivs {
 7272:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 7273:     if (defined($pr{$trole.':s'})) {
 7274:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 7275:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 7276:     }
 7277:     if ($tdomain ne '') {
 7278:         if (defined($pr{$trole.':d'})) {
 7279:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 7280:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 7281:         }
 7282:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 7283:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 7284:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 7285:         }
 7286:     }
 7287: }
 7288: 
 7289: sub set_userprivs {
 7290:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 7291:     my $author=0;
 7292:     my $adv=0;
 7293:     my $rar=0;
 7294:     my %grouproles = ();
 7295:     if (keys(%{$allgroups}) > 0) {
 7296:         my @groupkeys; 
 7297:         foreach my $role (keys(%{$allroles})) {
 7298:             push(@groupkeys,$role);
 7299:         }
 7300:         if (ref($groups_roles) eq 'HASH') {
 7301:             foreach my $key (keys(%{$groups_roles})) {
 7302:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 7303:                     push(@groupkeys,$key);
 7304:                 }
 7305:             }
 7306:         }
 7307:         if (@groupkeys > 0) {
 7308:             foreach my $role (@groupkeys) {
 7309:                 my ($trole,$area,$sec,$extendedarea);
 7310:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 7311:                     $trole = $1;
 7312:                     $area = $2;
 7313:                     $sec = $3;
 7314:                     $extendedarea = $area.$sec;
 7315:                     if (exists($$allgroups{$area})) {
 7316:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 7317:                             my $spec = $trole.'.'.$extendedarea;
 7318:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 7319:                                                 $$allgroups{$area}{$group};
 7320:                         }
 7321:                     }
 7322:                 }
 7323:             }
 7324:         }
 7325:     }
 7326:     foreach my $group (keys(%grouproles)) {
 7327:         $$allroles{$group} = $grouproles{$group};
 7328:     }
 7329:     foreach my $role (keys(%{$allroles})) {
 7330:         my %thesepriv;
 7331:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 7332:         foreach my $item (split(/:/,$$allroles{$role})) {
 7333:             if ($item ne '') {
 7334:                 my ($privilege,$restrictions)=split(/&/,$item);
 7335:                 if ($restrictions eq '') {
 7336:                     $thesepriv{$privilege}='F';
 7337:                 } elsif ($thesepriv{$privilege} ne 'F') {
 7338:                     $thesepriv{$privilege}.=$restrictions;
 7339:                 }
 7340:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 7341:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 7342:             }
 7343:         }
 7344:         my $thesestr='';
 7345:         foreach my $priv (sort(keys(%thesepriv))) {
 7346: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 7347: 	}
 7348:         $userroles->{'user.priv.'.$role} = $thesestr;
 7349:     }
 7350:     return ($author,$adv,$rar);
 7351: }
 7352: 
 7353: sub role_status {
 7354:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 7355:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 7356:         my ($one,$two) = split(m{\./},$rolekey,2);
 7357:         (undef,undef,$$role) = split(/\./,$one,3);
 7358:         unless (!defined($$role) || $$role eq '') {
 7359:             $$where = '/'.$two;
 7360:             $$trolecode=$$role.'.'.$$where;
 7361:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 7362:             $$tstatus='is';
 7363:             if ($$tstart && $$tstart>$update) {
 7364:                 $$tstatus='future';
 7365:                 if ($$tstart<$now) {
 7366:                     if ($$tstart && $$tstart>$refresh) {
 7367:                         if (($$where ne '') && ($$role ne '')) {
 7368:                             my (%allroles,%allgroups,$group_privs,
 7369:                                 %groups_roles,@rolecodes);
 7370:                             my %userroles = (
 7371:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 7372:                             );
 7373:                             @rolecodes = ('cm'); 
 7374:                             my $spec=$$role.'.'.$$where;
 7375:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 7376:                             if ($$role =~ /^cr\//) {
 7377:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 7378:                                 push(@rolecodes,'cr');
 7379:                             } elsif ($$role eq 'gr') {
 7380:                                 push(@rolecodes,$$role);
 7381:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 7382:                                                     $env{'user.name'});
 7383:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 7384:                                 (undef,my $group_privs) = split(/\//,$trole);
 7385:                                 $group_privs = &unescape($group_privs);
 7386:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 7387:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 7388:                                 &get_groups_roles($tdomain,$trest,
 7389:                                                   \%course_roles,\@rolecodes,
 7390:                                                   \%groups_roles);
 7391:                             } else {
 7392:                                 push(@rolecodes,$$role);
 7393:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 7394:                             }
 7395:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 7396:                                                                    \%groups_roles);
 7397:                             &appenv(\%userroles,\@rolecodes);
 7398:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7399:                         }
 7400:                     }
 7401:                     $$tstatus = 'is';
 7402:                 }
 7403:             }
 7404:             if ($$tend) {
 7405:                 if ($$tend<$update) {
 7406:                     $$tstatus='expired';
 7407:                 } elsif ($$tend<$now) {
 7408:                     $$tstatus='will_not';
 7409:                 }
 7410:             }
 7411:         }
 7412:     }
 7413: }
 7414: 
 7415: sub get_groups_roles {
 7416:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 7417:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 7418:                   (ref($rolecodes) eq 'ARRAY') && 
 7419:                   (ref($groups_roles) eq 'HASH')); 
 7420:     if (keys(%{$cdom_courseroles}) > 0) {
 7421:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 7422:         if ($cdom ne '' && $cnum ne '') {
 7423:             foreach my $key (keys(%{$cdom_courseroles})) {
 7424:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 7425:                     my $crsrole = $1;
 7426:                     my $crssec = $2;
 7427:                     if ($crsrole =~ /^cr/) {
 7428:                         unless (grep(/^cr$/,@{$rolecodes})) {
 7429:                             push(@{$rolecodes},'cr');
 7430:                         }
 7431:                     } else {
 7432:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 7433:                             push(@{$rolecodes},$crsrole);
 7434:                         }
 7435:                     }
 7436:                     my $rolekey = "$crsrole./$cdom/$cnum";
 7437:                     if ($crssec ne '') {
 7438:                         $rolekey .= "/$crssec";
 7439:                     }
 7440:                     $rolekey .= './';
 7441:                     $groups_roles->{$rolekey} = $rolecodes;
 7442:                 }
 7443:             }
 7444:         }
 7445:     }
 7446:     return;
 7447: }
 7448: 
 7449: sub delete_env_groupprivs {
 7450:     my ($where,$courseroles,$possroles) = @_;
 7451:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 7452:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 7453:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 7454:         %{$courseroles->{$udom}} =
 7455:             &get_my_roles('','','userroles',['active'],
 7456:                           $possroles,[$udom],1);
 7457:     }
 7458:     if (ref($courseroles->{$udom}) eq 'HASH') {
 7459:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 7460:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 7461:             my $area = '/'.$cdom.'/'.$cnum;
 7462:             my $privkey = "user.priv.$crsrole.$area";
 7463:             if ($crssec ne '') {
 7464:                 $privkey .= '/'.$crssec;
 7465:             }
 7466:             $privkey .= ".$area/$group";
 7467:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 7468:         }
 7469:     }
 7470:     return;
 7471: }
 7472: 
 7473: sub check_adhoc_privs {
 7474:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 7475:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 7476:     if ($sec) {
 7477:         $cckey .= '/'.$sec;
 7478:     } 
 7479:     my $setprivs;
 7480:     if ($env{$cckey}) {
 7481:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 7482:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 7483:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 7484:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7485:             $setprivs = 1;
 7486:         }
 7487:     } else {
 7488:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7489:         $setprivs = 1;
 7490:     }
 7491:     return $setprivs;
 7492: }
 7493: 
 7494: sub set_adhoc_privileges {
 7495: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7496:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7497:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7498:     if ($sec ne '') {
 7499:         $area .= '/'.$sec;
 7500:     }
 7501:     my $spec = $role.'.'.$area;
 7502:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7503:                                   $env{'user.name'},1);
 7504:     my %rolehash = ();
 7505:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7506:         my $rolename = $1;
 7507:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7508:         my %domdef = &get_domain_defaults($dcdom);
 7509:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7510:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7511:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7512:             }
 7513:         }
 7514:     } else {
 7515:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7516:     }
 7517:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7518:     &appenv(\%userroles,[$role,'cm']);
 7519:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7520:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7521:             ($caller eq 'tiny')) {
 7522:         &appenv( {'request.role'        => $spec,
 7523:                   'request.role.domain' => $dcdom,
 7524:                   'request.course.sec'  => $sec,
 7525:                  }
 7526:                );
 7527:         my $tadv=0;
 7528:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7529:         &appenv({'request.role.adv'    => $tadv});
 7530:     }
 7531:     if ($role eq 'ca') {
 7532:         my @ca_settings = ('authoreditors','coauthorlist');
 7533:         my %info = &userenvironment($dcdom,$pickedcourse,@ca_settings);
 7534:         foreach my $item (@ca_settings) {
 7535:             if (exists($info{$item})) {
 7536:                 my $name = $item;
 7537:                 if ($item eq 'authoreditors') {
 7538:                     $name = 'editors';
 7539:                     unless ($info{'authoreditors'}) {
 7540:                         my %domdefs = &get_domain_defaults($dcdom);
 7541:                         if ($domdefs{$name} ne '') {
 7542:                             $info{'authoreditors'} = $domdefs{$name};
 7543:                         } else {
 7544:                             $info{'authoreditors'} = 'edit,xml';
 7545:                         }
 7546:                     }
 7547:                 }
 7548:                 &appenv({"environment.internal.$name./$dcdom/$pickedcourse" => $info{$item}});
 7549:             }
 7550:         }
 7551:     }
 7552: }
 7553: 
 7554: # --------------------------------------------------------------- get interface
 7555: 
 7556: sub get {
 7557:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7558:    my $items='';
 7559:    foreach my $item (@$storearr) {
 7560:        $items.=&escape($item).'&';
 7561:    }
 7562:    $items=~s/\&$//;
 7563:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7564:    if (!$uname) { $uname=$env{'user.name'}; }
 7565:    my $uhome=&homeserver($uname,$udomain);
 7566: 
 7567:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7568:    my @pairs=split(/\&/,$rep);
 7569:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7570:      return @pairs;
 7571:    }
 7572:    my %returnhash=();
 7573:    my $i=0;
 7574:    foreach my $item (@$storearr) {
 7575:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7576:       $i++;
 7577:    }
 7578:    return %returnhash;
 7579: }
 7580: 
 7581: # --------------------------------------------------------------- del interface
 7582: 
 7583: sub del {
 7584:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7585:    my $items='';
 7586:    foreach my $item (@$storearr) {
 7587:        $items.=&escape($item).'&';
 7588:    }
 7589: 
 7590:    $items=~s/\&$//;
 7591:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7592:    if (!$uname) { $uname=$env{'user.name'}; }
 7593:    my $uhome=&homeserver($uname,$udomain);
 7594:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7595: }
 7596: 
 7597: # -------------------------------------------------------------- dump interface
 7598: 
 7599: sub unserialize {
 7600:     my ($rep, $escapedkeys) = @_;
 7601: 
 7602:     return {} if $rep =~ /^error/;
 7603: 
 7604:     my %returnhash=();
 7605: 	foreach my $item (split(/\&/,$rep)) {
 7606: 	    my ($key, $value) = split(/=/, $item, 2);
 7607: 	    $key = unescape($key) unless $escapedkeys;
 7608: 	    next if $key =~ /^error: 2 /;
 7609: 	    $returnhash{$key} = &thaw_unescape($value);
 7610: 	}
 7611:     #return %returnhash;
 7612:     return \%returnhash;
 7613: }        
 7614: 
 7615: # see Lond::dump_with_regexp
 7616: # if $escapedkeys hash keys won't get unescaped.
 7617: sub dump {
 7618:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
 7619:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7620:     if (!$uname) { $uname=$env{'user.name'}; }
 7621:     my $uhome=&homeserver($uname,$udomain);
 7622: 
 7623:     if ($regexp) {
 7624:         $regexp=&escape($regexp);
 7625:     } else {
 7626:         $regexp='.';
 7627:     }
 7628:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7629:         # user is hosted on this machine
 7630:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7631:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7632:         return %{unserialize($reply, $escapedkeys)};
 7633:     }
 7634:     my $rep;
 7635:     if ($encrypt) {
 7636:         $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7637:     } else {
 7638:         $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7639:     }
 7640:     my @pairs=split(/\&/,$rep);
 7641:     my %returnhash=();
 7642:     if (!($rep =~ /^error/ )) {
 7643: 	foreach my $item (@pairs) {
 7644: 	    my ($key,$value)=split(/=/,$item,2);
 7645:         $key = unescape($key) unless $escapedkeys;
 7646:         #$key = &unescape($key);
 7647: 	    next if ($key =~ /^error: 2 /);
 7648: 	    $returnhash{$key}=&thaw_unescape($value);
 7649: 	}
 7650:     }
 7651:     return %returnhash;
 7652: }
 7653: 
 7654: 
 7655: # --------------------------------------------------------- dumpstore interface
 7656: 
 7657: sub dumpstore {
 7658:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7659:    # same as dump but keys must be escaped. They may contain colon separated
 7660:    # lists of values that may themself contain colons (e.g. symbs).
 7661:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7662: }
 7663: 
 7664: # -------------------------------------------------------------- keys interface
 7665: 
 7666: sub getkeys {
 7667:    my ($namespace,$udomain,$uname)=@_;
 7668:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7669:    if (!$uname) { $uname=$env{'user.name'}; }
 7670:    my $uhome=&homeserver($uname,$udomain);
 7671:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7672:    my @keyarray=();
 7673:    foreach my $key (split(/\&/,$rep)) {
 7674:       next if ($key =~ /^error: 2 /);
 7675:       push(@keyarray,&unescape($key));
 7676:    }
 7677:    return @keyarray;
 7678: }
 7679: 
 7680: # --------------------------------------------------------------- currentdump
 7681: sub currentdump {
 7682:    my ($courseid,$sdom,$sname)=@_;
 7683:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7684:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7685:    $sname    = $env{'user.name'}         if (! defined($sname));
 7686:    my $uhome = &homeserver($sname,$sdom);
 7687:    my $rep;
 7688: 
 7689:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7690:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7691:                    $courseid)));
 7692:    } else {
 7693:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7694:    }
 7695: 
 7696:    return if ($rep =~ /^(error:|no_such_host)/);
 7697:    #
 7698:    my %returnhash=();
 7699:    #
 7700:    if ($rep eq 'unknown_cmd') {
 7701:        # an old lond will not know currentdump
 7702:        # Do a dump and make it look like a currentdump
 7703:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7704:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7705:        my %hash = @tmp;
 7706:        @tmp=();
 7707:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7708:    } else {
 7709:        my @pairs=split(/\&/,$rep);
 7710:        foreach my $pair (@pairs) {
 7711:            my ($key,$value)=split(/=/,$pair,2);
 7712:            my ($symb,$param) = split(/:/,$key);
 7713:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7714:                                                         &thaw_unescape($value);
 7715:        }
 7716:    }
 7717:    return %returnhash;
 7718: }
 7719: 
 7720: sub convert_dump_to_currentdump{
 7721:     my %hash = %{shift()};
 7722:     my %returnhash;
 7723:     # Code ripped from lond, essentially.  The only difference
 7724:     # here is the unescaping done by lonnet::dump().  Conceivably
 7725:     # we might run in to problems with parameter names =~ /^v\./
 7726:     while (my ($key,$value) = each(%hash)) {
 7727:         my ($v,$symb,$param) = split(/:/,$key);
 7728: 	$symb  = &unescape($symb);
 7729: 	$param = &unescape($param);
 7730:         next if ($v eq 'version' || $symb eq 'keys');
 7731:         next if (exists($returnhash{$symb}) &&
 7732:                  exists($returnhash{$symb}->{$param}) &&
 7733:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7734:         $returnhash{$symb}->{$param}=$value;
 7735:         $returnhash{$symb}->{'v.'.$param}=$v;
 7736:     }
 7737:     #
 7738:     # Remove all of the keys in the hashes which keep track of
 7739:     # the version of the parameter.
 7740:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7741:         # use a foreach because we are going to delete from the hash.
 7742:         foreach my $key (keys(%$param_hash)) {
 7743:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7744:         }
 7745:     }
 7746:     return \%returnhash;
 7747: }
 7748: 
 7749: # ------------------------------------------------------ critical inc interface
 7750: 
 7751: sub cinc {
 7752:     return &inc(@_,'critical');
 7753: }
 7754: 
 7755: # --------------------------------------------------------------- inc interface
 7756: 
 7757: sub inc {
 7758:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7759:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7760:     if (!$uname) { $uname=$env{'user.name'}; }
 7761:     my $uhome=&homeserver($uname,$udomain);
 7762:     my $items='';
 7763:     if (! ref($store)) {
 7764:         # got a single value, so use that instead
 7765:         $items = &escape($store).'=&';
 7766:     } elsif (ref($store) eq 'SCALAR') {
 7767:         $items = &escape($$store).'=&';        
 7768:     } elsif (ref($store) eq 'ARRAY') {
 7769:         $items = join('=&',map {&escape($_);} @{$store});
 7770:     } elsif (ref($store) eq 'HASH') {
 7771:         while (my($key,$value) = each(%{$store})) {
 7772:             $items.= &escape($key).'='.&escape($value).'&';
 7773:         }
 7774:     }
 7775:     $items=~s/\&$//;
 7776:     if ($critical) {
 7777: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7778:     } else {
 7779: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7780:     }
 7781: }
 7782: 
 7783: # --------------------------------------------------------------- put interface
 7784: 
 7785: sub put {
 7786:    my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
 7787:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7788:    if (!$uname) { $uname=$env{'user.name'}; }
 7789:    my $uhome=&homeserver($uname,$udomain);
 7790:    my $items='';
 7791:    foreach my $item (keys(%$storehash)) {
 7792:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7793:    }
 7794:    $items=~s/\&$//;
 7795:    if ($encrypt) {
 7796:        return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
 7797:    } else {
 7798:        return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7799:    }
 7800: }
 7801: 
 7802: # ------------------------------------------------------------ newput interface
 7803: 
 7804: sub newput {
 7805:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7806:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7807:    if (!$uname) { $uname=$env{'user.name'}; }
 7808:    my $uhome=&homeserver($uname,$udomain);
 7809:    my $items='';
 7810:    foreach my $key (keys(%$storehash)) {
 7811:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7812:    }
 7813:    $items=~s/\&$//;
 7814:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7815: }
 7816: 
 7817: # ---------------------------------------------------------  putstore interface
 7818: 
 7819: sub putstore {
 7820:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7821:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7822:    if (!$uname) { $uname=$env{'user.name'}; }
 7823:    my $uhome=&homeserver($uname,$udomain);
 7824:    my $items='';
 7825:    foreach my $key (keys(%$storehash)) {
 7826:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7827:    }
 7828:    $items=~s/\&$//;
 7829:    my $esc_symb=&escape($symb);
 7830:    my $esc_v=&escape($version);
 7831:    my $reply =
 7832:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7833: 	      $uhome);
 7834:    if (($tolog) && ($reply eq 'ok')) {
 7835:        my $namevalue='';
 7836:        foreach my $key (keys(%{$storehash})) {
 7837:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7838:        }
 7839:        my $ip = &get_requestor_ip();
 7840:        $namevalue .= 'ip='.&escape($ip).
 7841:                      '&host='.&escape($perlvar{'lonHostID'}).
 7842:                      '&version='.$esc_v.
 7843:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7844:        &courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7845:    }
 7846:    if ($reply eq 'unknown_cmd') {
 7847:        # gfall back to way things use to be done
 7848:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7849: 			    $uname);
 7850:    }
 7851:    return $reply;
 7852: }
 7853: 
 7854: sub old_putstore {
 7855:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7856:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7857:     if (!$uname) { $uname=$env{'user.name'}; }
 7858:     my $uhome=&homeserver($uname,$udomain);
 7859:     my %newstorehash;
 7860:     foreach my $item (keys(%$storehash)) {
 7861: 	my $key = $version.':'.&escape($symb).':'.$item;
 7862: 	$newstorehash{$key} = $storehash->{$item};
 7863:     }
 7864:     my $items='';
 7865:     my %allitems = ();
 7866:     foreach my $item (keys(%newstorehash)) {
 7867: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7868: 	    my $key = $1.':keys:'.$2;
 7869: 	    $allitems{$key} .= $3.':';
 7870: 	}
 7871: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7872:     }
 7873:     foreach my $item (keys(%allitems)) {
 7874: 	$allitems{$item} =~ s/\:$//;
 7875: 	$items.= $item.'='.$allitems{$item}.'&';
 7876:     }
 7877:     $items=~s/\&$//;
 7878:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7879: }
 7880: 
 7881: # ------------------------------------------------------ critical put interface
 7882: 
 7883: sub cput {
 7884:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7885:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7886:    if (!$uname) { $uname=$env{'user.name'}; }
 7887:    my $uhome=&homeserver($uname,$udomain);
 7888:    my $items='';
 7889:    foreach my $item (keys(%$storehash)) {
 7890:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7891:    }
 7892:    $items=~s/\&$//;
 7893:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7894: }
 7895: 
 7896: # -------------------------------------------------------------- eget interface
 7897: 
 7898: sub eget {
 7899:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7900:    my $items='';
 7901:    foreach my $item (@$storearr) {
 7902:        $items.=&escape($item).'&';
 7903:    }
 7904:    $items=~s/\&$//;
 7905:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7906:    if (!$uname) { $uname=$env{'user.name'}; }
 7907:    my $uhome=&homeserver($uname,$udomain);
 7908:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7909:    my @pairs=split(/\&/,$rep);
 7910:    my %returnhash=();
 7911:    my $i=0;
 7912:    foreach my $item (@$storearr) {
 7913:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7914:       $i++;
 7915:    }
 7916:    return %returnhash;
 7917: }
 7918: 
 7919: # ------------------------------------------------------------ tmpput interface
 7920: sub tmpput {
 7921:     my ($storehash,$server,$context)=@_;
 7922:     my $items='';
 7923:     foreach my $item (keys(%$storehash)) {
 7924: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7925:     }
 7926:     $items=~s/\&$//;
 7927:     if (defined($context)) {
 7928:         $items .= ':'.&escape($context);
 7929:     }
 7930:     return &reply("tmpput:$items",$server);
 7931: }
 7932: 
 7933: # ------------------------------------------------------------ tmpget interface
 7934: sub tmpget {
 7935:     my ($token,$server)=@_;
 7936:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7937:     my $rep=&reply("tmpget:$token",$server);
 7938:     my %returnhash;
 7939:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7940:         return %returnhash;
 7941:     }
 7942:     foreach my $item (split(/\&/,$rep)) {
 7943: 	my ($key,$value)=split(/=/,$item);
 7944: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7945:     }
 7946:     return %returnhash;
 7947: }
 7948: 
 7949: # ------------------------------------------------------------ tmpdel interface
 7950: sub tmpdel {
 7951:     my ($token,$server)=@_;
 7952:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7953:     return &reply("tmpdel:$token",$server);
 7954: }
 7955: 
 7956: # ------------------------------------------------------------ get_timebased_id 
 7957: 
 7958: sub get_timebased_id {
 7959:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7960:         $maxtries) = @_;
 7961:     my ($newid,$error,$dellock);
 7962:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7963:         return ('','ok','invalid call to get suffix');
 7964:     }
 7965: 
 7966: # set defaults for any optional args for which values were not supplied
 7967:     if ($who eq '') {
 7968:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7969:     }
 7970:     if (!$locktries) {
 7971:         $locktries = 3;
 7972:     }
 7973:     if (!$maxtries) {
 7974:         $maxtries = 10;
 7975:     }
 7976:     
 7977:     if (($cdom eq '') || ($cnum eq '')) {
 7978:         if ($env{'request.course.id'}) {
 7979:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7980:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7981:         }
 7982:         if (($cdom eq '') || ($cnum eq '')) {
 7983:             return ('','ok','call to get suffix not in course context');
 7984:         }
 7985:     }
 7986: 
 7987: # construct locking item
 7988:     my $lockhash = {
 7989:                       $prefix."\0".'locked_'.$keyid => $who,
 7990:                    };
 7991:     my $tries = 0;
 7992: 
 7993: # attempt to get lock on nohist_$namespace file
 7994:     my $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7995:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7996:         $tries ++;
 7997:         sleep 1;
 7998:         $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7999:     }
 8000: 
 8001: # attempt to get unique identifier, based on current timestamp
 8002:     if ($gotlock eq 'ok') {
 8003:         my %inuse = &dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 8004:         my $id = time;
 8005:         $newid = $id;
 8006:         if ($idtype eq 'addcode') {
 8007:             $newid .= &sixnum_code();
 8008:         }
 8009:         my $idtries = 0;
 8010:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 8011:             if ($idtype eq 'concat') {
 8012:                 $newid = $id.$idtries;
 8013:             } elsif ($idtype eq 'addcode') {
 8014:                 $newid = $newid.&sixnum_code();
 8015:             } else {
 8016:                 $newid ++;
 8017:             }
 8018:             $idtries ++;
 8019:         }
 8020:         if (!exists($inuse{$prefix."\0".$newid})) {
 8021:             my %new_item =  (
 8022:                               $prefix."\0".$newid => $who,
 8023:                             );
 8024:             my $putresult = &put('nohist_'.$namespace,\%new_item,
 8025:                                                  $cdom,$cnum);
 8026:             if ($putresult ne 'ok') {
 8027:                 undef($newid);
 8028:                 $error = 'error saving new item: '.$putresult;
 8029:             }
 8030:         } else {
 8031:              undef($newid);
 8032:              $error = ('error: no unique suffix available for the new item ');
 8033:         }
 8034: #  remove lock
 8035:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 8036:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 8037:     } else {
 8038:         $error = "error: could not obtain lockfile\n";
 8039:         $dellock = 'ok';
 8040:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 8041:             $dellock = 'nolock';
 8042:         }
 8043:     }
 8044:     return ($newid,$dellock,$error);
 8045: }
 8046: 
 8047: sub sixnum_code {
 8048:     my $code;
 8049:     for (0..6) {
 8050:         $code .= int( rand(9) );
 8051:     }
 8052:     return $code;
 8053: }
 8054: 
 8055: # -------------------------------------------------- portfolio access checking
 8056: 
 8057: sub portfolio_access {
 8058:     my ($requrl,$clientip) = @_;
 8059:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 8060:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 8061:     if ($result) {
 8062:         my %setters;
 8063:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 8064:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 8065:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
 8066:             if (($startblock && $endblock) || ($by_ip)) {
 8067:                 return 'B';
 8068:             }
 8069:         } else {
 8070:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 8071:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 8072:             if (($startblock && $endblock) || ($by_ip)) {
 8073:                 return 'B';
 8074:             }
 8075:         }
 8076:     }
 8077:     if ($result eq 'ok') {
 8078:        return 'F';
 8079:     } elsif ($result =~ /^[^:]+:guest_/) {
 8080:        return 'A';
 8081:     }
 8082:     return '';
 8083: }
 8084: 
 8085: sub get_portfolio_access {
 8086:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash,$portaccessref) = @_;
 8087: 
 8088:     if (!ref($access_hash)) {
 8089: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 8090: 	my %access_controls = &get_access_controls($current_perms,$group,
 8091: 						   $file_name);
 8092: 	$access_hash = $access_controls{$file_name};
 8093:     }
 8094: 
 8095:     my $portaccess;
 8096:     if (ref($portaccess) eq 'SCALAR') {
 8097:         $portaccess = $$portaccessref;
 8098:     } else {
 8099:         $portaccess = &usertools_access($unum,$udom,'portaccess',undef,'tools');
 8100:     }
 8101: 
 8102:     my ($public,$guest,@domains,@users,@courses,@groups,@ips,@userips);
 8103:     my $now = time;
 8104:     if (ref($access_hash) eq 'HASH') {
 8105:         foreach my $key (keys(%{$access_hash})) {
 8106:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 8107:             next if (($scope ne 'ip') && ($portaccess == 0));
 8108:             if ($start > $now) {
 8109:                 next;
 8110:             }
 8111:             if ($end && $end<$now) {
 8112:                 next;
 8113:             }
 8114:             if ($scope eq 'public') {
 8115:                 $public = $key;
 8116:                 last;
 8117:             } elsif ($scope eq 'guest') {
 8118:                 $guest = $key;
 8119:             } elsif ($scope eq 'domains') {
 8120:                 push(@domains,$key);
 8121:             } elsif ($scope eq 'users') {
 8122:                 push(@users,$key);
 8123:             } elsif ($scope eq 'course') {
 8124:                 push(@courses,$key);
 8125:             } elsif ($scope eq 'group') {
 8126:                 push(@groups,$key);
 8127:             } elsif ($scope eq 'ip') {
 8128:                 push(@ips,$key);
 8129:             } elsif ($scope eq 'userip') {
 8130:                 push(@userips,$key);
 8131:             }
 8132:         }
 8133:         if ($public) {
 8134:             return 'ok';
 8135:         } elsif (@ips > 0) {
 8136:             my $allowed;
 8137:             foreach my $ipkey (@ips) {
 8138:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 8139:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 8140:                         $allowed = 1;
 8141:                         last; 
 8142:                     }
 8143:                 }
 8144:             }
 8145:             if ($allowed) {
 8146:                 return 'ok';
 8147:             }
 8148:         } elsif (@userips > 0) {
 8149:             my $allowed;
 8150:             foreach my $useripkey (@userips) {
 8151:                 if (ref($access_hash->{$useripkey}{'ip'}) eq 'ARRAY') {
 8152:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$useripkey}{'ip'}}),$clientip)) {
 8153:                         $allowed = 1;
 8154:                         last;
 8155:                     }
 8156:                 }
 8157:             }
 8158:             if ($allowed) {
 8159:                 return 'ok';
 8160:             }
 8161:         }
 8162:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 8163:             if ($guest) {
 8164:                 return $guest;
 8165:             }
 8166:         } else {
 8167:             if (@domains > 0) {
 8168:                 foreach my $domkey (@domains) {
 8169:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 8170:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 8171:                             return 'ok';
 8172:                         }
 8173:                     }
 8174:                 }
 8175:             }
 8176:             if (@users > 0) {
 8177:                 foreach my $userkey (@users) {
 8178:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 8179:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 8180:                             if (ref($item) eq 'HASH') {
 8181:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 8182:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 8183:                                     return 'ok';
 8184:                                 }
 8185:                             }
 8186:                         }
 8187:                     } 
 8188:                 }
 8189:             }
 8190:             my %roleshash;
 8191:             my @courses_and_groups = @courses;
 8192:             push(@courses_and_groups,@groups); 
 8193:             if (@courses_and_groups > 0) {
 8194:                 my (%allgroups,%allroles); 
 8195:                 my ($start,$end,$role,$sec,$group);
 8196:                 foreach my $envkey (%env) {
 8197:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 8198:                         my $cid = $2.'_'.$3; 
 8199:                         if ($1 eq 'gr') {
 8200:                             $group = $4;
 8201:                             $allgroups{$cid}{$group} = $env{$envkey};
 8202:                         } else {
 8203:                             if ($4 eq '') {
 8204:                                 $sec = 'none';
 8205:                             } else {
 8206:                                 $sec = $4;
 8207:                             }
 8208:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 8209:                         }
 8210:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 8211:                         my $cid = $2.'_'.$3;
 8212:                         if ($4 eq '') {
 8213:                             $sec = 'none';
 8214:                         } else {
 8215:                             $sec = $4;
 8216:                         }
 8217:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 8218:                     }
 8219:                 }
 8220:                 if (keys(%allroles) == 0) {
 8221:                     return;
 8222:                 }
 8223:                 foreach my $key (@courses_and_groups) {
 8224:                     my %content = %{$$access_hash{$key}};
 8225:                     my $cnum = $content{'number'};
 8226:                     my $cdom = $content{'domain'};
 8227:                     my $cid = $cdom.'_'.$cnum;
 8228:                     if (!exists($allroles{$cid})) {
 8229:                         next;
 8230:                     }    
 8231:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 8232:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 8233:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 8234:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 8235:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 8236:                         foreach my $role (keys(%{$allroles{$cid}})) {
 8237:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 8238:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 8239:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 8240:                                         if (grep/^all$/,@sections) {
 8241:                                             return 'ok';
 8242:                                         } else {
 8243:                                             if (grep/^$sec$/,@sections) {
 8244:                                                 return 'ok';
 8245:                                             }
 8246:                                         }
 8247:                                     }
 8248:                                 }
 8249:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 8250:                                     if (grep/^none$/,@groups) {
 8251:                                         return 'ok';
 8252:                                     }
 8253:                                 } else {
 8254:                                     if (grep/^all$/,@groups) {
 8255:                                         return 'ok';
 8256:                                     } 
 8257:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 8258:                                         if (grep/^$group$/,@groups) {
 8259:                                             return 'ok';
 8260:                                         }
 8261:                                     }
 8262:                                 } 
 8263:                             }
 8264:                         }
 8265:                     }
 8266:                 }
 8267:             }
 8268:             if ($guest) {
 8269:                 return $guest;
 8270:             }
 8271:         }
 8272:     }
 8273:     return;
 8274: }
 8275: 
 8276: sub course_group_datechecker {
 8277:     my ($dates,$now,$status) = @_;
 8278:     my ($start,$end) = split(/\./,$dates);
 8279:     if (!$start && !$end) {
 8280:         return 'ok';
 8281:     }
 8282:     if (grep/^active$/,@{$status}) {
 8283:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 8284:             return 'ok';
 8285:         }
 8286:     }
 8287:     if (grep/^previous$/,@{$status}) {
 8288:         if ($end > $now ) {
 8289:             return 'ok';
 8290:         }
 8291:     }
 8292:     if (grep/^future$/,@{$status}) {
 8293:         if ($start > $now) {
 8294:             return 'ok';
 8295:         }
 8296:     }
 8297:     return; 
 8298: }
 8299: 
 8300: sub parse_portfolio_url {
 8301:     my ($url) = @_;
 8302: 
 8303:     my ($type,$udom,$unum,$group,$file_name);
 8304:     
 8305:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 8306: 	$type = 1;
 8307:         $udom = $1;
 8308:         $unum = $2;
 8309:         $file_name = $3;
 8310:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 8311: 	$type = 2;
 8312:         $udom = $1;
 8313:         $unum = $2;
 8314:         $group = $3;
 8315:         $file_name = $3.'/'.$4;
 8316:     }
 8317:     if (wantarray) {
 8318: 	return ($type,$udom,$unum,$file_name,$group);
 8319:     }
 8320:     return $type;
 8321: }
 8322: 
 8323: sub is_portfolio_url {
 8324:     my ($url) = @_;
 8325:     return scalar(&parse_portfolio_url($url));
 8326: }
 8327: 
 8328: sub is_portfolio_file {
 8329:     my ($file) = @_;
 8330:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 8331:         return 1;
 8332:     }
 8333:     return;
 8334: }
 8335: 
 8336: sub is_coursetool_logo {
 8337:     my ($uri) = @_;
 8338:     if ($env{'request.course.id'}) {
 8339:         my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 8340:         if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
 8341:             return 1;
 8342:         }
 8343:     }
 8344:     return;
 8345: }
 8346: 
 8347: sub usertools_access {
 8348:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 8349:     my ($access,%tools);
 8350:     if ($context eq '') {
 8351:         $context = 'tools';
 8352:     }
 8353:     if ($context eq 'requestcourses') {
 8354:         %tools = (
 8355:                       official   => 1,
 8356:                       unofficial => 1,
 8357:                       community  => 1,
 8358:                       textbook   => 1,
 8359:                       placement  => 1,
 8360:                       lti        => 1,
 8361:                  );
 8362:     } elsif ($context eq 'requestauthor') {
 8363:         %tools = (
 8364:                       requestauthor => 1,
 8365:                  );
 8366:     } elsif ($context eq 'authordefaults') {
 8367:         %tools = (
 8368:                       webdav    => 1,
 8369:                  );
 8370:     } else {
 8371:         %tools = (
 8372:                       aboutme   => 1,
 8373:                       blog      => 1,
 8374:                       webdav    => 1,
 8375:                       portfolio => 1,
 8376:                       portaccess => 1,
 8377:                       timezone  => 1,
 8378:                  );
 8379:     }
 8380:     return if (!defined($tools{$tool}));
 8381: 
 8382:     if (($udom eq '') || ($uname eq '')) {
 8383:         $udom = $env{'user.domain'};
 8384:         $uname = $env{'user.name'};
 8385:     }
 8386: 
 8387:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 8388:         if ($action ne 'reload') {
 8389:             if ($context eq 'requestcourses') {
 8390:                 return $env{'environment.canrequest.'.$tool};
 8391:             } elsif ($context eq 'requestauthor') {
 8392:                 return $env{'environment.canrequest.author'};
 8393:             } elsif ($context eq 'authordefaults') {
 8394:                 if ($tool eq 'webdav') {
 8395:                     return $env{'environment.availabletools.'.$tool};
 8396:                 }
 8397:             } else {
 8398:                 return $env{'environment.availabletools.'.$tool};
 8399:             }
 8400:         }
 8401:     }
 8402: 
 8403:     my ($toolstatus,$inststatus,$envkey);
 8404:     if ($context eq 'requestauthor') {
 8405:         $envkey = $context;
 8406:     } elsif ($context eq 'authordefaults') {
 8407:         if ($tool eq 'webdav') {
 8408:             $envkey = 'tools.'.$tool;
 8409:         }
 8410:     } else {
 8411:         $envkey = $context.'.'.$tool;
 8412:     }
 8413: 
 8414:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 8415:          ($action ne 'reload')) {
 8416:         $toolstatus = $env{'environment.'.$envkey};
 8417:         $inststatus = $env{'environment.inststatus'};
 8418:     } else {
 8419:         if (ref($userenvref) eq 'HASH') {
 8420:             $toolstatus = $userenvref->{$envkey};
 8421:             $inststatus = $userenvref->{'inststatus'};
 8422:         } else {
 8423:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 8424:             $toolstatus = $userenv{$envkey};
 8425:             $inststatus = $userenv{'inststatus'};
 8426:         }
 8427:     }
 8428: 
 8429:     if ($toolstatus ne '') {
 8430:         if ($toolstatus) {
 8431:             $access = 1;
 8432:         } else {
 8433:             $access = 0;
 8434:         }
 8435:         return $access;
 8436:     }
 8437: 
 8438:     my ($is_adv,%domdef);
 8439:     if (ref($is_advref) eq 'HASH') {
 8440:         $is_adv = $is_advref->{'is_adv'};
 8441:     } else {
 8442:         $is_adv = &is_advanced_user($udom,$uname);
 8443:     }
 8444:     if (ref($domdefref) eq 'HASH') {
 8445:         %domdef = %{$domdefref};
 8446:     } else {
 8447:         %domdef = &get_domain_defaults($udom);
 8448:     }
 8449:     if (ref($domdef{$tool}) eq 'HASH') {
 8450:         if ($is_adv) {
 8451:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 8452:                 if ($domdef{$tool}{'_LC_adv'}) { 
 8453:                     $access = 1;
 8454:                 } else {
 8455:                     $access = 0;
 8456:                 }
 8457:                 return $access;
 8458:             }
 8459:         }
 8460:         if ($inststatus ne '') {
 8461:             my ($hasaccess,$hasnoaccess);
 8462:             foreach my $affiliation (split(/:/,$inststatus)) {
 8463:                 if ($domdef{$tool}{$affiliation} ne '') { 
 8464:                     if ($domdef{$tool}{$affiliation}) {
 8465:                         $hasaccess = 1;
 8466:                     } else {
 8467:                         $hasnoaccess = 1;
 8468:                     }
 8469:                 }
 8470:             }
 8471:             if ($hasaccess || $hasnoaccess) {
 8472:                 if ($hasaccess) {
 8473:                     $access = 1;
 8474:                 } elsif ($hasnoaccess) {
 8475:                     $access = 0; 
 8476:                 }
 8477:                 return $access;
 8478:             }
 8479:         } else {
 8480:             if ($domdef{$tool}{'default'} ne '') {
 8481:                 if ($domdef{$tool}{'default'}) {
 8482:                     $access = 1;
 8483:                 } elsif ($domdef{$tool}{'default'} == 0) {
 8484:                     $access = 0;
 8485:                 }
 8486:                 return $access;
 8487:             }
 8488:         }
 8489:     } else {
 8490:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 8491:             $access = 1;
 8492:         } else {
 8493:             $access = 0;
 8494:         }
 8495:         return $access;
 8496:     }
 8497: }
 8498: 
 8499: sub is_course_owner {
 8500:     my ($cdom,$cnum,$udom,$uname) = @_;
 8501:     if (($udom eq '') || ($uname eq '')) {
 8502:         $udom = $env{'user.domain'};
 8503:         $uname = $env{'user.name'};
 8504:     }
 8505:     unless (($udom eq '') || ($uname eq '')) {
 8506:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 8507:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 8508:                 return 1;
 8509:             } else {
 8510:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
 8511:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 8512:                     return 1;
 8513:                 }
 8514:             }
 8515:         }
 8516:     }
 8517:     return;
 8518: }
 8519: 
 8520: sub is_advanced_user {
 8521:     my ($udom,$uname,$nocache) = @_;
 8522:     my ($is_adv,$is_author,$use_cache,$hashid);
 8523:     if ($udom ne '' && $uname ne '') {
 8524:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 8525:             if (wantarray) {
 8526:                 return ($env{'user.adv'},$env{'user.author'});
 8527:             } else {
 8528:                 return $env{'user.adv'};
 8529:             }
 8530:         } elsif (!$nocache) {
 8531:             $use_cache = 1;
 8532:             $hashid = "$udom:$uname";  
 8533:             my ($info,$cached)=&is_cached_new('isadvau',$hashid);
 8534:             if ($cached) {
 8535:                 ($is_adv,$is_author) = split(/:/,$info);
 8536:                 if (wantarray) {
 8537:                     return ($is_adv,$is_author);
 8538:                 }
 8539:                 return $is_adv; 
 8540:             }
 8541:         }
 8542:     }
 8543:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 8544:     my %allroles;
 8545:     foreach my $role (keys(%roleshash)) {
 8546:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 8547:         my $area = '/'.$tdomain.'/'.$trest;
 8548:         if ($sec ne '') {
 8549:             $area .= '/'.$sec;
 8550:         }
 8551:         if (($area ne '') && ($trole ne '')) {
 8552:             my $spec=$trole.'.'.$area;
 8553:             if ($trole =~ /^cr\//) {
 8554:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 8555:             } elsif ($trole ne 'gr') {
 8556:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 8557:             }
 8558:             if ($trole eq 'au') {
 8559:                 $is_author = 1;
 8560:             }
 8561:         }
 8562:     }
 8563:     foreach my $role (keys(%allroles)) {
 8564:         last if ($is_adv);
 8565:         foreach my $item (split(/:/,$allroles{$role})) {
 8566:             if ($item ne '') {
 8567:                 my ($privilege,$restrictions)=split(/&/,$item);
 8568:                 if ($privilege eq 'adv') {
 8569:                     $is_adv = 1;
 8570:                     last;
 8571:                 }
 8572:             }
 8573:         }
 8574:     }
 8575:     if ($use_cache) {
 8576:         my $cachetime = 600;
 8577:         &do_cache_new('isadvau',$hashid,$is_adv.':'.$is_author,$cachetime);
 8578:     }
 8579:     if (wantarray) {
 8580:         return ($is_adv,$is_author);
 8581:     }
 8582:     return $is_adv;
 8583: }
 8584: 
 8585: sub check_can_request {
 8586:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8587:     my $canreq = 0;
 8588:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8589:         $uname = $env{'user.name'};
 8590:         $udom = $env{'user.domain'};
 8591:     }
 8592:     my ($types,$typename) = &Apache::loncommon::course_types();
 8593:     my @options = ('approval','validate','autolimit');
 8594:     my $optregex = join('|',@options);
 8595:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8596:         my %willtrust;
 8597:         foreach my $type (@{$types}) {
 8598:             if (&usertools_access($uname,$udom,$type,undef,
 8599:                                   'requestcourses')) {
 8600:                 $canreq ++;
 8601:                 if (ref($request_domains) eq 'HASH') {
 8602:                     push(@{$request_domains->{$type}},$udom);
 8603:                 }
 8604:                 if ($dom eq $udom) {
 8605:                     $can_request->{$type} = 1;
 8606:                 }
 8607:             }
 8608:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8609:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8610:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8611:                 if (@curr > 0) {
 8612:                     foreach my $item (@curr) {
 8613:                         if (ref($request_domains) eq 'HASH') {
 8614:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8615:                             if ($otherdom ne '') {
 8616:                                 unless (exists($willtrust{$otherdom})) {
 8617:                                     $willtrust{$otherdom} = &will_trust('reqcrs',$env{'user.domain'},$otherdom);
 8618:                                 }
 8619:                                 if ($willtrust{$otherdom}) {
 8620:                                     if (ref($request_domains->{$type}) eq 'ARRAY') {
 8621:                                         unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8622:                                             push(@{$request_domains->{$type}},$otherdom);
 8623:                                         }
 8624:                                     } else {
 8625:                                         push(@{$request_domains->{$type}},$otherdom);
 8626:                                     }
 8627:                                 }
 8628:                             }
 8629:                         }
 8630:                     }
 8631:                     unless ($dom eq $env{'user.domain'}) {
 8632:                         $canreq ++;
 8633:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8634:                             $can_request->{$type} = 1;
 8635:                         }
 8636:                     }
 8637:                 }
 8638:             }
 8639:         }
 8640:     }
 8641:     return $canreq;
 8642: }
 8643: 
 8644: # ---------------------------------------------- Custom access rule evaluation
 8645: 
 8646: sub customaccess {
 8647:     my ($priv,$uri)=@_;
 8648:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8649:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8650:     $udom = &LONCAPA::clean_domain($udom);
 8651:     $ucrs = &LONCAPA::clean_username($ucrs);
 8652:     my $access=0;
 8653:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8654: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8655: 	if ($type eq 'user') {
 8656: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8657: 		my ($tdom,$tuname)=split(m{/},$scope);
 8658: 		if ($tdom) {
 8659: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8660: 		}
 8661: 		if ($tuname) {
 8662: 		    if ($tuname ne $env{'user.name'}) { next; }
 8663: 		}
 8664: 		$access=($effect eq 'allow');
 8665: 		last;
 8666: 	    }
 8667: 	} else {
 8668: 	    if ($role) {
 8669: 		if ($role ne $urole) { next; }
 8670: 	    }
 8671: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8672: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8673: 		if ($tdom) {
 8674: 		    if ($tdom ne $udom) { next; }
 8675: 		}
 8676: 		if ($tcrs) {
 8677: 		    if ($tcrs ne $ucrs) { next; }
 8678: 		}
 8679: 		if ($tsec) {
 8680: 		    if ($tsec ne $usec) { next; }
 8681: 		}
 8682: 		$access=($effect eq 'allow');
 8683: 		last;
 8684: 	    }
 8685: 	    if ($realm eq '' && $role eq '') {
 8686: 		$access=($effect eq 'allow');
 8687: 	    }
 8688: 	}
 8689:     }
 8690:     return $access;
 8691: }
 8692: 
 8693: # ------------------------------------------------- Check for a user privilege
 8694: 
 8695: sub allowed {
 8696:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
 8697:     my $ver_orguri=$uri;
 8698:     $uri=&deversion($uri);
 8699:     my $orguri=$uri;
 8700:     $uri=&declutter($uri);
 8701: 
 8702:     if ($priv eq 'evb') {
 8703: # Evade communication block restrictions for specified role in a course or domain
 8704:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8705:             return $1;
 8706:         } else {
 8707:             return;
 8708:         }
 8709:     }
 8710: 
 8711:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8712: # Free bre access to adm and meta resources
 8713:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8714: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8715: 	&& ($priv eq 'bre')) {
 8716: 	return 'F';
 8717:     }
 8718: 
 8719: # Free bre access to user's own portfolio contents
 8720:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8721:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8722: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8723:         my %setters;
 8724:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
 8725:             &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 8726:         if (($startblock && $endblock) || ($by_ip)) {
 8727:             return 'B';
 8728:         } else {
 8729:             return 'F';
 8730:         }
 8731:     }
 8732: 
 8733: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8734:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8735:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8736:         if (exists($env{'request.course.id'})) {
 8737:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8738:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8739:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8740:                 my $courseprivid=$env{'request.course.id'};
 8741:                 $courseprivid=~s/\_/\//;
 8742:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8743:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8744:                     return $1; 
 8745:                 } else {
 8746:                     if ($env{'request.course.sec'}) {
 8747:                         $courseprivid.='/'.$env{'request.course.sec'};
 8748:                     }
 8749:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8750:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8751:                         return $2;
 8752:                     }
 8753:                 }
 8754:             }
 8755:         }
 8756:     }
 8757: 
 8758: # Free bre to public access
 8759: 
 8760:     if ($priv eq 'bre') {
 8761:         my $copyright;
 8762:         unless ($uri =~ /ext\.tool/) {
 8763:             $copyright=&metadata($uri,'copyright');
 8764:         }
 8765: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8766:            return 'F'; 
 8767:         }
 8768:         if ($copyright eq 'priv') {
 8769:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8770: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8771: 		return '';
 8772:             }
 8773:         }
 8774:         if ($copyright eq 'domain') {
 8775:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8776: 	    unless (($env{'user.domain'} eq $1) ||
 8777:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8778: 		return '';
 8779:             }
 8780:         }
 8781:         if ($env{'request.role'}=~ /li\.\//) {
 8782:             # Library role, so allow browsing of resources in this domain.
 8783:             return 'F';
 8784:         }
 8785:         if ($copyright eq 'custom') {
 8786: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8787:         }
 8788:     }
 8789:     # Domain coordinator is trying to create a course
 8790:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8791:         # uri is the requested domain in this case.
 8792:         # comparison to 'request.role.domain' shows if the user has selected
 8793:         # a role of dc for the domain in question.
 8794:         return 'F' if ($uri eq $env{'request.role.domain'});
 8795:     }
 8796: 
 8797:     my $thisallowed='';
 8798:     my $statecond=0;
 8799:     my $courseprivid='';
 8800: 
 8801:     my $ownaccess;
 8802:     # Community Coordinator or Assistant Co-author browsing resource space.
 8803:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8804:         if ($uri eq '') {
 8805:             $ownaccess = 1;
 8806:         } else {
 8807:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8808:                 my $udom = $env{'user.domain'};
 8809:                 my $uname = $env{'user.name'};
 8810:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8811:                     $ownaccess = 1;
 8812:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8813:                     unless ($uri =~ m{\.\./}) {
 8814:                         $ownaccess = 1;
 8815:                     }
 8816:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8817:                     my $now = time;
 8818:                     if ($uri =~ m{^([^/]+)/?$}) {
 8819:                         my $adom = $1;
 8820:                         foreach my $key (keys(%env)) {
 8821:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8822:                                 my ($start,$end) = split(/\./,$env{$key});
 8823:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8824:                                     $ownaccess = 1;
 8825:                                     last;
 8826:                                 }
 8827:                             }
 8828:                         }
 8829:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8830:                         my $adom = $1;
 8831:                         my $aname = $2;
 8832:                         foreach my $role ('ca','aa') { 
 8833:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8834:                                 my ($start,$end) =
 8835:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
 8836:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8837:                                     $ownaccess = 1;
 8838:                                     last;
 8839:                                 }
 8840:                             }
 8841:                         }
 8842:                     }
 8843:                 }
 8844:             }
 8845:         }
 8846:     }
 8847: 
 8848: # Course
 8849: 
 8850:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8851:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8852:             $thisallowed.=$1;
 8853:         }
 8854:     }
 8855: 
 8856: # Domain
 8857: 
 8858:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8859:        =~/\Q$priv\E\&([^\:]*)/) {
 8860:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8861:             $thisallowed.=$1;
 8862:         }
 8863:     }
 8864: 
 8865: # User who is not author or co-author might still be able to edit
 8866: # resource of an author in the domain (e.g., if Domain Coordinator).
 8867:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8868:         (&allowed('mdc',$env{'request.course.id'}))) {
 8869:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8870:             $thisallowed.=$1;
 8871:         }
 8872:     }
 8873: 
 8874: # Course: uri itself is a course
 8875:     my $courseuri=$uri;
 8876:     $courseuri=~s/\_(\d)/\/$1/;
 8877:     $courseuri=~s/^([^\/])/\/$1/;
 8878: 
 8879:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8880:        =~/\Q$priv\E\&([^\:]*)/) {
 8881:         if ($priv eq 'mip') {
 8882:             my $rem = $1;
 8883:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8884:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8885:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8886:                 if ($cdom ne '') {
 8887:                     my %passwdconf = &get_passwdconf($cdom);
 8888:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8889:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8890:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8891:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8892:                                 unless (@inststatuses) {
 8893:                                     @inststatuses = ('default');
 8894:                                 }
 8895:                                 foreach my $status (@inststatuses) {
 8896:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8897:                                         $thisallowed.=$rem;
 8898:                                     }
 8899:                                 }
 8900:                             }
 8901:                         }
 8902:                     }
 8903:                 }
 8904:             }
 8905:         } else {
 8906:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8907:                 $thisallowed.=$1;
 8908:             }
 8909:         }
 8910:     }
 8911: 
 8912: # URI is an uploaded document for this course, default permissions don't matter
 8913: # not allowing 'edit' access (editupload) to uploaded course docs
 8914:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8915: 	$thisallowed='';
 8916:         my ($match)=&is_on_map($uri);
 8917:         if ($match) {
 8918:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8919:                   =~/\Q$priv\E\&([^\:]*)/) {
 8920:                 my $value = $1;
 8921:                 my $deeplinkblock;
 8922:                 unless ($nodeeplinkcheck) {
 8923:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8924:                 }
 8925:                 if ($deeplinkblock) {
 8926:                     $thisallowed='D';
 8927:                 } elsif ($noblockcheck) {
 8928:                     $thisallowed.=$value;
 8929:                 } else {
 8930:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8931:                     if (@blockers > 0) {
 8932:                         $thisallowed = 'B';
 8933:                     } else {
 8934:                         $thisallowed.=$value;
 8935:                     }
 8936:                 }
 8937:             }
 8938:         } else {
 8939:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8940:             if ($refuri) {
 8941:                 if ($refuri =~ m|^/adm/|) {
 8942:                     $thisallowed='F';
 8943:                 } else {
 8944:                     $refuri=&declutter($refuri);
 8945:                     my ($match) = &is_on_map($refuri);
 8946:                     if ($match) {
 8947:                         my $deeplinkblock;
 8948:                         unless ($nodeeplinkcheck) {
 8949:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8950:                         }
 8951:                         if ($deeplinkblock) {
 8952:                             $thisallowed='D';
 8953:                         } elsif ($noblockcheck) {
 8954:                             $thisallowed='F';
 8955:                         } else {
 8956:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8957:                             if (@blockers > 0) {
 8958:                                 $thisallowed = 'B';
 8959:                             } else {
 8960:                                 $thisallowed='F';
 8961:                             }
 8962:                         }
 8963:                     }
 8964:                 }
 8965:             }
 8966:         }
 8967:     }
 8968: 
 8969:     if ($priv eq 'bre'
 8970: 	&& $thisallowed ne 'F' 
 8971: 	&& $thisallowed ne '2'
 8972: 	&& &is_portfolio_url($uri)) {
 8973: 	$thisallowed = &portfolio_access($uri,$clientip);
 8974:     }
 8975: 
 8976: # Full access at system, domain or course-wide level? Exit.
 8977:     if ($thisallowed=~/F/) {
 8978: 	return 'F';
 8979:     }
 8980: 
 8981: # If this is generating or modifying users, exit with special codes
 8982: 
 8983:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:vca:vaa:'=~/\:\Q$priv\E\:/) {
 8984: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8985: 	    my ($audom,$auname)=split('/',$uri);
 8986: # no author name given, so this just checks on the general right to make a co-author in this domain
 8987: 	    unless ($auname) { return $thisallowed; }
 8988: # an author name is given, so we are about to actually make a co-author for a certain account
 8989: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8990: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8991: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8992: 	} elsif (($priv eq 'vca') || ($priv eq 'vaa')) {
 8993:             my ($audom,$auname)=split('/',$uri);
 8994:             unless ($auname) { return $thisallowed; }
 8995:             unless (($env{'request.role'} eq "dc./$audom") ||
 8996:                     ($env{'request.role'} eq "ca./$uri")) {
 8997:                 return '';
 8998:             }
 8999: 	}
 9000: 	return $thisallowed;
 9001:     }
 9002: #
 9003: # Gathered so far: system, domain and course wide privileges
 9004: #
 9005: # Course: See if uri or referer is an individual resource that is part of 
 9006: # the course
 9007: 
 9008:     if ($env{'request.course.id'}) {
 9009: 
 9010:         if ($priv eq 'bre') {
 9011:             if (&is_coursetool_logo($uri)) {
 9012:                 return 'F';
 9013:             }
 9014:         }
 9015: 
 9016: # If this is modifying password (internal auth) domains must match for user and user's role.
 9017: 
 9018:         if ($priv eq 'mip') {
 9019:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 9020:                 return $thisallowed;
 9021:             } else {
 9022:                 return '';
 9023:             }
 9024:         }
 9025: 
 9026:        $courseprivid=$env{'request.course.id'};
 9027:        if ($env{'request.course.sec'}) {
 9028:           $courseprivid.='/'.$env{'request.course.sec'};
 9029:        }
 9030:        $courseprivid=~s/\_/\//;
 9031:        my $checkreferer=1;
 9032:        my ($match,$cond)=&is_on_map($uri);
 9033:        if ($match) {
 9034:            $statecond=$cond;
 9035:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 9036:                =~/\Q$priv\E\&([^\:]*)/) {
 9037:                my $value = $1;
 9038:                if ($priv eq 'bre') {
 9039:                    my $deeplinkblock;
 9040:                    unless ($nodeeplinkcheck) {
 9041:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 9042:                    }
 9043:                    if ($deeplinkblock) {
 9044:                        $thisallowed = 'D';
 9045:                    } elsif ($noblockcheck) {
 9046:                        $thisallowed.=$value;
 9047:                    } else {
 9048:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 9049:                        if (@blockers > 0) {
 9050:                            $thisallowed = 'B';
 9051:                        } else {
 9052:                            $thisallowed.=$value;
 9053:                        }
 9054:                    }
 9055:                } else {
 9056:                    $thisallowed.=$value;
 9057:                }
 9058:                $checkreferer=0;
 9059:            }
 9060:        }
 9061: 
 9062:        if ($checkreferer) {
 9063: 	  my $refuri=$env{'httpref.'.$orguri};
 9064:             unless ($refuri) {
 9065:                 foreach my $key (keys(%env)) {
 9066: 		    if ($key=~/^httpref\..*\*/) {
 9067: 			my $pattern=$key;
 9068:                         $pattern=~s/^httpref\.\/res\///;
 9069:                         $pattern=~s/\*/\[\^\/\]\+/g;
 9070:                         $pattern=~s/\//\\\//g;
 9071:                         if ($orguri=~/$pattern/) {
 9072: 			    $refuri=$env{$key};
 9073:                         }
 9074:                     }
 9075:                 }
 9076:             }
 9077: 
 9078:          if ($refuri) { 
 9079: 	  $refuri=&declutter($refuri);
 9080:           my ($match,$cond)=&is_on_map($refuri);
 9081:             if ($match) {
 9082:               my $refstatecond=$cond;
 9083:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 9084:                   =~/\Q$priv\E\&([^\:]*)/) {
 9085:                   my $value = $1;
 9086:                   if ($priv eq 'bre') {
 9087:                       my $deeplinkblock;
 9088:                       unless ($nodeeplinkcheck) {
 9089:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 9090:                       }
 9091:                       if ($deeplinkblock) {
 9092:                           $thisallowed = 'D';
 9093:                       } elsif ($noblockcheck) {
 9094:                           $thisallowed.=$value;
 9095:                       } else {
 9096:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 9097:                           if (@blockers > 0) {
 9098:                               $thisallowed = 'B';
 9099:                           } else {
 9100:                               $thisallowed.=$value;
 9101:                           }
 9102:                       }
 9103:                   } else {
 9104:                       $thisallowed.=$value;
 9105:                   }
 9106:                   $uri=$refuri;
 9107:                   $statecond=$refstatecond;
 9108:               }
 9109:           }
 9110:         }
 9111:        }
 9112:    }
 9113: 
 9114: #
 9115: # Gathered now: all privileges that could apply, and condition number
 9116: # 
 9117: #
 9118: # Full or no access?
 9119: #
 9120: 
 9121:     if ($thisallowed=~/F/) {
 9122: 	return 'F';
 9123:     }
 9124: 
 9125:     unless ($thisallowed) {
 9126:         return '';
 9127:     }
 9128: 
 9129: # Restrictions exist, deal with them
 9130: #
 9131: #   C:according to course preferences
 9132: #   R:according to resource settings
 9133: #   L:unless locked
 9134: #   X:according to user session state
 9135: #
 9136: 
 9137: # Possibly locked functionality, check all courses
 9138: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
 9139: # Locks might take effect only after 10 minutes cache expiration for other
 9140: # courses, and 2 minutes for current course, in which user has st or ta role
 9141: # which is neither expired nor a future role (unless current course).
 9142: 
 9143:     my ($needlockcheck,$now,$crsonly);
 9144:     if ($thisallowed=~/L/) {
 9145:         $now = time;
 9146:         if ($priv eq 'bre') {
 9147:             if ($uri ne '') {
 9148:                 if ($orguri =~ m{^/+res/}) {
 9149:                     if ($uri =~ m{^lib/templates/}) {
 9150:                         if ($env{'request.course.id'}) {
 9151:                             $crsonly = 1;
 9152:                             $needlockcheck = 1;
 9153:                         }
 9154:                     } else {
 9155:                         $needlockcheck = 1;
 9156:                     }
 9157:                 } elsif ($env{'request.course.id'}) {
 9158:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
 9159:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
 9160:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
 9161:                         $crsonly = 1;
 9162:                     }
 9163:                     $needlockcheck = 1;
 9164:                 }
 9165:             }
 9166:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
 9167:             $needlockcheck = 1;
 9168:         }
 9169:     }
 9170:     if ($needlockcheck) {
 9171:         foreach my $envkey (keys(%env)) {
 9172:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 9173:                my $courseid=$2;
 9174:                my $roleid=$1.'.'.$2;
 9175:                $courseid=~s/^\///;
 9176:                unless ($env{'request.role'} eq $roleid) {
 9177:                    my ($start,$end) = split(/\./,$env{$envkey});
 9178:                    next unless (($now >= $start) && (!$end || $end > $now));
 9179:                }
 9180:                my $expiretime=600;
 9181:                if ($env{'request.role'} eq $roleid) {
 9182: 		  $expiretime=120;
 9183:                }
 9184: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 9185:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 9186:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 9187: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 9188:                }
 9189:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 9190:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 9191: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 9192:                        &log($env{'user.domain'},$env{'user.name'},
 9193:                             $env{'user.home'},
 9194:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 9195:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 9196:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 9197: 		       return '';
 9198:                    }
 9199:                }
 9200:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 9201:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 9202: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
 9203:                        &log($env{'user.domain'},$env{'user.name'},
 9204:                             $env{'user.home'},
 9205:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 9206:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 9207:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 9208: 		       return '';
 9209:                    }
 9210:                }
 9211: 	   }
 9212:        }
 9213:     }
 9214: 
 9215: #
 9216: # Rest of the restrictions depend on selected course
 9217: #
 9218: 
 9219:     unless ($env{'request.course.id'}) {
 9220: 	if ($thisallowed eq 'A') {
 9221: 	    return 'A';
 9222:         } elsif ($thisallowed eq 'B') {
 9223:             return 'B';
 9224: 	} else {
 9225: 	    return '1';
 9226: 	}
 9227:     }
 9228: 
 9229: #
 9230: # Now user is definitely in a course
 9231: #
 9232: 
 9233: 
 9234: # Course preferences
 9235: 
 9236:    if ($thisallowed=~/C/) {
 9237:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 9238:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 9239:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 9240: 	   =~/\Q$rolecode\E/) {
 9241: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 9242: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 9243: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 9244: 			$env{'request.course.id'});
 9245: 	   }
 9246:            return '';
 9247:        }
 9248: 
 9249:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 9250: 	   =~/\Q$unamedom\E/) {
 9251: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 9252: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 9253: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 9254: 			$env{'request.course.id'});
 9255: 	   }
 9256:            return '';
 9257:        }
 9258:    }
 9259: 
 9260: # Resource preferences
 9261: 
 9262:    if ($thisallowed=~/R/) {
 9263:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 9264:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 9265: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 9266: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 9267: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 9268: 	   }
 9269: 	   return '';
 9270:        }
 9271:    }
 9272: 
 9273: # Restricted for deeplinked session?
 9274: 
 9275:     if ($env{'request.deeplink.login'}) {
 9276:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
 9277:             if (!$symb) { $symb=&symbread($uri,1); }
 9278:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
 9279:                 return '';
 9280:             }
 9281:         }
 9282:     }
 9283: 
 9284: # Restricted by state or randomout?
 9285: 
 9286:    if ($thisallowed=~/X/) {
 9287:       if ($env{'acc.randomout'}) {
 9288: 	 if (!$symb) { $symb=&symbread($uri,1); }
 9289:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 9290:             return ''; 
 9291:          }
 9292:       }
 9293:       if (&condval($statecond)) {
 9294: 	 return '2';
 9295:       } else {
 9296:          return '';
 9297:       }
 9298:    }
 9299: 
 9300:     if ($thisallowed eq 'A') {
 9301: 	return 'A';
 9302:     } elsif ($thisallowed eq 'B') {
 9303:         return 'B';
 9304:     } elsif ($thisallowed eq 'D') {
 9305:         return 'D';
 9306:     }
 9307:    return 'F';
 9308: }
 9309: 
 9310: # ------------------------------------------- Check construction space access
 9311: 
 9312: sub constructaccess {
 9313:     my ($url,$setpriv)=@_;
 9314: 
 9315: # We do not allow editing of previous versions of files
 9316:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 9317: 
 9318: # Get username and domain from URL
 9319:     my ($ownername,$ownerdomain,$ownerhome);
 9320: 
 9321:     ($ownerdomain,$ownername) =
 9322:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 9323: 
 9324: # The URL does not really point to any authorspace, forget it
 9325:     unless (($ownername) && ($ownerdomain)) { return ''; }
 9326: 
 9327: # Now we need to see if the user has access to the authorspace of
 9328: # $ownername at $ownerdomain
 9329: 
 9330:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 9331: # Real author for this?
 9332:        $ownerhome = $env{'user.home'};
 9333:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 9334:           return ($ownername,$ownerdomain,$ownerhome);
 9335:        }
 9336:     } elsif (&is_course($ownerdomain,$ownername)) {
 9337: # Course Authoring Space?
 9338:         if ($env{'request.course.id'}) {
 9339:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 9340:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 9341:                 if (&allowed('mdc',$env{'request.course.id'})) {
 9342:                     return if ($env{'course.'.$env{'request.course.id'}.'.internal.crsauthor'} eq '0');
 9343:                     unless ($env{'course.'.$env{'request.course.id'}.'.internal.crsauthor'}) {
 9344:                         my %domdefs = &get_domain_defaults($ownerdomain);
 9345:                         my $type = lc($env{'course.'.$env{'request.course.id'}.'.type'});
 9346:                         unless (($type eq 'community') || ($type eq 'placement')) {
 9347:                             $type = 'unofficial';
 9348:                             if ($env{'course.'.$env{'request.course.id'}.'internal.coursecode'} ne '') {
 9349:                                 $type = 'official';
 9350:                             } elsif ($env{'course.'.$env{'request.course.id'}.'internal.textbook'} ne '') {
 9351:                                 $type = 'textbook';
 9352:                             } else {
 9353:                                 $type = 'unofficial';
 9354:                             }
 9355:                         }
 9356:                         return if ($domdefs{$type.'crsauthor'} eq '0');
 9357:                     }
 9358:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 9359:                     return ($ownername,$ownerdomain,$ownerhome);
 9360:                 }
 9361:             }
 9362:         }
 9363:         return '';
 9364:     } else {
 9365: # Co-author for this?
 9366:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 9367:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 9368:             $ownerhome = &homeserver($ownername,$ownerdomain);
 9369:             return ($ownername,$ownerdomain,$ownerhome);
 9370:         }
 9371:     }
 9372: 
 9373: # We don't have any access right now. If we are not possibly going to do anything about this,
 9374: # we might as well leave
 9375:    unless ($setpriv) { return ''; }
 9376: 
 9377: # Backdoor access?
 9378:     my $allowed=&allowed('eco',$ownerdomain);
 9379: # Nope
 9380:     unless ($allowed) { return ''; }
 9381: # Looks like we may have access, but could be locked by the owner of the construction space
 9382:     if ($allowed eq 'U') {
 9383:         my %blocked=&get('environment',['domcoord.author'],
 9384:                          $ownerdomain,$ownername);
 9385: # Is blocked by owner
 9386:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 9387:     }
 9388:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 9389: # Grant temporary access
 9390:         my $then=$env{'user.login.time'};
 9391:         my $update=$env{'user.update.time'};
 9392:         if (!$update) { $update = $then; }
 9393:         my $refresh=$env{'user.refresh.time'};
 9394:         if (!$refresh) { $refresh = $update; }
 9395:         my $now = time;
 9396:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 9397:                            $now,'ca','constructaccess');
 9398:         $ownerhome = &homeserver($ownername,$ownerdomain);
 9399:         return($ownername,$ownerdomain,$ownerhome);
 9400:     }
 9401: # No business here
 9402:     return '';
 9403: }
 9404: 
 9405: # ----------------------------------------------------------- Content Blocking
 9406: 
 9407: {
 9408: # Caches for faster Course Contents display where content blocking
 9409: # is in operation (i.e., interval param set) for timed quiz.
 9410: #
 9411: # User for whom data are being temporarily cached.
 9412: my $cacheduser='';
 9413: # Course for which data are being temporarily cached.
 9414: my $cachedcid='';
 9415: # Cached blockers for this user (a hash of blocking items). 
 9416: my %cachedblockers=();
 9417: # When the data were last cached.
 9418: my $cachedlast='';
 9419: 
 9420: sub load_all_blockers {
 9421:     my ($uname,$udom)=@_;
 9422:     if (($uname ne '') && ($udom ne '')) { 
 9423:         if (($cacheduser eq $uname.':'.$udom) &&
 9424:             ($cachedcid eq $env{'request.course.id'}) &&
 9425:             (abs($cachedlast-time)<5)) {
 9426:             return;
 9427:         }
 9428:     }
 9429:     $cachedlast=time;
 9430:     $cacheduser=$uname.':'.$udom;
 9431:     $cachedcid=$env{'request.course.id'};
 9432:     %cachedblockers = &get_commblock_resources();
 9433:     return;
 9434: }
 9435: 
 9436: sub get_comm_blocks {
 9437:     my ($cdom,$cnum) = @_;
 9438:     if ($cdom eq '' || $cnum eq '') {
 9439:         return unless ($env{'request.course.id'});
 9440:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9441:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9442:     }
 9443:     my %commblocks;
 9444:     my $hashid=$cdom.'_'.$cnum;
 9445:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 9446:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 9447:         %commblocks = %{$blocksref};
 9448:     } else {
 9449:         %commblocks = &dump('comm_block',$cdom,$cnum);
 9450:         my $cachetime = 600;
 9451:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 9452:     }
 9453:     return %commblocks;
 9454: }
 9455: 
 9456: sub get_commblock_resources {
 9457:     my ($blocks) = @_;
 9458:     my %blockers = ();
 9459:     return %blockers unless ($env{'request.course.id'});
 9460:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9461:     if ($env{'request.course.sec'}) {
 9462:         $courseurl .= '/'.$env{'request.course.sec'};
 9463:     }
 9464:     return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9465:     my %commblocks;
 9466:     if (ref($blocks) eq 'HASH') {
 9467:         %commblocks = %{$blocks};
 9468:     } else {
 9469:         %commblocks = &get_comm_blocks();
 9470:     }
 9471:     return %blockers unless (keys(%commblocks) > 0); 
 9472:     my $navmap = Apache::lonnavmaps::navmap->new();
 9473:     return %blockers unless (ref($navmap));
 9474:     my $now = time;
 9475:     foreach my $block (keys(%commblocks)) {
 9476:         if ($block =~ /^(\d+)____(\d+)$/) {
 9477:             my ($start,$end) = ($1,$2);
 9478:             if ($start <= $now && $end >= $now) {
 9479:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 9480:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 9481:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 9482:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 9483:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 9484:                             }
 9485:                         }
 9486:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 9487:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 9488:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 9489:                             }
 9490:                         }
 9491:                     }
 9492:                 }
 9493:             }
 9494:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 9495:             my $item = $1;
 9496:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 9497:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 9498:                     my (@interval,$mapname);
 9499:                     my $type = 'map';
 9500:                     if ($item eq 'course') {
 9501:                         $type = 'course';
 9502:                         @interval=&EXT("resource.0.interval");
 9503:                     } else {
 9504:                         if ($item =~ /___\d+___/) {
 9505:                             $type = 'resource';
 9506:                             @interval=&EXT("resource.0.interval",$item);
 9507:                         } else {
 9508:                             $mapname = &deversion($item);
 9509:                             if (ref($navmap)) {
 9510:                                 my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
 9511:                                 @interval = ($timelimit,'map');
 9512:                             }
 9513:                         }
 9514:                     }
 9515:                     if ($interval[0] =~ /^(\d+)/) {
 9516:                         my $timelimit = $1; 
 9517:                         my $first_access;
 9518:                         if ($type eq 'resource') {
 9519:                             $first_access=&get_first_access($interval[1],$item);
 9520:                         } elsif ($type eq 'map') {
 9521:                             $first_access=&get_first_access($interval[1],undef,$item);
 9522:                         } else {
 9523:                             $first_access=&get_first_access($interval[1]);
 9524:                         }
 9525:                         if ($first_access) {
 9526:                             my $timesup = $first_access+$timelimit;
 9527:                             if ($timesup > $now) {
 9528:                                 my $activeblock;
 9529:                                 if ($type eq 'resource') {
 9530:                                     if (ref($navmap)) {
 9531:                                         my $res = $navmap->getBySymb($item);
 9532:                                         if ($res->answerable()) {
 9533:                                             $activeblock = 1;
 9534:                                         }
 9535:                                     }
 9536:                                 } elsif ($type eq 'map') {
 9537:                                     my $mapsymb = &symbread($mapname,1);
 9538:                                     if (($mapsymb) && (ref($navmap))) {
 9539:                                         my $mapres = $navmap->getBySymb($mapsymb);
 9540:                                         if (ref($mapres)) {
 9541:                                             my $first = $mapres->map_start();
 9542:                                             my $finish = $mapres->map_finish();
 9543:                                             my $it = $navmap->getIterator($first,$finish,undef,0,0);
 9544:                                             if (ref($it)) {
 9545:                                                 my $res;
 9546:                                                 while ($res = $it->next(undef,1)) {
 9547:                                                     next unless (ref($res));
 9548:                                                     my $symb = $res->symb();
 9549:                                                     next if (($symb eq $mapsymb) || ($symb eq ''));
 9550:                                                     @interval=&EXT("resource.0.interval",$symb);
 9551:                                                     if ($interval[1] eq 'map') {
 9552:                                                         if ($res->answerable()) {
 9553:                                                             $activeblock = 1;
 9554:                                                             last;
 9555:                                                         }
 9556:                                                     }
 9557:                                                 }
 9558:                                             }
 9559:                                         }
 9560:                                     }
 9561:                                 }
 9562:                                 if ($activeblock) {
 9563:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 9564:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 9565:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 9566:                                          }
 9567:                                     }
 9568:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 9569:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 9570:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 9571:                                         }
 9572:                                     }
 9573:                                 }
 9574:                             }
 9575:                         }
 9576:                     }
 9577:                 }
 9578:             }
 9579:         }
 9580:     }
 9581:     return %blockers;
 9582: }
 9583: 
 9584: sub has_comm_blocking {
 9585:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 9586:     my @blockers;
 9587:     return unless ($env{'request.course.id'});
 9588:     return unless ($priv eq 'bre');
 9589:     return if ($env{'request.state'} eq 'construct');
 9590:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9591:     if ($env{'request.course.sec'}) {
 9592:         $courseurl .= '/'.$env{'request.course.sec'};
 9593:     }
 9594:     return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9595:     my %blockinfo;
 9596:     if (ref($blocks) eq 'HASH') {
 9597:         %blockinfo = &get_commblock_resources($blocks);
 9598:     } else {
 9599:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 9600:         %blockinfo = %cachedblockers;
 9601:     }
 9602:     return unless (keys(%blockinfo) > 0);
 9603:     my (%possibles,@symbs);
 9604:     if (!$symb) {
 9605:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 9606:     }
 9607:     if ($symb) {
 9608:         @symbs = ($symb);
 9609:     } elsif (keys(%possibles)) { 
 9610:         @symbs = keys(%possibles);
 9611:     }
 9612:     my $noblock;
 9613:     foreach my $symb (@symbs) {
 9614:         last if ($noblock);
 9615:         my ($map,$resid,$resurl)=&decode_symb($symb);
 9616:         foreach my $block (keys(%blockinfo)) {
 9617:             if ($block =~ /^firstaccess____(.+)$/) {
 9618:                 my $item = $1;
 9619:                 unless ($blocked) {
 9620:                     if (($item eq $map) || ($item eq $symb)) {
 9621:                         $noblock = 1;
 9622:                         last;
 9623:                     }
 9624:                 }
 9625:             }
 9626:             if (ref($blockinfo{$block}) eq 'HASH') {
 9627:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 9628:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 9629:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9630:                             push(@blockers,$block);
 9631:                         }
 9632:                     }
 9633:                 }
 9634:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 9635:                     if ($blockinfo{$block}{'maps'}{$map}) {
 9636:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9637:                             push(@blockers,$block);
 9638:                         }
 9639:                     }
 9640:                 }
 9641:             }
 9642:         }
 9643:     }
 9644:     unless ($noblock) { 
 9645:         return @blockers;
 9646:     }
 9647:     return;
 9648: }
 9649: }
 9650: 
 9651: sub deeplink_check {
 9652:     my ($priv,$symb,$uri) = @_;
 9653:     return unless ($env{'request.course.id'});
 9654:     return unless ($priv eq 'bre');
 9655:     return if ($env{'request.state'} eq 'construct');
 9656:     return if ($env{'request.role.adv'});
 9657:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9658:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9659:     my (%possibles,@symbs);
 9660:     if (!$symb) {
 9661:         $symb = &symbread($uri,1,1,1,\%possibles);
 9662:     }
 9663:     if ($symb) {
 9664:         @symbs = ($symb);
 9665:     } elsif (keys(%possibles)) {
 9666:         @symbs = keys(%possibles);
 9667:     }
 9668: 
 9669:     my ($deeplink_symb,$allow);
 9670:     if ($env{'request.deeplink.login'}) {
 9671:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
 9672:     }
 9673:     foreach my $symb (@symbs) {
 9674:         last if ($allow);
 9675:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9676:         if ($deeplink eq '') {
 9677:             $allow = 1;
 9678:         } else {
 9679:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
 9680:             if ($state ne 'only') {
 9681:                 $allow = 1;
 9682:             } else {
 9683:                 my $check_deeplink_entry;
 9684:                 if ($protect ne 'none') {
 9685:                     my ($acctype,$item) = split(/:/,$protect);
 9686:                     if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
 9687:                         if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
 9688:                             $check_deeplink_entry = 1
 9689:                         }
 9690:                     } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
 9691:                         if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
 9692:                             $check_deeplink_entry = 1;
 9693:                         }
 9694:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9695:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9696:                             $check_deeplink_entry = 1;
 9697:                         }
 9698:                     }
 9699:                 }
 9700:                 if (($protect eq 'none') || ($check_deeplink_entry)) {
 9701:                     if ($scope eq 'res') {
 9702:                         if ($symb eq $deeplink_symb) {
 9703:                             $allow = 1;
 9704:                         }
 9705:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
 9706:                         my ($map_from_symb,$map_from_login);
 9707:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
 9708:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
 9709:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
 9710:                         } else {
 9711:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
 9712:                         }
 9713:                         if (($map_from_symb) && ($map_from_login)) {
 9714:                             if ($map_from_symb eq $map_from_login) {
 9715:                                 $allow = 1;
 9716:                             } elsif ($scope eq 'rec') {
 9717:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
 9718:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
 9719:                                     $allow = 1;
 9720:                                 }
 9721:                             }
 9722:                         }
 9723:                     }
 9724:                 }
 9725:             }
 9726:         }
 9727:     }
 9728:     return if ($allow);
 9729:     return 1;
 9730: }
 9731: 
 9732: # -------------------------------- Deversion and split uri into path an filename   
 9733: 
 9734: #
 9735: #   Removes the version from a URI and
 9736: #   splits it in to its filename and path to the filename.
 9737: #   Seems like File::Basename could have done this more clearly.
 9738: #   Parameters:
 9739: #      $uri   - input URI
 9740: #   Returns:
 9741: #     Two element list consisting of 
 9742: #     $pathname  - the URI up to and excluding the trailing /
 9743: #     $filename  - The part of the URI following the last /
 9744: #  NOTE:
 9745: #    Another realization of this is simply:
 9746: #    use File::Basename;
 9747: #    ...
 9748: #    $uri = shift;
 9749: #    $filename = basename($uri);
 9750: #    $path     = dirname($uri);
 9751: #    return ($filename, $path);
 9752: #
 9753: #     The implementation below is probably faster however.
 9754: #
 9755: sub split_uri_for_cond {
 9756:     my $uri=&deversion(&declutter(shift));
 9757:     my @uriparts=split(/\//,$uri);
 9758:     my $filename=pop(@uriparts);
 9759:     my $pathname=join('/',@uriparts);
 9760:     return ($pathname,$filename);
 9761: }
 9762: # --------------------------------------------------- Is a resource on the map?
 9763: 
 9764: sub is_on_map {
 9765:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9766:     #Trying to find the conditional for the file
 9767:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9768: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9769:     if ($match) {
 9770: 	return (1,$1);
 9771:     } else {
 9772: 	return (0,0);
 9773:     }
 9774: }
 9775: 
 9776: # --------------------------------------------------------- Get symb from alias
 9777: 
 9778: sub get_symb_from_alias {
 9779:     my $symb=shift;
 9780:     my ($map,$resid,$url)=&decode_symb($symb);
 9781: # Already is a symb
 9782:     if ($url) { return $symb; }
 9783: # Must be an alias
 9784:     my $aliassymb='';
 9785:     my %bighash;
 9786:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9787:                             &GDBM_READER(),0640)) {
 9788:         my $rid=$bighash{'mapalias_'.$symb};
 9789: 	if ($rid) {
 9790: 	    my ($mapid,$resid)=split(/\./,$rid);
 9791: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9792: 				    $resid,$bighash{'src_'.$rid});
 9793: 	}
 9794:         untie %bighash;
 9795:     }
 9796:     return $aliassymb;
 9797: }
 9798: 
 9799: # ----------------------------------------------------------------- Define Role
 9800: 
 9801: sub definerole {
 9802:   if (allowed('mcr','/')) {
 9803:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9804:     foreach my $role (split(':',$sysrole)) {
 9805: 	my ($crole,$cqual)=split(/\&/,$role);
 9806:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9807:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9808: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9809:                return "refused:s:$crole&$cqual"; 
 9810:             }
 9811:         }
 9812:     }
 9813:     foreach my $role (split(':',$domrole)) {
 9814: 	my ($crole,$cqual)=split(/\&/,$role);
 9815:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9816:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9817: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9818:                return "refused:d:$crole&$cqual"; 
 9819:             }
 9820:         }
 9821:     }
 9822:     foreach my $role (split(':',$courole)) {
 9823: 	my ($crole,$cqual)=split(/\&/,$role);
 9824:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9825:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9826: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9827:                return "refused:c:$crole&$cqual"; 
 9828:             }
 9829:         }
 9830:     }
 9831:     my $uhome;
 9832:     if (($uname ne '') && ($udom ne '')) {
 9833:         $uhome = &homeserver($uname,$udom);
 9834:         return $uhome if ($uhome eq 'no_host');
 9835:     } else {
 9836:         $uname = $env{'user.name'};
 9837:         $udom = $env{'user.domain'};
 9838:         $uhome = $env{'user.home'};
 9839:     }
 9840:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9841:                 "$udom:$uname:rolesdef_$rolename=".
 9842:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9843:     return reply($command,$uhome);
 9844:   } else {
 9845:     return 'refused';
 9846:   }
 9847: }
 9848: 
 9849: # ---------------- Make a metadata query against the network of library servers
 9850: 
 9851: sub metadata_query {
 9852:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9853:     my %rhash;
 9854:     my %libserv = &all_library();
 9855:     my @server_list = (defined($server_array) ? @$server_array
 9856:                                               : keys(%libserv) );
 9857:     for my $server (@server_list) {
 9858:         my $domains = ''; 
 9859:         if (ref($domains_hash) eq 'HASH') {
 9860:             $domains = $domains_hash->{$server}; 
 9861:         }
 9862: 	unless ($custom or $customshow) {
 9863: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9864: 	    $rhash{$server}=$reply;
 9865: 	}
 9866: 	else {
 9867: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9868: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9869: 			     $server);
 9870: 	    $rhash{$server}=$reply;
 9871: 	}
 9872:     }
 9873:     return \%rhash;
 9874: }
 9875: 
 9876: # ----------------------------------------- Send log queries and wait for reply
 9877: 
 9878: sub log_query {
 9879:     my ($uname,$udom,$query,%filters)=@_;
 9880:     my $uhome=&homeserver($uname,$udom);
 9881:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9882:     my $uhost=&hostname($uhome);
 9883:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9884:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9885:                        $uhome);
 9886:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9887:     return get_query_reply($queryid);
 9888: }
 9889: 
 9890: # -------------------------- Update MySQL table for portfolio file
 9891: 
 9892: sub update_portfolio_table {
 9893:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9894:     if ($group ne '') {
 9895:         $file_name =~s /^\Q$group\E//;
 9896:     }
 9897:     my $homeserver = &homeserver($uname,$udom);
 9898:     my $queryid=
 9899:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9900:                ':'.&escape($file_name).':'.$action,$homeserver);
 9901:     my $reply = &get_query_reply($queryid);
 9902:     return $reply;
 9903: }
 9904: 
 9905: # -------------------------- Update MySQL allusers table
 9906: 
 9907: sub update_allusers_table {
 9908:     my ($uname,$udom,$names) = @_;
 9909:     my $homeserver = &homeserver($uname,$udom);
 9910:     my $queryid=
 9911:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9912:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9913:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9914:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9915:                'generation='.&escape($names->{'generation'}).'%%'.
 9916:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9917:                'id='.&escape($names->{'id'}),$homeserver);
 9918:     return;
 9919: }
 9920: 
 9921: # ------- Request retrieval of institutional classlists for course(s)
 9922: 
 9923: sub fetch_enrollment_query {
 9924:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9925:     my ($homeserver,$sleep,$loopmax);
 9926:     my $maxtries = 1;
 9927:     if ($context eq 'automated') {
 9928:         $homeserver = $perlvar{'lonHostID'};
 9929:         $sleep = 2;
 9930:         $loopmax = 100;
 9931:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9932:     } else {
 9933:         $homeserver = &homeserver($cnum,$dom);
 9934:     }
 9935:     my $host=&hostname($homeserver);
 9936:     my $cmd = '';
 9937:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9938:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9939:     }
 9940:     $cmd =~ s/%%$//;
 9941:     $cmd = &escape($cmd);
 9942:     my $query = 'fetchenrollment';
 9943:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9944:     unless ($queryid=~/^\Q$host\E\_/) { 
 9945:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9946:         return 'error: '.$queryid;
 9947:     }
 9948:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9949:     my $tries = 1;
 9950:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9951:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9952:         $tries ++;
 9953:     }
 9954:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9955:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9956:     } else {
 9957:         my @responses = split(/:/,$reply);
 9958:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9959:             foreach my $line (@responses) {
 9960:                 my ($key,$value) = split(/=/,$line,2);
 9961:                 $$replyref{$key} = $value;
 9962:             }
 9963:         } else {
 9964:             my $pathname = LONCAPA::tempdir();
 9965:             foreach my $line (@responses) {
 9966:                 my ($key,$value) = split(/=/,$line);
 9967:                 $$replyref{$key} = $value;
 9968:                 if ($value > 0) {
 9969:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9970:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9971:                         my $destname = $pathname.'/'.$filename;
 9972:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9973:                         if ($xml_classlist =~ /^error/) {
 9974:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9975:                         } else {
 9976:                             if ( open(FILE,">",$destname) ) {
 9977:                                 print FILE &unescape($xml_classlist);
 9978:                                 close(FILE);
 9979:                             } else {
 9980:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9981:                             }
 9982:                         }
 9983:                     }
 9984:                 }
 9985:             }
 9986:         }
 9987:         return 'ok';
 9988:     }
 9989:     return 'error';
 9990: }
 9991: 
 9992: sub get_query_reply {
 9993:     my ($queryid,$sleep,$loopmax) = @_;
 9994:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9995:         $sleep = 0.2;
 9996:     }
 9997:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9998:         $loopmax = 100;
 9999:     }
10000:     my $replyfile=LONCAPA::tempdir().$queryid;
10001:     my $reply='';
10002:     for (1..$loopmax) {
10003: 	sleep($sleep);
10004:         if (-e $replyfile.'.end') {
10005: 	    if (open(my $fh,"<",$replyfile)) {
10006: 		$reply = join('',<$fh>);
10007: 		close($fh);
10008: 	   } else { return 'error: reply_file_error'; }
10009:            return &unescape($reply);
10010: 	}
10011:     }
10012:     return 'timeout:'.$queryid;
10013: }
10014: 
10015: sub courselog_query {
10016: #
10017: # possible filters:
10018: # url: url or symb
10019: # username
10020: # domain
10021: # action: view, submit, grade
10022: # start: timestamp
10023: # end: timestamp
10024: #
10025:     my (%filters)=@_;
10026:     unless ($env{'request.course.id'}) { return 'no_course'; }
10027:     if ($filters{'url'}) {
10028: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
10029:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
10030:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
10031:     }
10032:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
10033:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
10034:     return &log_query($cname,$cdom,'courselog',%filters);
10035: }
10036: 
10037: sub userlog_query {
10038: #
10039: # possible filters:
10040: # action: log check role
10041: # start: timestamp
10042: # end: timestamp
10043: #
10044:     my ($uname,$udom,%filters)=@_;
10045:     return &log_query($uname,$udom,'userlog',%filters);
10046: }
10047: 
10048: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
10049: 
10050: sub auto_run {
10051:     my ($cnum,$cdom) = @_;
10052:     my $response = 0;
10053:     my $settings;
10054:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
10055:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10056:         $settings = $domconfig{'autoenroll'};
10057:         if ($settings->{'run'} eq '1') {
10058:             $response = 1;
10059:         }
10060:     } else {
10061:         my $homeserver;
10062:         if (&is_course($cdom,$cnum)) {
10063:             $homeserver = &homeserver($cnum,$cdom);
10064:         } else {
10065:             $homeserver = &domain($cdom,'primary');
10066:         }
10067:         if ($homeserver ne 'no_host') {
10068:             $response = &reply('autorun:'.$cdom,$homeserver);
10069:         }
10070:     }
10071:     return $response;
10072: }
10073: 
10074: sub auto_get_sections {
10075:     my ($cnum,$cdom,$inst_coursecode) = @_;
10076:     my $homeserver;
10077:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
10078:         $homeserver = &homeserver($cnum,$cdom);
10079:     }
10080:     if (!defined($homeserver)) { 
10081:         if ($cdom =~ /^$match_domain$/) {
10082:             $homeserver = &domain($cdom,'primary');
10083:         }
10084:     }
10085:     my @secs;
10086:     if (defined($homeserver)) {
10087:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
10088:         unless ($response eq 'refused') {
10089:             @secs = split(/:/,$response);
10090:         }
10091:     }
10092:     return @secs;
10093: }
10094: 
10095: sub auto_new_course {
10096:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
10097:     my $homeserver = &homeserver($cnum,$cdom);
10098:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
10099:     return $response;
10100: }
10101: 
10102: sub auto_validate_courseID {
10103:     my ($cnum,$cdom,$inst_course_id) = @_;
10104:     my $homeserver = &homeserver($cnum,$cdom);
10105:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
10106:     return $response;
10107: }
10108: 
10109: sub auto_validate_instcode {
10110:     my ($cnum,$cdom,$instcode,$owner) = @_;
10111:     my ($homeserver,$response);
10112:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
10113:         $homeserver = &homeserver($cnum,$cdom);
10114:     }
10115:     if (!defined($homeserver)) {
10116:         if ($cdom =~ /^$match_domain$/) {
10117:             $homeserver = &domain($cdom,'primary');
10118:         }
10119:     }
10120:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
10121:                         &escape($instcode).':'.&escape($owner),$homeserver));
10122:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
10123:     return ($outcome,$description,$defaultcredits);
10124: }
10125: 
10126: sub auto_validate_inst_crosslist {
10127:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
10128:     my ($homeserver,$response);
10129:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
10130:         $homeserver = &homeserver($cnum,$cdom);
10131:     }
10132:     if (!defined($homeserver)) {
10133:         if ($cdom =~ /^$match_domain$/) {
10134:             $homeserver = &domain($cdom,'primary');
10135:         }
10136:     }
10137:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
10138:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
10139:                          &escape($instcode).':'.&escape($inst_xlist).':'.
10140:                          &escape($coowner),$homeserver);
10141:     }
10142:     return $response;
10143: }
10144: 
10145: sub auto_create_password {
10146:     my ($cnum,$cdom,$authparam,$udom) = @_;
10147:     my ($homeserver,$response);
10148:     my $create_passwd = 0;
10149:     my $authchk = '';
10150:     if ($udom =~ /^$match_domain$/) {
10151:         $homeserver = &domain($udom,'primary');
10152:     }
10153:     if ($homeserver eq '') {
10154:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
10155:             $homeserver = &homeserver($cnum,$cdom);
10156:         }
10157:     }
10158:     if ($homeserver eq '') {
10159:         $authchk = 'nodomain';
10160:     } else {
10161:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
10162:         if ($response eq 'refused') {
10163:             $authchk = 'refused';
10164:         } else {
10165:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
10166:         }
10167:     }
10168:     return ($authparam,$create_passwd,$authchk);
10169: }
10170: 
10171: sub auto_photo_permission {
10172:     my ($cnum,$cdom,$students) = @_;
10173:     my $homeserver = &homeserver($cnum,$cdom);
10174:     my ($outcome,$perm_reqd,$conditions) = 
10175: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
10176:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
10177: 	return (undef,undef);
10178:     }
10179:     return ($outcome,$perm_reqd,$conditions);
10180: }
10181: 
10182: sub auto_checkphotos {
10183:     my ($uname,$udom,$pid) = @_;
10184:     my $homeserver = &homeserver($uname,$udom);
10185:     my ($result,$resulttype);
10186:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
10187: 				   &escape($uname).':'.&escape($pid),
10188: 				   $homeserver));
10189:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
10190: 	return (undef,undef);
10191:     }
10192:     if ($outcome) {
10193:         ($result,$resulttype) = split(/:/,$outcome);
10194:     } 
10195:     return ($result,$resulttype);
10196: }
10197: 
10198: sub auto_photochoice {
10199:     my ($cnum,$cdom) = @_;
10200:     my $homeserver = &homeserver($cnum,$cdom);
10201:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
10202: 						       &escape($cdom),
10203: 						       $homeserver)));
10204:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
10205: 	return (undef,undef);
10206:     }
10207:     return ($update,$comment);
10208: }
10209: 
10210: sub auto_photoupdate {
10211:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
10212:     my $homeserver = &homeserver($cnum,$dom);
10213:     my $host=&hostname($homeserver);
10214:     my $cmd = '';
10215:     my $maxtries = 1;
10216:     foreach my $affiliate (keys(%{$affiliatesref})) {
10217:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
10218:     }
10219:     $cmd =~ s/%%$//;
10220:     $cmd = &escape($cmd);
10221:     my $query = 'institutionalphotos';
10222:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
10223:     unless ($queryid=~/^\Q$host\E\_/) {
10224:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
10225:         return 'error: '.$queryid;
10226:     }
10227:     my $reply = &get_query_reply($queryid);
10228:     my $tries = 1;
10229:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
10230:         $reply = &get_query_reply($queryid);
10231:         $tries ++;
10232:     }
10233:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
10234:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
10235:     } else {
10236:         my @responses = split(/:/,$reply);
10237:         my $outcome = shift(@responses); 
10238:         foreach my $item (@responses) {
10239:             my ($key,$value) = split(/=/,$item);
10240:             $$photo{$key} = $value;
10241:         }
10242:         return $outcome;
10243:     }
10244:     return 'error';
10245: }
10246: 
10247: sub auto_instcode_format {
10248:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
10249: 	$cat_order) = @_;
10250:     my $courses = '';
10251:     my @homeservers;
10252:     if ($caller eq 'global') {
10253: 	my %servers = &get_servers($codedom,'library');
10254: 	foreach my $tryserver (keys(%servers)) {
10255: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10256: 		push(@homeservers,$tryserver);
10257: 	    }
10258:         }
10259:     } elsif ($caller eq 'requests') {
10260:         if ($codedom =~ /^$match_domain$/) {
10261:             my $chome = &domain($codedom,'primary');
10262:             unless ($chome eq 'no_host') {
10263:                 push(@homeservers,$chome);
10264:             }
10265:         }
10266:     } else {
10267:         push(@homeservers,&homeserver($caller,$codedom));
10268:     }
10269:     foreach my $code (keys(%{$instcodes})) {
10270:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
10271:     }
10272:     chop($courses);
10273:     my $ok_response = 0;
10274:     my $response;
10275:     while (@homeservers > 0 && $ok_response == 0) {
10276:         my $server = shift(@homeservers); 
10277:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
10278:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
10279:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
10280: 		split(/:/,$response);
10281:             %{$codes} = (%{$codes},&str2hash($codes_str));
10282:             push(@{$codetitles},&str2array($codetitles_str));
10283:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
10284:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
10285:             $ok_response = 1;
10286:         }
10287:     }
10288:     if ($ok_response) {
10289:         return 'ok';
10290:     } else {
10291:         return $response;
10292:     }
10293: }
10294: 
10295: sub auto_instcode_defaults {
10296:     my ($domain,$returnhash,$code_order) = @_;
10297:     my @homeservers;
10298: 
10299:     my %servers = &get_servers($domain,'library');
10300:     foreach my $tryserver (keys(%servers)) {
10301: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10302: 	    push(@homeservers,$tryserver);
10303: 	}
10304:     }
10305: 
10306:     my $response;
10307:     foreach my $server (@homeservers) {
10308:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
10309:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10310: 	
10311: 	foreach my $pair (split(/\&/,$response)) {
10312: 	    my ($name,$value)=split(/\=/,$pair);
10313: 	    if ($name eq 'code_order') {
10314: 		@{$code_order} = split(/\&/,&unescape($value));
10315: 	    } else {
10316: 		$returnhash->{&unescape($name)}=&unescape($value);
10317: 	    }
10318: 	}
10319: 	return 'ok';
10320:     }
10321: 
10322:     return $response;
10323: }
10324: 
10325: sub auto_possible_instcodes {
10326:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10327:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
10328:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10329:         return;
10330:     }
10331:     my (@homeservers,$uhome);
10332:     if (defined(&domain($domain,'primary'))) {
10333:         $uhome=&domain($domain,'primary');
10334:         push(@homeservers,&domain($domain,'primary'));
10335:     } else {
10336:         my %servers = &get_servers($domain,'library');
10337:         foreach my $tryserver (keys(%servers)) {
10338:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10339:                 push(@homeservers,$tryserver);
10340:             }
10341:         }
10342:     }
10343:     my $response;
10344:     foreach my $server (@homeservers) {
10345:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
10346:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10347:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
10348:             split(':',$response);
10349:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10350:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
10351:         foreach my $item (split('&',$cat_title)) {   
10352:             my ($name,$value)=split('=',$item);
10353:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
10354:         }
10355:         foreach my $item (split('&',$cat_order)) {
10356:             my ($name,$value)=split('=',$item);
10357:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
10358:         }
10359:         return 'ok';
10360:     }
10361:     return $response;
10362: }
10363: 
10364: sub auto_courserequest_checks {
10365:     my ($dom) = @_;
10366:     my ($homeserver,%validations);
10367:     if ($dom =~ /^$match_domain$/) {
10368:         $homeserver = &domain($dom,'primary');
10369:     }
10370:     unless ($homeserver eq 'no_host') {
10371:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10372:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10373:             my @items = split(/&/,$response);
10374:             foreach my $item (@items) {
10375:                 my ($key,$value) = split('=',$item);
10376:                 $validations{&unescape($key)} = &thaw_unescape($value);
10377:             }
10378:         }
10379:     }
10380:     return %validations; 
10381: }
10382: 
10383: sub auto_courserequest_validation {
10384:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
10385:     my ($homeserver,$response);
10386:     if ($dom =~ /^$match_domain$/) {
10387:         $homeserver = &domain($dom,'primary');
10388:     }
10389:     unless ($homeserver eq 'no_host') {
10390:         my $customdata;
10391:         if (ref($custominfo) eq 'HASH') {
10392:             $customdata = &freeze_escape($custominfo);
10393:         }
10394:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
10395:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
10396:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
10397:                                     $customdata,$homeserver));
10398:     }
10399:     return $response;
10400: }
10401: 
10402: sub auto_validate_class_sec {
10403:     my ($cdom,$cnum,$owners,$inst_class) = @_;
10404:     my $homeserver = &homeserver($cnum,$cdom);
10405:     my $ownerlist;
10406:     if (ref($owners) eq 'ARRAY') {
10407:         $ownerlist = join(',',@{$owners});
10408:     } else {
10409:         $ownerlist = $owners;
10410:     }
10411:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
10412:                         &escape($ownerlist).':'.$cdom,$homeserver);
10413:     return $response;
10414: }
10415: 
10416: sub auto_instsec_reformat {
10417:     my ($cdom,$action,$instsecref) = @_;
10418:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
10419:     my @homeservers;
10420:     if (defined(&domain($cdom,'primary'))) {
10421:         push(@homeservers,&domain($cdom,'primary'));
10422:     } else {
10423:         my %servers = &get_servers($cdom,'library');
10424:         foreach my $tryserver (keys(%servers)) {
10425:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10426:                 push(@homeservers,$tryserver);
10427:             }
10428:         }
10429:     }
10430:     my $response;
10431:     my %reformatted = %{$instsecref};
10432:     foreach my $server (@homeservers) {
10433:         if (ref($instsecref) eq 'HASH') {
10434:             my $info = &freeze_escape($instsecref);
10435:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10436:                                 $action.':'.$info,$server);
10437:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/);
10438:             my @items = split(/&/,$response);
10439:             foreach my $item (@items) {
10440:                 my ($key,$value) = split(/=/,$item);
10441:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
10442:             }
10443:         }
10444:     }
10445:     return %reformatted;
10446: }
10447: 
10448: sub auto_validate_instclasses {
10449:     my ($cdom,$cnum,$owners,$classesref) = @_;
10450:     my ($homeserver,%validations);
10451:     $homeserver = &homeserver($cnum,$cdom);
10452:     unless ($homeserver eq 'no_host') {
10453:         my $ownerlist;
10454:         if (ref($owners) eq 'ARRAY') {
10455:             $ownerlist = join(',',@{$owners});
10456:         } else {
10457:             $ownerlist = $owners;
10458:         }
10459:         if (ref($classesref) eq 'HASH') {
10460:             my $classes = &freeze_escape($classesref);
10461:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10462:                                 ':'.$cdom.':'.$classes,$homeserver);
10463:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10464:                 my @items = split(/&/,$response);
10465:                 foreach my $item (@items) {
10466:                     my ($key,$value) = split('=',$item);
10467:                     $validations{&unescape($key)} = &thaw_unescape($value);
10468:                 }
10469:             }
10470:         }
10471:     }
10472:     return %validations;
10473: }
10474: 
10475: sub auto_crsreq_update {
10476:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
10477:         $code,$accessstart,$accessend,$inbound) = @_;
10478:     my ($homeserver,%crsreqresponse);
10479:     if ($cdom =~ /^$match_domain$/) {
10480:         $homeserver = &domain($cdom,'primary');
10481:     }
10482:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10483:         my $info;
10484:         if (ref($inbound) eq 'HASH') {
10485:             $info = &freeze_escape($inbound);
10486:         }
10487:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10488:                             ':'.&escape($action).':'.&escape($ownername).':'.
10489:                             &escape($ownerdomain).':'.&escape($fullname).':'.
10490:                             &escape($title).':'.&escape($code).':'.
10491:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
10492:                             $homeserver);
10493:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10494:             my @items = split(/&/,$response);
10495:             foreach my $item (@items) {
10496:                 my ($key,$value) = split('=',$item);
10497:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10498:             }
10499:         }
10500:     }
10501:     return \%crsreqresponse;
10502: }
10503: 
10504: sub auto_export_grades {
10505:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
10506:     my ($homeserver,%exportresponse);
10507:     if ($cdom =~ /^$match_domain$/) {
10508:         $homeserver = &domain($cdom,'primary');
10509:     }
10510:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10511:         my $info;
10512:         if (ref($inforef) eq 'HASH') {
10513:             $info = &freeze_escape($inforef);
10514:         }
10515:         if (ref($gradesref) eq 'HASH') {
10516:             my $grades = &freeze_escape($gradesref);
10517:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10518:                                 $info.':'.$grades,$homeserver);
10519:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_cmd)/) {
10520:                 my @items = split(/&/,$response);
10521:                 foreach my $item (@items) {
10522:                     my ($key,$value) = split('=',$item);
10523:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
10524:                 }
10525:             }
10526:         }
10527:     }
10528:     return \%exportresponse;
10529: }
10530: 
10531: sub check_instcode_cloning {
10532:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10533:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10534:         return;
10535:     }
10536:     my $canclone;
10537:     if (@{$code_order} > 0) {
10538:         my $instcoderegexp ='^';
10539:         my @clonecodes = split(/\&/,$cloner);
10540:         foreach my $item (@{$code_order}) {
10541:             if (grep(/^\Q$item\E=/,@clonecodes)) {
10542:                 foreach my $pair (@clonecodes) {
10543:                     my ($key,$val) = split(/\=/,$pair,2);
10544:                     $val = &unescape($val);
10545:                     if ($key eq $item) {
10546:                         $instcoderegexp .= '('.$val.')';
10547:                         last;
10548:                     }
10549:                 }
10550:             } else {
10551:                 $instcoderegexp .= $codedefaults->{$item};
10552:             }
10553:         }
10554:         $instcoderegexp .= '$';
10555:         my (@from,@to);
10556:         eval {
10557:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
10558:                (@to) = ($clonetocode =~ /$instcoderegexp/);
10559:         };
10560:         if ((@from > 0) && (@to > 0)) {
10561:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10562:             if (!@diffs) {
10563:                 $canclone = 1;
10564:             }
10565:         }
10566:     }
10567:     return $canclone;
10568: }
10569: 
10570: sub default_instcode_cloning {
10571:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10572:     my (%codedefaults,@code_order,$canclone);
10573:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10574:         %codedefaults = %{$codedefaultsref};
10575:         @code_order = @{$codeorderref};
10576:     } elsif ($clonedom) {
10577:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10578:     }
10579:     if (($domdefclone) && (@code_order)) {
10580:         my @clonecodes = split(/\+/,$domdefclone);
10581:         my $instcoderegexp ='^';
10582:         foreach my $item (@code_order) {
10583:             if (grep(/^\Q$item\E$/,@clonecodes)) {
10584:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
10585:             } else {
10586:                 $instcoderegexp .= $codedefaults{$item};
10587:             }
10588:         }
10589:         $instcoderegexp .= '$';
10590:         my (@from,@to);
10591:         eval {
10592:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
10593:             (@to) = ($clonetocode =~ /$instcoderegexp/);
10594:         };
10595:         if ((@from > 0) && (@to > 0)) {
10596:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10597:             if (!@diffs) {
10598:                 $canclone = 1;
10599:             }
10600:         }
10601:     }
10602:     return $canclone;
10603: }
10604: 
10605: # ------------------------------------------------------- Course Group routines
10606: 
10607: sub get_coursegroups {
10608:     my ($cdom,$cnum,$group,$namespace) = @_;
10609:     return(&dump($namespace,$cdom,$cnum,$group));
10610: }
10611: 
10612: sub modify_coursegroup {
10613:     my ($cdom,$cnum,$groupsettings) = @_;
10614:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10615: }
10616: 
10617: sub toggle_coursegroup_status {
10618:     my ($cdom,$cnum,$group,$action) = @_;
10619:     my ($from_namespace,$to_namespace);
10620:     if ($action eq 'delete') {
10621:         $from_namespace = 'coursegroups';
10622:         $to_namespace = 'deleted_groups';
10623:     } else {
10624:         $from_namespace = 'deleted_groups';
10625:         $to_namespace = 'coursegroups';
10626:     }
10627:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
10628:     if (my $tmp = &error(%curr_group)) {
10629:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10630:         return ('read error',$tmp);
10631:     } else {
10632:         my %savedsettings = %curr_group; 
10633:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
10634:         my $deloutcome;
10635:         if ($result eq 'ok') {
10636:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
10637:         } else {
10638:             return ('write error',$result);
10639:         }
10640:         if ($deloutcome eq 'ok') {
10641:             return 'ok';
10642:         } else {
10643:             return ('delete error',$deloutcome);
10644:         }
10645:     }
10646: }
10647: 
10648: sub modify_group_roles {
10649:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context,
10650:         $othdomby,$requester) = @_;
10651:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10652:     my $role = 'gr/'.&escape($userprivs);
10653:     my ($uname,$udom) = split(/:/,$user);
10654:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context,
10655:                              $othdomby,$requester);
10656:     if ($result eq 'ok') {
10657:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10658:     }
10659:     return $result;
10660: }
10661: 
10662: sub modify_coursegroup_membership {
10663:     my ($cdom,$cnum,$membership) = @_;
10664:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
10665:     return $result;
10666: }
10667: 
10668: sub get_active_groups {
10669:     my ($udom,$uname,$cdom,$cnum) = @_;
10670:     my $now = time;
10671:     my %groups = ();
10672:     foreach my $key (keys(%env)) {
10673:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
10674:             my ($start,$end) = split(/\./,$env{$key});
10675:             if (($end!=0) && ($end<$now)) { next; }
10676:             if (($start!=0) && ($start>$now)) { next; }
10677:             if ($1 eq $cdom && $2 eq $cnum) {
10678:                 $groups{$3} = $env{$key} ;
10679:             }
10680:         }
10681:     }
10682:     return %groups;
10683: }
10684: 
10685: sub get_group_membership {
10686:     my ($cdom,$cnum,$group) = @_;
10687:     return(&dump('groupmembership',$cdom,$cnum,$group));
10688: }
10689: 
10690: sub get_users_groups {
10691:     my ($udom,$uname,$courseid) = @_;
10692:     my @usersgroups;
10693:     my $cachetime=1800;
10694: 
10695:     my $hashid="$udom:$uname:$courseid";
10696:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10697:     if (defined($cached)) {
10698:         @usersgroups = split(/:/,$grouplist);
10699:     } else {  
10700:         $grouplist = '';
10701:         my $courseurl = &courseid_to_courseurl($courseid);
10702:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
10703:         my $access_end = $env{'course.'.$courseid.
10704:                               '.default_enrollment_end_date'};
10705:         my $now = time;
10706:         foreach my $key (keys(%roleshash)) {
10707:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10708:                 my $group = $1;
10709:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10710:                     my $start = $2;
10711:                     my $end = $1;
10712:                     if ($start == -1) { next; } # deleted from group
10713:                     if (($start!=0) && ($start>$now)) { next; }
10714:                     if (($end!=0) && ($end<$now)) {
10715:                         if ($access_end && $access_end < $now) {
10716:                             if ($access_end - $end < 86400) {
10717:                                 push(@usersgroups,$group);
10718:                             }
10719:                         }
10720:                         next;
10721:                     }
10722:                     push(@usersgroups,$group);
10723:                 }
10724:             }
10725:         }
10726:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10727:         $grouplist = join(':',@usersgroups);
10728:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10729:     }
10730:     return @usersgroups;
10731: }
10732: 
10733: sub devalidate_getgroups_cache {
10734:     my ($udom,$uname,$cdom,$cnum)=@_;
10735:     my $courseid = $cdom.'_'.$cnum;
10736: 
10737:     my $hashid="$udom:$uname:$courseid";
10738:     &devalidate_cache_new('getgroups',$hashid);
10739: }
10740: 
10741: # ------------------------------------------------------------------ Plain Text
10742: 
10743: sub plaintext {
10744:     my ($short,$type,$cid,$forcedefault) = @_;
10745:     if ($short =~ m{^cr/}) {
10746: 	return (split('/',$short))[-1];
10747:     }
10748:     if (!defined($cid)) {
10749:         $cid = $env{'request.course.id'};
10750:     }
10751:     my %rolenames = (
10752:                       Course    => 'std',
10753:                       Community => 'alt1',
10754:                       Placement => 'std',
10755:                     );
10756:     if ($cid ne '') {
10757:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10758:             unless ($forcedefault) {
10759:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10760:                 &Apache::lonlocal::mt_escape(\$roletext);
10761:                 return &Apache::lonlocal::mt($roletext);
10762:             }
10763:         }
10764:     }
10765:     if ((defined($type)) && (defined($rolenames{$type})) &&
10766:         (defined($rolenames{$type})) && 
10767:         (defined($prp{$short}{$rolenames{$type}}))) {
10768:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10769:     } elsif ($cid ne '') {
10770:         my $crstype = $env{'course.'.$cid.'.type'};
10771:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10772:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10773:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10774:         }
10775:     }
10776:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10777: }
10778: 
10779: # ----------------------------------------------------------------- Assign Role
10780: 
10781: sub assignrole {
10782:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10783:         $context,$othdomby,$requester,$reqsec,$reqrole)=@_;
10784:     my ($mrole,$rolelogcontext);
10785:     if ($role =~ /^cr\//) {
10786:         my $cwosec=$url;
10787:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10788:         if ((!&allowed('ccr',$cwosec)) && (!&allowed('ccr',$udom))) {
10789:             my $refused = 1;
10790:             if ($context eq 'requestcourses') {
10791:                 if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10792:                     if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10793:                         if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10794:                             my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10795:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10796:                             if ($crsenv{'internal.courseowner'} eq
10797:                                 $env{'user.name'}.':'.$env{'user.domain'}) {
10798:                                 $refused = '';
10799:                             }
10800:                         }
10801:                     }
10802:                 }
10803:             } elsif (($context eq 'course') && ($othdomby eq 'othdombyuser')) {
10804:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10805:                 my ($sec) = ($url =~ m{^/\Q$cwosec\E/(.*)$});
10806:                 my $key = "$uname:$udom:$role:$sec";
10807:                 my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
10808:                 if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10809:                     if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10810:                         $refused = '';
10811:                     }
10812:                 }
10813:             }
10814:             if ($refused) {
10815:                 &logthis('Refused custom assignrole: '.
10816:                          $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10817:                          ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10818:                 return 'refused';
10819:             }
10820:         }
10821:         $mrole='cr';
10822:     } elsif ($role =~ /^gr\//) {
10823:         my $cwogrp=$url;
10824:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10825:         if (!&allowed('mdg',$cwogrp)) {
10826:             my $refused = 1;
10827:             if (($refused) && ($othdomby eq 'othdombyuser') && ($requester ne '') && ($reqrole ne '')) {
10828:                 my ($cdom,$cnum) = ($cwogrp =~ m{^/?($match_domain)/($match_courseid)$});
10829:                 my $key = "$uname:$udom:$reqrole:$reqsec";
10830:                 my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
10831:                 if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10832:                     if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10833:                         $refused = '';
10834:                     }
10835:                 }
10836:             }
10837:             if ($refused) {
10838:                 &logthis('Refused group assignrole: '.
10839:                          $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10840:                          $env{'user.name'}.' at '.$env{'user.domain'});
10841:                 return 'refused';
10842:             }
10843:         }
10844:         $mrole='gr';
10845:     } else {
10846:         my $cwosec=$url;
10847:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10848:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10849:             my $refused;
10850:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10851:                 if (!(&allowed('c'.$role,$url))) {
10852:                     $refused = 1;
10853:                 }
10854:             } else {
10855:                 $refused = 1;
10856:             }
10857:             if ($refused) {
10858:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10859:                 if (!$selfenroll && ($othdomby ne 'othdombyuser') &&
10860:                    (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10861:                     my %crsenv;
10862:                     if ($role eq 'cc' || $role eq 'co') {
10863:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10864:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10865:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10866:                                 if ($crsenv{'internal.courseowner'} eq 
10867:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10868:                                     $refused = '';
10869:                                 }
10870:                             }
10871:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10872:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10873:                                 if ($crsenv{'internal.courseowner'} eq 
10874:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10875:                                     $refused = '';
10876:                                 }
10877:                             }
10878:                         }
10879:                     }
10880:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10881:                     if ($role eq 'st') {
10882:                         $refused = '';
10883:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10884:                         $refused = '';
10885:                     }
10886:                 } elsif ($othdomby eq 'othdombyuser') {
10887:                     my ($key,%queuedrolereq);
10888:                     if ($context eq 'course') {
10889:                         my ($sec) = ($url =~ m{^/\Q$cwosec\E/(.*)$});
10890:                         $key = "$uname:$udom:$role:$sec";
10891:                         %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$cdom,$cnum);
10892:                         if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10893:                             if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10894:                                 if ((($role eq 'cc') && ($cnum !~ /^$match_community$/)) || 
10895:                                     (($role eq 'co') && ($cnum =~ /^$match_community$/))) {
10896:                                     my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10897:                                     if ($crsenv{'internal.courseowner'} eq $requester) {
10898:                                         $refused = '';
10899:                                     }
10900:                                 } elsif ($role =~ /^(?:in|ta|ep|st)$/) {
10901:                                     $refused = '';
10902:                                 }
10903:                             }
10904:                         }
10905:                     } elsif (($context eq 'author') && ($role =~ /^ca|aa$/)) {
10906:                         my $key = "$uname:$udom:$role"; 
10907:                         my ($audom,$auname) = ($url =~ m{^/($match_domain)/($match_username)$});
10908:                         if (($audom ne '') && ($auname ne '')) {
10909:                             my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$audom,$auname);
10910:                             if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10911:                                 if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10912:                                     $refused = '';
10913:                                 }
10914:                             }
10915:                         }
10916:                     } elsif (($context eq 'domain') && ($role ne 'dc') && ($role ne 'su')) {
10917:                         my $key = "$uname:$udom:$role";
10918:                         my ($roledom) = ($url =~ m{^/($match_domain)/\Q$role\E$});
10919:                         if ($roledom ne '') {
10920:                             my $confname = $roledom.'-domainconfig';
10921:                             my %queuedrolereq = &Apache::lonnet::get('nohist_othdomqueued',[$key],$roledom,$confname);
10922:                             if ((exists($queuedrolereq{$key})) && (ref($queuedrolereq{$key}) eq 'HASH')) {
10923:                                 if (($queuedrolereq{$key}{'adj'} eq 'user') && ($queuedrolereq{$key}{'requester'} eq $requester)) {
10924:                                     $refused = '';
10925:                                 }
10926:                             }
10927:                         }
10928:                     }
10929:                 } elsif ($context eq 'requestcourses') {
10930:                     my @possroles = ('st','ta','ep','in','cc','co');
10931:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10932:                         my $wrongcc;
10933:                         if ($cnum =~ /^$match_community$/) {
10934:                             $wrongcc = 1 if ($role eq 'cc');
10935:                         } else {
10936:                             $wrongcc = 1 if ($role eq 'co');
10937:                         }
10938:                         unless ($wrongcc) {
10939:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10940:                             if ($crsenv{'internal.courseowner'} eq 
10941:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10942:                                 $refused = '';
10943:                             }
10944:                         }
10945:                     }
10946:                 } elsif ($context eq 'requestauthor') {
10947:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10948:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10949:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10950:                             $refused = '';
10951:                         } else {
10952:                             my %domdefaults = &get_domain_defaults($udom);
10953:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10954:                                 my $checkbystatus;
10955:                                 if ($env{'user.adv'}) { 
10956:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10957:                                     if ($disposition eq 'automatic') {
10958:                                         $refused = '';
10959:                                     } elsif ($disposition eq '') {
10960:                                         $checkbystatus = 1;
10961:                                     } 
10962:                                 } else {
10963:                                     $checkbystatus = 1;
10964:                                 }
10965:                                 if ($checkbystatus) {
10966:                                     if ($env{'environment.inststatus'}) {
10967:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10968:                                         foreach my $type (@inststatuses) {
10969:                                             if (($type ne '') &&
10970:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10971:                                                 $refused = '';
10972:                                             }
10973:                                         }
10974:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10975:                                         $refused = '';
10976:                                     }
10977:                                 }
10978:                             }
10979:                         }
10980:                     }
10981:                 } elsif (($context eq 'author') && (($role eq 'ca' || $role eq 'aa'))) {
10982:                     if ($url =~ m{^/($match_domain)/($match_username)$}) {
10983:                         my ($audom,$auname) = ($1,$2);
10984:                         if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) &&
10985:                             ($env{"environment.internal.manager.$url"})) {
10986:                             $refused = '';
10987:                             $rolelogcontext = 'coauthor';
10988:                         }
10989:                     }
10990:                 }
10991:                 if ($refused) {
10992:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10993:                              ' '.$role.' '.$end.' '.$start.' by '.
10994: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10995:                     return 'refused';
10996:                 }
10997:             }
10998:         } elsif ($role eq 'au') {
10999:             if ($url ne '/'.$udom.'/') {
11000:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
11001:                          ' to assign author role for '.$uname.':'.$udom.
11002:                          ' in domain: '.$url.' refused (wrong domain).');
11003:                 return 'refused';
11004:             }
11005:         }
11006:         $mrole=$role;
11007:     }
11008:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
11009:                 "$udom:$uname:$url".'_'."$mrole=$role";
11010:     if ($end) { $command.='_'.$end; }
11011:     if ($start) {
11012: 	if ($end) { 
11013:            $command.='_'.$start; 
11014:         } else {
11015:            $command.='_0_'.$start;
11016:         }
11017:     }
11018:     my $origstart = $start;
11019:     my $origend = $end;
11020:     my $delflag;
11021: # actually delete
11022:     if ($deleteflag) {
11023: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
11024: # modify command to delete the role
11025:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
11026:                 "$udom:$uname:$url".'_'."$mrole";
11027: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
11028: # set start and finish to negative values for userrolelog
11029:            $start=-1;
11030:            $end=-1;
11031:            $delflag = 1;
11032:         }
11033:     }
11034: # send command
11035:     my $answer=&reply($command,&homeserver($uname,$udom));
11036: # log new user role if status is ok
11037:     if ($answer eq 'ok') {
11038: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
11039:         if (($role eq 'cc') || ($role eq 'in') ||
11040:             ($role eq 'ep') || ($role eq 'ad') ||
11041:             ($role eq 'ta') || ($role eq 'st') ||
11042:             ($role=~/^cr/) || ($role eq 'gr') ||
11043:             ($role eq 'co')) {
11044: # for course roles, perform group memberships changes triggered by role change.
11045:             unless ($role =~ /^gr/) {
11046:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
11047:                                                  $origstart,$selfenroll,$context);
11048:             }
11049:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
11050:                            $selfenroll,$context,$othdomby,$requester);
11051:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
11052:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
11053:                  ($role eq 'da')) {
11054:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
11055:                            $context,$othdomby,$requester);
11056:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
11057:             if ($rolelogcontext eq '') {
11058:                 $rolelogcontext = $context;
11059:             }
11060:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
11061:                              $rolelogcontext,$othdomby,$requester); 
11062:         }
11063:         if ($role eq 'cc') {
11064:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
11065:         }
11066:     }
11067:     return $answer;
11068: }
11069: 
11070: sub autoupdate_coowners {
11071:     my ($url,$end,$start,$uname,$udom) = @_;
11072:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
11073:     if (($cdom ne '') && ($cnum ne '')) {
11074:         my $now = time;
11075:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
11076:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
11077:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
11078:             my $instcode = $coursehash{'internal.coursecode'};
11079:             my $xlists = $coursehash{'internal.crosslistings'};
11080:             if ($instcode ne '') {
11081:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
11082:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
11083:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
11084:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
11085:                         unless ($result eq 'valid') {
11086:                             if ($xlists ne '') {
11087:                                 foreach my $xlist (split(',',$xlists)) {
11088:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
11089:                                     $result =
11090:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
11091:                                                                       $inst_crosslist,$uname.':'.$udom);
11092:                                     last if ($result eq 'valid');
11093:                                 }
11094:                             }
11095:                         }
11096:                         if ($result eq 'valid') {
11097:                             if ($coursehash{'internal.co-owners'}) {
11098:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
11099:                                     push(@newcoowners,$coowner);
11100:                                 }
11101:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
11102:                                     push(@newcoowners,$uname.':'.$udom);
11103:                                 }
11104:                                 @newcoowners = sort(@newcoowners);
11105:                             } else {
11106:                                 push(@newcoowners,$uname.':'.$udom);
11107:                             }
11108:                         } elsif ($coursehash{'internal.co-owners'}) {
11109:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
11110:                                 unless ($coowner eq $uname.':'.$udom) {
11111:                                     push(@newcoowners,$coowner);
11112:                                 }
11113:                             }
11114:                             unless (@newcoowners > 0) {
11115:                                 $delcoowners = 1;
11116:                                 $coowners = '';
11117:                             }
11118:                         }
11119:                         if (@newcoowners || $delcoowners) {
11120:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
11121:                                             $delcoowners,@newcoowners);
11122:                         }
11123:                     }
11124:                 }
11125:             }
11126:         }
11127:     }
11128: }
11129: 
11130: sub store_coowners {
11131:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
11132:     my $cid = $cdom.'_'.$cnum;
11133:     my ($coowners,$delresult,$putresult);
11134:     if (@newcoowners) {
11135:         $coowners = join(',',@newcoowners);
11136:         my %coownershash = (
11137:                             'internal.co-owners' => $coowners,
11138:                            );
11139:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
11140:         if ($putresult eq 'ok') {
11141:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
11142:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
11143:             }
11144:         }
11145:     }
11146:     if ($delcoowners) {
11147:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
11148:         if ($delresult eq 'ok') {
11149:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
11150:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
11151:             }
11152:         }
11153:     }
11154:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
11155:         my %crsinfo =
11156:             &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
11157:         if (ref($crsinfo{$cid}) eq 'HASH') {
11158:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
11159:             my $cidput = &courseidput($cdom,\%crsinfo,$chome,'notime');
11160:         }
11161:     }
11162: }
11163: 
11164: # -------------------------------------------------- Modify user authentication
11165: # Overrides without validation
11166: 
11167: sub modifyuserauth {
11168:     my ($udom,$uname,$umode,$upass)=@_;
11169:     my $uhome=&homeserver($uname,$udom);
11170:     my $allowed;
11171:     if (&allowed('mau',$udom)) {
11172:         $allowed = 1;
11173:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
11174:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
11175:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
11176:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
11177:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
11178:         if (($cdom ne '') && ($cnum ne '')) {
11179:             my $is_owner = &is_course_owner($cdom,$cnum);
11180:             if ($is_owner) {
11181:                 $allowed = 1;
11182:             }
11183:         }
11184:     }
11185:     unless ($allowed) { return 'refused'; }
11186:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
11187:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
11188:              ' in domain '.$env{'request.role.domain'});  
11189:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
11190: 		     &escape($upass),$uhome);
11191:     my $ip = &get_requestor_ip();
11192:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
11193:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
11194:          '(Remote '.$ip.'): '.$reply);
11195:     &log($udom,,$uname,$uhome,
11196:         'Authentication changed by '.$env{'user.domain'}.', '.
11197:                                      $env{'user.name'}.', '.$umode.
11198:          '(Remote '.$ip.'): '.$reply);
11199:     unless ($reply eq 'ok') {
11200:         &logthis('Authentication mode error: '.$reply);
11201: 	return 'error: '.$reply;
11202:     }   
11203:     return 'ok';
11204: }
11205: 
11206: # --------------------------------------------------------------- Modify a user
11207: 
11208: sub modifyuser {
11209:     my ($udom,    $uname, $uid,
11210:         $umode,   $upass, $first,
11211:         $middle,  $last,  $gene,
11212:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
11213:     $udom= &LONCAPA::clean_domain($udom);
11214:     $uname=&LONCAPA::clean_username($uname);
11215:     my $showcandelete = 'none';
11216:     if (ref($candelete) eq 'ARRAY') {
11217:         if (@{$candelete} > 0) {
11218:             $showcandelete = join(', ',@{$candelete});
11219:         }
11220:     }
11221:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
11222:              $umode.', '.$first.', '.$middle.', '.
11223: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
11224:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
11225:                                      ' desiredhome not specified'). 
11226:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
11227:              ' in domain '.$env{'request.role.domain'});
11228:     my $uhome=&homeserver($uname,$udom,'true');
11229:     my $newuser;
11230:     if ($uhome eq 'no_host') {
11231:         $newuser = 1;
11232:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
11233:                 ($umode eq 'lti')) {
11234:             return 'error: more information needed to create new user';
11235:         }
11236:     }
11237: # ----------------------------------------------------------------- Create User
11238:     if (($uhome eq 'no_host') && 
11239: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
11240:         my $unhome='';
11241:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
11242:             $unhome = $desiredhome;
11243: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
11244: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
11245:         } else { # load balancing routine for determining $unhome
11246:             my $loadm=10000000;
11247: 	    my %servers = &get_servers($udom,'library');
11248: 	    foreach my $tryserver (keys(%servers)) {
11249: 		my $answer=reply('load',$tryserver);
11250: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
11251: 		    $loadm=$answer;
11252: 		    $unhome=$tryserver;
11253: 		}
11254: 	    }
11255:         }
11256:         if (($unhome eq '') || ($unhome eq 'no_host')) {
11257: 	    return 'error: unable to find a home server for '.$uname.
11258:                    ' in domain '.$udom;
11259:         }
11260:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
11261:                          &escape($upass),$unhome);
11262: 	unless ($reply eq 'ok') {
11263:             return 'error: '.$reply;
11264:         }   
11265:         $uhome=&homeserver($uname,$udom,'true');
11266:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
11267: 	    return 'error: unable verify users home machine.';
11268:         }
11269:     }   # End of creation of new user
11270: # ---------------------------------------------------------------------- Add ID
11271:     if ($uid) {
11272:        $uid=~tr/A-Z/a-z/;
11273:        my %uidhash=&idrget($udom,$uname);
11274:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
11275:          && (!$forceid)) {
11276: 	  unless ($uid eq $uidhash{$uname}) {
11277: 	      return 'error: user id "'.$uid.'" does not match '.
11278:                   'current user id "'.$uidhash{$uname}.'".';
11279:           }
11280:        } else {
11281: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
11282:        }
11283:     }
11284: # -------------------------------------------------------------- Add names, etc
11285:     my @tmp=&get('environment',
11286: 		   ['firstname','middlename','lastname','generation','id',
11287:                     'permanentemail','inststatus'],
11288: 		   $udom,$uname);
11289:     my (%names,%oldnames);
11290:     if ($tmp[0] =~ m/^error:.*/) { 
11291:         %names=(); 
11292:     } else {
11293:         %names = @tmp;
11294:         %oldnames = %names;
11295:     }
11296: #
11297: # If name, email and/or uid are blank (e.g., because an uploaded file
11298: # of users did not contain them), do not overwrite existing values
11299: # unless field is in $candelete array ref.  
11300: #
11301: 
11302:     my @fields = ('firstname','middlename','lastname','generation',
11303:                   'permanentemail','id');
11304:     my %newvalues;
11305:     if (ref($candelete) eq 'ARRAY') {
11306:         foreach my $field (@fields) {
11307:             if (grep(/^\Q$field\E$/,@{$candelete})) {
11308:                 if ($field eq 'firstname') {
11309:                     $names{$field} = $first;
11310:                 } elsif ($field eq 'middlename') {
11311:                     $names{$field} = $middle;
11312:                 } elsif ($field eq 'lastname') {
11313:                     $names{$field} = $last;
11314:                 } elsif ($field eq 'generation') { 
11315:                     $names{$field} = $gene;
11316:                 } elsif ($field eq 'permanentemail') {
11317:                     $names{$field} = $email;
11318:                 } elsif ($field eq 'id') {
11319:                     $names{$field}  = $uid;
11320:                 }
11321:             }
11322:         }
11323:     }
11324:     if ($first)  { $names{'firstname'}  = $first; }
11325:     if (defined($middle)) { $names{'middlename'} = $middle; }
11326:     if ($last)   { $names{'lastname'}   = $last; }
11327:     if (defined($gene))   { $names{'generation'} = $gene; }
11328:     if ($email) {
11329:        $email=~s/[^\w\@\.\-\,]//gs;
11330:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
11331:     }
11332:     if ($uid) { $names{'id'}  = $uid; }
11333:     if (defined($inststatus)) {
11334:         $names{'inststatus'} = '';
11335:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
11336:         if (ref($usertypes) eq 'HASH') {
11337:             my @okstatuses; 
11338:             foreach my $item (split(/:/,$inststatus)) {
11339:                 if (defined($usertypes->{$item})) {
11340:                     push(@okstatuses,$item);  
11341:                 }
11342:             }
11343:             if (@okstatuses) {
11344:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
11345:             }
11346:         }
11347:     }
11348:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
11349:                  $umode.', '.$first.', '.$middle.', '.
11350:                  $last.', '.$gene.', '.$email.', '.$inststatus;
11351:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
11352:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
11353:     } else {
11354:         $logmsg .= ' during self creation';
11355:     }
11356:     my $changed;
11357:     if ($newuser) {
11358:         $changed = 1;
11359:     } else {
11360:         foreach my $field (@fields) {
11361:             if ($names{$field} ne $oldnames{$field}) {
11362:                 $changed = 1;
11363:                 last;
11364:             }
11365:         }
11366:     }
11367:     unless ($changed) {
11368:         $logmsg = 'No changes in user information needed for: '.$logmsg;
11369:         &logthis($logmsg);
11370:         return 'ok';
11371:     }
11372:     my $reply = &put('environment', \%names, $udom,$uname);
11373:     if ($reply ne 'ok') { 
11374:         return 'error: '.$reply;
11375:     }
11376:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
11377:         &devalidate_cache_new('emailscache',$uname.':'.$udom);
11378:     }
11379:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
11380:     &devalidate_cache_new('namescache',$uname.':'.$udom);
11381:     $logmsg = 'Success modifying user '.$logmsg;
11382:     &logthis($logmsg);
11383:     return 'ok';
11384: }
11385: 
11386: # -------------------------------------------------------------- Modify student
11387: 
11388: sub modifystudent {
11389:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
11390:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
11391:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
11392:     if (!$cid) {
11393: 	unless ($cid=$env{'request.course.id'}) {
11394: 	    return 'not_in_class';
11395: 	}
11396:     }
11397: # --------------------------------------------------------------- Make the user
11398:     my $reply=&modifyuser
11399: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
11400:          $desiredhome,$email,$inststatus);
11401:     unless ($reply eq 'ok') { return $reply; }
11402:     # This will cause &modify_student_enrollment to get the uid from the
11403:     # student's environment
11404:     $uid = undef if (!$forceid);
11405:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
11406:                                         $gene,$usec,$end,$start,$type,$locktype,
11407:                                         $cid,$selfenroll,$context,$credits,$instsec);
11408:     return $reply;
11409: }
11410: 
11411: sub modify_student_enrollment {
11412:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
11413:         $locktype,$cid,$selfenroll,$context,$credits,$instsec,$othdomby,$requester) = @_;
11414:     my ($cdom,$cnum,$chome);
11415:     if (!$cid) {
11416: 	unless ($cid=$env{'request.course.id'}) {
11417: 	    return 'not_in_class';
11418: 	}
11419: 	$cdom=$env{'course.'.$cid.'.domain'};
11420: 	$cnum=$env{'course.'.$cid.'.num'};
11421:     } else {
11422: 	($cdom,$cnum)=split(/_/,$cid);
11423:     }
11424:     $chome=$env{'course.'.$cid.'.home'};
11425:     if (!$chome) {
11426: 	$chome=&homeserver($cnum,$cdom);
11427:     }
11428:     if (!$chome) { return 'unknown_course'; }
11429:     # Make sure the user exists
11430:     my $uhome=&homeserver($uname,$udom);
11431:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
11432: 	return 'error: no such user';
11433:     }
11434:     # Get student data if we were not given enough information
11435:     if (!defined($first)  || $first  eq '' || 
11436:         !defined($last)   || $last   eq '' || 
11437:         !defined($uid)    || $uid    eq '' || 
11438:         !defined($middle) || $middle eq '' || 
11439:         !defined($gene)   || $gene   eq '') {
11440:         # They did not supply us with enough data to enroll the student, so
11441:         # we need to pick up more information.
11442:         my %tmp = &get('environment',
11443:                        ['firstname','middlename','lastname', 'generation','id']
11444:                        ,$udom,$uname);
11445: 
11446:         #foreach my $key (keys(%tmp)) {
11447:         #    &logthis("key $key = ".$tmp{$key});
11448:         #}
11449:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
11450:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11451:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
11452:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
11453:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
11454:     }
11455:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
11456:     my $user = "$uname:$udom";
11457:     my %old_entry = &get('classlist',[$user],$cdom,$cnum);
11458:     my $reply=cput('classlist',
11459: 		   {$user => 
11460: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
11461: 		   $cdom,$cnum);
11462:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
11463:         &devalidate_getsection_cache($udom,$uname,$cid);
11464:     } else { 
11465: 	return 'error: '.$reply;
11466:     }
11467:     # Add student role to user
11468:     my $uurl='/'.$cid;
11469:     $uurl=~s/\_/\//g;
11470:     if ($usec) {
11471: 	$uurl.='/'.$usec;
11472:     }
11473:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11474:                              $selfenroll,$context,$othdomby,$requester);
11475:     if ($result ne 'ok') {
11476:         if ($old_entry{$user} ne '') {
11477:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11478:         } else {
11479:             $reply = &del('classlist',[$user],$cdom,$cnum);
11480:         }
11481:     }
11482:     return $result; 
11483: }
11484: 
11485: sub format_name {
11486:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11487:     my $name;
11488:     if ($first ne 'lastname') {
11489: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11490:     } else {
11491: 	if ($lastname=~/\S/) {
11492: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11493: 	    $name=~s/\s+,/,/;
11494: 	} else {
11495: 	    $name.= $firstname.' '.$middlename.' '.$generation;
11496: 	}
11497:     }
11498:     $name=~s/^\s+//;
11499:     $name=~s/\s+$//;
11500:     $name=~s/\s+/ /g;
11501:     return $name;
11502: }
11503: 
11504: # ------------------------------------------------- Write to course preferences
11505: 
11506: sub writecoursepref {
11507:     my ($courseid,%prefs)=@_;
11508:     $courseid=~s/^\///;
11509:     $courseid=~s/\_/\//g;
11510:     my ($cdomain,$cnum)=split(/\//,$courseid);
11511:     my $chome=homeserver($cnum,$cdomain);
11512:     if (($chome eq '') || ($chome eq 'no_host')) { 
11513: 	return 'error: no such course';
11514:     }
11515:     my $cstring='';
11516:     foreach my $pref (keys(%prefs)) {
11517: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
11518:     }
11519:     $cstring=~s/\&$//;
11520:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11521: }
11522: 
11523: # ---------------------------------------------------------- Make/modify course
11524: 
11525: sub createcourse {
11526:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
11527:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
11528:     $url=&declutter($url);
11529:     my $cid='';
11530:     if ($context eq 'requestcourses') {
11531:         my $can_create = 0;
11532:         my ($ownername,$ownerdom) = split(':',$course_owner);
11533:         if ($udom eq $ownerdom) {
11534:             my $reload;
11535:             if (($callercontext eq 'auto') &&
11536:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11537:                 $reload = 'reload';
11538:             }
11539:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
11540:                                   $context)) {
11541:                 $can_create = 1;
11542:             }
11543:         } else {
11544:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11545:                                            $category);
11546:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11547:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11548:                 if (@curr > 0) {
11549:                     my @options = qw(approval validate autolimit);
11550:                     my $optregex = join('|',@options);
11551:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11552:                         $can_create = 1;
11553:                     }
11554:                 }
11555:             }
11556:         }
11557:         if ($can_create) {
11558:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11559:                 unless (&allowed('ccc',$udom)) {
11560:                     return 'refused'; 
11561:                 }
11562:             }
11563:         } else {
11564:             return 'refused';
11565:         }
11566:     } elsif (!&allowed('ccc',$udom)) {
11567:         return 'refused';
11568:     }
11569: # --------------------------------------------------------------- Get Unique ID
11570:     my $uname;
11571:     if ($cnum =~ /^$match_courseid$/) {
11572:         my $chome=&homeserver($cnum,$udom,'true');
11573:         if (($chome eq '') || ($chome eq 'no_host')) {
11574:             $uname = $cnum;
11575:         } else {
11576:             $uname = &generate_coursenum($udom,$crstype);
11577:         }
11578:     } else {
11579:         $uname = &generate_coursenum($udom,$crstype);
11580:     }
11581:     return $uname if ($uname =~ /^error/);
11582: # -------------------------------------------------- Check supplied server name
11583:     if (!defined($course_server)) {
11584:         if (defined(&domain($udom,'primary'))) {
11585:             $course_server = &domain($udom,'primary');
11586:         } else {
11587:             $course_server = $env{'user.home'}; 
11588:         }
11589:     }
11590:     my %host_servers =
11591:         &get_servers($udom,'library');
11592:     unless ($host_servers{$course_server}) {
11593:         return 'error: invalid home server for course: '.$course_server;
11594:     }
11595: # ------------------------------------------------------------- Make the course
11596:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
11597:                       $course_server);
11598:     unless ($reply eq 'ok') { return 'error: '.$reply; }
11599:     my $uhome=&homeserver($uname,$udom,'true');
11600:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
11601: 	return 'error: no such course';
11602:     }
11603: # ----------------------------------------------------------------- Course made
11604: # log existence
11605:     my $now = time;
11606:     my $newcourse = {
11607:                     $udom.'_'.$uname => {
11608:                                      description => $description,
11609:                                      inst_code   => $inst_code,
11610:                                      owner       => $course_owner,
11611:                                      type        => $crstype,
11612:                                      creator     => $env{'user.name'}.':'.
11613:                                                     $env{'user.domain'},
11614:                                      created     => $now,
11615:                                      context     => $context,
11616:                                                 },
11617:                     };
11618:     &courseidput($udom,$newcourse,$uhome,'notime');
11619: # set toplevel url
11620:     my $topurl=$url;
11621:     unless ($nonstandard) {
11622: # ------------------------------------------ For standard courses, make top url
11623:         my $mapurl=&clutter($url);
11624:         if ($mapurl eq '/res/') { $mapurl=''; }
11625:         $env{'form.initmap'}=(<<ENDINITMAP);
11626: <map>
11627: <resource id="1" type="start"></resource>
11628: <resource id="2" src="$mapurl"></resource>
11629: <resource id="3" type="finish"></resource>
11630: <link index="1" from="1" to="2"></link>
11631: <link index="2" from="2" to="3"></link>
11632: </map>
11633: ENDINITMAP
11634:         $topurl=&declutter(
11635:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
11636:                           );
11637:     }
11638: # ----------------------------------------------------------- Write preferences
11639:     &writecoursepref($udom.'_'.$uname,
11640:                      ('description'              => $description,
11641:                       'url'                      => $topurl,
11642:                       'internal.creator'         => $env{'user.name'}.':'.
11643:                                                     $env{'user.domain'},
11644:                       'internal.created'         => $now,
11645:                       'internal.creationcontext' => $context)
11646:                     );
11647:     return '/'.$udom.'/'.$uname;
11648: }
11649: 
11650: # ------------------------------------------------------------------- Create ID
11651: sub generate_coursenum {
11652:     my ($udom,$crstype) = @_;
11653:     my $domdesc = &domain($udom);
11654:     return 'error: invalid domain' if ($domdesc eq '');
11655:     my $first;
11656:     if ($crstype eq 'Community') {
11657:         $first = '0';
11658:     } else {
11659:         $first = int(1+rand(9)); 
11660:     } 
11661:     my $uname=$first.
11662:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11663:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
11664:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11665: # ----------------------------------------------- Make sure that does not exist
11666:     my $uhome=&homeserver($uname,$udom,'true');
11667:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
11668:         if ($crstype eq 'Community') {
11669:             $first = '0';
11670:         } else {
11671:             $first = int(1+rand(9));
11672:         }
11673:         $uname=$first.
11674:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11675:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
11676:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11677:         $uhome=&homeserver($uname,$udom,'true');
11678:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
11679:             return 'error: unable to generate unique course-ID';
11680:         }
11681:     }
11682:     return $uname;
11683: }
11684: 
11685: sub is_course {
11686:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
11687:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
11688: 
11689:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11690:     my $uhome=&homeserver($cnum,$cdom);
11691:     my $iscourse;
11692:     if (grep { $_ eq $uhome } current_machine_ids()) {
11693:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11694:     } else {
11695:         my $hashid = $cdom.':'.$cnum;
11696:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11697:         unless (defined($cached)) {
11698:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11699:                                         $cnum,undef,undef,'.');
11700:             $iscourse = 0;
11701:             if (exists($courses{$cdom.'_'.$cnum})) {
11702:                 $iscourse = 1;
11703:             }
11704:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
11705:         }
11706:     }
11707:     return unless ($iscourse);
11708:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
11709: }
11710: 
11711: sub store_userdata {
11712:     my ($storehash,$datakey,$namespace,$udom,$uname,$ip) = @_;
11713:     my $result;
11714:     if ($datakey ne '') {
11715:         if (ref($storehash) eq 'HASH') {
11716:             if ($udom eq '' || $uname eq '') {
11717:                 $udom = $env{'user.domain'};
11718:                 $uname = $env{'user.name'};
11719:             }
11720:             my $uhome=&homeserver($uname,$udom);
11721:             if (($uhome eq '') || ($uhome eq 'no_host')) {
11722:                 $result = 'error: no_host';
11723:             } else {
11724:                 if ($ip ne '') {
11725:                     $storehash->{'ip'} = $ip;
11726:                 } else {
11727:                     $storehash->{'ip'} = &get_requestor_ip();
11728:                 }
11729:                 $storehash->{'host'} = $perlvar{'lonHostID'};
11730: 
11731:                 my $namevalue='';
11732:                 foreach my $key (keys(%{$storehash})) {
11733:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11734:                 }
11735:                 $namevalue=~s/\&$//;
11736:                 unless ($namespace eq 'courserequests') {
11737:                     $datakey = &escape($datakey);
11738:                 }
11739:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
11740:                                   $namevalue,$uhome);
11741:             }
11742:         } else {
11743:             $result = 'error: data to store was not a hash reference'; 
11744:         }
11745:     } else {
11746:         $result= 'error: invalid requestkey'; 
11747:     }
11748:     return $result;
11749: }
11750: 
11751: # ---------------------------------------------------------- Assign Custom Role
11752: 
11753: sub assigncustomrole {
11754:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,
11755:         $selfenroll,$context,$othdomby,$requester)=@_;
11756:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
11757:                        $end,$start,$deleteflag,$selfenroll,$context,$othdomby,
11758:                        $requester);
11759: }
11760: 
11761: # ----------------------------------------------------------------- Revoke Role
11762: 
11763: sub revokerole {
11764:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
11765:     my $now=time;
11766:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
11767: }
11768: 
11769: # ---------------------------------------------------------- Revoke Custom Role
11770: 
11771: sub revokecustomrole {
11772:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
11773:     my $now=time;
11774:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
11775:            $deleteflag,$selfenroll,$context);
11776: }
11777: 
11778: # ------------------------------------------------------------ Disk usage
11779: sub diskusage {
11780:     my ($udom,$uname,$directorypath,$getpropath)=@_;
11781:     $directorypath =~ s/\/$//;
11782:     my $listing=&reply('du2:'.&escape($directorypath).':'
11783:                        .&escape($getpropath).':'.&escape($uname).':'
11784:                        .&escape($udom),homeserver($uname,$udom));
11785:     if ($listing eq 'unknown_cmd') {
11786:         if ($getpropath) {
11787:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
11788:         }
11789:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11790:     }
11791:     return $listing;
11792: }
11793: 
11794: sub is_locked {
11795:     my ($file_name, $domain, $user, $which) = @_;
11796:     my @check;
11797:     my $is_locked;
11798:     push (@check,$file_name);
11799:     my %locked = &get('file_permissions',\@check,
11800: 		      $env{'user.domain'},$env{'user.name'});
11801:     my ($tmp)=keys(%locked);
11802:     if ($tmp=~/^error:/) { undef(%locked); }
11803:     
11804:     if (ref($locked{$file_name}) eq 'ARRAY') {
11805:         $is_locked = 'false';
11806:         foreach my $entry (@{$locked{$file_name}}) {
11807:            if (ref($entry) eq 'ARRAY') {
11808:                $is_locked = 'true';
11809:                if (ref($which) eq 'ARRAY') {
11810:                    push(@{$which},$entry);
11811:                } else {
11812:                    last;
11813:                }
11814:            }
11815:        }
11816:     } else {
11817:         $is_locked = 'false';
11818:     }
11819:     return $is_locked;
11820: }
11821: 
11822: sub declutter_portfile {
11823:     my ($file) = @_;
11824:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11825:     return $file;
11826: }
11827: 
11828: # ------------------------------------------------------------- Mark as Read Only
11829: 
11830: sub mark_as_readonly {
11831:     my ($domain,$user,$files,$what) = @_;
11832:     my %current_permissions = &dump('file_permissions',$domain,$user);
11833:     my ($tmp)=keys(%current_permissions);
11834:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11835:     foreach my $file (@{$files}) {
11836: 	$file = &declutter_portfile($file);
11837:         push(@{$current_permissions{$file}},$what);
11838:     }
11839:     &put('file_permissions',\%current_permissions,$domain,$user);
11840:     return;
11841: }
11842: 
11843: # ------------------------------------------------------------Save Selected Files
11844: 
11845: sub save_selected_files {
11846:     my ($user, $path, @files) = @_;
11847:     my $filename = $user."savedfiles";
11848:     my @other_files = &files_not_in_path($user, $path);
11849:     open (OUT,'>',LONCAPA::tempdir().$filename);
11850:     foreach my $file (@files) {
11851:         print (OUT $env{'form.currentpath'}.$file."\n");
11852:     }
11853:     foreach my $file (@other_files) {
11854:         print (OUT $file."\n");
11855:     }
11856:     close (OUT);
11857:     return 'ok';
11858: }
11859: 
11860: sub clear_selected_files {
11861:     my ($user) = @_;
11862:     my $filename = $user."savedfiles";
11863:     open (OUT,'>',LONCAPA::tempdir().$filename);
11864:     print (OUT undef);
11865:     close (OUT);
11866:     return ("ok");    
11867: }
11868: 
11869: sub files_in_path {
11870:     my ($user, $path) = @_;
11871:     my $filename = $user."savedfiles";
11872:     my %return_files;
11873:     open (IN,'<',LONCAPA::tempdir().$filename);
11874:     while (my $line_in = <IN>) {
11875:         chomp ($line_in);
11876:         my @paths_and_file = split (m!/!, $line_in);
11877:         my $file_part = pop (@paths_and_file);
11878:         my $path_part = join ('/', @paths_and_file);
11879:         $path_part.='/';
11880:         my $path_and_file = $path_part.$file_part;
11881:         if ($path_part eq $path) {
11882:             $return_files{$file_part}= 'selected';
11883:         }
11884:     }
11885:     close (IN);
11886:     return (\%return_files);
11887: }
11888: 
11889: # called in portfolio select mode, to show files selected NOT in current directory
11890: sub files_not_in_path {
11891:     my ($user, $path) = @_;
11892:     my $filename = $user."savedfiles";
11893:     my @return_files;
11894:     my $path_part;
11895:     open(IN, '<',LONCAPA::tempdir().$filename);
11896:     while (my $line = <IN>) {
11897:         #ok, I know it's clunky, but I want it to work
11898:         my @paths_and_file = split(m|/|, $line);
11899:         my $file_part = pop(@paths_and_file);
11900:         chomp($file_part);
11901:         my $path_part = join('/', @paths_and_file);
11902:         $path_part .= '/';
11903:         my $path_and_file = $path_part.$file_part;
11904:         if ($path_part ne $path) {
11905:             push(@return_files, ($path_and_file));
11906:         }
11907:     }
11908:     close(OUT);
11909:     return (@return_files);
11910: }
11911: 
11912: #------------------------------Submitted/Handedback Portfolio Files Versioning
11913:  
11914: sub portfiles_versioning {
11915:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11916:     my $portfolio_root = '/userfiles/portfolio';
11917:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11918:     foreach my $file (@{$portfiles}) {
11919:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11920:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11921:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11922:         my $getpropath = 1;
11923:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11924:                                              $stu_name,$getpropath);
11925:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11926:         my $new_answer = 
11927:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11928:         if ($new_answer ne 'problem getting file') {
11929:             push(@{$versioned_portfiles}, $directory.$new_answer);
11930:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11931:                               [$symb,$env{'request.course.id'},'graded']);
11932:         }
11933:     }
11934: }
11935: 
11936: sub get_next_version {
11937:     my ($answer_name, $answer_ext, $dir_list) = @_;
11938:     my $version;
11939:     if (ref($dir_list) eq 'ARRAY') {
11940:         foreach my $row (@{$dir_list}) {
11941:             my ($file) = split(/\&/,$row,2);
11942:             my ($file_name,$file_version,$file_ext) =
11943:                 &file_name_version_ext($file);
11944:             if (($file_name eq $answer_name) &&
11945:                 ($file_ext eq $answer_ext)) {
11946:                      # gets here if filename and extension match,
11947:                      # regardless of version
11948:                 if ($file_version ne '') {
11949:                     # a versioned file is found  so save it for later
11950:                     if ($file_version > $version) {
11951:                         $version = $file_version;
11952:                     }
11953:                 }
11954:             }
11955:         }
11956:     }
11957:     $version ++;
11958:     return($version);
11959: }
11960: 
11961: sub version_selected_portfile {
11962:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11963:     my ($answer_name,$answer_ver,$answer_ext) =
11964:         &file_name_version_ext($file_name);
11965:     my $new_answer;
11966:     $env{'form.copy'} =
11967:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11968:     if($env{'form.copy'} eq '-1') {
11969:         $new_answer = 'problem getting file';
11970:     } else {
11971:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11972:         my $copy_result = 
11973:             &finishuserfileupload($stu_name,$domain,'copy',
11974:                                   '/portfolio'.$directory.$new_answer);
11975:     }
11976:     undef($env{'form.copy'});
11977:     return ($new_answer);
11978: }
11979: 
11980: sub file_name_version_ext {
11981:     my ($file)=@_;
11982:     my @file_parts = split(/\./, $file);
11983:     my ($name,$version,$ext);
11984:     if (@file_parts > 1) {
11985:         $ext=pop(@file_parts);
11986:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11987:             $version=pop(@file_parts);
11988:         }
11989:         $name=join('.',@file_parts);
11990:     } else {
11991:         $name=join('.',@file_parts);
11992:     }
11993:     return($name,$version,$ext);
11994: }
11995: 
11996: #----------------------------------------------Get portfolio file permissions
11997: 
11998: sub get_portfile_permissions {
11999:     my ($domain,$user) = @_;
12000:     my %current_permissions = &dump('file_permissions',$domain,$user);
12001:     my ($tmp)=keys(%current_permissions);
12002:     if ($tmp=~/^error:/) { undef(%current_permissions); }
12003:     return \%current_permissions;
12004: }
12005: 
12006: #---------------------------------------------Get portfolio file access controls
12007: 
12008: sub get_access_controls {
12009:     my ($current_permissions,$group,$file) = @_;
12010:     my %access;
12011:     my $real_file = $file;
12012:     $file =~ s/\.meta$//;
12013:     if (defined($file)) {
12014:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
12015:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
12016:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
12017:             }
12018:         }
12019:     } else {
12020:         foreach my $key (keys(%{$current_permissions})) {
12021:             if ($key =~ /\0accesscontrol$/) {
12022:                 if (defined($group)) {
12023:                     if ($key !~ m-^\Q$group\E/-) {
12024:                         next;
12025:                     }
12026:                 }
12027:                 my ($fullpath) = split(/\0/,$key);
12028:                 if (ref($$current_permissions{$key}) eq 'HASH') {
12029:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
12030:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
12031:                     }
12032:                 }
12033:             }
12034:         }
12035:     }
12036:     return %access;
12037: }
12038: 
12039: sub modify_access_controls {
12040:     my ($file_name,$changes,$domain,$user)=@_;
12041:     my ($outcome,$deloutcome);
12042:     my %store_permissions;
12043:     my %new_values;
12044:     my %new_control;
12045:     my %translation;
12046:     my @deletions = ();
12047:     my $now = time;
12048:     if (exists($$changes{'activate'})) {
12049:         if (ref($$changes{'activate'}) eq 'HASH') {
12050:             my @newitems = sort(keys(%{$$changes{'activate'}}));
12051:             my $numnew = scalar(@newitems);
12052:             for (my $i=0; $i<$numnew; $i++) {
12053:                 my $newkey = $newitems[$i];
12054:                 my $newid = &Apache::loncommon::get_cgi_id();
12055:                 if ($newkey =~ /^\d+:/) { 
12056:                     $newkey =~ s/^(\d+)/$newid/;
12057:                     $translation{$1} = $newid;
12058:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
12059:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
12060:                     $translation{$1} = $newid;
12061:                 }
12062:                 $new_values{$file_name."\0".$newkey} = 
12063:                                           $$changes{'activate'}{$newitems[$i]};
12064:                 $new_control{$newkey} = $now;
12065:             }
12066:         }
12067:     }
12068:     my %todelete;
12069:     my %changed_items;
12070:     foreach my $action ('delete','update') {
12071:         if (exists($$changes{$action})) {
12072:             if (ref($$changes{$action}) eq 'HASH') {
12073:                 foreach my $key (keys(%{$$changes{$action}})) {
12074:                     my ($itemnum) = ($key =~ /^([^:]+):/);
12075:                     if ($action eq 'delete') { 
12076:                         $todelete{$itemnum} = 1;
12077:                     } else {
12078:                         $changed_items{$itemnum} = $key;
12079:                     }
12080:                 }
12081:             }
12082:         }
12083:     }
12084:     # get lock on access controls for file.
12085:     my $lockhash = {
12086:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
12087:                                                        ':'.$env{'user.domain'},
12088:                    }; 
12089:     my $tries = 0;
12090:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
12091:    
12092:     while (($gotlock ne 'ok') && $tries < 10) {
12093:         $tries ++;
12094:         sleep(0.1);
12095:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
12096:     }
12097:     if ($gotlock eq 'ok') {
12098:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
12099:         my ($tmp)=keys(%curr_permissions);
12100:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
12101:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
12102:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
12103:             if (ref($curr_controls) eq 'HASH') {
12104:                 foreach my $control_item (keys(%{$curr_controls})) {
12105:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
12106:                     if (defined($todelete{$itemnum})) {
12107:                         push(@deletions,$file_name."\0".$control_item);
12108:                     } else {
12109:                         if (defined($changed_items{$itemnum})) {
12110:                             $new_control{$changed_items{$itemnum}} = $now;
12111:                             push(@deletions,$file_name."\0".$control_item);
12112:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
12113:                         } else {
12114:                             $new_control{$control_item} = $$curr_controls{$control_item};
12115:                         }
12116:                     }
12117:                 }
12118:             }
12119:         }
12120:         my ($group);
12121:         if (&is_course($domain,$user)) {
12122:             ($group,my $file) = split(/\//,$file_name,2);
12123:         }
12124:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
12125:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
12126:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
12127:         #  remove lock
12128:         my @del_lock = ($file_name."\0".'locked_access_records');
12129:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
12130:         my $sqlresult =
12131:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
12132:                                     $group);
12133:     } else {
12134:         $outcome = "error: could not obtain lockfile\n";  
12135:     }
12136:     return ($outcome,$deloutcome,\%new_values,\%translation);
12137: }
12138: 
12139: sub make_public_indefinitely {
12140:     my (@requrl) = @_;
12141:     return &automated_portfile_access('public',\@requrl);
12142: }
12143: 
12144: sub automated_portfile_access {
12145:     my ($accesstype,$addsref,$delsref,$info) = @_;
12146:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
12147:         return 'invalid';
12148:     }
12149:     my %urls;
12150:     if (ref($addsref) eq 'ARRAY') {
12151:         foreach my $requrl (@{$addsref}) {
12152:             if (&is_portfolio_url($requrl)) {
12153:                 unless (exists($urls{$requrl})) {
12154:                     $urls{$requrl} = 'add';
12155:                 }
12156:             }
12157:         }
12158:     }
12159:     if (ref($delsref) eq 'ARRAY') {
12160:         foreach my $requrl (@{$delsref}) { 
12161:             if (&is_portfolio_url($requrl)) {
12162:                 unless (exists($urls{$requrl})) {
12163:                     $urls{$requrl} = 'delete'; 
12164:                 }
12165:             }
12166:         }
12167:     }
12168:     unless (keys(%urls)) {
12169:         return 'invalid';
12170:     }
12171:     my $ip;
12172:     if ($accesstype eq 'ip') {
12173:         if (ref($info) eq 'HASH') {
12174:             if ($info->{'ip'} ne '') {
12175:                 $ip = $info->{'ip'};
12176:             }
12177:         }
12178:         if ($ip eq '') {
12179:             return 'invalid';
12180:         }
12181:     }
12182:     my $errors;
12183:     my $now = time;
12184:     my %current_perms;
12185:     foreach my $requrl (sort(keys(%urls))) {
12186:         my $action;
12187:         if ($urls{$requrl} eq 'add') {
12188:             $action = 'activate';
12189:         } else {
12190:             $action = 'none';
12191:         }
12192:         my $aclnum = 0;
12193:         my (undef,$udom,$unum,$file_name,$group) =
12194:             &parse_portfolio_url($requrl);
12195:         unless (exists($current_perms{$unum.':'.$udom})) {
12196:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
12197:         }
12198:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
12199:                                                    $group,$file_name);
12200:         foreach my $key (keys(%{$access_controls{$file_name}})) {
12201:             my ($num,$scope,$end,$start) = 
12202:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
12203:             if ($scope eq $accesstype) {
12204:                 if (($start <= $now) && ($end == 0)) {
12205:                     if ($accesstype eq 'ip') {
12206:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
12207:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
12208:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
12209:                                     if ($urls{$requrl} eq 'add') {
12210:                                         $action = 'none';
12211:                                         last;
12212:                                     } else {
12213:                                         $action = 'delete';
12214:                                         $aclnum = $num;
12215:                                         last;
12216:                                     }
12217:                                 }
12218:                             }
12219:                         }
12220:                     } elsif ($accesstype eq 'public') {
12221:                         if ($urls{$requrl} eq 'add') {
12222:                             $action = 'none';
12223:                             last;
12224:                         } else {
12225:                             $action = 'delete';
12226:                             $aclnum = $num;
12227:                             last;
12228:                         }
12229:                     }
12230:                 } elsif ($accesstype eq 'public') {
12231:                     $action = 'update';
12232:                     $aclnum = $num;
12233:                     last;
12234:                 }
12235:             }
12236:         }
12237:         if ($action eq 'none') {
12238:             next;
12239:         } else {
12240:             my %changes;
12241:             my $newend = 0;
12242:             my $newstart = $now;
12243:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
12244:             $changes{$action}{$newkey} = {
12245:                 type => $accesstype,
12246:                 time => {
12247:                     start => $newstart,
12248:                     end   => $newend,
12249:                 },
12250:             };
12251:             if ($accesstype eq 'ip') {
12252:                 $changes{$action}{$newkey}{'ip'} = [$ip];
12253:             }
12254:             my ($outcome,$deloutcome,$new_values,$translation) =
12255:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
12256:             unless ($outcome eq 'ok') {
12257:                 $errors .= $outcome.' ';
12258:             }
12259:         }
12260:     }
12261:     if ($errors) {
12262:         $errors =~ s/\s$//;
12263:         return $errors;
12264:     } else {
12265:         return 'ok';
12266:     }
12267: }
12268: 
12269: #------------------------------------------------------Get Marked as Read Only
12270: 
12271: sub get_marked_as_readonly {
12272:     my ($domain,$user,$what,$group) = @_;
12273:     my $current_permissions = &get_portfile_permissions($domain,$user);
12274:     my @readonly_files;
12275:     my $cmp1=$what;
12276:     if (ref($what)) { $cmp1=join('',@{$what}) };
12277:     while (my ($file_name,$value) = each(%{$current_permissions})) {
12278:         if (defined($group)) {
12279:             if ($file_name !~ m-^\Q$group\E/-) {
12280:                 next;
12281:             }
12282:         }
12283:         if (ref($value) eq "ARRAY"){
12284:             foreach my $stored_what (@{$value}) {
12285:                 my $cmp2=$stored_what;
12286:                 if (ref($stored_what) eq 'ARRAY') {
12287:                     $cmp2=join('',@{$stored_what});
12288:                 }
12289:                 if ($cmp1 eq $cmp2) {
12290:                     push(@readonly_files, $file_name);
12291:                     last;
12292:                 } elsif (!defined($what)) {
12293:                     push(@readonly_files, $file_name);
12294:                     last;
12295:                 }
12296:             }
12297:         }
12298:     }
12299:     return @readonly_files;
12300: }
12301: #-----------------------------------------------------------Get Marked as Read Only Hash
12302: 
12303: sub get_marked_as_readonly_hash {
12304:     my ($current_permissions,$group,$what) = @_;
12305:     my %readonly_files;
12306:     while (my ($file_name,$value) = each(%{$current_permissions})) {
12307:         if (defined($group)) {
12308:             if ($file_name !~ m-^\Q$group\E/-) {
12309:                 next;
12310:             }
12311:         }
12312:         if (ref($value) eq "ARRAY"){
12313:             foreach my $stored_what (@{$value}) {
12314:                 if (ref($stored_what) eq 'ARRAY') {
12315:                     foreach my $lock_descriptor(@{$stored_what}) {
12316:                         if ($lock_descriptor eq 'graded') {
12317:                             $readonly_files{$file_name} = 'graded';
12318:                         } elsif ($lock_descriptor eq 'handback') {
12319:                             $readonly_files{$file_name} = 'handback';
12320:                         } else {
12321:                             if (!exists($readonly_files{$file_name})) {
12322:                                 $readonly_files{$file_name} = 'locked';
12323:                             }
12324:                         }
12325:                     }
12326:                 } 
12327:             }
12328:         } 
12329:     }
12330:     return %readonly_files;
12331: }
12332: # ------------------------------------------------------------ Unmark as Read Only
12333: 
12334: sub unmark_as_readonly {
12335:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
12336:     # for portfolio submissions, $what contains [$symb,$crsid] 
12337:     my ($domain,$user,$what,$file_name,$group) = @_;
12338:     $file_name = &declutter_portfile($file_name);
12339:     my $symb_crs = $what;
12340:     if (ref($what)) { $symb_crs=join('',@$what); }
12341:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
12342:     my ($tmp)=keys(%current_permissions);
12343:     if ($tmp=~/^error:/) { undef(%current_permissions); }
12344:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
12345:     foreach my $file (@readonly_files) {
12346: 	my $clean_file = &declutter_portfile($file);
12347: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
12348: 	my $current_locks = $current_permissions{$file};
12349:         my @new_locks;
12350:         my @del_keys;
12351:         if (ref($current_locks) eq "ARRAY"){
12352:             foreach my $locker (@{$current_locks}) {
12353:                 my $compare=$locker;
12354:                 if (ref($locker) eq 'ARRAY') {
12355:                     $compare=join('',@{$locker});
12356:                     if ($compare ne $symb_crs) {
12357:                         push(@new_locks, $locker);
12358:                     }
12359:                 }
12360:             }
12361:             if (scalar(@new_locks) > 0) {
12362:                 $current_permissions{$file} = \@new_locks;
12363:             } else {
12364:                 push(@del_keys, $file);
12365:                 &del('file_permissions',\@del_keys, $domain, $user);
12366:                 delete($current_permissions{$file});
12367:             }
12368:         }
12369:     }
12370:     &put('file_permissions',\%current_permissions,$domain,$user);
12371:     return;
12372: }
12373: 
12374: # ------------------------------------------------------------ Directory lister
12375: 
12376: sub dirlist {
12377:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
12378:     $uri=~s/^\///;
12379:     $uri=~s/\/$//;
12380:     my ($udom, $uname);
12381:     if ($getuserdir) {
12382:         $udom = $userdomain;
12383:         $uname = $username;
12384:     } else {
12385:         (undef,$udom,$uname)=split(/\//,$uri);
12386:         if(defined($userdomain)) {
12387:             $udom = $userdomain;
12388:         }
12389:         if(defined($username)) {
12390:             $uname = $username;
12391:         }
12392:     }
12393:     my ($dirRoot,$listing,@listing_results);
12394: 
12395:     $dirRoot = $perlvar{'lonDocRoot'};
12396:     if (defined($getpropath)) {
12397:         $dirRoot = &propath($udom,$uname);
12398:         $dirRoot =~ s/\/$//;
12399:     } elsif (defined($getuserdir)) {
12400:         my $subdir=$uname.'__';
12401:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
12402:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
12403:                    ."/$udom/$subdir/$uname";
12404:     } elsif (defined($alternateRoot)) {
12405:         $dirRoot = $alternateRoot;
12406:     }
12407: 
12408:     if($udom) {
12409:         if($uname) {
12410:             my $uhome = &homeserver($uname,$udom);
12411:             if ($uhome eq 'no_host') {
12412:                 return ([],'no_host');
12413:             }
12414:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
12415:                               .$getuserdir.':'.&escape($dirRoot)
12416:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
12417:             if ($listing eq 'unknown_cmd') {
12418:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
12419:             } else {
12420:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
12421:             }
12422:             if ($listing eq 'unknown_cmd') {
12423:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
12424:                 @listing_results = split(/:/,$listing);
12425:             } else {
12426:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
12427:             }
12428:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
12429:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
12430:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
12431:                 return ([],$listing);
12432:             } else {
12433:                 return (\@listing_results);
12434:             }
12435:         } elsif(!$alternateRoot) {
12436:             my (%allusers,%listerror);
12437: 	    my %servers = &get_servers($udom,'library');
12438:  	    foreach my $tryserver (keys(%servers)) {
12439:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
12440:                                   &escape($udom),$tryserver);
12441:                 if ($listing eq 'unknown_cmd') {
12442: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
12443: 				      $udom, $tryserver);
12444:                 } else {
12445:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
12446:                 }
12447: 		if ($listing eq 'unknown_cmd') {
12448: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
12449: 				      $udom, $tryserver);
12450: 		    @listing_results = split(/:/,$listing);
12451: 		} else {
12452: 		    @listing_results =
12453: 			map { &unescape($_); } split(/:/,$listing);
12454: 		}
12455:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
12456:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
12457:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
12458:                     $listerror{$tryserver} = $listing;
12459:                 } else {
12460: 		    foreach my $line (@listing_results) {
12461: 			my ($entry) = split(/&/,$line,2);
12462: 			$allusers{$entry} = 1;
12463: 		    }
12464: 		}
12465:             }
12466:             my @alluserslist=();
12467:             foreach my $user (sort(keys(%allusers))) {
12468:                 push(@alluserslist,$user.'&user');
12469:             }
12470: 
12471:             if (!%listerror) {
12472:                 # no errors
12473:                 return (\@alluserslist);
12474:             } elsif (scalar(keys(%servers)) == 1) {
12475:                 # one library server, one error 
12476:                 my ($key) = keys(%listerror);
12477:                 return (\@alluserslist, $listerror{$key});
12478:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
12479:                 # con_lost indicates that we might miss data from at least one
12480:                 # library server
12481:                 return (\@alluserslist, 'con_lost');
12482:             } else {
12483:                 # multiple library servers and no con_lost -> data should be
12484:                 # complete. 
12485:                 return (\@alluserslist);
12486:             }
12487: 
12488:         } else {
12489:             return ([],'missing username');
12490:         }
12491:     } elsif(!defined($getpropath)) {
12492:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
12493:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
12494:         return (\@all_domains);
12495:     } else {
12496:         return ([],'missing domain');
12497:     }
12498: }
12499: 
12500: # --------------------------------------------- GetFileTimestamp
12501: # This function utilizes dirlist and returns the date stamp for
12502: # when it was last modified.  It will also return an error of -1
12503: # if an error occurs
12504: 
12505: sub GetFileTimestamp {
12506:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
12507:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
12508:     $studentName   = &LONCAPA::clean_username($studentName);
12509:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
12510:                                     undef,$getuserdir);
12511:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12512:         return -1;
12513:     }
12514:     if (ref($fileref) eq 'ARRAY') {
12515:         my @stats = split('&',$fileref->[0]);
12516:         # @stats contains first the filename, then the stat output
12517:         return $stats[10]; # so this is 10 instead of 9.
12518:     } else {
12519:         return -1;
12520:     }
12521: }
12522: 
12523: sub stat_file {
12524:     my ($uri) = @_;
12525:     $uri = &clutter_with_no_wrapper($uri);
12526: 
12527:     my ($udom,$uname,$file);
12528:     if ($uri =~ m-^/(uploaded|editupload)/-) {
12529: 	($udom,$uname,$file) =
12530: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
12531: 	$file = 'userfiles/'.$file;
12532:     }
12533:     if ($uri =~ m-^/res/-) {
12534: 	($udom,$uname) = 
12535: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
12536: 	$file = $uri;
12537:     }
12538: 
12539:     if (!$udom || !$uname || !$file) {
12540: 	# unable to handle the uri
12541: 	return ();
12542:     }
12543:     my $getpropath;
12544:     if ($file =~ /^userfiles\//) {
12545:         $getpropath = 1;
12546:     }
12547:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
12548:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12549:         return ();
12550:     } else {
12551:         if (ref($listref) eq 'ARRAY') {
12552:             my @stats = split('&',$listref->[0]);
12553: 	    shift(@stats); #filename is first
12554: 	    return @stats;
12555:         }
12556:     }
12557:     return ();
12558: }
12559: 
12560: # --------------------------------------------------------- recursedirs
12561: # Recursive function to traverse either a specific user's Authoring Space
12562: # or corresponding Published Resource Space, and populate the hash ref:
12563: # $dirhashref with URLs of all directories, and if $filehashref hash
12564: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
12565: # or .rights files in resource space, and .meta, .save, .log, .bak and
12566: # .rights files in Authoring Space.
12567: #
12568: # Inputs:
12569: #
12570: # $is_home - true if current server is home server for user's space
12571: # $recurse - if true will also traverse subdirectories recursively
12572: # $include - reference to hash containing allowed file extensions.  If provided,
12573: #             files which do not have a matching extension will be ignored.
12574: # $exclude - reference to hash containing excluded file extensions.  If provided,
12575: #             files which have a matching extension will be ignored.
12576: # $nonemptydir - if true, will only populate $fileshashref hash entry for a particular
12577: #             directory with first file found (with acceptable extension).
12578: # $addtopdir - if true, set $dirhashref->{'/'} = 1 
12579: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
12580: # $relpath - Current path (relative to top level).
12581: # $dirhashref - reference to hash to populate with URLs of directories (Required)
12582: # $filehashref - reference to hash to populate with URLs of files (Optional)
12583: # $getlastmod - if true, will set value for each key in innerhash in $filehashref
12584: #               to last modification time of file; value set to 1 otherwise.
12585: #
12586: # Returns: nothing
12587: #
12588: # Side Effects: populates $dirhashref, and $filehashref (if provided).
12589: #
12590: # Currently used by interface/londocs.pm to create linked select boxes for
12591: # directory and filename to import a Course "Author" resource into a course, and
12592: # also to create linked select boxes for Authoring Space and Directory to choose
12593: # save location for creation of a new "standard" problem from the Course Editor.
12594: #
12595: 
12596: sub recursedirs {
12597:     my ($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,
12598:         $relpath,$dirhashref,$filehashref,$getlastmod) = @_;
12599:     return unless (ref($dirhashref) eq 'HASH');
12600:     my $docroot = $perlvar{'lonDocRoot'};
12601:     my $currpath = $docroot.$toppath;
12602:     if ($relpath ne '') {
12603:         $currpath .= "/$relpath";
12604:     }
12605:     my ($savefile,$checkinc,$checkexc);
12606:     if (ref($filehashref) eq 'HASH') {
12607:         $savefile = 1;
12608:     }
12609:     if (ref($include) eq 'HASH') {
12610:         $checkinc = 1;
12611:     }
12612:     if (ref($exclude) eq 'HASH') {
12613:         $checkexc = 1;
12614:     }
12615:     if ($is_home) {
12616:         if ((-e $currpath) && (opendir(my $dirh,$currpath))) {
12617:             my $filecount = 0;
12618:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
12619:                 next if ($item eq '');
12620:                 if (-d "$currpath/$item") {
12621:                     my $newpath;
12622:                     if ($relpath ne '') {
12623:                         $newpath = "$relpath/$item";
12624:                     } else {
12625:                         $newpath = $item;
12626:                     }
12627:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
12628:                     if ($recurse) {
12629:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,
12630:                                      $toppath,$newpath,$dirhashref,$filehashref,$getlastmod);
12631:                     }
12632:                 } elsif (($savefile) || ($relpath eq '')) {
12633:                     next if ($nonemptydir && $filecount);
12634:                     if ($checkinc || $checkexc) {
12635:                         my ($extension) = ($item =~ /\.(\w+)$/);
12636:                         if ($checkinc) {
12637:                             next unless ($extension && $include->{$extension});
12638:                         }
12639:                         if ($checkexc) {
12640:                             next if ($extension && $exclude->{$extension});
12641:                         }
12642:                     }
12643:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12644:                         $dirhashref->{'/'} = 1;
12645:                     }
12646:                     if ($savefile) {
12647:                         my $value;
12648:                         if ($getlastmod) {
12649:                             ($value) = (stat("$currpath/$item"))[9];
12650:                         } else {
12651:                             $value = 1;
12652:                         }
12653:                         if ($relpath eq '') {
12654:                             $filehashref->{'/'}{$item} = $value
12655:                         } else {
12656:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = $value;
12657:                         }
12658:                     }
12659:                     $filecount ++;
12660:                 }
12661:             }
12662:             closedir($dirh);
12663:         }
12664:     } else {
12665:         my $url = $toppath;
12666:         if ($relpath ne '') {
12667:             $url = $toppath.'/'.$relpath;
12668:         }
12669:         my ($dirlistref,$listerror) = &dirlist($url);
12670:         my @dir_lines;
12671:         my $dirptr=16384;
12672:         if (ref($dirlistref) eq 'ARRAY') {
12673:             my $filecount = 0;
12674:             foreach my $dir_line (sort
12675:                               {
12676:                                   my ($afile)=split('&',$a,2);
12677:                                   my ($bfile)=split('&',$b,2);
12678:                                   return (lc($afile) cmp lc($bfile));
12679:                               } (@{$dirlistref})) {
12680:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
12681:                     split(/\&/,$dir_line,16);
12682:                 $item =~ s/\s+$//;
12683:                 next if (($item =~ /^\.\.?$/) || ($obs));
12684:                 if ($dirptr&$testdir) {
12685:                     my $newpath;
12686:                     if ($relpath) {
12687:                         $newpath = "$relpath/$item";
12688:                     } else {
12689:                         $newpath = $item;
12690:                     }
12691:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
12692:                     if ($recurse) {
12693:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,
12694:                                      $toppath,$newpath,$dirhashref,$filehashref,$getlastmod);
12695:                     }
12696:                 } elsif (($savefile) || ($relpath eq '')) {
12697:                     next if ($nonemptydir && $filecount);
12698:                     if ($checkinc || $checkexc) {
12699:                         my ($extension) = ($item =~ /\.(\w+)$/);
12700:                         if ($checkinc) {
12701:                             next unless ($extension && $include->{$extension});
12702:                         }
12703:                         if ($checkexc) {
12704:                             next if ($extension && $exclude->{$extension});
12705:                         }
12706:                     }
12707:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12708:                         $dirhashref->{'/'} = 1;
12709:                     }
12710:                     if ($savefile) {
12711:                         my $value;
12712:                         if ($getlastmod) {
12713:                             $value = $mtime;
12714:                         } else {
12715:                             $value = 1;
12716:                         }
12717:                         if ($relpath eq '') {
12718:                             $filehashref->{'/'}{$item} = $value;
12719:                         } else {
12720:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = $value;
12721:                         }
12722:                     }
12723:                     $filecount ++; 
12724:                 }
12725:             }
12726:         }
12727:     }
12728:     if ($addtopdir) {
12729:         if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12730:             $dirhashref->{'/'} = 1;
12731:         }
12732:     }
12733:     return;
12734: }
12735: 
12736: sub priv_exclude {
12737:     return {
12738:              meta => 1,
12739:              save => 1,
12740:              log => 1,
12741:              bak => 1,
12742:              rights => 1,
12743:              DS_Store => 1,
12744:            };
12745: }
12746: 
12747: sub res_exclude {
12748:     return {
12749:              meta => 1,
12750:              subscription => 1,
12751:              rights => 1,
12752:            };
12753: }
12754: 
12755: # -------------------------------------------------------- Value of a Condition
12756: 
12757: # gets the value of a specific preevaluated condition
12758: #    stored in the string  $env{user.state.<cid>}
12759: # or looks up a condition reference in the bighash and if if hasn't
12760: # already been evaluated recurses into docondval to get the value of
12761: # the condition, then memoizing it to 
12762: #   $env{user.state.<cid>.<condition>}
12763: sub directcondval {
12764:     my $number=shift;
12765:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
12766: 	&Apache::lonuserstate::evalstate();
12767:     }
12768:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12769: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12770:     } elsif ($number =~ /^_/) {
12771: 	my $sub_condition;
12772: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12773: 		&GDBM_READER(),0640)) {
12774: 	    $sub_condition=$bighash{'conditions'.$number};
12775: 	    untie(%bighash);
12776: 	}
12777: 	my $value = &docondval($sub_condition);
12778: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
12779: 	return $value;
12780:     }
12781:     if ($env{'user.state.'.$env{'request.course.id'}}) {
12782:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
12783:     } else {
12784:        return 2;
12785:     }
12786: }
12787: 
12788: # get the collection of conditions for this resource
12789: sub condval {
12790:     my $condidx=shift;
12791:     my $allpathcond='';
12792:     foreach my $cond (split(/\|/,$condidx)) {
12793: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12794: 	    $allpathcond.=
12795: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12796: 	}
12797:     }
12798:     $allpathcond=~s/\|$//;
12799:     return &docondval($allpathcond);
12800: }
12801: 
12802: #evaluates an expression of conditions
12803: sub docondval {
12804:     my ($allpathcond) = @_;
12805:     my $result=0;
12806:     if ($env{'request.course.id'}
12807: 	&& defined($allpathcond)) {
12808: 	my $operand='|';
12809: 	my @stack;
12810: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12811: 	    if ($chunk eq '(') {
12812: 		push @stack,($operand,$result);
12813: 	    } elsif ($chunk eq ')') {
12814: 		my $before=pop @stack;
12815: 		if (pop @stack eq '&') {
12816: 		    $result=$result>$before?$before:$result;
12817: 		} else {
12818: 		    $result=$result>$before?$result:$before;
12819: 		}
12820: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
12821: 		$operand=$chunk;
12822: 	    } else {
12823: 		my $new=directcondval($chunk);
12824: 		if ($operand eq '&') {
12825: 		    $result=$result>$new?$new:$result;
12826: 		} else {
12827: 		    $result=$result>$new?$result:$new;
12828: 		}
12829: 	    }
12830: 	}
12831:     }
12832:     return $result;
12833: }
12834: 
12835: # ---------------------------------------------------- Devalidate courseresdata
12836: 
12837: sub devalidatecourseresdata {
12838:     my ($coursenum,$coursedomain)=@_;
12839:     my $hashid=$coursenum.':'.$coursedomain;
12840:     &devalidate_cache_new('courseres',$hashid);
12841: }
12842: 
12843: 
12844: # --------------------------------------------------- Course Resourcedata Query
12845: #
12846: #  Parameters:
12847: #      $coursenum    - Number of the course.
12848: #      $coursedomain - Domain at which the course was created.
12849: #  Returns:
12850: #     A hash of the course parameters along (I think) with timestamps
12851: #     and version info.
12852: 
12853: sub get_courseresdata {
12854:     my ($coursenum,$coursedomain)=@_;
12855:     my $coursehom=&homeserver($coursenum,$coursedomain);
12856:     my $hashid=$coursenum.':'.$coursedomain;
12857:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
12858:     my %dumpreply;
12859:     unless (defined($cached)) {
12860: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
12861: 	$result=\%dumpreply;
12862: 	my ($tmp) = keys(%dumpreply);
12863: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12864: 	    &do_cache_new('courseres',$hashid,$result,600);
12865: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12866: 	    return $tmp;
12867: 	} elsif ($tmp =~ /^(error)/) {
12868: 	    $result=undef;
12869: 	    &do_cache_new('courseres',$hashid,$result,600);
12870: 	}
12871:     }
12872:     return $result;
12873: }
12874: 
12875: sub devalidateuserresdata {
12876:     my ($uname,$udom)=@_;
12877:     my $hashid="$udom:$uname";
12878:     &devalidate_cache_new('userres',$hashid);
12879: }
12880: 
12881: sub get_userresdata {
12882:     my ($uname,$udom)=@_;
12883:     #most student don\'t have any data set, check if there is some data
12884:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12885: 
12886:     my $hashid="$udom:$uname";
12887:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12888:     if (!defined($cached)) {
12889: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12890: 	$result=\%resourcedata;
12891: 	&do_cache_new('userres',$hashid,$result,600);
12892:     }
12893:     my ($tmp)=keys(%$result);
12894:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12895: 	return $result;
12896:     }
12897:     #error 2 occurs when the .db doesn't exist
12898:     if ($tmp!~/error: 2 /) {
12899:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12900: 	    &logthis("<font color=\"blue\">WARNING:".
12901: 		     " Trying to get resource data for ".
12902: 		     $uname." at ".$udom.": ".
12903: 		     $tmp."</font>");
12904:         }
12905:     } elsif ($tmp=~/error: 2 /) {
12906: 	#&EXT_cache_set($udom,$uname);
12907: 	&do_cache_new('userres',$hashid,undef,600);
12908: 	undef($tmp); # not really an error so don't send it back
12909:     }
12910:     return $tmp;
12911: }
12912: #----------------------------------------------- resdata - return resource data
12913: #  Purpose:
12914: #    Return resource data for either users or for a course.
12915: #  Parameters:
12916: #     $name      - Course/user name.
12917: #     $domain    - Name of the domain the user/course is registered on.
12918: #     $type      - Type of thing $name is (must be 'course' or 'user')
12919: #     $mapp      - decluttered URL of enclosing map  
12920: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12921: #     $recurseup - Ref to array of map URLs, starting with map containing
12922: #                  $mapp up through hierarchy of nested maps to top level map.  
12923: #     $courseid  - CourseID (first part of param identifier).
12924: #     $modifier  - Middle part of param identifier.
12925: #     $what      - Last part of param identifier.
12926: #     @which     - Array of names of resources desired.
12927: #  Returns:
12928: #     The value of the first reasource in @which that is found in the
12929: #     resource hash.
12930: #  Exceptional Conditions:
12931: #     If the $type passed in is not valid (not the string 'course' or 
12932: #     'user', an undefined  reference is returned.
12933: #     If none of the resources are found, an undef is returned
12934: sub resdata {
12935:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12936:         $modifier,$what,@which)=@_;
12937:     my $result;
12938:     if ($type eq 'course') {
12939: 	$result=&get_courseresdata($name,$domain);
12940:     } elsif ($type eq 'user') {
12941: 	$result=&get_userresdata($name,$domain);
12942:     }
12943:     if (!ref($result)) { return $result; }    
12944:     foreach my $item (@which) {
12945:         if ($item->[1] eq 'course') {
12946:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12947:                 unless ($$recursed) {
12948:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12949:                     $$recursed = 1;
12950:                 }
12951:                 foreach my $item (@${recurseup}) {
12952:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12953:                     last if (defined($result->{$norecursechk}));
12954:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12955:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12956:                 }
12957:             }
12958:         }
12959:         if (defined($result->{$item->[0]})) {
12960: 	    return [$result->{$item->[0]},$item->[1]];
12961: 	}
12962:     }
12963:     return undef;
12964: }
12965: 
12966: sub get_domain_lti {
12967:     my ($cdom,$context) = @_;
12968:     my ($name,$cachename,%lti);
12969:     if ($context eq 'consumer') {
12970:         $name = 'ltitools';
12971:     } elsif ($context eq 'provider') {
12972:         $name = 'lti';
12973:     } elsif ($context eq 'linkprot') {
12974:         $name = 'ltisec';
12975:     } else {
12976:         return %lti;
12977:     }
12978:     if ($context eq 'linkprot') {
12979:         $cachename = $context;
12980:     } else {
12981:         $cachename = $name;
12982:     }
12983:     my ($result,$cached)=&is_cached_new($cachename,$cdom);
12984:     if (defined($cached)) {
12985:         if (ref($result) eq 'HASH') {
12986:             %lti = %{$result};
12987:         }
12988:     } else {
12989:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12990:         if (ref($domconfig{$name}) eq 'HASH') {
12991:             if ($context eq 'linkprot') {
12992:                 if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12993:                     %lti = %{$domconfig{$name}{'linkprot'}};
12994:                 }
12995:             } else {
12996:                 %lti = %{$domconfig{$name}};
12997:             }
12998:         }
12999:         my $cachetime = 24*60*60;
13000:         &do_cache_new($cachename,$cdom,\%lti,$cachetime);
13001:     }
13002:     return %lti;
13003: }
13004: 
13005: sub get_course_lti {
13006:     my ($cnum,$cdom,$context) = @_;
13007:     my ($name,$cachename,%lti);
13008:     if ($context eq 'consumer') {
13009:         $name = 'ltitools';
13010:         $cachename = 'courseltitools';
13011:     } elsif ($context eq 'provider') {
13012:         $name = 'lti';
13013:         $cachename = 'courselti';
13014:     } else {
13015:         return %lti;
13016:     }
13017:     my $hashid=$cdom.'_'.$cnum;
13018:     my ($result,$cached)=&is_cached_new($cachename,$hashid);
13019:     if (defined($cached)) {
13020:         if (ref($result) eq 'HASH') {
13021:             %lti = %{$result};
13022:         }
13023:     } else {
13024:         %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
13025:         my $cachetime = 24*60*60;
13026:         &do_cache_new($cachename,$hashid,\%lti,$cachetime);
13027:     }
13028:     return %lti;
13029: }
13030: 
13031: sub courselti_itemid {
13032:     my ($cnum,$cdom,$url,$method,$params,$context) = @_;
13033:     my ($chome,$itemid);
13034:     $chome = &homeserver($cnum,$cdom);
13035:     return if ($chome eq 'no_host');
13036:     if (ref($params) eq 'HASH') {
13037:         my $rep;
13038:         if (grep { $_ eq $chome } current_machine_ids()) {
13039:             $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
13040:         } else {
13041:             my $escurl = &escape($url);
13042:             my $escmethod = &escape($method);
13043:             my $items = &freeze_escape($params);
13044:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
13045:         }
13046:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
13047:                 ($rep eq 'unknown_cmd')) {
13048:             $itemid = $rep;
13049:         }
13050:     }
13051:     return $itemid;
13052: }
13053: 
13054: sub domainlti_itemid {
13055:     my ($cdom,$url,$method,$params,$context) = @_;
13056:     my ($primary_id,$itemid);
13057:     $primary_id = &domain($cdom,'primary');
13058:     return if ($primary_id eq '');
13059:     if (ref($params) eq 'HASH') {
13060:         my $rep;
13061:         if (grep { $_ eq $primary_id } current_machine_ids()) {
13062:             $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
13063:         } else {
13064:             my $cnum = '';
13065:             my $escurl = &escape($url);
13066:             my $escmethod = &escape($method);
13067:             my $items = &freeze_escape($params);
13068:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
13069:         }
13070:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
13071:                 ($rep eq 'unknown_cmd')) {
13072:             $itemid = $rep;
13073:         }
13074:     }
13075:     return $itemid;
13076: }
13077: 
13078: sub get_ltitools_id {
13079:     my ($context,$cdom,$cnum,$title) = @_;
13080:     my ($lockhash,$tries,$gotlock,$id,$error);
13081: 
13082:     # get lock on ltitools db
13083:     $lockhash = {
13084:                    lock => $env{'user.name'}.
13085:                            ':'.$env{'user.domain'},
13086:                 };
13087:     $tries = 0;
13088:     if ($context eq 'domain') {
13089:         $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
13090:     } else {
13091:         $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
13092:     }
13093:     while (($gotlock ne 'ok') && ($tries<10)) {
13094:         $tries ++;
13095:         sleep (0.1);
13096:         if ($context eq 'domain') {
13097:             $gotlock = &newput_dom('ltitools',$lockhash,$cdom);
13098:         } else {
13099:             $gotlock = &newput('ltitools',$lockhash,$cdom,$cnum);
13100:         }
13101:     }
13102:     if ($gotlock eq 'ok') {
13103:         my %currids;
13104:         if ($context eq 'domain') {
13105:             %currids = &dump_dom('ltitools',$cdom);
13106:         } else {
13107:             %currids = &dump('ltitools',$cdom,$cnum);
13108:         }
13109:         if ($currids{'lock'}) {
13110:             delete($currids{'lock'});
13111:             if (keys(%currids)) {
13112:                 my @curr = sort { $a <=> $b } keys(%currids);
13113:                 if ($curr[-1] =~ /^\d+$/) {
13114:                     $id = 1 + $curr[-1];
13115:                 }
13116:             } else {
13117:                 $id = 1;
13118:             }
13119:             if ($id) {
13120:                 if ($context eq 'domain') {
13121:                     unless (&newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
13122:                         $error = 'nostore';
13123:                     }
13124:                 } else {
13125:                     unless (&newput('ltitools',{ $id => $title },$cdom,$cnum) eq 'ok') {
13126:                         $error = 'nostore';
13127:                     }
13128:                 }
13129:             } else {
13130:                 $error = 'nonumber';
13131:             }
13132:         }
13133:         my $dellockoutcome;
13134:         if ($context eq 'domain') {
13135:             $dellockoutcome = &del_dom('ltitools',['lock'],$cdom);
13136:         } else {
13137:             $dellockoutcome = &del('ltitools',['lock'],$cdom,$cnum);
13138:         }
13139:     } else {
13140:         $error = 'nolock';
13141:     }
13142:     return ($id,$error);
13143: }
13144: 
13145: sub count_supptools {
13146:     my ($cnum,$cdom,$ignorecache,$reload)=@_;
13147:     my $hashid=$cnum.':'.$cdom;
13148:     my ($numexttools,$cached);
13149:     unless ($ignorecache) {
13150:         ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
13151:     }
13152:     unless (defined($cached)) {
13153:         my $chome=&homeserver($cnum,$cdom);
13154:         $numexttools = 0;
13155:         unless ($chome eq 'no_host') {
13156:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
13157:             if (ref($supplemental) eq 'HASH') {
13158:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
13159:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
13160:                         if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
13161:                             $numexttools ++;
13162:                         }
13163:                     }
13164:                 }
13165:             }
13166:         }
13167:         &do_cache_new('supptools',$hashid,$numexttools,600);
13168:     }
13169:     return $numexttools;
13170: }
13171: 
13172: sub has_unhidden_suppfiles {
13173:     my ($cnum,$cdom,$ignorecache,$possdel)=@_;
13174:     my $hashid=$cnum.':'.$cdom;
13175:     my ($showsupp,$cached);
13176:     unless ($ignorecache) {
13177:         ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
13178:     }
13179:     unless (defined($cached)) {
13180:         my $chome=&homeserver($cnum,$cdom);
13181:         unless ($chome eq 'no_host') {
13182:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
13183:             if (ref($supplemental) eq 'HASH') {
13184:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
13185:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
13186:                         next if ($key =~ /\.sequence$/);
13187:                         if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
13188:                             foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
13189:                                 unless ($supplemental->{'hidden'}->{$id}) {
13190:                                     $showsupp = 1;
13191:                                     last;
13192:                                 }
13193:                             }
13194:                         }
13195:                         last if ($showsupp);
13196:                     }
13197:                 }
13198:             }
13199:         }
13200:         &do_cache_new('showsupp',$hashid,$showsupp,600);
13201:     }
13202:     return $showsupp;
13203: }
13204: 
13205: #
13206: # EXT resource caching routines
13207: #
13208: 
13209: {
13210: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
13211: #
13212: # The course for which we cache
13213: my $cachedmapkey='';
13214: # The cached recursive maps for this course
13215: my %cachedmaps=();
13216: # When this was last done
13217: my $cachedmaptime='';
13218: 
13219: # Cache (5 seconds) of mapsymb hierarchy for speedup of reservations display
13220: #
13221: # The course for which we cache
13222: my $cachedmapsymbkey='';
13223: # The cached recursive map symbs for this course
13224: my %cachedmapsymbs=();
13225: # When this was last done
13226: my $cachedmapsymbtime='';
13227: 
13228: sub clear_EXT_cache_status {
13229:     &delenv('cache.EXT.');
13230: }
13231: 
13232: sub EXT_cache_status {
13233:     my ($target_domain,$target_user) = @_;
13234:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
13235:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
13236:         # We know already the user has no data
13237:         return 1;
13238:     } else {
13239:         return 0;
13240:     }
13241: }
13242: 
13243: sub EXT_cache_set {
13244:     my ($target_domain,$target_user) = @_;
13245:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
13246:     #&appenv({$cachename => time});
13247: }
13248: 
13249: # --------------------------------------------------------- Value of a Variable
13250: sub EXT {
13251: 
13252:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
13253:     unless ($varname) { return ''; }
13254:     #get real user name/domain, courseid and symb
13255:     my $courseid;
13256:     my $publicuser;
13257:     if ($symbparm) {
13258: 	$symbparm=&get_symb_from_alias($symbparm);
13259:     }
13260:     if (!($uname && $udom)) {
13261:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
13262:       if (!$symbparm) {	$symbparm=$cursymb; }
13263:     } else {
13264: 	$courseid=$env{'request.course.id'};
13265:     }
13266:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
13267:     my $rest;
13268:     if (defined($therest[0])) {
13269:        $rest=join('.',@therest);
13270:     } else {
13271:        $rest='';
13272:     }
13273: 
13274:     my $qualifierrest=$qualifier;
13275:     if ($rest) { $qualifierrest.='.'.$rest; }
13276:     my $spacequalifierrest=$space;
13277:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
13278:     if ($realm eq 'user') {
13279: # --------------------------------------------------------------- user.resource
13280: 	if ($space eq 'resource') {
13281: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
13282: 		  || defined($Apache::lonhomework::parsing_a_task))
13283: 		 &&
13284: 		 ($symbparm eq &symbread()) ) {
13285: 		# if we are in the middle of processing the resource the
13286: 		# get the value we are planning on committing
13287:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
13288:                     return $Apache::lonhomework::results{$qualifierrest};
13289:                 } else {
13290:                     return $Apache::lonhomework::history{$qualifierrest};
13291:                 }
13292: 	    } else {
13293: 		my %restored;
13294: 		if ($publicuser || $env{'request.state'} eq 'construct') {
13295: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
13296: 		} else {
13297: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
13298: 		}
13299: 		return $restored{$qualifierrest};
13300: 	    }
13301: # ----------------------------------------------------------------- user.access
13302:         } elsif ($space eq 'access') {
13303: 	    # FIXME - not supporting calls for a specific user
13304:             return &allowed($qualifier,$rest);
13305: # ------------------------------------------ user.preferences, user.environment
13306:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
13307: 	    if (($uname eq $env{'user.name'}) &&
13308: 		($udom eq $env{'user.domain'})) {
13309: 		return $env{join('.',('environment',$qualifierrest))};
13310: 	    } else {
13311: 		my %returnhash;
13312: 		if (!$publicuser) {
13313: 		    %returnhash=&userenvironment($udom,$uname,
13314: 						 $qualifierrest);
13315: 		}
13316: 		return $returnhash{$qualifierrest};
13317: 	    }
13318: # ----------------------------------------------------------------- user.course
13319:         } elsif ($space eq 'course') {
13320: 	    # FIXME - not supporting calls for a specific user
13321:             return $env{join('.',('request.course',$qualifier))};
13322: # ------------------------------------------------------------------- user.role
13323:         } elsif ($space eq 'role') {
13324: 	    # FIXME - not supporting calls for a specific user
13325:             my ($role,$where)=split(/\./,$env{'request.role'});
13326:             if ($qualifier eq 'value') {
13327: 		return $role;
13328:             } elsif ($qualifier eq 'extent') {
13329:                 return $where;
13330:             }
13331: # ----------------------------------------------------------------- user.domain
13332:         } elsif ($space eq 'domain') {
13333:             return $udom;
13334: # ------------------------------------------------------------------- user.name
13335:         } elsif ($space eq 'name') {
13336:             return $uname;
13337: # ---------------------------------------------------- Any other user namespace
13338:         } else {
13339: 	    my %reply;
13340: 	    if (!$publicuser) {
13341: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
13342: 	    }
13343: 	    return $reply{$qualifierrest};
13344:         }
13345:     } elsif ($realm eq 'query') {
13346: # ---------------------------------------------- pull stuff out of query string
13347:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
13348: 						[$spacequalifierrest]);
13349: 	return $env{'form.'.$spacequalifierrest}; 
13350:    } elsif ($realm eq 'request') {
13351: # ------------------------------------------------------------- request.browser
13352:         if ($space eq 'browser') {
13353:             return $env{'browser.'.$qualifier};
13354: # ------------------------------------------------------------ request.filename
13355:         } else {
13356:             return $env{'request.'.$spacequalifierrest};
13357:         }
13358:     } elsif ($realm eq 'course') {
13359: # ---------------------------------------------------------- course.description
13360:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
13361:     } elsif ($realm eq 'resource') {
13362: 
13363: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
13364: 	    if (!$symbparm) { $symbparm=&symbread(); }
13365: 	}
13366: 
13367:         if ($qualifier eq '') {
13368: 	    if ($space eq 'title') {
13369: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
13370: 	        return &gettitle($symbparm);
13371: 	    }
13372: 	
13373: 	    if ($space eq 'map') {
13374: 	        my ($map) = &decode_symb($symbparm);
13375: 	        return &symbread($map);
13376: 	    }
13377:             if ($space eq 'maptitle') {
13378:                 my ($map) = &decode_symb($symbparm);
13379:                 return &gettitle($map);
13380:             }
13381: 	    if ($space eq 'filename') {
13382: 	        if ($symbparm) {
13383: 		    return &clutter((&decode_symb($symbparm))[2]);
13384: 	        }
13385: 	        return &hreflocation('',$env{'request.filename'});
13386: 	    }
13387: 
13388:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
13389:                 if ($space eq 'visibleparts') {
13390:                     my $navmap = Apache::lonnavmaps::navmap->new();
13391:                     my $item;
13392:                     if (ref($navmap)) {
13393:                         my $res = $navmap->getBySymb($symbparm);
13394:                         my $parts = $res->parts();
13395:                         if (ref($parts) eq 'ARRAY') {
13396:                             $item = join(',',@{$parts});
13397:                         }
13398:                         undef($navmap);
13399:                     }
13400:                     return $item;
13401:                 }
13402:             }
13403:         }
13404: 
13405: 	my ($section, $group, @groups, @recurseup, $recursed);
13406:         if (ref($recurseupref) eq 'ARRAY') {
13407:             @recurseup = @{$recurseupref};
13408:             $recursed = 1;
13409:         }
13410: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
13411:         if (($courseid eq '') && ($cid)) {
13412:             $courseid = $cid;
13413:         }
13414: 	if (($symbparm && $courseid) && 
13415: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
13416: 
13417: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
13418: 
13419: # --------------------------------------------- Special handling for encrypturl
13420: 
13421:             if ($spacequalifierrest eq '0.encrypturl') {
13422:                 unless ($recursed) {
13423:                     my ($map_from_symb,@mapsymbs);
13424:                     if ($symbparm =~ /\.(page|sequence)$/) {
13425:                         push(@mapsymbs,$symbparm);
13426:                         $map_from_symb = &deversion((&decode_symb($symbparm))[2]);
13427:                     } else {
13428:                         $map_from_symb = &deversion((&decode_symb($symbparm))[0]);
13429:                     }
13430:                     if (($map_from_symb ne '') && ($map_from_symb !~ /default\.sequence$/)) {
13431:                         my @parents = &get_mapsymb_hierarchy($map_from_symb,$courseid);
13432:                         if (@parents) {
13433:                             push(@mapsymbs,@parents);
13434:                         }
13435:                     }
13436:                     if (@mapsymbs) {
13437:                         my $earlyout;
13438:                         my %parmhash=();
13439:                         if (tie(%parmhash,'GDBM_File',
13440:                                 $env{'request.course.fn'}.'_parms.db',
13441:                                 &GDBM_READER(),0640)) {
13442:                             foreach my $mapsymb (@mapsymbs) {
13443:                                 if ((exists($parmhash{$mapsymb.'.'.$spacequalifierrest})) &&
13444:                                     (lc($parmhash{$mapsymb.'.'.$spacequalifierrest}) eq 'yes')) {
13445:                                     $earlyout = $parmhash{$mapsymb.'.'.$spacequalifierrest};
13446:                                     last;
13447:                                 }
13448:                             }
13449:                             untie(%parmhash);
13450:                         }
13451:                         if ($earlyout) { return &get_reply([$earlyout,'map']); }
13452:                     }
13453:                 }
13454:             }
13455: 
13456: # ----------------------------------------------------- Cascading lookup scheme
13457: 	    my $symbp=$symbparm;
13458: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
13459: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
13460:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
13461: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
13462: 	    if (($env{'user.name'} eq $uname) &&
13463: 		($env{'user.domain'} eq $udom)) {
13464: 		$section=$env{'request.course.sec'};
13465:                 @groups = split(/:/,$env{'request.course.groups'});  
13466:                 @groups=&sort_course_groups($courseid,@groups); 
13467: 	    } else {
13468: 		if (! defined($usection)) {
13469: 		    $section=&getsection($udom,$uname,$courseid);
13470: 		} else {
13471: 		    $section = $usection;
13472: 		}
13473:                 @groups = &get_users_groups($udom,$uname,$courseid);
13474: 	    }
13475: 
13476: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
13477: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
13478:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
13479: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
13480: 
13481: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
13482: 	    my $courselevelr=$courseid.'.'.$symbparm;
13483:             $courseleveli=$courseid.'.'.$recurseparm;
13484: 	    $courselevelm=$courseid.'.'.$mapparm;
13485: 
13486: # ----------------------------------------------------------- first, check user
13487: 
13488: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
13489:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
13490: 				       ([$courselevelr,'resource'],
13491: 					[$courselevelm,'map'     ],
13492:                                         [$courseleveli,'map'     ],
13493: 					[$courselevel, 'course'  ]));
13494: 	    if (defined($userreply)) { return &get_reply($userreply); }
13495: 
13496: # ------------------------------------------------ second, check some of course
13497:             my $coursereply;
13498:             if (@groups > 0) {
13499:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
13500:                                        $recurseparm,$mapparm,$spacequalifierrest,
13501:                                        $mapp,\$recursed,\@recurseup);
13502:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
13503:             }
13504: 
13505: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
13506: 				  $env{'course.'.$courseid.'.domain'},
13507: 				  'course',$mapp,\$recursed,\@recurseup,
13508:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
13509: 				  ([$seclevelr,   'resource'],
13510: 				   [$seclevelm,   'map'     ],
13511:                                    [$secleveli,   'map'     ],
13512: 				   [$seclevel,    'course'  ],
13513: 				   [$courselevelr,'resource']));
13514: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
13515: 
13516: # ------------------------------------------------------ third, check map parms
13517: 	    my %parmhash=();
13518: 	    my $thisparm='';
13519: 	    if (tie(%parmhash,'GDBM_File',
13520: 		    $env{'request.course.fn'}.'_parms.db',
13521: 		    &GDBM_READER(),0640)) {
13522: 		$thisparm=$parmhash{$symbparm};
13523: 		untie(%parmhash);
13524: 	    }
13525: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
13526: 	}
13527: # ------------------------------------------ fourth, look in resource metadata
13528:  
13529:         my $what = $spacequalifierrest;
13530: 	$what=~s/\./\_/;
13531: 	my $filename;
13532: 	if (!$symbparm) { $symbparm=&symbread(); }
13533: 	if ($symbparm) {
13534: 	    $filename=(&decode_symb($symbparm))[2];
13535: 	} else {
13536: 	    $filename=$env{'request.filename'};
13537: 	}
13538:         my $toolsymb;
13539:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
13540:             $toolsymb = $symbparm;
13541:         }
13542: 	my $metadata=&metadata($filename,$what,$toolsymb);
13543: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
13544: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
13545: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
13546: 
13547: # ----------------------------------------------- fifth, look in rest of course
13548: 	if ($symbparm && defined($courseid) && 
13549: 	    $courseid eq $env{'request.course.id'}) {
13550: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
13551: 				     $env{'course.'.$courseid.'.domain'},
13552: 				     'course',$mapp,\$recursed,\@recurseup,
13553:                                      $courseid,'.',$spacequalifierrest,
13554: 				     ([$courselevelm,'map'   ],
13555:                                       [$courseleveli,'map'   ],
13556: 				      [$courselevel, 'course']));
13557: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
13558: 	}
13559: # ------------------------------------------------------------------ Cascade up
13560: 	unless ($space eq '0') {
13561: 	    my @parts=split(/_/,$space);
13562: 	    my $id=pop(@parts);
13563: 	    my $part=join('_',@parts);
13564: 	    if ($part eq '') { $part='0'; }
13565: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
13566: 				 $symbparm,$udom,$uname,$section,1);
13567: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
13568: 	}
13569: 	if ($recurse) { return undef; }
13570: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
13571: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
13572: # ---------------------------------------------------- Any other user namespace
13573:     } elsif ($realm eq 'environment') {
13574: # ----------------------------------------------------------------- environment
13575: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
13576: 	    return $env{'environment.'.$spacequalifierrest};
13577: 	} else {
13578: 	    if ($uname eq 'anonymous' && $udom eq '') {
13579: 		return '';
13580: 	    }
13581: 	    my %returnhash=&userenvironment($udom,$uname,
13582: 					    $spacequalifierrest);
13583: 	    return $returnhash{$spacequalifierrest};
13584: 	}
13585:     } elsif ($realm eq 'system') {
13586: # ----------------------------------------------------------------- system.time
13587: 	if ($space eq 'time') {
13588: 	    return time;
13589:         }
13590:     } elsif ($realm eq 'server') {
13591: # ----------------------------------------------------------------- system.time
13592: 	if ($space eq 'name') {
13593: 	    return $ENV{'SERVER_NAME'};
13594:         }
13595:     } elsif ($realm eq 'client') {
13596:         if ($space eq 'remote_addr') {
13597:             return &get_requestor_ip();
13598:         }
13599:     }
13600:     return '';
13601: }
13602: 
13603: sub get_reply {
13604:     my ($reply_value) = @_;
13605:     if (ref($reply_value) eq 'ARRAY') {
13606:         if (wantarray) {
13607: 	    return @$reply_value;
13608:         }
13609:         return $reply_value->[0];
13610:     } else {
13611:         return $reply_value;
13612:     }
13613: }
13614: 
13615: sub check_group_parms {
13616:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
13617:         $recursed,$recurseupref) = @_;
13618:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
13619:                   [$what,'course']);
13620:     my $coursereply;
13621:     foreach my $group (@{$groups}) {
13622:         my @groupitems = ();
13623:         foreach my $level (@levels) {
13624:              my $item = $courseid.'.['.$group.'].'.$level->[0];
13625:              push(@groupitems,[$item,$level->[1]]);
13626:         }
13627:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
13628:                                    $env{'course.'.$courseid.'.domain'},
13629:                                    'course',$mapp,$recursed,$recurseupref,
13630:                                    $courseid,'.['.$group.'].',$what,
13631:                                    @groupitems);
13632:         last if (defined($coursereply));
13633:     }
13634:     return $coursereply;
13635: }
13636: 
13637: sub get_map_hierarchy {
13638:     my ($mapname,$courseid) = @_;
13639:     my @recurseup = ();
13640:     if ($mapname) {
13641:         if (($cachedmapkey eq $courseid) &&
13642:             (abs($cachedmaptime-time)<5)) {
13643:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
13644:                 return @{$cachedmaps{$mapname}};
13645:             }
13646:         }
13647:         my $navmap = Apache::lonnavmaps::navmap->new();
13648:         if (ref($navmap)) {
13649:             @recurseup = $navmap->recurseup_maps($mapname);
13650:             undef($navmap);
13651:             $cachedmaps{$mapname} = \@recurseup;
13652:             $cachedmaptime=time;
13653:             $cachedmapkey=$courseid;
13654:         }
13655:     }
13656:     return @recurseup;
13657: }
13658: 
13659: sub get_mapsymb_hierarchy {
13660:     my ($mapname,$courseid) = @_;
13661:     my @recurseup;
13662:     if ($mapname) {
13663:         if (($cachedmapsymbkey eq $courseid) &&
13664:             (abs($cachedmapsymbtime-time)<5)) {
13665:             if (ref($cachedmapsymbs{$mapname}) eq 'ARRAY') {
13666:                 return @{$cachedmapsymbs{$mapname}};
13667:             }
13668:         }
13669:         my $navmap = Apache::lonnavmaps::navmap->new();
13670:         if (ref($navmap)) {
13671:             my $getsymb = 1;
13672:             my $inclusive = 1;
13673:             @recurseup = $navmap->recurseup_maps($mapname,$getsymb,$inclusive);
13674:             undef($navmap);
13675:             $cachedmapsymbs{$mapname} = \@recurseup;
13676:             $cachedmapsymbtime=time;
13677:             $cachedmapsymbkey=$courseid;
13678:         }
13679:     }
13680:     return @recurseup;
13681: }
13682: 
13683: }
13684: 
13685: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
13686:     my ($courseid,@groups) = @_;
13687:     @groups = sort(@groups);
13688:     return @groups;
13689: }
13690: 
13691: sub packages_tab_default {
13692:     my ($uri,$varname,$toolsymb)=@_;
13693:     my (undef,$part,$name)=split(/\./,$varname);
13694: 
13695:     my (@extension,@specifics,$do_default);
13696:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
13697: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
13698: 	if ($pack_type eq 'default') {
13699: 	    $do_default=1;
13700: 	} elsif ($pack_type eq 'extension') {
13701: 	    push(@extension,[$package,$pack_type,$pack_part]);
13702: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
13703: 	    # only look at packages defaults for packages that this id is
13704: 	    push(@specifics,[$package,$pack_type,$pack_part]);
13705: 	}
13706:     }
13707:     # first look for a package that matches the requested part id
13708:     foreach my $package (@specifics) {
13709: 	my (undef,$pack_type,$pack_part)=@{$package};
13710: 	next if ($pack_part ne $part);
13711: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13712: 	    return $packagetab{"$pack_type&$name&default"};
13713: 	}
13714:     }
13715:     # look for any possible matching non extension_ package
13716:     foreach my $package (@specifics) {
13717: 	my (undef,$pack_type,$pack_part)=@{$package};
13718: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13719: 	    return $packagetab{"$pack_type&$name&default"};
13720: 	}
13721: 	if ($pack_type eq 'part') { $pack_part='0'; }
13722: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
13723: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
13724: 	}
13725:     }
13726:     # look for any posible extension_ match
13727:     foreach my $package (@extension) {
13728: 	my ($package,$pack_type)=@{$package};
13729: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13730: 	    return $packagetab{"$pack_type&$name&default"};
13731: 	}
13732: 	if (defined($packagetab{$package."&$name&default"})) {
13733: 	    return $packagetab{$package."&$name&default"};
13734: 	}
13735:     }
13736:     # look for a global default setting
13737:     if ($do_default && defined($packagetab{"default&$name&default"})) {
13738: 	return $packagetab{"default&$name&default"};
13739:     }
13740:     return undef;
13741: }
13742: 
13743: sub add_prefix_and_part {
13744:     my ($prefix,$part)=@_;
13745:     my $keyroot;
13746:     if (defined($prefix) && $prefix !~ /^__/) {
13747: 	# prefix that has a part already
13748: 	$keyroot=$prefix;
13749:     } elsif (defined($prefix)) {
13750: 	# prefix that is missing a part
13751: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
13752:     } else {
13753: 	# no prefix at all
13754: 	if (defined($part)) { $keyroot='_'.$part; }
13755:     }
13756:     return $keyroot;
13757: }
13758: 
13759: # ---------------------------------------------------------------- Get metadata
13760: 
13761: my %metaentry;
13762: my %importedpartids;
13763: my %importedrespids;
13764: sub metadata {
13765:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
13766:     $uri=&declutter($uri);
13767:     # if it is a non metadata possible uri return quickly
13768:     if (($uri eq '') || 
13769: 	(($uri =~ m|^/*adm/|) && 
13770: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
13771:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
13772: 	return undef;
13773:     }
13774:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
13775: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
13776: 	return undef;
13777:     }
13778:     my $filename=$uri;
13779:     $uri=~s/\.meta$//;
13780: #
13781: # Is the metadata already cached?
13782: # Look at timestamp of caching
13783: # Everything is cached by the main uri, libraries are never directly cached
13784: #
13785:     if (!defined($liburi)) {
13786: 	my ($result,$cached)=&is_cached_new('meta',$uri);
13787: 	if (defined($cached)) { return $result->{':'.$what}; }
13788:     }
13789: 
13790: #
13791: # If the uri is for an external tool the file from
13792: # which metadata should be retrieved depends on whether
13793: # the tool had been configured to be gradable (set in the Course
13794: # Editor or Resource Editor).
13795: #
13796: # If a valid symb has been included as the third arg in the call
13797: # to &metadata() that can be used to retrieve the value of
13798: # parameter_0_gradable set for the resource, and included in the
13799: # uploaded map containing the tool. The value is retrieved via
13800: # &EXT(), if a valid symb is available.  Otherwise the value of
13801: # gradable in the exttool_$marker.db file for the tool instance
13802: # is retrieved via &get().
13803: #
13804: # When lonuserstate::traceroute() calls lonnet::EXT() for 
13805: # hiddenresource and encrypturl (during course initialization)
13806: # the map-level parameter for resource.0.gradable included in the 
13807: # uploaded map containing the tool will not yet have been stored
13808: # in the user_course_parms.db file for the user's session, so in 
13809: # this case fall back to retrieving gradable status from the
13810: # exttool_$marker.db file.
13811: #
13812: # In order to avoid an infinite loop, &metadata() will return
13813: # before a call to &EXT(), if the uri is for an external tool
13814: # and the $what for which metadata is being requested is
13815: # parameter_0_gradable or 0_gradable.
13816: #
13817: 
13818:     if ($uri =~ /ext\.tool$/) {
13819:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
13820:             return;
13821:         } else {
13822:             my ($checked,$use_passback);
13823:             if ($toolsymb ne '') {
13824:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
13825:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
13826:                     $checked = 1;
13827:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
13828:                         $use_passback = 1;
13829:                     }
13830:                 }
13831:             }
13832:             unless ($checked) {
13833:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
13834:                 $marker=~s/\D//g;
13835:                 if ($marker) {
13836:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
13837:                     $use_passback = $toolsettings{'gradable'};
13838:                 }
13839:             }
13840:             if ($use_passback) {
13841:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
13842:             } else {
13843:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
13844:             }
13845:         }
13846:     }
13847: 
13848:     {
13849: # Imported parts would go here
13850:         my @origfiletagids=();
13851:         my $importedparts=0;
13852: 
13853: # Imported responseids would go here
13854:         my $importedresponses=0;
13855: #
13856: # Is this a recursive call for a library?
13857: #
13858: #	if (! exists($metacache{$uri})) {
13859: #	    $metacache{$uri}={};
13860: #	}
13861: 	my $cachetime = 60*60;
13862:         if ($liburi) {
13863: 	    $liburi=&declutter($liburi);
13864:             $filename=$liburi;
13865:         } else {
13866: 	    &devalidate_cache_new('meta',$uri);
13867: 	    undef(%metaentry);
13868: 	}
13869:         my %metathesekeys=();
13870:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
13871: 	my $metastring;
13872: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
13873: 	    my $which = &hreflocation('','/'.($liburi || $uri));
13874: 	    $metastring = 
13875: 		&Apache::lonnet::ssi_body($which,
13876: 					  ('grade_target' => 'meta'));
13877: 	    $cachetime = 1; # only want this cached in the child not long term
13878: 	} elsif (($uri !~ m -^(editupload)/-) && 
13879:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
13880: 	    my $file=&filelocation('',&clutter($filename));
13881: 	    #push(@{$metaentry{$uri.'.file'}},$file);
13882: 	    $metastring=&getfile($file);
13883: 	}
13884:         my $parser=HTML::LCParser->new(\$metastring);
13885:         my $token;
13886:         undef %metathesekeys;
13887:         while ($token=$parser->get_token) {
13888: 	    if ($token->[0] eq 'S') {
13889: 		if (defined($token->[2]->{'package'})) {
13890: #
13891: # This is a package - get package info
13892: #
13893: 		    my $package=$token->[2]->{'package'};
13894: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13895: 		    if (defined($token->[2]->{'id'})) { 
13896: 			$keyroot.='_'.$token->[2]->{'id'}; 
13897: 		    }
13898: 		    if ($metaentry{':packages'}) {
13899: 			$metaentry{':packages'}.=','.$package.$keyroot;
13900: 		    } else {
13901: 			$metaentry{':packages'}=$package.$keyroot;
13902: 		    }
13903: 		    foreach my $pack_entry (keys(%packagetab)) {
13904: 			my $part=$keyroot;
13905: 			$part=~s/^\_//;
13906: 			if ($pack_entry=~/^\Q$package\E\&/ || 
13907: 			    $pack_entry=~/^\Q$package\E_0\&/) {
13908: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
13909: 			    # ignore package.tab specified default values
13910:                             # here &package_tab_default() will fetch those
13911: 			    if ($subp eq 'default') { next; }
13912: 			    my $value=$packagetab{$pack_entry};
13913: 			    my $unikey;
13914: 			    if ($pack =~ /_0$/) {
13915: 				$unikey='parameter_0_'.$name;
13916: 				$part=0;
13917: 			    } else {
13918: 				$unikey='parameter'.$keyroot.'_'.$name;
13919: 			    }
13920: 			    if ($subp eq 'display') {
13921: 				$value.=' [Part: '.$part.']';
13922: 			    }
13923: 			    $metaentry{':'.$unikey.'.part'}=$part;
13924: 			    $metathesekeys{$unikey}=1;
13925: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13926: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
13927: 			    }
13928: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
13929: 				$metaentry{':'.$unikey}=
13930: 				    $metaentry{':'.$unikey.'.default'};
13931: 			    }
13932: 			}
13933: 		    }
13934: 		} else {
13935: #
13936: # This is not a package - some other kind of start tag
13937: #
13938: 		    my $entry=$token->[1];
13939: 		    my $unikey='';
13940: 
13941: 		    if ($entry eq 'import') {
13942: #
13943: # Importing a library here
13944: #
13945:                         my $location=$parser->get_text('/import');
13946:                         my $dir=$filename;
13947:                         $dir=~s|[^/]*$||;
13948:                         $location=&filelocation($dir,$location);
13949: 
13950:                         my $importid=$token->[2]->{'id'};
13951:                         my $importmode=$token->[2]->{'importmode'};
13952: #
13953: # Check metadata for imported file to
13954: # see if it contained response items
13955: #
13956:                         my ($origfile,@libfilekeys);
13957:                         my %currmetaentry = %metaentry;
13958:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
13959:                                                            $depthcount+1));
13960:                         if (grep(/^responseorder$/,@libfilekeys)) {
13961:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
13962:                                                              undef,$depthcount+1);
13963:                             if ($libresponseorder ne '') {
13964:                                 if ($#origfiletagids<0) {
13965:                                     undef(%importedrespids);
13966:                                     undef(%importedpartids);
13967:                                 }
13968:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
13969:                                 if (@respids) {
13970:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
13971:                                 }
13972:                                 if ($importedrespids{$importid} ne '') {
13973:                                     $importedresponses = 1;
13974: # We need to get the original file and the imported file to get the response order correct
13975: # Load and inspect original file
13976:                                     if ($#origfiletagids<0) {
13977:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13978:                                         $origfile=&getfile($origfilelocation);
13979:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13980:                                     }
13981:                                 }
13982:                             }
13983:                         }
13984: # Do not overwrite contents of %metaentry hash for resource itself with 
13985: # hash populated for imported library file
13986:                         %metaentry = %currmetaentry;
13987:                         undef(%currmetaentry);
13988:                         if ($importmode eq 'part') {
13989: # Import as part(s)
13990:                            $importedparts=1;
13991: # We need to get the original file and the imported file to get the part order correct
13992: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
13993: # Load and inspect original file if we didn't do that already
13994:                            if ($#origfiletagids<0) {
13995:                                undef(%importedrespids);
13996:                                undef(%importedpartids);
13997:                                if ($origfile eq '') {
13998:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13999:                                    $origfile=&getfile($origfilelocation);
14000:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
14001:                                }
14002:                            }
14003:                            my @impfilepartids;
14004: # If <partorder> tag is included in metadata for the imported file
14005: # get the parts in the imported file from that.
14006:                            if (grep(/^partorder$/,@libfilekeys)) {
14007:                                %currmetaentry = %metaentry;
14008:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
14009:                                                             $depthcount+1);
14010:                                %metaentry = %currmetaentry;
14011:                                undef(%currmetaentry);
14012:                                if ($libpartorder ne '') {
14013:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
14014:                                }
14015:                            } else {
14016: # If no <partorder> tag available, load and inspect imported file
14017:                                my $impfile=&getfile($location);
14018:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
14019:                            }
14020:                            if ($#impfilepartids>=0) {
14021: # This problem had parts
14022:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
14023:                            } else {
14024: # Importing by turning a single problem into a problem part
14025: # It gets the import-tags ID as part-ID
14026:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
14027:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
14028:                            }
14029:                         } else {
14030: # Import as problem or as normal import
14031:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
14032:                             unless ($importmode eq 'problem') {
14033: # Normal import
14034:                                 if (defined($token->[2]->{'id'})) {
14035:                                     $unikey.='_'.$token->[2]->{'id'};
14036:                                 }
14037:                             }
14038: # Check metadata for imported file to
14039: # see if it contained parts
14040:                             if (grep(/^partorder$/,@libfilekeys)) {
14041:                                 %currmetaentry = %metaentry;
14042:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
14043:                                                              $depthcount+1);
14044:                                 %metaentry = %currmetaentry;
14045:                                 undef(%currmetaentry);
14046:                                 if ($libpartorder ne '') {
14047:                                     $importedparts = 1;
14048:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
14049:                                 }
14050:                             }
14051:                         }
14052: 			if ($depthcount<20) {
14053: 			    my $metadata = 
14054: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
14055: 					  $depthcount+1);
14056: 			    foreach my $meta (split(',',$metadata)) {
14057: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
14058: 				$metathesekeys{$meta}=1;
14059: 			    }
14060:                         }
14061: 		    } else {
14062: #
14063: # Not importing, some other kind of non-package, non-library start tag
14064: # 
14065:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
14066:                         if (defined($token->[2]->{'id'})) {
14067:                             $unikey.='_'.$token->[2]->{'id'};
14068:                         }
14069: 			if (defined($token->[2]->{'name'})) { 
14070: 			    $unikey.='_'.$token->[2]->{'name'}; 
14071: 			}
14072: 			$metathesekeys{$unikey}=1;
14073: 			foreach my $param (@{$token->[3]}) {
14074: 			    $metaentry{':'.$unikey.'.'.$param} =
14075: 				$token->[2]->{$param};
14076: 			}
14077: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
14078: 			my $default=$metaentry{':'.$unikey.'.default'};
14079: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
14080: 		 # only ws inside the tag, and not in default, so use default
14081: 		 # as value
14082: 			    $metaentry{':'.$unikey}=$default;
14083: 			} elsif ( $internaltext =~ /\S/ ) {
14084: 		  # something interesting inside the tag
14085: 			    $metaentry{':'.$unikey}=$internaltext;
14086: 			} else {
14087: 		  # no interesting values, don't set a default
14088: 			}
14089: # end of not-a-package not-a-library import
14090: 		    }
14091: # end of not-a-package start tag
14092: 		}
14093: # the next is the end of "start tag"
14094: 	    }
14095: 	}
14096: 	my ($extension) = ($uri =~ /\.(\w+)$/);
14097: 	$extension = lc($extension);
14098: 	if ($extension eq 'htm') { $extension='html'; }
14099: 
14100: 	foreach my $key (keys(%packagetab)) {
14101: 	    #no specific packages #how's our extension
14102: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
14103: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
14104: 					 \%metathesekeys);
14105: 	}
14106: 
14107: 	if (!exists($metaentry{':packages'})
14108: 	    || $packagetab{"import_defaults&extension_$extension"}) {
14109: 	    foreach my $key (keys(%packagetab)) {
14110: 		#no specific packages well let's get default then
14111: 		if ($key!~/^default&/) { next; }
14112: 		&metadata_create_package_def($uri,$key,'default',
14113: 					     \%metathesekeys);
14114: 	    }
14115: 	}
14116: # are there custom rights to evaluate
14117: 	if ($metaentry{':copyright'} eq 'custom') {
14118: 
14119:     #
14120:     # Importing a rights file here
14121:     #
14122: 	    unless ($depthcount) {
14123: 		my $location=$metaentry{':customdistributionfile'};
14124: 		my $dir=$filename;
14125: 		$dir=~s|[^/]*$||;
14126: 		$location=&filelocation($dir,$location);
14127: 		my $rights_metadata =
14128: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
14129: 			      $depthcount+1);
14130: 		foreach my $rights (split(',',$rights_metadata)) {
14131: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
14132: 		    $metathesekeys{$rights}=1;
14133: 		}
14134: 	    }
14135: 	}
14136: 	# uniqifiy package listing
14137: 	my %seen;
14138: 	my @uniq_packages =
14139: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
14140: 	$metaentry{':packages'} = join(',',@uniq_packages);
14141: 
14142:         if (($importedresponses) || ($importedparts)) {
14143:             if ($importedparts) {
14144: # We had imported parts and need to rebuild partorder
14145:                 $metaentry{':partorder'}='';
14146:                 $metathesekeys{'partorder'}=1;
14147:             }
14148:             if ($importedresponses) {
14149: # We had imported responses and need to rebuil responseorder
14150:                 $metaentry{':responseorder'}='';
14151:                 $metathesekeys{'responseorder'}=1;
14152:             }
14153:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
14154:                 my $origid = $origfiletagids[$index+1];
14155:                 if ($origfiletagids[$index] eq 'part') {
14156: # Original part, part of the problem
14157:                     if ($importedparts) {
14158:                         $metaentry{':partorder'}.=','.$origid;
14159:                     }
14160:                 } elsif ($origfiletagids[$index] eq 'import') {
14161:                     if ($importedparts) {
14162: # We have imported parts at this position
14163:                         if ($importedpartids{$origid} ne '') {
14164:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
14165:                         }
14166:                     }
14167:                     if ($importedresponses) {
14168: # We have imported responses at this position
14169:                         if ($importedrespids{$origid} ne '') {
14170:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
14171:                         }
14172:                     }
14173:                 } else {
14174: # Original response item, part of the problem
14175:                     if ($importedresponses) {
14176:                         $metaentry{':responseorder'}.=','.$origid;
14177:                     }
14178:                 }
14179:             }
14180:             if ($importedparts) {
14181:                 $metaentry{':partorder'}=~s/^\,//;
14182:             }
14183:             if ($importedresponses) {
14184:                 $metaentry{':responseorder'}=~s/^\,//;
14185:             }
14186:         }
14187: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
14188: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
14189: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
14190:         unless ($liburi) {
14191: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
14192:         }
14193: # this is the end of "was not already recently cached
14194:     }
14195:     return $metaentry{':'.$what};
14196: }
14197: 
14198: sub metadata_create_package_def {
14199:     my ($uri,$key,$package,$metathesekeys)=@_;
14200:     my ($pack,$name,$subp)=split(/\&/,$key);
14201:     if ($subp eq 'default') { next; }
14202:     
14203:     if (defined($metaentry{':packages'})) {
14204: 	$metaentry{':packages'}.=','.$package;
14205:     } else {
14206: 	$metaentry{':packages'}=$package;
14207:     }
14208:     my $value=$packagetab{$key};
14209:     my $unikey;
14210:     $unikey='parameter_0_'.$name;
14211:     $metaentry{':'.$unikey.'.part'}=0;
14212:     $$metathesekeys{$unikey}=1;
14213:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
14214: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
14215:     }
14216:     if (defined($metaentry{':'.$unikey.'.default'})) {
14217: 	$metaentry{':'.$unikey}=
14218: 	    $metaentry{':'.$unikey.'.default'};
14219:     }
14220: }
14221: 
14222: sub metadata_generate_part0 {
14223:     my ($metadata,$metacache,$uri) = @_;
14224:     my %allnames;
14225:     foreach my $metakey (keys(%$metadata)) {
14226: 	if ($metakey=~/^parameter\_(.*)/) {
14227: 	  my $part=$$metacache{':'.$metakey.'.part'};
14228: 	  my $name=$$metacache{':'.$metakey.'.name'};
14229: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
14230: 	    $allnames{$name}=$part;
14231: 	  }
14232: 	}
14233:     }
14234:     foreach my $name (keys(%allnames)) {
14235:       $$metadata{"parameter_0_$name"}=1;
14236:       my $key=":parameter_0_$name";
14237:       $$metacache{"$key.part"}='0';
14238:       $$metacache{"$key.name"}=$name;
14239:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
14240: 					   $allnames{$name}.'_'.$name.
14241: 					   '.type'};
14242:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
14243: 			     '.display'};
14244:       my $expr='[Part: '.$allnames{$name}.']';
14245:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
14246:       $$metacache{"$key.display"}=$olddis;
14247:     }
14248: }
14249: 
14250: # ------------------------------------------------------ Devalidate title cache
14251: 
14252: sub devalidate_title_cache {
14253:     my ($url)=@_;
14254:     if (!$env{'request.course.id'}) { return; }
14255:     my $symb=&symbread($url);
14256:     if (!$symb) { return; }
14257:     my $key=$env{'request.course.id'}."\0".$symb;
14258:     &devalidate_cache_new('title',$key);
14259: }
14260: 
14261: # ------------------------------------------------- Get the title of a course
14262: 
14263: sub current_course_title {
14264:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
14265: }
14266: # ------------------------------------------------- Get the title of a resource
14267: 
14268: sub gettitle {
14269:     my $urlsymb=shift;
14270:     my $symb=&symbread($urlsymb);
14271:     if ($symb) {
14272: 	my $key=$env{'request.course.id'}."\0".$symb;
14273: 	my ($result,$cached)=&is_cached_new('title',$key);
14274: 	if (defined($cached)) { 
14275: 	    return $result;
14276: 	}
14277: 	my ($map,$resid,$url)=&decode_symb($symb);
14278: 	my $title='';
14279: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
14280: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
14281: 	} else {
14282: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14283: 		    &GDBM_READER(),0640)) {
14284: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
14285: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
14286: 		untie(%bighash);
14287: 	    }
14288: 	}
14289: 	$title=~s/\&colon\;/\:/gs;
14290: 	if ($title) {
14291: # Remember both $symb and $title for dynamic metadata
14292:             $accesshash{$symb.'___crstitle'}=$title;
14293:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
14294: # Cache this title and then return it
14295: 	    return &do_cache_new('title',$key,$title,600);
14296: 	}
14297: 	$urlsymb=$url;
14298:     }
14299:     my $title=&metadata($urlsymb,'title');
14300:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
14301:     return $title;
14302: }
14303: 
14304: sub get_slot {
14305:     my ($which,$cnum,$cdom)=@_;
14306:     if (!$cnum || !$cdom) {
14307: 	(undef,my $courseid)=&whichuser();
14308: 	$cdom=$env{'course.'.$courseid.'.domain'};
14309: 	$cnum=$env{'course.'.$courseid.'.num'};
14310:     }
14311:     my $key=join("\0",'slots',$cdom,$cnum,$which);
14312:     my %slotinfo;
14313:     if (exists($remembered{$key})) {
14314: 	$slotinfo{$which} = $remembered{$key};
14315:     } else {
14316: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
14317: 	&Apache::lonhomework::showhash(%slotinfo);
14318: 	my ($tmp)=keys(%slotinfo);
14319: 	if ($tmp=~/^error:/) { return (); }
14320: 	$remembered{$key} = $slotinfo{$which};
14321:     }
14322:     if (ref($slotinfo{$which}) eq 'HASH') {
14323: 	return %{$slotinfo{$which}};
14324:     }
14325:     return $slotinfo{$which};
14326: }
14327: 
14328: sub get_reservable_slots {
14329:     my ($cnum,$cdom,$uname,$udom) = @_;
14330:     my $now = time;
14331:     my $reservable_info;
14332:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
14333:     if (exists($remembered{$key})) {
14334:         $reservable_info = $remembered{$key};
14335:     } else {
14336:         my %resv;
14337:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
14338:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
14339:         $reservable_info = \%resv;
14340:         $remembered{$key} = $reservable_info;
14341:     }
14342:     return $reservable_info;
14343: }
14344: 
14345: sub get_course_slots {
14346:     my ($cnum,$cdom) = @_;
14347:     my $hashid=$cnum.':'.$cdom;
14348:     my ($result,$cached) = &is_cached_new('allslots',$hashid);
14349:     if (defined($cached)) {
14350:         if (ref($result) eq 'HASH') {
14351:             return %{$result};
14352:         }
14353:     } else {
14354:         my %slots=&dump('slots',$cdom,$cnum);
14355:         my ($tmp) = keys(%slots);
14356:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
14357:             &do_cache_new('allslots',$hashid,\%slots,600);
14358:             return %slots;
14359:         }
14360:     }
14361:     return;
14362: }
14363: 
14364: sub devalidate_slots_cache {
14365:     my ($cnum,$cdom)=@_;
14366:     my $hashid=$cnum.':'.$cdom;
14367:     &devalidate_cache_new('allslots',$hashid);
14368: }
14369: 
14370: sub get_coursechange {
14371:     my ($cdom,$cnum) = @_;
14372:     if ($cdom eq '' || $cnum eq '') {
14373:         return unless ($env{'request.course.id'});
14374:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
14375:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
14376:     }
14377:     my $hashid=$cdom.'_'.$cnum;
14378:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
14379:     if ((defined($cached)) && ($change ne '')) {
14380:         return $change;
14381:     } else {
14382:         my %crshash;
14383:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
14384:         if ($crshash{'internal.contentchange'} eq '') {
14385:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
14386:             if ($change eq '') {
14387:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
14388:                 $change = $crshash{'internal.created'};
14389:             }
14390:         } else {
14391:             $change = $crshash{'internal.contentchange'};
14392:         }
14393:         my $cachetime = 600;
14394:         &do_cache_new('crschange',$hashid,$change,$cachetime);
14395:     }
14396:     return $change;
14397: }
14398: 
14399: sub devalidate_coursechange_cache {
14400:     my ($cdom,$cnum)=@_;
14401:     my $hashid=$cdom.'_'.$cnum;
14402:     &devalidate_cache_new('crschange',$hashid);
14403: }
14404: 
14405: sub get_suppchange {
14406:     my ($cdom,$cnum) = @_;
14407:     if ($cdom eq '' || $cnum eq '') {
14408:         return unless ($env{'request.course.id'});
14409:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
14410:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
14411:     }
14412:     my $hashid=$cdom.'_'.$cnum;
14413:     my ($change,$cached)=&is_cached_new('suppchange',$hashid);
14414:     if ((defined($cached)) && ($change ne '')) {
14415:         return $change;
14416:     } else {
14417:         my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
14418:         if ($crshash{'internal.supplementalchange'} eq '') {
14419:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
14420:             if ($change eq '') {
14421:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
14422:                 $change = $crshash{'internal.created'};
14423:             }
14424:         } else {
14425:             $change = $crshash{'internal.supplementalchange'};
14426:         }
14427:         my $cachetime = 600;
14428:         &do_cache_new('suppchange',$hashid,$change,$cachetime);
14429:     }
14430:     return $change;
14431: }
14432: 
14433: sub devalidate_suppchange_cache {
14434:     my ($cdom,$cnum)=@_;
14435:     my $hashid=$cdom.'_'.$cnum;
14436:     &devalidate_cache_new('suppchange',$hashid);
14437: }
14438: 
14439: sub update_supp_caches {
14440:     my ($cdom,$cnum) = @_;
14441:     my %servers = &internet_dom_servers($cdom);
14442:     my @ids=&current_machine_ids();
14443:     foreach my $server (keys(%servers)) {
14444:         next if (grep(/^\Q$server\E$/,@ids));
14445:         my $hashid=$cnum.':'.$cdom;
14446:         my $cachekey = &escape('showsupp').':'.&escape($hashid);
14447:         &remote_devalidate_cache($server,[$cachekey]);
14448:     }
14449:     &has_unhidden_suppfiles($cnum,$cdom,1,1);
14450:     &count_supptools($cnum,$cdom,1);
14451:     my $now = time;
14452:     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
14453:         &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
14454:     }
14455:     &put('environment',{'internal.supplementalchange' => $now},
14456:          $cdom,$cnum);
14457:     &Apache::lonnet::appenv(
14458:         {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
14459:     &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
14460: }
14461: 
14462: # ------------------------------------------------- Update symbolic store links
14463: 
14464: sub symblist {
14465:     my ($mapname,%newhash)=@_;
14466:     $mapname=&deversion(&declutter($mapname));
14467:     my %hash;
14468:     if (($env{'request.course.fn'}) && (%newhash)) {
14469:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
14470:                       &GDBM_WRCREAT(),0640)) {
14471: 	    foreach my $url (keys(%newhash)) {
14472: 		next if ($url eq 'last_known'
14473: 			 && $env{'form.no_update_last_known'});
14474: 		$hash{declutter($url)}=&encode_symb($mapname,
14475: 						    $newhash{$url}->[1],
14476: 						    $newhash{$url}->[0]);
14477:             }
14478:             if (untie(%hash)) {
14479: 		return 'ok';
14480:             }
14481:         }
14482:     }
14483:     return 'error';
14484: }
14485: 
14486: # --------------------------------------------------------------- Verify a symb
14487: 
14488: sub symbverify {
14489:     my ($symb,$thisurl,$encstate)=@_;
14490:     my $thisfn=$thisurl;
14491:     $thisfn=&declutter($thisfn);
14492: # direct jump to resource in page or to a sequence - will construct own symbs
14493:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
14494: # check URL part
14495:     my ($map,$resid,$url)=&decode_symb($symb);
14496: 
14497:     unless ($url eq $thisfn) { return 0; }
14498: 
14499:     $symb=&symbclean($symb);
14500:     $thisurl=&deversion($thisurl);
14501:     $thisfn=&deversion($thisfn);
14502: 
14503:     my %bighash;
14504:     my $okay=0;
14505: 
14506:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14507:                             &GDBM_READER(),0640)) {
14508:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
14509:             $thisurl =~ s/\?.+$//;
14510:             if ($map =~ m{^uploaded/.+\.page$}) {
14511:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
14512:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
14513:             }
14514:         }
14515:         my $ids;
14516:         if ($map =~ m{^uploaded/.+\.page$}) {
14517:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
14518:         } else {
14519:             $ids=$bighash{'ids_'.&clutter($thisurl)};
14520:         }
14521:         unless ($ids) {
14522:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
14523:             $ids=$bighash{$idkey};
14524:         }
14525:         if ($ids) {
14526: # ------------------------------------------------------------------- Has ID(s)
14527:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
14528:                 $symb =~ s/\?.+$//;
14529:             }
14530: 	    foreach my $id (split(/\,/,$ids)) {
14531: 	       my ($mapid,$resid)=split(/\./,$id);
14532:                if (
14533:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
14534:    eq $symb) {
14535:                    if (ref($encstate)) {
14536:                        $$encstate = $bighash{'encrypted_'.$id};
14537:                    }
14538: 		   if (($env{'request.role.adv'}) ||
14539: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
14540:                        ($thisurl eq '/adm/navmaps')) {
14541: 		       $okay=1;
14542:                        last;
14543: 		   }
14544: 	       }
14545: 	   }
14546:         }
14547: 	untie(%bighash);
14548:     }
14549:     return $okay;
14550: }
14551: 
14552: # --------------------------------------------------------------- Clean-up symb
14553: 
14554: sub symbclean {
14555:     my $symb=shift;
14556:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
14557: # remove version from map
14558:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
14559: 
14560: # remove version from URL
14561:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
14562: 
14563: # remove wrapper
14564: 
14565:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
14566:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
14567:     return $symb;
14568: }
14569: 
14570: # ---------------------------------------------- Split symb to find map and url
14571: 
14572: sub encode_symb {
14573:     my ($map,$resid,$url)=@_;
14574:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
14575: }
14576: 
14577: sub decode_symb {
14578:     my $symb=shift;
14579:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
14580:     my ($map,$resid,$url)=split(/___/,$symb);
14581:     return (&fixversion($map),$resid,&fixversion($url));
14582: }
14583: 
14584: sub fixversion {
14585:     my $fn=shift;
14586:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
14587:     my %bighash;
14588:     my $uri=&clutter($fn);
14589:     my $key=$env{'request.course.id'}.'_'.$uri;
14590: # is this cached?
14591:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
14592:     if (defined($cached)) { return $result; }
14593: # unfortunately not cached, or expired
14594:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14595: 	    &GDBM_READER(),0640)) {
14596:  	if ($bighash{'version_'.$uri}) {
14597:  	    my $version=$bighash{'version_'.$uri};
14598:  	    unless (($version eq 'mostrecent') || 
14599: 		    ($version==&getversion($uri))) {
14600:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
14601:  	    }
14602:  	}
14603:  	untie %bighash;
14604:     }
14605:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
14606: }
14607: 
14608: sub deversion {
14609:     my $url=shift;
14610:     $url=~s/\.\d+\.(\w+)$/\.$1/;
14611:     return $url;
14612: }
14613: 
14614: # ------------------------------------------------------ Return symb list entry
14615: 
14616: sub symbread {
14617:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
14618:         $ignoresymbdb,$noenccheck)=@_;
14619:     my $cache_str='request.symbread.cached.'.$thisfn;
14620:     if (defined($env{$cache_str})) {
14621:         unless (ref($possibles) eq 'HASH') {
14622:             if ($ignorecachednull) {
14623:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
14624:             } else {
14625:                 return $env{$cache_str};
14626:             }
14627:         }
14628:     }
14629: # no filename provided? try from environment
14630:     unless ($thisfn) {
14631:         if ($env{'request.symb'}) {
14632:             return $env{$cache_str}=&symbclean($env{'request.symb'});
14633: 	}
14634: 	$thisfn=$env{'request.filename'};
14635:     }
14636:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14637: # is that filename actually a symb? Verify, clean, and return
14638:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
14639: 	if (&symbverify($thisfn,$1)) {
14640: 	    return $env{$cache_str}=&symbclean($thisfn);
14641: 	}
14642:     }
14643:     $thisfn=declutter($thisfn);
14644:     my %hash;
14645:     my %bighash;
14646:     my $syval='';
14647:     if (($env{'request.course.fn'}) && ($thisfn)) {
14648:         unless ($ignoresymbdb) {
14649:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
14650:                           &GDBM_READER(),0640)) {
14651: 	        $syval=$hash{$thisfn};
14652:                 untie(%hash);
14653:             }
14654:             if ($syval && $checkforblock) {
14655:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
14656:                 if (@blockers) {
14657:                     $syval='';
14658:                 }
14659:             }
14660:         }
14661: # ---------------------------------------------------------- There was an entry
14662:         if ($syval) {
14663: 	    #unless ($syval=~/\_\d+$/) {
14664: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
14665: 		    #&appenv({'request.ambiguous' => $thisfn});
14666: 		    #return $env{$cache_str}='';
14667: 		#}    
14668: 		#$syval.=$1;
14669: 	    #}
14670:         } else {
14671: # ------------------------------------------------------- Was not in symb table
14672:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14673:                             &GDBM_READER(),0640)) {
14674: # ---------------------------------------------- Get ID(s) for current resource
14675:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
14676:               unless ($ids) { 
14677:                  $ids=$bighash{'ids_/'.$thisfn};
14678:               }
14679:               unless ($ids) {
14680: # alias?
14681: 		  $ids=$bighash{'mapalias_'.$thisfn};
14682:               }
14683:               if ($ids) {
14684: # ------------------------------------------------------------------- Has ID(s)
14685:                  my @possibilities=split(/\,/,$ids);
14686:                  if ($#possibilities==0) {
14687: # ----------------------------------------------- There is only one possibility
14688: 		     my ($mapid,$resid)=split(/\./,$ids);
14689: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
14690: 						    $resid,$thisfn);
14691:                      if (ref($possibles) eq 'HASH') {
14692:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
14693:                              $possibles->{$syval} = 1;
14694:                          }
14695:                      }
14696:                      if ($checkforblock) {
14697:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
14698:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
14699:                              if (@blockers) {
14700:                                  $syval = '';
14701:                                  untie(%bighash);
14702:                                  return $env{$cache_str}='';
14703:                              }
14704:                          }
14705:                      }
14706:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
14707: # ------------------------------------------ There is more than one possibility
14708:                      my $realpossible=0;
14709:                      foreach my $id (@possibilities) {
14710: 			 my $file=$bighash{'src_'.$id};
14711:                          my $canaccess;
14712:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
14713:                              $canaccess = 1;
14714:                          } else { 
14715:                              $canaccess = &allowed('bre',$file);
14716:                          }
14717:                          if ($canaccess) {
14718:          		     my ($mapid,$resid)=split(/\./,$id);
14719:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
14720:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
14721: 						             $resid,$thisfn);
14722:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
14723:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
14724:                                  if ($checkforblock) {
14725:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
14726:                                      if (@blockers > 0) {
14727:                                          $syval = '';
14728:                                      } else {
14729:                                          $syval = $poss_syval;
14730:                                          $realpossible++;
14731:                                      }
14732:                                  } else {
14733:                                      $syval = $poss_syval;
14734:                                      $realpossible++;
14735:                                  }
14736:                                  if ($syval) {
14737:                                      if (ref($possibles) eq 'HASH') {
14738:                                          $possibles->{$syval} = 1;
14739:                                      }
14740:                                  }
14741:                              }
14742: 			 }
14743:                      }
14744: 		     if ($realpossible!=1) { $syval=''; }
14745:                  } else {
14746:                      $syval='';
14747:                  }
14748: 	      }
14749:               untie(%bighash);
14750:            }
14751:         }
14752:         if ($syval) {
14753: 	    return $env{$cache_str}=$syval;
14754:         }
14755:     }
14756:     &appenv({'request.ambiguous' => $thisfn});
14757:     return $env{$cache_str}='';
14758: }
14759: 
14760: # ---------------------------------------------------------- Return random seed
14761: 
14762: sub numval {
14763:     my $txt=shift;
14764:     $txt=~tr/A-J/0-9/;
14765:     $txt=~tr/a-j/0-9/;
14766:     $txt=~tr/K-T/0-9/;
14767:     $txt=~tr/k-t/0-9/;
14768:     $txt=~tr/U-Z/0-5/;
14769:     $txt=~tr/u-z/0-5/;
14770:     $txt=~s/\D//g;
14771:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
14772:     return int($txt);
14773: }
14774: 
14775: sub numval2 {
14776:     my $txt=shift;
14777:     $txt=~tr/A-J/0-9/;
14778:     $txt=~tr/a-j/0-9/;
14779:     $txt=~tr/K-T/0-9/;
14780:     $txt=~tr/k-t/0-9/;
14781:     $txt=~tr/U-Z/0-5/;
14782:     $txt=~tr/u-z/0-5/;
14783:     $txt=~s/\D//g;
14784:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
14785:     my $total;
14786:     foreach my $val (@txts) { $total+=$val; }
14787:     if ($_64bit) { if ($total > 2**32) { return -1; } }
14788:     return int($total);
14789: }
14790: 
14791: sub numval3 {
14792:     use integer;
14793:     my $txt=shift;
14794:     $txt=~tr/A-J/0-9/;
14795:     $txt=~tr/a-j/0-9/;
14796:     $txt=~tr/K-T/0-9/;
14797:     $txt=~tr/k-t/0-9/;
14798:     $txt=~tr/U-Z/0-5/;
14799:     $txt=~tr/u-z/0-5/;
14800:     $txt=~s/\D//g;
14801:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
14802:     my $total;
14803:     foreach my $val (@txts) { $total+=$val; }
14804:     if ($_64bit) { $total=(($total<<32)>>32); }
14805:     return $total;
14806: }
14807: 
14808: sub digest {
14809:     my ($data)=@_;
14810:     my $digest=&Digest::MD5::md5($data);
14811:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
14812:     my ($e,$f);
14813:     {
14814:         use integer;
14815:         $e=($a+$b);
14816:         $f=($c+$d);
14817:         if ($_64bit) {
14818:             $e=(($e<<32)>>32);
14819:             $f=(($f<<32)>>32);
14820:         }
14821:     }
14822:     if (wantarray) {
14823: 	return ($e,$f);
14824:     } else {
14825: 	my $g;
14826: 	{
14827: 	    use integer;
14828: 	    $g=($e+$f);
14829: 	    if ($_64bit) {
14830: 		$g=(($g<<32)>>32);
14831: 	    }
14832: 	}
14833: 	return $g;
14834:     }
14835: }
14836: 
14837: sub latest_rnd_algorithm_id {
14838:     return '64bit5';
14839: }
14840: 
14841: sub get_rand_alg {
14842:     my ($courseid)=@_;
14843:     if (!$courseid) { $courseid=(&whichuser())[1]; }
14844:     if ($courseid) {
14845: 	return $env{"course.$courseid.rndseed"};
14846:     }
14847:     return &latest_rnd_algorithm_id();
14848: }
14849: 
14850: sub validCODE {
14851:     my ($CODE)=@_;
14852:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
14853:     return 0;
14854: }
14855: 
14856: sub getCODE {
14857:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
14858:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
14859: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
14860: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
14861: 	return $Apache::lonhomework::history{'resource.CODE'};
14862:     }
14863:     return undef;
14864: }
14865: #
14866: #  Determines the random seed for a specific context:
14867: #
14868: # parameters:
14869: #   symb      - in course context the symb for the seed.
14870: #   course_id - The course id of the form domain_coursenum.
14871: #   domain    - Domain for the user.
14872: #   course    - Course for the user.
14873: #   cenv      - environment of the course.
14874: #
14875: # NOTE:
14876: #   All parameters are picked out of the environment if missing
14877: #   or not defined.
14878: #   If a symb cannot be determined the current time is used instead.
14879: #
14880: #  For a given well defined symb, courside, domain, username,
14881: #  and course environment, the seed is reproducible.
14882: #
14883: sub rndseed {
14884:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
14885:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
14886:     if (!defined($symb)) {
14887: 	unless ($symb=$wsymb) { return time; }
14888:     }
14889:     if (!defined $courseid) { 
14890: 	$courseid=$wcourseid; 
14891:     }
14892:     if (!defined $domain) { $domain=$wdomain; }
14893:     if (!defined $username) { $username=$wusername }
14894: 
14895:     my $which;
14896:     if (defined($cenv->{'rndseed'})) {
14897: 	$which = $cenv->{'rndseed'};
14898:     } else {
14899: 	$which =&get_rand_alg($courseid);
14900:     }
14901:     if (defined(&getCODE())) {
14902: 
14903: 	if ($which eq '64bit5') {
14904: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14905: 	} elsif ($which eq '64bit4') {
14906: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14907: 	} else {
14908: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14909: 	}
14910:     } elsif ($which eq '64bit5') {
14911: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
14912:     } elsif ($which eq '64bit4') {
14913: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
14914:     } elsif ($which eq '64bit3') {
14915: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
14916:     } elsif ($which eq '64bit2') {
14917: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
14918:     } elsif ($which eq '64bit') {
14919: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
14920:     }
14921:     return &rndseed_32bit($symb,$courseid,$domain,$username);
14922: }
14923: 
14924: sub rndseed_32bit {
14925:     my ($symb,$courseid,$domain,$username)=@_;
14926:     {
14927: 	use integer;
14928: 	my $symbchck=unpack("%32C*",$symb) << 27;
14929: 	my $symbseed=numval($symb) << 22;
14930: 	my $namechck=unpack("%32C*",$username) << 17;
14931: 	my $nameseed=numval($username) << 12;
14932: 	my $domainseed=unpack("%32C*",$domain) << 7;
14933: 	my $courseseed=unpack("%32C*",$courseid);
14934: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
14935: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14936: 	#&logthis("rndseed :$num:$symb");
14937: 	if ($_64bit) { $num=(($num<<32)>>32); }
14938: 	return $num;
14939:     }
14940: }
14941: 
14942: sub rndseed_64bit {
14943:     my ($symb,$courseid,$domain,$username)=@_;
14944:     {
14945: 	use integer;
14946: 	my $symbchck=unpack("%32S*",$symb) << 21;
14947: 	my $symbseed=numval($symb) << 10;
14948: 	my $namechck=unpack("%32S*",$username);
14949: 	
14950: 	my $nameseed=numval($username) << 21;
14951: 	my $domainseed=unpack("%32S*",$domain) << 10;
14952: 	my $courseseed=unpack("%32S*",$courseid);
14953: 	
14954: 	my $num1=$symbchck+$symbseed+$namechck;
14955: 	my $num2=$nameseed+$domainseed+$courseseed;
14956: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14957: 	#&logthis("rndseed :$num:$symb");
14958: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14959: 	return "$num1,$num2";
14960:     }
14961: }
14962: 
14963: sub rndseed_64bit2 {
14964:     my ($symb,$courseid,$domain,$username)=@_;
14965:     {
14966: 	use integer;
14967: 	# strings need to be an even # of cahracters long, it it is odd the
14968:         # last characters gets thrown away
14969: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14970: 	my $symbseed=numval($symb) << 10;
14971: 	my $namechck=unpack("%32S*",$username.' ');
14972: 	
14973: 	my $nameseed=numval($username) << 21;
14974: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14975: 	my $courseseed=unpack("%32S*",$courseid.' ');
14976: 	
14977: 	my $num1=$symbchck+$symbseed+$namechck;
14978: 	my $num2=$nameseed+$domainseed+$courseseed;
14979: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14980: 	#&logthis("rndseed :$num:$symb");
14981: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14982: 	return "$num1,$num2";
14983:     }
14984: }
14985: 
14986: sub rndseed_64bit3 {
14987:     my ($symb,$courseid,$domain,$username)=@_;
14988:     {
14989: 	use integer;
14990: 	# strings need to be an even # of cahracters long, it it is odd the
14991:         # last characters gets thrown away
14992: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14993: 	my $symbseed=numval2($symb) << 10;
14994: 	my $namechck=unpack("%32S*",$username.' ');
14995: 	
14996: 	my $nameseed=numval2($username) << 21;
14997: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14998: 	my $courseseed=unpack("%32S*",$courseid.' ');
14999: 	
15000: 	my $num1=$symbchck+$symbseed+$namechck;
15001: 	my $num2=$nameseed+$domainseed+$courseseed;
15002: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
15003: 	#&logthis("rndseed :$num1:$num2:$_64bit");
15004: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
15005: 	
15006: 	return "$num1:$num2";
15007:     }
15008: }
15009: 
15010: sub rndseed_64bit4 {
15011:     my ($symb,$courseid,$domain,$username)=@_;
15012:     {
15013: 	use integer;
15014: 	# strings need to be an even # of cahracters long, it it is odd the
15015:         # last characters gets thrown away
15016: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
15017: 	my $symbseed=numval3($symb) << 10;
15018: 	my $namechck=unpack("%32S*",$username.' ');
15019: 	
15020: 	my $nameseed=numval3($username) << 21;
15021: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
15022: 	my $courseseed=unpack("%32S*",$courseid.' ');
15023: 	
15024: 	my $num1=$symbchck+$symbseed+$namechck;
15025: 	my $num2=$nameseed+$domainseed+$courseseed;
15026: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
15027: 	#&logthis("rndseed :$num1:$num2:$_64bit");
15028: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
15029: 	
15030: 	return "$num1:$num2";
15031:     }
15032: }
15033: 
15034: sub rndseed_64bit5 {
15035:     my ($symb,$courseid,$domain,$username)=@_;
15036:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
15037:     return "$num1:$num2";
15038: }
15039: 
15040: sub rndseed_CODE_64bit {
15041:     my ($symb,$courseid,$domain,$username)=@_;
15042:     {
15043: 	use integer;
15044: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
15045: 	my $symbseed=numval2($symb);
15046: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
15047: 	my $CODEseed=numval(&getCODE());
15048: 	my $courseseed=unpack("%32S*",$courseid.' ');
15049: 	my $num1=$symbseed+$CODEchck;
15050: 	my $num2=$CODEseed+$courseseed+$symbchck;
15051: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
15052: 	#&logthis("rndseed :$num1:$num2:$symb");
15053: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
15054: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
15055: 	return "$num1:$num2";
15056:     }
15057: }
15058: 
15059: sub rndseed_CODE_64bit4 {
15060:     my ($symb,$courseid,$domain,$username)=@_;
15061:     {
15062: 	use integer;
15063: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
15064: 	my $symbseed=numval3($symb);
15065: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
15066: 	my $CODEseed=numval3(&getCODE());
15067: 	my $courseseed=unpack("%32S*",$courseid.' ');
15068: 	my $num1=$symbseed+$CODEchck;
15069: 	my $num2=$CODEseed+$courseseed+$symbchck;
15070: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
15071: 	#&logthis("rndseed :$num1:$num2:$symb");
15072: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
15073: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
15074: 	return "$num1:$num2";
15075:     }
15076: }
15077: 
15078: sub rndseed_CODE_64bit5 {
15079:     my ($symb,$courseid,$domain,$username)=@_;
15080:     my $code = &getCODE();
15081:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
15082:     return "$num1:$num2";
15083: }
15084: 
15085: sub setup_random_from_rndseed {
15086:     my ($rndseed)=@_;
15087:     if ($rndseed =~/([,:])/) {
15088:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
15089:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
15090:             &Math::Random::random_set_seed_from_phrase($rndseed);
15091:         } else {
15092:             &Math::Random::random_set_seed($num1,$num2);
15093:         }
15094:     } else {
15095: 	&Math::Random::random_set_seed_from_phrase($rndseed);
15096:     }
15097: }
15098: 
15099: sub latest_receipt_algorithm_id {
15100:     return 'receipt3';
15101: }
15102: 
15103: sub recunique {
15104:     my $fucourseid=shift;
15105:     my $unique;
15106:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
15107: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
15108: 	$unique=$env{"course.$fucourseid.internal.encseed"};
15109:     } else {
15110: 	$unique=$perlvar{'lonReceipt'};
15111:     }
15112:     return unpack("%32C*",$unique);
15113: }
15114: 
15115: sub recprefix {
15116:     my $fucourseid=shift;
15117:     my $prefix;
15118:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
15119: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
15120: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
15121:     } else {
15122: 	$prefix=$perlvar{'lonHostID'};
15123:     }
15124:     return unpack("%32C*",$prefix);
15125: }
15126: 
15127: sub ireceipt {
15128:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
15129: 
15130:     my $return =&recprefix($fucourseid).'-';
15131: 
15132:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
15133: 	$env{'request.state'} eq 'construct') {
15134: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
15135: 	return $return;
15136:     }
15137: 
15138:     my $cuname=unpack("%32C*",$funame);
15139:     my $cudom=unpack("%32C*",$fudom);
15140:     my $cucourseid=unpack("%32C*",$fucourseid);
15141:     my $cusymb=unpack("%32C*",$fusymb);
15142:     my $cunique=&recunique($fucourseid);
15143:     my $cpart=unpack("%32S*",$part);
15144:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
15145: 
15146: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
15147: 			       
15148: 	$return.= ($cunique%$cuname+
15149: 		   $cunique%$cudom+
15150: 		   $cusymb%$cuname+
15151: 		   $cusymb%$cudom+
15152: 		   $cucourseid%$cuname+
15153: 		   $cucourseid%$cudom+
15154: 		   $cpart%$cuname+
15155: 		   $cpart%$cudom);
15156:     } else {
15157: 	$return.= ($cunique%$cuname+
15158: 		   $cunique%$cudom+
15159: 		   $cusymb%$cuname+
15160: 		   $cusymb%$cudom+
15161: 		   $cucourseid%$cuname+
15162: 		   $cucourseid%$cudom);
15163:     }
15164:     return $return;
15165: }
15166: 
15167: sub receipt {
15168:     my ($part)=@_;
15169:     my ($symb,$courseid,$domain,$name) = &whichuser();
15170:     return &ireceipt($name,$domain,$courseid,$symb,$part);
15171: }
15172: 
15173: sub whichuser {
15174:     my ($passedsymb,$ignorecachednull)=@_;
15175:     my ($symb,$courseid,$domain,$name,$publicuser);
15176:     if (defined($env{'form.grade_symb'})) {
15177: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
15178: 	my $allowed=&allowed('vgr',$tmp_courseid);
15179: 	if (!$allowed &&
15180: 	    exists($env{'request.course.sec'}) &&
15181: 	    $env{'request.course.sec'} !~ /^\s*$/) {
15182: 	    $allowed=&allowed('vgr',$tmp_courseid.
15183: 			      '/'.$env{'request.course.sec'});
15184: 	}
15185: 	if ($allowed) {
15186: 	    ($symb)=&get_env_multiple('form.grade_symb');
15187: 	    $courseid=$tmp_courseid;
15188: 	    ($domain)=&get_env_multiple('form.grade_domain');
15189: 	    ($name)=&get_env_multiple('form.grade_username');
15190:             if ($name eq 'public' && $domain eq 'public') {
15191:                 $publicuser = 1;
15192:             }
15193: 	    return ($symb,$courseid,$domain,$name,$publicuser);
15194: 	}
15195:     }
15196:     if (!$passedsymb) {
15197: 	$symb=&symbread('','',$ignorecachednull);
15198:     } else {
15199: 	$symb=$passedsymb;
15200:     }
15201:     $courseid=$env{'request.course.id'};
15202:     $domain=$env{'user.domain'};
15203:     $name=$env{'user.name'};
15204:     if ($name eq 'public' && $domain eq 'public') {
15205: 	if (!defined($env{'form.username'})) {
15206: 	    $env{'form.username'}.=time.rand(10000000);
15207: 	}
15208: 	$name.=$env{'form.username'};
15209:         $publicuser = 1;
15210:     }
15211:     return ($symb,$courseid,$domain,$name,$publicuser);
15212: 
15213: }
15214: 
15215: # ------------------------------------------------------------ Serves up a file
15216: # returns either the contents of the file or 
15217: # -1 if the file doesn't exist
15218: #
15219: # if the target is a file that was uploaded via DOCS, 
15220: # a check will be made to see if a current copy exists on the local server,
15221: # if it does this will be served, otherwise a copy will be retrieved from
15222: # the home server for the course and stored in /home/httpd/html/userfiles on
15223: # the local server.   
15224: 
15225: sub getfile {
15226:     my ($file) = @_;
15227:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
15228:     &repcopy($file);
15229:     return &readfile($file);
15230: }
15231: 
15232: sub repcopy_userfile {
15233:     my ($file)=@_;
15234:     my $londocroot = $perlvar{'lonDocRoot'};
15235:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
15236:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
15237:     my ($cdom,$cnum,$filename) = 
15238: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
15239:     my $uri="/uploaded/$cdom/$cnum/$filename";
15240:     if (-e "$file") {
15241: # we already have a local copy, check it out
15242: 	my @fileinfo = stat($file);
15243: 	my $rtncode;
15244: 	my $info;
15245: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
15246: 	if ($lwpresp ne 'ok') {
15247: # there is no such file anymore, even though we had a local copy
15248: 	    if ($rtncode eq '404') {
15249: 		unlink($file);
15250: 	    }
15251: 	    return -1;
15252: 	}
15253: 	if ($info < $fileinfo[9]) {
15254: # nice, the file we have is up-to-date, just say okay
15255: 	    return 'ok';
15256: 	} else {
15257: # the file is outdated, get rid of it
15258: 	    unlink($file);
15259: 	}
15260:     }
15261: # one way or the other, at this point, we don't have the file
15262: # construct the correct path for the file
15263:     my @parts = ($cdom,$cnum); 
15264:     if ($filename =~ m|^(.+)/[^/]+$|) {
15265: 	push @parts, split(/\//,$1);
15266:     }
15267:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
15268:     foreach my $part (@parts) {
15269: 	$path .= '/'.$part;
15270: 	if (!-e $path) {
15271: 	    mkdir($path,0770);
15272: 	}
15273:     }
15274: # now the path exists for sure
15275: # get a user agent
15276:     my $transferfile=$file.'.in.transfer';
15277: # FIXME: this should flock
15278:     if (-e $transferfile) { return 'ok'; }
15279:     my $request;
15280:     $uri=~s/^\///;
15281:     my $homeserver = &homeserver($cnum,$cdom);
15282:     my $hostname = &hostname($homeserver);
15283:     my $protocol = $protocol{$homeserver};
15284:     $protocol = 'http' if ($protocol ne 'https');
15285:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
15286:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
15287: # did it work?
15288:     if ($response->is_error()) {
15289: 	unlink($transferfile);
15290: 	&logthis("Userfile repcopy failed for $uri");
15291: 	return -1;
15292:     }
15293: # worked, rename the transfer file
15294:     rename($transferfile,$file);
15295:     return 'ok';
15296: }
15297: 
15298: sub repcopy_crsprivfile {
15299:     my ($src,$dest) = @_;
15300:     my $result;
15301:     if ($src =~ m{^/priv/($match_domain)/($match_courseid)/(.+)$}) {
15302:         my ($cdom,$cnum,$filepath) = ($1,$2,$3);
15303:         $filepath =~ s/\.{2,}//g;
15304:         my $chome = &homeserver($cnum,$cdom);
15305:         unless ($chome eq 'no_host') {
15306:             my @ids=&current_machine_ids();
15307:             unless (grep(/^\Q$chome\E$/,@ids)) {
15308:                 if (&is_course($cdom,$cnum)) {
15309:                     my $londocroot = $perlvar{'lonDocRoot'};
15310:                     if ($dest =~ m{^\Q$londocroot/priv/\E$match_domain/$match_username/.*\Q$filepath\E$}) {
15311:                         my $cmd = 'crsfilefrompriv:'.&escape($filepath).':'.&escape($cnum).':'.&escape($cdom);
15312:                         $result = &reply($cmd,$chome);
15313:                         unless (($result eq 'unknown_cmd') || ($result =~ /^error:/)) {
15314:                             my $url = &unescape($result);
15315:                             if ($url =~ m{^https?://[^/]+\Q/userfiles/$cdom/$cnum/priv/$filepath\E$}) {
15316:                                 my $request=new HTTP::Request('GET',$url);
15317:                                 my $response=&LONCAPA::LWPReq::makerequest($chome,$request,'',\%perlvar,1200,1);
15318:                                 if ($response->is_error()) {
15319:                                     $result = 'error: '.$response->status_line;
15320:                                 } else {
15321:                                     if (open(my $fh,'>',$dest)) {
15322:                                         print $fh $response->content;
15323:                                         close($fh);
15324:                                         $result = 'ok';
15325:                                     } else {
15326:                                         $result = 'error: nowrite';
15327:                                     }
15328:                                 }
15329:                             } else {
15330:                                 $result = 'error: invalidurl';
15331:                             }
15332:                         }
15333:                     }
15334:                 }
15335:             }
15336:         }
15337:     }
15338:     return $result;
15339: }
15340: 
15341: sub tokenwrapper {
15342:     my $uri=shift;
15343:     $uri=~s|^https?\://([^/]+)||;
15344:     $uri=~s|^/||;
15345:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
15346:     my $token=$1;
15347:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
15348:     if ($udom && $uname && $file) {
15349: 	$file=~s|(\?\.*)*$||;
15350:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
15351:         my $homeserver = &homeserver($uname,$udom);
15352:         my $hostname = &hostname($homeserver);
15353:         my $protocol = $protocol{$homeserver};
15354:         $protocol = 'http' if ($protocol ne 'https');
15355:         return $protocol.'://'.$hostname.'/'.$uri.
15356:                (($uri=~/\?/)?'&':'?').'token='.$token.
15357:                                '&tokenissued='.$perlvar{'lonHostID'};
15358:     } else {
15359:         return '/adm/notfound.html';
15360:     }
15361: }
15362: 
15363: # call with reqtype HEAD: get last modification time
15364: # call with reqtype GET: get the file contents
15365: # Do not call this with reqtype GET for large files! It loads everything into memory
15366: #
15367: sub getuploaded {
15368:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
15369:     $uri=~s/^\///;
15370:     my $homeserver = &homeserver($cnum,$cdom);
15371:     my $hostname = &hostname($homeserver);
15372:     my $protocol = $protocol{$homeserver};
15373:     $protocol = 'http' if ($protocol ne 'https');
15374:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
15375:     my $request=new HTTP::Request($reqtype,$uri);
15376:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
15377:     $$rtncode = $response->code;
15378:     if (! $response->is_success()) {
15379: 	return 'failed';
15380:     }      
15381:     if ($reqtype eq 'HEAD') {
15382: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
15383:     } elsif ($reqtype eq 'GET') {
15384: 	$$info = $response->content;
15385:     }
15386:     return 'ok';
15387: }
15388: 
15389: sub readfile {
15390:     my $file = shift;
15391:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
15392:     my $fh;
15393:     open($fh,"<",$file);
15394:     my $a='';
15395:     while (my $line = <$fh>) { $a .= $line; }
15396:     return $a;
15397: }
15398: 
15399: sub filelocation {
15400:     my ($dir,$file) = @_;
15401:     my $location;
15402:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
15403: 
15404:     if ($file =~ m-^/adm/-) {
15405: 	$file=~s-^/adm/wrapper/-/-;
15406: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
15407:     }
15408: 
15409:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
15410:         $location = $file;
15411:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
15412:         my ($udom,$uname,$filename)=
15413:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
15414:         my $home=&homeserver($uname,$udom);
15415:         my $is_me=0;
15416:         my @ids=&current_machine_ids();
15417:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
15418:         if ($is_me) {
15419:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
15420:         } else {
15421:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
15422:   	      $udom.'/'.$uname.'/'.$filename;
15423:         }
15424:     } elsif ($file =~ m-^/adm/-) {
15425: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
15426:     } else {
15427:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
15428:         $file=~s:^/(res|priv)/:/:;
15429:         my $space=$1;
15430:         if ( !( $file =~ m:^/:) ) {
15431:             $location = $dir. '/'.$file;
15432:         } else {
15433:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
15434:         }
15435:     }
15436:     $location=~s://+:/:g; # remove duplicate /
15437:     while ($location=~m{/\.\./}) {
15438: 	if ($location =~ m{/[^/]+/\.\./}) {
15439: 	    $location=~ s{/[^/]+/\.\./}{/}g;
15440: 	} else {
15441: 	    $location=~ s{/\.\./}{/}g;
15442: 	}
15443:     } #remove dir/..
15444:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
15445:     return $location;
15446: }
15447: 
15448: sub hreflocation {
15449:     my ($dir,$file)=@_;
15450:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
15451: 	$file=filelocation($dir,$file);
15452:     } elsif ($file=~m-^/adm/-) {
15453: 	$file=~s-^/adm/wrapper/-/-;
15454: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
15455:     }
15456:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
15457: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
15458:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
15459: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
15460: 	        {/uploaded/$1/$2/}x;
15461:     }
15462:     if ($file=~ m{^/userfiles/}) {
15463: 	$file =~ s{^/userfiles/}{/uploaded/};
15464:     }
15465:     return $file;
15466: }
15467: 
15468: 
15469: 
15470: 
15471: 
15472: sub current_machine_domains {
15473:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
15474: }
15475: 
15476: sub machine_domains {
15477:     my ($hostname) = @_;
15478:     my @domains;
15479:     my %hostname = &all_hostnames();
15480:     while( my($id, $name) = each(%hostname)) {
15481: #	&logthis("-$id-$name-$hostname-");
15482: 	if ($hostname eq $name) {
15483: 	    push(@domains,&host_domain($id));
15484: 	}
15485:     }
15486:     return @domains;
15487: }
15488: 
15489: sub current_machine_ids {
15490:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
15491: }
15492: 
15493: sub machine_ids {
15494:     my ($hostname) = @_;
15495:     $hostname ||= &hostname($perlvar{'lonHostID'});
15496:     my @ids;
15497:     my %name_to_host = &all_names();
15498:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
15499: 	return @{ $name_to_host{$hostname} };
15500:     }
15501:     return;
15502: }
15503: 
15504: sub additional_machine_domains {
15505:     my @domains;
15506:     if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
15507:         if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
15508:             while (my $line = <$fh>) {
15509:                 chomp($line);           
15510:                 $line =~ s/\s//g;
15511:                 push(@domains,$line);
15512:             }
15513:             close($fh);
15514:         }
15515:     }
15516:     return @domains;
15517: }
15518: 
15519: sub default_login_domain {
15520:     my $domain = $perlvar{'lonDefDomain'};
15521:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
15522:     foreach my $posdom (&current_machine_domains(),
15523:                         &additional_machine_domains()) {
15524:         if (lc($posdom) eq lc($testdomain)) {
15525:             $domain=$posdom;
15526:             last;
15527:         }
15528:     }
15529:     return $domain;
15530: }
15531: 
15532: sub shared_institution {
15533:     my ($dom,$lonhost) = @_;
15534:     if ($lonhost eq '') {
15535:         $lonhost = $perlvar{'lonHostID'};
15536:     }
15537:     my $same_intdom;
15538:     my $hostintdom = &internet_dom($lonhost);
15539:     if ($hostintdom ne '') {
15540:         my %iphost = &get_iphost();
15541:         my $primary_id = &domain($dom,'primary');
15542:         my $primary_ip = &get_host_ip($primary_id);
15543:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
15544:             foreach my $id (@{$iphost{$primary_ip}}) {
15545:                 my $intdom = &internet_dom($id);
15546:                 if ($intdom eq $hostintdom) {
15547:                     $same_intdom = 1;
15548:                     last;
15549:                 }
15550:             }
15551:         }
15552:     }
15553:     return $same_intdom;
15554: }
15555: 
15556: sub uses_sts {
15557:     my ($ignore_cache) = @_;
15558:     my $lonhost = $perlvar{'lonHostID'};
15559:     my $hostname = &hostname($lonhost);
15560:     my $sts_on;
15561:     if ($protocol{$lonhost} eq 'https') {
15562:         my $cachetime = 12*3600;
15563:         if (!$ignore_cache) {
15564:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
15565:             if (defined($cached)) {
15566:                 return $sts_on;
15567:             }
15568:         }
15569:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
15570:         my $request=new HTTP::Request('HEAD',$url);
15571:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
15572:         if ($response->is_success) {
15573:             my $has_sts = $response->header('Strict-Transport-Security');
15574:             if ($has_sts eq '') {
15575:                 $sts_on = 0;
15576:             } else {
15577:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
15578:                     my $maxage = $1;
15579:                     if ($maxage) {
15580:                         $sts_on = 1;
15581:                     } else {
15582:                         $sts_on = 0;
15583:                     }
15584:                 } else {
15585:                     $sts_on = 0;
15586:                 }
15587:             }
15588:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
15589:         }
15590:     }
15591:     return;
15592: }
15593: 
15594: sub waf_allssl {
15595:     my ($host_name) = @_;
15596:     my $alias = &get_proxy_alias();
15597:     if ($host_name eq '') {
15598:         $host_name = $ENV{'SERVER_NAME'};
15599:     }
15600:     if (($host_name ne '') && ($alias eq $host_name)) {
15601:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
15602:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
15603:         if ($defdomdefaults{'waf_sslopt'}) {
15604:             return $defdomdefaults{'waf_sslopt'};
15605:         }
15606:     }
15607:     return;
15608: }
15609: 
15610: sub get_requestor_ip {
15611:     my ($r,$nolookup,$noproxy) = @_;
15612:     my $from_ip;
15613:     if (ref($r)) {
15614:         if ($r->can('useragent_ip')) {
15615:             if ($noproxy && $r->can('client_ip')) {
15616:                 $from_ip = $r->client_ip();
15617:             } else {
15618:                 $from_ip = $r->useragent_ip();
15619:             }
15620:         } elsif ($r->connection->can('remote_ip')) {
15621:             $from_ip = $r->connection->remote_ip();
15622:         } else {
15623:             $from_ip = $r->get_remote_host($nolookup);
15624:         }
15625:     } else {
15626:         $from_ip = $ENV{'REMOTE_ADDR'};
15627:     }
15628:     return $from_ip if ($noproxy); 
15629:     # Who controls proxy settings for server
15630:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
15631:     my $proxyinfo = &get_proxy_settings($dom_in_use);
15632:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
15633:         if ($proxyinfo->{'vpnint'}) {
15634:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
15635:                 return $from_ip;
15636:             }
15637:         }
15638:         if ($proxyinfo->{'trusted'}) {
15639:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
15640:                 my $ipheader = $proxyinfo->{'ipheader'};
15641:                 my ($ip,$xfor);
15642:                 if (ref($r)) {
15643:                     if ($ipheader) {
15644:                         $ip = $r->headers_in->{$ipheader};
15645:                     }
15646:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
15647:                 } else {
15648:                     if ($ipheader) {
15649:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
15650:                     }
15651:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
15652:                 }
15653:                 if (($ip eq '') && ($xfor ne '')) {
15654:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
15655:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
15656:                             $ip = $poss_ip;
15657:                             last;
15658:                         }
15659:                     }
15660:                 }
15661:                 if ($ip ne '') {
15662:                     return $ip;
15663:                 }
15664:             }
15665:         }
15666:     }
15667:     return $from_ip;
15668: }
15669: 
15670: sub get_proxy_settings {
15671:     my ($dom_in_use) = @_;
15672:     my %domdefaults = &get_domain_defaults($dom_in_use);
15673:     my $proxyinfo = {
15674:                        ipheader => $domdefaults{'waf_ipheader'},
15675:                        trusted  => $domdefaults{'waf_trusted'},
15676:                        vpnint   => $domdefaults{'waf_vpnint'},
15677:                        vpnext   => $domdefaults{'waf_vpnext'},
15678:                        sslopt   => $domdefaults{'waf_sslopt'},
15679:                     };
15680:     return $proxyinfo;
15681: }
15682: 
15683: sub ip_match {
15684:     my ($ip,$pattern_str) = @_;
15685:     $ip=Net::CIDR::cidrvalidate($ip);
15686:     if ($ip) {
15687:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
15688:     }
15689:     return;
15690: }
15691: 
15692: sub get_proxy_alias {
15693:     my ($lonid) = @_;
15694:     if ($lonid eq '') {
15695:         $lonid = $perlvar{'lonHostID'};
15696:     }
15697:     if (!defined(&hostname($lonid))) {
15698:         return;
15699:     }
15700:     if ($lonid ne '') {
15701:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
15702:         if ($cached) {
15703:             return $alias;
15704:         }
15705:         my $dom = &host_domain($lonid);
15706:         if ($dom ne '') {
15707:             my $cachetime = 60*60*24;
15708:             my %domconfig =
15709:                 &get_dom('configuration',['wafproxy'],$dom);
15710:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
15711:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
15712:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
15713:                 }
15714:             }
15715:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
15716:         }
15717:     }
15718:     return;
15719: }
15720: 
15721: sub use_proxy_alias {
15722:     my ($r,$lonid) = @_;
15723:     my $alias = &get_proxy_alias($lonid);
15724:     if ($alias) {
15725:         my $dom = &host_domain($lonid);
15726:         if ($dom ne '') {
15727:             my $proxyinfo = &get_proxy_settings($dom);
15728:             my ($vpnint,$remote_ip);
15729:             if (ref($proxyinfo) eq 'HASH') {
15730:                 $vpnint = $proxyinfo->{'vpnint'};
15731:                 if ($vpnint) {
15732:                     $remote_ip = &get_requestor_ip($r,1,1);
15733:                 }
15734:             }
15735:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
15736:                 return $alias;
15737:             }
15738:         }
15739:     }
15740:     return;
15741: }
15742: 
15743: sub alias_sso {
15744:     my ($lonid) = @_;
15745:     if ($lonid eq '') {
15746:         $lonid = $perlvar{'lonHostID'};
15747:     }
15748:     if (!defined(&hostname($lonid))) {
15749:         return;
15750:     }
15751:     if ($lonid ne '') {
15752:         my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
15753:         if ($cached) {
15754:             return $use_alias;
15755:         }
15756:         my $dom = &host_domain($lonid);
15757:         if ($dom ne '') {
15758:             my $cachetime = 60*60*24;
15759:             my %domconfig =
15760:                 &get_dom('configuration',['wafproxy'],$dom);
15761:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
15762:                 if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
15763:                     $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
15764:                 }
15765:             }
15766:             return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
15767:         }
15768:     }
15769:     return;
15770: }
15771: 
15772: sub get_saml_landing {
15773:     my ($lonid) = @_;
15774:     if ($lonid eq '') {
15775:         my $defdom = &default_login_domain();
15776:         my @hosts = &current_machine_ids();
15777:         if (@hosts > 1) {
15778:             foreach my $hostid (@hosts) {
15779:                 if (&host_domain($hostid) eq $defdom) {
15780:                     $lonid = $hostid;
15781:                     last;
15782:                 }
15783:             }
15784:         } else {
15785:             $lonid = $perlvar{'lonHostID'};
15786:         }
15787:         if ($lonid) {
15788:             unless (&host_domain($lonid) eq $defdom) {
15789:                 return;
15790:             }
15791:         } else {
15792:             return;
15793:         }
15794:     } elsif (!defined(&hostname($lonid))) {
15795:         return;
15796:     }
15797:     my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
15798:     if ($cached) {
15799:         return $landing;
15800:     }
15801:     my $dom = &host_domain($lonid);
15802:     if ($dom ne '') {
15803:         my $cachetime = 60*60*24;
15804:         my %domconfig =
15805:             &get_dom('configuration',['login'],$dom);
15806:         if (ref($domconfig{'login'}) eq 'HASH') {
15807:             if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
15808:                 if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
15809:                     $landing = 1;
15810:                 }
15811:             }
15812:         }
15813:         return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
15814:     }
15815:     return;
15816: }
15817: 
15818: # ------------------------------------------------------------- Declutters URLs
15819: 
15820: sub declutter {
15821:     my $thisfn=shift;
15822:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
15823:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
15824:         $thisfn=~s{^/home/httpd/html}{};
15825:     }
15826:     $thisfn=~s/^\///;
15827:     $thisfn=~s|^adm/wrapper/||;
15828:     $thisfn=~s|^adm/coursedocs/showdoc/||;
15829:     $thisfn=~s/^res\///;
15830:     $thisfn=~s/^priv\///;
15831:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
15832:         $thisfn=~s/\?.+$//;
15833:     }
15834:     return $thisfn;
15835: }
15836: 
15837: # ------------------------------------------------------------- Clutter up URLs
15838: 
15839: sub clutter {
15840:     my $thisfn='/'.&declutter(shift);
15841:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
15842: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
15843:        $thisfn='/res'.$thisfn; 
15844:     }
15845:     if ($thisfn !~m|^/adm|) {
15846: 	if ($thisfn =~ m|^/ext/|) {
15847: 	    $thisfn='/adm/wrapper'.$thisfn;
15848: 	} else {
15849: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
15850: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
15851: 	    if ($embstyle eq 'ssi'
15852: 		|| ($embstyle eq 'hdn')
15853: 		|| ($embstyle eq 'rat')
15854: 		|| ($embstyle eq 'prv')
15855: 		|| ($embstyle eq 'ign')) {
15856: 		#do nothing with these
15857: 	    } elsif (($embstyle eq 'img') 
15858: 		|| ($embstyle eq 'emb')
15859: 		|| ($embstyle eq 'wrp')) {
15860: 		$thisfn='/adm/wrapper'.$thisfn;
15861: 	    } elsif ($embstyle eq 'unk'
15862: 		     && $thisfn!~/\.(sequence|page)$/) {
15863: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
15864: 	    } else {
15865: #		&logthis("Got a blank emb style");
15866: 	    }
15867: 	}
15868:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
15869:         $thisfn='/adm/wrapper'.$thisfn;
15870:     }
15871:     return $thisfn;
15872: }
15873: 
15874: sub clutter_with_no_wrapper {
15875:     my $uri = &clutter(shift);
15876:     if ($uri =~ m-^/adm/-) {
15877: 	$uri =~ s-^/adm/wrapper/-/-;
15878: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
15879:     }
15880:     return $uri;
15881: }
15882: 
15883: sub freeze_escape {
15884:     my ($value)=@_;
15885:     if (ref($value)) {
15886: 	$value=&nfreeze($value);
15887: 	return '__FROZEN__'.&escape($value);
15888:     }
15889:     return &escape($value);
15890: }
15891: 
15892: 
15893: sub thaw_unescape {
15894:     my ($value)=@_;
15895:     if ($value =~ /^__FROZEN__/) {
15896: 	substr($value,0,10,undef);
15897: 	$value=&unescape($value);
15898: 	return &thaw($value);
15899:     }
15900:     return &unescape($value);
15901: }
15902: 
15903: sub correct_line_ends {
15904:     my ($result)=@_;
15905:     $$result =~s/\r\n/\n/mg;
15906:     $$result =~s/\r/\n/mg;
15907: }
15908: # ================================================================ Main Program
15909: 
15910: sub goodbye {
15911:    &logthis("Starting Shut down");
15912: #not converted to using infrastruture and probably shouldn't be
15913:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
15914: #converted
15915: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
15916:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
15917: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
15918: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
15919: #1.1 only
15920: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
15921: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
15922: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
15923: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
15924:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
15925:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
15926:    &logthis(sprintf("%-20s is %s",'hits',$hits));
15927:    &flushcourselogs();
15928:    &logthis("Shutting down");
15929: }
15930: 
15931: sub get_dns {
15932:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
15933:     if (!$ignore_cache) {
15934: 	my ($content,$cached)=
15935: 	    &is_cached_new('dns',$url);
15936: 	if ($cached) {
15937: 	    &$func($content,$hashref);
15938: 	    return;
15939: 	}
15940:     }
15941: 
15942:     my %alldns;
15943:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
15944:         foreach my $dns (<$config>) {
15945: 	    next if ($dns !~ /^\^(\S*)/x);
15946:             my $line = $1;
15947:             my ($host,$protocol) = split(/:/,$line);
15948:             if ($protocol ne 'https') {
15949:                 $protocol = 'http';
15950:             }
15951: 	    $alldns{$host} = $protocol;
15952:         }
15953:         close($config);
15954:     }
15955:     while (%alldns) {
15956: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
15957:         my ($contents,@content);
15958:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15959:             my $command = (split('/',$url))[3];
15960:             my ($dir,$file) = &parse_getdns_url($command,$url);
15961:             delete($alldns{$dns});
15962:             next if (($dir eq '') || ($file eq ''));
15963:             if (open(my $config,'<',"$dir/$file")) {
15964:                 @content = <$config>;
15965:                 close($config);
15966:             }
15967:             if ($url eq '/adm/dns/loncapaCRL') {
15968:                 $contents = join('',@content);
15969:             }
15970:         } else {
15971: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15972:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
15973:             delete($alldns{$dns});
15974: 	    next if ($response->is_error());
15975:             if ($url eq '/adm/dns/loncapaCRL') {
15976:                 $contents = $response->content;
15977:             } else {
15978:                 @content = split("\n",$response->content);
15979:             }
15980:         }
15981:         if ($url eq '/adm/dns/loncapaCRL') {
15982:             return &$func($contents);
15983:         } else {
15984: 	    unless ($nocache) {
15985: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
15986: 	    }
15987: 	    &$func(\@content,$hashref);
15988:             return;
15989:         }
15990:     }
15991:     my $which = (split('/',$url,4))[3];
15992:     if ($which eq 'loncapaCRL') {
15993:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
15994:         if (-e $diskfile) {
15995:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
15996:         } else {
15997:             &logthis("unable to contact DNS, no on disk file $diskfile available");
15998:         }
15999:     } else {
16000:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
16001:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
16002:             my @content = <$config>;
16003:             close($config);
16004:             &$func(\@content,$hashref);
16005:         }
16006:     }
16007:     return;
16008: }
16009: 
16010: # ------------------------------------------------------Get DNS checksums file
16011: sub parse_dns_checksums_tab {
16012:     my ($lines,$hashref) = @_;
16013:     my $lonhost = $perlvar{'lonHostID'};
16014:     my $machine_dom = &host_domain($lonhost);
16015:     my $loncaparev = &get_server_loncaparev($machine_dom);
16016:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
16017:     my $webconfdir = '/etc/httpd/conf';
16018:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
16019:         $webconfdir = '/etc/apache2';
16020:     } elsif ($distro =~ /^sles(\d+)$/) {
16021:         if ($1 >= 10) {
16022:             $webconfdir = '/etc/apache2';
16023:         }
16024:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
16025:         if ($1 >= 10.0) {
16026:             $webconfdir = '/etc/apache2';
16027:         }
16028:     }
16029:     my ($release,$timestamp) = split(/\-/,$loncaparev);
16030:     my (%chksum,%revnum);
16031:     if (ref($lines) eq 'ARRAY') {
16032:         chomp(@{$lines});
16033:         my $version = shift(@{$lines});
16034:         if ($version eq $release) {  
16035:             foreach my $line (@{$lines}) {
16036:                 my ($file,$version,$shasum) = split(/,/,$line);
16037:                 if ($file =~ m{^/etc/httpd/conf}) {
16038:                     if ($webconfdir eq '/etc/apache2') {
16039:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
16040:                     }
16041:                 }
16042:                 $chksum{$file} = $shasum;
16043:                 $revnum{$file} = $version;
16044:             }
16045:             if (ref($hashref) eq 'HASH') {
16046:                 %{$hashref} = (
16047:                                 sums     => \%chksum,
16048:                                 versions => \%revnum,
16049:                               );
16050:             }
16051:         }
16052:     }
16053:     return;
16054: }
16055: 
16056: sub fetch_dns_checksums {
16057:     my %checksums;
16058:     my $machine_dom = &host_domain($perlvar{'lonHostID'});
16059:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
16060:     my ($release,$timestamp) = split(/\-/,$loncaparev);
16061:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
16062:              \%checksums);
16063:     return \%checksums;
16064: }
16065: 
16066: sub fetch_crl_pemfile {
16067:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
16068: }
16069: 
16070: sub save_crl_pem {
16071:     my ($content) = @_;
16072:     my ($msg,$hadchanges);
16073:     if ($content ne '') {
16074:         my $now = time;
16075:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
16076:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
16077:         if (open(my $fh,'>',"$tmpcrl")) {
16078:             print $fh $content;
16079:             close($fh);
16080:             if (-e $lonca) {
16081:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
16082:                     my $check = <PIPE>;
16083:                     close(PIPE);
16084:                     chomp($check);
16085:                     if ($check eq 'verify OK') {
16086:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
16087:                         my $backup;
16088:                         if (-e $dest) {
16089:                             if (&File::Copy::move($dest,"$dest.bak")) {
16090:                                 $backup = 'ok';
16091:                             }
16092:                         }
16093:                         if (&File::Copy::move($tmpcrl,$dest)) {
16094:                             $msg = 'ok';
16095:                             if ($backup) {
16096:                                 my (%oldnums,%newnums);
16097:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
16098:                                     while (<PIPE>) {
16099:                                         $oldnums{(split(/:/))[1]} = 1;
16100:                                     }
16101:                                     close(PIPE);
16102:                                 }
16103:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
16104:                                     while(<PIPE>) {
16105:                                         $newnums{(split(/:/))[1]} = 1;
16106:                                     }
16107:                                     close(PIPE);
16108:                                 }
16109:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
16110:                                     unless (exists($oldnums{$key})) {
16111:                                         $hadchanges = 1;
16112:                                         last;
16113:                                     }
16114:                                 }
16115:                                 unless ($hadchanges) {
16116:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
16117:                                         unless (exists($newnums{$key})) {
16118:                                             $hadchanges = 1;
16119:                                             last;
16120:                                         }
16121:                                     }
16122:                                 }
16123:                             }
16124:                         }
16125:                     } else {
16126:                         unlink($tmpcrl);
16127:                     }
16128:                 } else {
16129:                     unlink($tmpcrl);
16130:                 }
16131:             } else {
16132:                 unlink($tmpcrl);
16133:             }
16134:         }
16135:     }
16136:     return ($msg,$hadchanges);
16137: }
16138: 
16139: sub parse_getdns_url {
16140:     my ($command,$url) = @_;
16141:     my $dir = $perlvar{'lonTabDir'};
16142:     my $file;
16143:     if ($command eq 'hosts') {
16144:         $file = 'dns_hosts.tab';
16145:     } elsif ($command eq 'domain') {
16146:         $file = 'dns_domain.tab';
16147:     } elsif ($command eq 'checksums') {
16148:         my $version = (split('/',$url))[4];
16149:         $file = "dns_checksums/$version.tab",
16150:     } elsif ($command eq 'loncapaCRL') {
16151:         $dir = $perlvar{'lonCertificateDirectory'};
16152:         $file = $perlvar{'lonnetCertRevocationList'};
16153:     }
16154:     return ($dir,$file);
16155: }
16156: 
16157: # ------------------------------------------------------------ Read domain file
16158: {
16159:     my $loaded;
16160:     my %domain;
16161: 
16162:     sub parse_domain_tab {
16163: 	my ($lines) = @_;
16164: 	foreach my $line (@$lines) {
16165: 	    next if ($line =~ /^(\#|\s*$ )/x);
16166: 
16167: 	    chomp($line);
16168: 	    my ($name,@elements) = split(/:/,$line,9);
16169: 	    my %this_domain;
16170: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
16171: 			       'lang_def', 'city', 'longi', 'lati',
16172: 			       'primary') {
16173: 		$this_domain{$field} = shift(@elements);
16174: 	    }
16175: 	    $domain{$name} = \%this_domain;
16176: 	}
16177:     }
16178: 
16179:     sub reset_domain_info {
16180: 	undef($loaded);
16181: 	undef(%domain);
16182:     }
16183: 
16184:     sub load_domain_tab {
16185: 	my ($ignore_cache,$nocache) = @_;
16186: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
16187: 	my $fh;
16188: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
16189: 	    my @lines = <$fh>;
16190: 	    &parse_domain_tab(\@lines);
16191: 	}
16192: 	close($fh);
16193: 	$loaded = 1;
16194:     }
16195: 
16196:     sub domain {
16197: 	&load_domain_tab() if (!$loaded);
16198: 
16199: 	my ($name,$what) = @_;
16200: 	return if ( !exists($domain{$name}) );
16201: 
16202: 	if (!$what) {
16203: 	    return $domain{$name}{'description'};
16204: 	}
16205: 	return $domain{$name}{$what};
16206:     }
16207: 
16208:     sub domain_info {
16209:         &load_domain_tab() if (!$loaded);
16210:         return %domain;
16211:     }
16212: 
16213: }
16214: 
16215: 
16216: # ------------------------------------------------------------- Read hosts file
16217: {
16218:     my %hostname;
16219:     my %hostdom;
16220:     my %libserv;
16221:     my $loaded;
16222:     my %name_to_host;
16223:     my %internetdom;
16224:     my %LC_dns_serv;
16225: 
16226:     sub parse_hosts_tab {
16227: 	my ($file) = @_;
16228: 	foreach my $configline (@$file) {
16229: 	    next if ($configline =~ /^(\#|\s*$ )/x);
16230:             chomp($configline);
16231: 	    if ($configline =~ /^\^/) {
16232:                 if ($configline =~ /^\^([\w.\-]+)/) {
16233:                     $LC_dns_serv{$1} = 1;
16234:                 }
16235:                 next;
16236:             }
16237: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
16238: 	    $name=~s/\s//g;
16239: 	    if ($id && $domain && $role && $name) {
16240:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
16241:                     my $curr = $hostname{$id};
16242:                     my $skip;
16243:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
16244:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
16245:                             $skip = 1;
16246:                         } else {
16247:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
16248:                         }
16249:                     }
16250:                     unless ($skip) {
16251:                         push(@{$name_to_host{$name}},$id);
16252:                     }
16253:                 } else {
16254:                     push(@{$name_to_host{$name}},$id);
16255:                 }
16256: 		$hostname{$id}=$name;
16257: 		$hostdom{$id}=$domain;
16258: 		if ($role eq 'library') { $libserv{$id}=$name; }
16259:                 if (defined($protocol)) {
16260:                     if ($protocol eq 'https') {
16261:                         $protocol{$id} = $protocol;
16262:                     } else {
16263:                         $protocol{$id} = 'http'; 
16264:                     }
16265:                 } else {
16266:                     $protocol{$id} = 'http';
16267:                 }
16268:                 if (defined($intdom)) {
16269:                     $internetdom{$id} = $intdom;
16270:                 }
16271: 	    }
16272: 	}
16273:     }
16274:     
16275:     sub reset_hosts_info {
16276: 	&purge_remembered();
16277: 	&reset_domain_info();
16278: 	&reset_hosts_ip_info();
16279:         undef(%internetdom);
16280: 	undef(%name_to_host);
16281: 	undef(%hostname);
16282: 	undef(%hostdom);
16283: 	undef(%libserv);
16284: 	undef($loaded);
16285:     }
16286: 
16287:     sub load_hosts_tab {
16288: 	my ($ignore_cache,$nocache) = @_;
16289: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
16290: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
16291: 	my @config = <$config>;
16292: 	&parse_hosts_tab(\@config);
16293: 	close($config);
16294: 	$loaded=1;
16295:     }
16296: 
16297:     sub hostname {
16298: 	&load_hosts_tab() if (!$loaded);
16299: 
16300: 	my ($lonid) = @_;
16301: 	return $hostname{$lonid};
16302:     }
16303: 
16304:     sub all_hostnames {
16305: 	&load_hosts_tab() if (!$loaded);
16306: 
16307: 	return %hostname;
16308:     }
16309: 
16310:     sub all_names {
16311:         my ($ignore_cache,$nocache) = @_;
16312: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
16313: 
16314: 	return %name_to_host;
16315:     }
16316: 
16317:     sub all_host_domain {
16318:         &load_hosts_tab() if (!$loaded);
16319:         return %hostdom;
16320:     }
16321: 
16322:     sub all_host_intdom {
16323:         &load_hosts_tab() if (!$loaded);
16324:         return %internetdom;
16325:     }
16326: 
16327:     sub is_library {
16328: 	&load_hosts_tab() if (!$loaded);
16329: 
16330: 	return exists($libserv{$_[0]});
16331:     }
16332: 
16333:     sub all_library {
16334: 	&load_hosts_tab() if (!$loaded);
16335: 
16336: 	return %libserv;
16337:     }
16338: 
16339:     sub unique_library {
16340: 	#2x reverse removes all hostnames that appear more than once
16341:         my %unique = reverse &all_library();
16342:         return reverse %unique;
16343:     }
16344: 
16345:     sub get_servers {
16346: 	&load_hosts_tab() if (!$loaded);
16347: 
16348: 	my ($domain,$type) = @_;
16349: 	my %possible_hosts = ($type eq 'library') ? %libserv
16350: 	                                          : %hostname;
16351: 	my %result;
16352: 	if (ref($domain) eq 'ARRAY') {
16353: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
16354: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
16355: 		    $result{$host} = $hostname;
16356: 		}
16357: 	    }
16358: 	} else {
16359: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
16360: 		if ($hostdom{$host} eq $domain) {
16361: 		    $result{$host} = $hostname;
16362: 		}
16363: 	    }
16364: 	}
16365: 	return %result;
16366:     }
16367: 
16368:     sub get_unique_servers {
16369:         my %unique = reverse &get_servers(@_);
16370: 	return reverse %unique;
16371:     }
16372: 
16373:     sub host_domain {
16374: 	&load_hosts_tab() if (!$loaded);
16375: 
16376: 	my ($lonid) = @_;
16377: 	return $hostdom{$lonid};
16378:     }
16379: 
16380:     sub all_domains {
16381: 	&load_hosts_tab() if (!$loaded);
16382: 
16383: 	my %seen;
16384: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
16385: 	return @uniq;
16386:     }
16387: 
16388:     sub internet_dom {
16389:         &load_hosts_tab() if (!$loaded);
16390: 
16391:         my ($lonid) = @_;
16392:         return $internetdom{$lonid};
16393:     }
16394: 
16395:     sub is_LC_dns {
16396:         &load_hosts_tab() if (!$loaded);
16397: 
16398:         my ($hostname) = @_;
16399:         return exists($LC_dns_serv{$hostname});
16400:     }
16401: 
16402: }
16403: 
16404: { 
16405:     my %iphost;
16406:     my %name_to_ip;
16407:     my %lonid_to_ip;
16408: 
16409:     sub get_hosts_from_ip {
16410: 	my ($ip) = @_;
16411: 	my %iphosts = &get_iphost();
16412: 	if (ref($iphosts{$ip})) {
16413: 	    return @{$iphosts{$ip}};
16414: 	}
16415: 	return;
16416:     }
16417:     
16418:     sub reset_hosts_ip_info {
16419: 	undef(%iphost);
16420: 	undef(%name_to_ip);
16421: 	undef(%lonid_to_ip);
16422:     }
16423: 
16424:     sub get_host_ip {
16425: 	my ($lonid) = @_;
16426: 	if (exists($lonid_to_ip{$lonid})) {
16427: 	    return $lonid_to_ip{$lonid};
16428: 	}
16429: 	my $name=&hostname($lonid);
16430:    	my $ip = gethostbyname($name);
16431: 	return if (!$ip || length($ip) ne 4);
16432: 	$ip=inet_ntoa($ip);
16433: 	$name_to_ip{$name}   = $ip;
16434: 	$lonid_to_ip{$lonid} = $ip;
16435: 	return $ip;
16436:     }
16437:     
16438:     sub get_iphost {
16439: 	my ($ignore_cache,$nocache) = @_;
16440: 
16441: 	if (!$ignore_cache) {
16442: 	    if (%iphost) {
16443: 		return %iphost;
16444: 	    }
16445: 	    my ($ip_info,$cached)=
16446: 		&is_cached_new('iphost','iphost');
16447: 	    if ($cached) {
16448: 		%iphost      = %{$ip_info->[0]};
16449: 		%name_to_ip  = %{$ip_info->[1]};
16450: 		%lonid_to_ip = %{$ip_info->[2]};
16451: 		return %iphost;
16452: 	    }
16453: 	}
16454: 
16455: 	# get yesterday's info for fallback
16456: 	my %old_name_to_ip;
16457: 	my ($ip_info,$cached)=
16458: 	    &is_cached_new('iphost','iphost');
16459: 	if ($cached) {
16460: 	    %old_name_to_ip = %{$ip_info->[1]};
16461: 	}
16462: 
16463: 	my %name_to_host = &all_names($ignore_cache,$nocache);
16464: 	foreach my $name (keys(%name_to_host)) {
16465: 	    my $ip;
16466: 	    if (!exists($name_to_ip{$name})) {
16467: 		$ip = gethostbyname($name);
16468: 		if (!$ip || length($ip) ne 4) {
16469: 		    if (defined($old_name_to_ip{$name})) {
16470: 			$ip = $old_name_to_ip{$name};
16471: 			&logthis("Can't find $name defaulting to old $ip");
16472: 		    } else {
16473: 			&logthis("Name $name no IP found");
16474: 			next;
16475: 		    }
16476: 		} else {
16477: 		    $ip=inet_ntoa($ip);
16478: 		}
16479: 		$name_to_ip{$name} = $ip;
16480: 	    } else {
16481: 		$ip = $name_to_ip{$name};
16482: 	    }
16483: 	    foreach my $id (@{ $name_to_host{$name} }) {
16484: 		$lonid_to_ip{$id} = $ip;
16485: 	    }
16486: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
16487: 	}
16488:         unless ($nocache) {
16489: 	    &do_cache_new('iphost','iphost',
16490: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
16491: 		          48*60*60);
16492:         }
16493: 
16494: 	return %iphost;
16495:     }
16496: 
16497:     #
16498:     #  Given a DNS returns the loncapa host name for that DNS 
16499:     # 
16500:     sub host_from_dns {
16501:         my ($dns) = @_;
16502:         my @hosts;
16503:         my $ip;
16504: 
16505:         if (exists($name_to_ip{$dns})) {
16506:             $ip = $name_to_ip{$dns};
16507:         }
16508:         if (!$ip) {
16509:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
16510:             if (length($ip) == 4) { 
16511: 	        $ip   = &IO::Socket::inet_ntoa($ip);
16512:             }
16513:         }
16514:         if ($ip) {
16515: 	    @hosts = get_hosts_from_ip($ip);
16516: 	    return $hosts[0];
16517:         }
16518:         return undef;
16519:     }
16520: 
16521:     sub get_internet_names {
16522:         my ($lonid) = @_;
16523:         return if ($lonid eq '');
16524:         my ($idnref,$cached)=
16525:             &is_cached_new('internetnames',$lonid);
16526:         if ($cached) {
16527:             return $idnref;
16528:         }
16529:         my $ip = &get_host_ip($lonid);
16530:         my @hosts = &get_hosts_from_ip($ip);
16531:         my %iphost = &get_iphost();
16532:         my (@idns,%seen);
16533:         foreach my $id (@hosts) {
16534:             my $dom = &host_domain($id);
16535:             my $prim_id = &domain($dom,'primary');
16536:             my $prim_ip = &get_host_ip($prim_id);
16537:             next if ($seen{$prim_ip});
16538:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
16539:                 foreach my $id (@{$iphost{$prim_ip}}) {
16540:                     my $intdom = &internet_dom($id);
16541:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
16542:                         push(@idns,$intdom);
16543:                     }
16544:                 }
16545:             }
16546:             $seen{$prim_ip} = 1;
16547:         }
16548:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
16549:     }
16550: 
16551: }
16552: 
16553: sub all_loncaparevs {
16554:     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);
16555: }
16556: 
16557: # ---------------------------------------------------------- Read loncaparev table
16558: {
16559:     sub load_loncaparevs { 
16560:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
16561:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
16562:                 while (my $configline=<$config>) {
16563:                     chomp($configline);
16564:                     my ($hostid,$loncaparev)=split(/:/,$configline);
16565:                     $loncaparevs{$hostid}=$loncaparev;
16566:                 }
16567:                 close($config);
16568:             }
16569:         }
16570:     }
16571: }
16572: 
16573: # ---------------------------------------------------------- Read serverhostID table
16574: {
16575:     sub load_serverhomeIDs {
16576:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
16577:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
16578:                 while (my $configline=<$config>) {
16579:                     chomp($configline);
16580:                     my ($name,$id)=split(/:/,$configline);
16581:                     $serverhomeIDs{$name}=$id;
16582:                 }
16583:                 close($config);
16584:             }
16585:         }
16586:     }
16587: }
16588: 
16589: 
16590: BEGIN {
16591: 
16592: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
16593:     unless ($readit) {
16594: {
16595:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
16596:     %perlvar = (%perlvar,%{$configvars});
16597: }
16598: 
16599: 
16600: # ------------------------------------------------------ Read spare server file
16601: {
16602:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
16603: 
16604:     while (my $configline=<$config>) {
16605:        chomp($configline);
16606:        if ($configline) {
16607: 	   my ($host,$type) = split(':',$configline,2);
16608: 	   if (!defined($type) || $type eq '') { $type = 'default' };
16609: 	   push(@{ $spareid{$type} }, $host);
16610:        }
16611:     }
16612:     close($config);
16613: }
16614: # ------------------------------------------------------------ Read permissions
16615: {
16616:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
16617: 
16618:     while (my $configline=<$config>) {
16619: 	chomp($configline);
16620: 	if ($configline) {
16621: 	    my ($role,$perm)=split(/ /,$configline);
16622: 	    if ($perm ne '') { $pr{$role}=$perm; }
16623: 	}
16624:     }
16625:     close($config);
16626: }
16627: 
16628: # -------------------------------------------- Read plain texts for permissions
16629: {
16630:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
16631: 
16632:     while (my $configline=<$config>) {
16633: 	chomp($configline);
16634: 	if ($configline) {
16635: 	    my ($short,@plain)=split(/:/,$configline);
16636:             %{$prp{$short}} = ();
16637: 	    if (@plain > 0) {
16638:                 $prp{$short}{'std'} = $plain[0];
16639:                 for (my $i=1; $i<@plain; $i++) {
16640:                     $prp{$short}{'alt'.$i} = $plain[$i];  
16641:                 }
16642:             }
16643: 	}
16644:     }
16645:     close($config);
16646: }
16647: 
16648: # ---------------------------------------------------------- Read package table
16649: {
16650:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
16651: 
16652:     while (my $configline=<$config>) {
16653: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
16654: 	chomp($configline);
16655: 	my ($short,$plain)=split(/:/,$configline);
16656: 	my ($pack,$name)=split(/\&/,$short);
16657: 	if ($plain ne '') {
16658: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
16659: 	    $packagetab{$short}=$plain; 
16660: 	}
16661:     }
16662:     close($config);
16663: }
16664: 
16665: # ---------------------------------------------------------- Read loncaparev table
16666: 
16667: &load_loncaparevs();
16668: 
16669: # ---------------------------------------------------------- Read serverhostID table
16670: 
16671: &load_serverhomeIDs();
16672: 
16673: # ---------------------------------------------------------- Read releaseslist XML
16674: {
16675:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
16676:     if (-e $file) {
16677:         my $parser = HTML::LCParser->new($file);
16678:         while (my $token = $parser->get_token()) {
16679:             if ($token->[0] eq 'S') {
16680:                 my $item = $token->[1];
16681:                 my $name = $token->[2]{'name'};
16682:                 my $value = $token->[2]{'value'};
16683:                 my $valuematch = $token->[2]{'valuematch'};
16684:                 my $namematch = $token->[2]{'namematch'};
16685:                 if ($item eq 'parameter') {
16686:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
16687:                         my $release = $parser->get_text();
16688:                         $release =~ s/(^\s*|\s*$ )//gx;
16689:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
16690:                     }
16691:                 } elsif ($item ne '' && $name ne '') {
16692:                     my $release = $parser->get_text();
16693:                     $release =~ s/(^\s*|\s*$ )//gx;
16694:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
16695:                 }
16696:             }
16697:         }
16698:     }
16699: }
16700: 
16701: # ---------------------------------------------------------- Read managers table
16702: {
16703:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
16704:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
16705:             while (my $configline=<$config>) {
16706:                 chomp($configline);
16707:                 next if ($configline =~ /^\#/);
16708:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
16709:                     $managerstab{$configline} = 1;
16710:                 }
16711:             }
16712:             close($config);
16713:         }
16714:     }
16715: }
16716: 
16717: # ------------- set up temporary directory
16718: {
16719:     $tmpdir = LONCAPA::tempdir();
16720: 
16721: }
16722: 
16723: # ------------- set default texengine (domain default overrides this)
16724: {
16725:     $deftex = LONCAPA::texengine();
16726: }
16727: 
16728: # ------------- set default minimum length for passwords for internal auth users
16729: {
16730:     $passwdmin = LONCAPA::passwd_min();
16731: }
16732: 
16733: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
16734: 				'compress_threshold'=> 20_000,
16735:  			        });
16736: 
16737: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
16738: $dumpcount=0;
16739: $locknum=0;
16740: 
16741: &logtouch();
16742: &logthis('<font color="yellow">INFO: Read configuration</font>');
16743: $readit=1;
16744:     {
16745: 	use integer;
16746: 	my $test=(2**32)+1;
16747: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
16748: 	&logthis(" Detected 64bit platform ($_64bit)");
16749:     }
16750: }
16751: }
16752: 
16753: 1;
16754: __END__
16755: 
16756: =pod
16757: 
16758: =head1 NAME
16759: 
16760: Apache::lonnet - Subroutines to ask questions about things in the network.
16761: 
16762: =head1 SYNOPSIS
16763: 
16764: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
16765: 
16766:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
16767: 
16768: Common parameters:
16769: 
16770: =over 4
16771: 
16772: =item *
16773: 
16774: $uname : an internal username (if $cname expecting a course Id specifically)
16775: 
16776: =item *
16777: 
16778: $udom : a domain (if $cdom expecting a course's domain specifically)
16779: 
16780: =item *
16781: 
16782: $symb : a resource instance identifier
16783: 
16784: =item *
16785: 
16786: $namespace : the name of a .db file that contains the data needed or
16787: being set.
16788: 
16789: =back
16790: 
16791: =head1 OVERVIEW
16792: 
16793: lonnet provides subroutines which interact with the
16794: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
16795: about classes, users, and resources.
16796: 
16797: For many of these objects you can also use this to store data about
16798: them or modify them in various ways.
16799: 
16800: =head2 Symbs
16801: 
16802: To identify a specific instance of a resource, LON-CAPA uses symbols
16803: or "symbs"X<symb>. These identifiers are built from the URL of the
16804: map, the resource number of the resource in the map, and the URL of
16805: the resource itself. The latter is somewhat redundant, but might help
16806: if maps change.
16807: 
16808: An example is
16809: 
16810:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
16811: 
16812: The respective map entry is
16813: 
16814:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
16815:   title="Problem 2">
16816:  </resource>
16817: 
16818: Symbs are used by the random number generator, as well as to store and
16819: restore data specific to a certain instance of for example a problem.
16820: 
16821: =head2 Storing And Retrieving Data
16822: 
16823: X<store()>X<cstore()>X<restore()>Three of the most important functions
16824: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
16825: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
16826: is is the non-critical message twin of cstore. These functions are for
16827: handlers to store a perl hash to a user's permanent data space in an
16828: easy manner, and to retrieve it again on another call. It is expected
16829: that a handler would use this once at the beginning to retrieve data,
16830: and then again once at the end to send only the new data back.
16831: 
16832: The data is stored in the user's data directory on the user's
16833: homeserver under the ID of the course.
16834: 
16835: The hash that is returned by restore will have all of the previous
16836: value for all of the elements of the hash.
16837: 
16838: Example:
16839: 
16840:  #creating a hash
16841:  my %hash;
16842:  $hash{'foo'}='bar';
16843: 
16844:  #storing it
16845:  &Apache::lonnet::cstore(\%hash);
16846: 
16847:  #changing a value
16848:  $hash{'foo'}='notbar';
16849: 
16850:  #adding a new value
16851:  $hash{'bar'}='foo';
16852:  &Apache::lonnet::cstore(\%hash);
16853: 
16854:  #retrieving the hash
16855:  my %history=&Apache::lonnet::restore();
16856: 
16857:  #print the hash
16858:  foreach my $key (sort(keys(%history))) {
16859:    print("\%history{$key} = $history{$key}");
16860:  }
16861: 
16862: Will print out:
16863: 
16864:  %history{1:foo} = bar
16865:  %history{1:keys} = foo:timestamp
16866:  %history{1:timestamp} = 990455579
16867:  %history{2:bar} = foo
16868:  %history{2:foo} = notbar
16869:  %history{2:keys} = foo:bar:timestamp
16870:  %history{2:timestamp} = 990455580
16871:  %history{bar} = foo
16872:  %history{foo} = notbar
16873:  %history{timestamp} = 990455580
16874:  %history{version} = 2
16875: 
16876: Note that the special hash entries C<keys>, C<version> and
16877: C<timestamp> were added to the hash. C<version> will be equal to the
16878: total number of versions of the data that have been stored. The
16879: C<timestamp> attribute will be the UNIX time the hash was
16880: stored. C<keys> is available in every historical section to list which
16881: keys were added or changed at a specific historical revision of a
16882: hash.
16883: 
16884: B<Warning>: do not store the hash that restore returns directly. This
16885: will cause a mess since it will restore the historical keys as if the
16886: were new keys. I.E. 1:foo will become 1:1:foo etc.
16887: 
16888: Calling convention:
16889: 
16890:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
16891:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
16892: 
16893: For more detailed information, see lonnet specific documentation.
16894: 
16895: =head1 RETURN MESSAGES
16896: 
16897: =over 4
16898: 
16899: =item * B<con_lost>: unable to contact remote host
16900: 
16901: =item * B<con_delayed>: unable to contact remote host, message will be delivered
16902: when the connection is brought back up
16903: 
16904: =item * B<con_failed>: unable to contact remote host and unable to save message
16905: for later delivery
16906: 
16907: =item * B<error:>: an error a occurred, a description of the error follows the :
16908: 
16909: =item * B<no_such_host>: unable to fund a host associated with the user/domain
16910: that was requested
16911: 
16912: =back
16913: 
16914: =head1 PUBLIC SUBROUTINES
16915: 
16916: =head2 Session Environment Functions
16917: 
16918: =over 4
16919: 
16920: =item * 
16921: X<appenv()>
16922: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
16923: the user envirnoment file, and will be restored for each access this
16924: user makes during this session, also modifies the %env for the current
16925: process. Optional rolesarrayref - if defined contains a reference to an array
16926: of roles which are exempt from the restriction on modifying user.role entries 
16927: in the user's environment.db and in %env.    
16928: 
16929: =item *
16930: X<delenv()>
16931: B<delenv($delthis,$regexp)>: removes all items from the session
16932: environment file that begin with $delthis. If the 
16933: optional second arg - $regexp - is true, $delthis is treated as a 
16934: regular expression, otherwise \Q$delthis\E is used. 
16935: The values are also deleted from the current processes %env.
16936: 
16937: =item * get_env_multiple($name) 
16938: 
16939: gets $name from the %env hash, it seemlessly handles the cases where multiple
16940: values may be defined and end up as an array ref.
16941: 
16942: returns an array of values
16943: 
16944: =back
16945: 
16946: =head2 User Information
16947: 
16948: =over 4
16949: 
16950: =item *
16951: X<queryauthenticate()>
16952: B<queryauthenticate($uname,$udom)>: try to determine user's current 
16953: authentication scheme
16954: 
16955: =item *
16956: X<authenticate()>
16957: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
16958: authenticate user from domain's lib servers (first use the current
16959: one). C<$upass> should be the users password.
16960: $checkdefauth is optional (value is 1 if a check should be made to
16961:    authenticate user using default authentication method, and allow
16962:    account creation if username does not have account in the domain).
16963: $clientcancheckhost is optional (value is 1 if checking whether the
16964:    server can host will occur on the client side in lonauth.pm).   
16965: 
16966: =item *
16967: X<homeserver()>
16968: B<homeserver($uname,$udom)>: find the server which has
16969: the user's directory and files (there must be only one), this caches
16970: the answer, and also caches if there is a borken connection.
16971: 
16972: =item *
16973: X<idget()>
16974: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
16975: a list of student/employee IDs or clicker IDs
16976: (student/employee IDs are a unique resource in a domain, there must be 
16977: only 1 ID per username, and only 1 username per ID in a specific domain).
16978: clickerIDs are not necessarily unique, as students might share clickers.
16979: (returns hash: id=>name,id=>name)
16980: 
16981: =item *
16982: X<idrget()>
16983: B<idrget($udom,@unames)>: find the IDs behind a list of
16984: usernames (returns hash: name=>id,name=>id)
16985: 
16986: =item *
16987: X<idput()>
16988: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
16989: names and associated student/employee IDs or clicker IDs.
16990: 
16991: =item *
16992: X<iddel()>
16993: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
16994: student/employee ID or clicker ID username look-ups from domain.
16995: The homeserver ($uhome) and namespace ($namespace) are optional.
16996: If no $uhome is provided, it will be determined usig &homeserver()
16997: for each user.  If no $namespace is provided, the default is ids.
16998: 
16999: =item *
17000: X<updateclickers()>
17001: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
17002: clicker ID-to-username look-ups in clickers.db on library server.
17003: Permitted actions are add or del (i.e., add or delete). The 
17004: clickers.db contains clickerID as keys (escaped), and each corresponding
17005: value is an escaped comma-separated list of usernames (for whom the
17006: library server is the homeserver), who registered that particular ID.
17007: If $critical is true, the update will be sent via &critical, otherwise
17008: &reply() will be used.
17009: 
17010: =item *
17011: X<rolesinit()>
17012: B<rolesinit($udom,$username)>: get user privileges.
17013: returns user role, first access and timer interval hashes
17014: 
17015: =item *
17016: X<privileged()>
17017: B<privileged($username,$domain)>: returns a true if user has a
17018: privileged and active role (i.e. su or dc), false otherwise.
17019: 
17020: =item *
17021: X<getsection()>
17022: B<getsection($udom,$uname,$cname)>: finds the section of student in the
17023: course $cname, return section name/number or '' for "not in course"
17024: and '-1' for "no section"
17025: 
17026: =item *
17027: X<userenvironment()>
17028: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
17029: passed in @what from the requested user's environment, returns a hash
17030: 
17031: =item * 
17032: X<userlog_query()>
17033: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
17034: activity.log file. %filters defines filters applied when parsing the
17035: log file. These can be start or end timestamps, or the type of action
17036: - log to look for Login or Logout events, check for Checkin or
17037: Checkout, role for role selection. The response is in the form
17038: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
17039: escaped strings of the action recorded in the activity.log file.
17040: 
17041: =back
17042: 
17043: =head2 User Roles
17044: 
17045: =over 4
17046: 
17047: =item *
17048: 
17049: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
17050: returns codes for allowed actions.
17051: 
17052: The first argument is required, all others are optional.
17053: 
17054: $priv is the privilege being checked.
17055: $uri contains additional information about what is being checked for access (e.g.,
17056: URL, course ID etc.). 
17057: $symb is the unique resource instance identifier in a course; if needed,
17058: but not provided, it will be retrieved via a call to &symbread(). 
17059: $role is the role for which a priv is being checked (only used if priv is evb). 
17060: $clientip is the user's IP address (only used when checking for access to portfolio 
17061: files).
17062: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
17063: prevents recursive calls to &allowed.
17064: 
17065:  F: full access
17066:  U,I,K: authentication modes (cxx only)
17067:  '': forbidden
17068:  1: user needs to choose course
17069:  2: browse allowed
17070:  A: passphrase authentication needed
17071:  B: access temporarily blocked because of a blocking event in a course.
17072:  D: access blocked because access is required via session initiated via deep-link 
17073: 
17074: =item *
17075: 
17076: constructaccess($url,$setpriv) : check for access to construction space URL
17077: 
17078: See if the owner domain and name in the URL match those in the
17079: expected environment.  If so, return three element list
17080: ($ownername,$ownerdomain,$ownerhome).
17081: 
17082: Otherwise return the null string.
17083: 
17084: If second argument 'setpriv' is true, it assigns the privileges,
17085: and returns the same three element list, unless the owner has
17086: blocked "ad hoc" Domain Coordinator access to the Author Space,
17087: in which case the null string is returned.
17088: 
17089: =item *
17090: 
17091: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
17092: define a custom role rolename set privileges in format of lonTabs/roles.tab
17093: for system, domain, and course level. $uname and $udom are optional (current
17094: user's username and domain will be used when either of $uname or $udom are absent.
17095: 
17096: =item *
17097: 
17098: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
17099: (rolesplain.tab); plain text explanation of a user role term.
17100: $type is Course (default) or Community.
17101: If $forcedefault evaluates to true, text returned will be default 
17102: text for $type. Otherwise, if this is a course, the text returned 
17103: will be a custom name for the role (if defined in the course's 
17104: environment).  If no custom name is defined the default is returned.
17105:    
17106: =item *
17107: 
17108: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
17109: All arguments are optional. Returns a hash of a roles, either for
17110: co-author/assistant author roles for a user's Construction Space
17111: (default), or if $context is 'userroles', roles for the user himself,
17112: In the hash, keys are set to colon-separated $uname,$udom,$role, and
17113: (optionally) if $withsec is true, a fourth colon-separated item - $section.
17114: For each key, value is set to colon-separated start and end times for
17115: the role.  If no username and domain are specified, will default to
17116: current user/domain. Types, roles, and roledoms are references to arrays
17117: of role statuses (active, future or previous), roles 
17118: (e.g., cc,in, st etc.) and domains of the roles which can be used
17119: to restrict the list of roles reported. If no array ref is 
17120: provided for types, will default to return only active roles.
17121: 
17122: =item *
17123: 
17124: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
17125: user: $uname:$udom has a role in the course: $cdom_$cnum. 
17126: 
17127: Additional optional arguments are: $type (if role checking is to be restricted 
17128: to certain user status types -- previous (expired roles), active (currently
17129: available roles) or future (roles available in the future), and
17130: $hideprivileged -- if true will not report course roles for users who
17131: have active Domain Coordinator role in course's domain or in additional
17132: domains (specified in 'Domains to check for privileged users' in course
17133: environment -- set via:  Course Settings -> Classlists and staff listing).
17134: 
17135: =item *
17136: 
17137: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
17138: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
17139: $possdomains and $possroles are optional array refs -- to domains to check and
17140: roles to check.  If $possdomains is not specified, a dump will be done of the
17141: users' roles.db to check for a dc or su role in any domain. This can be
17142: time consuming if &privileged is called repeatedly (e.g., when displaying a
17143: classlist), so in such cases, supplying a $possdomains array is preferred, as
17144: this then allows &privileged_by_domain() to be used, which caches the identity
17145: of privileged users, eliminating the need for repeated calls to &dump().
17146: 
17147: =item *
17148: 
17149: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
17150: where the outer hash keys are domains specified in the $possdomains array ref,
17151: next inner hash keys are privileged roles specified in the $roles array ref,
17152: and the innermost hash contains key = value pairs for username:domain = end:start
17153: for active or future "privileged" users with that role in that domain. To avoid
17154: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
17155: innerhash are cached using priv_$role and $dom as the identifiers.
17156: 
17157: =back
17158: 
17159: =head2 User Modification
17160: 
17161: =over 4
17162: 
17163: =item *
17164: 
17165: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
17166: user for the level given by URL.  Optional start and end dates (leave empty
17167: string or zero for "no date")
17168: 
17169: =item *
17170: 
17171: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
17172: change a users, password, possible return values are: ok,
17173: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
17174: refused
17175: 
17176: =item *
17177: 
17178: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
17179: 
17180: =item *
17181: 
17182: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
17183:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
17184: 
17185: will update user information (firstname,middlename,lastname,generation,
17186: permanentemail), and if forceid is true, student/employee ID also.
17187: A user's institutional affiliation(s) can also be updated.
17188: User information fields will not be overwritten with empty entries 
17189: unless the field is included in the $candelete array reference.
17190: This array is included when a single user is modified via "Manage Users",
17191: or when Autoupdate.pl is run by cron in a domain.
17192: 
17193: =item *
17194: 
17195: modifystudent
17196: 
17197: modify a student's enrollment and identification information.
17198: The course id is resolved based on the current user's environment.  
17199: This means the invoking user must be a course coordinator or otherwise
17200: associated with a course.
17201: 
17202: This call is essentially a wrapper for lonnet::modifyuser and
17203: lonnet::modify_student_enrollment
17204: 
17205: Inputs: 
17206: 
17207: =over 4
17208: 
17209: =item B<$udom> Student's loncapa domain
17210: 
17211: =item B<$uname> Student's loncapa login name
17212: 
17213: =item B<$uid> Student/Employee ID
17214: 
17215: =item B<$umode> Student's authentication mode
17216: 
17217: =item B<$upass> Student's password
17218: 
17219: =item B<$first> Student's first name
17220: 
17221: =item B<$middle> Student's middle name
17222: 
17223: =item B<$last> Student's last name
17224: 
17225: =item B<$gene> Student's generation
17226: 
17227: =item B<$usec> Student's section in course
17228: 
17229: =item B<$end> Unix time of the roles expiration
17230: 
17231: =item B<$start> Unix time of the roles start date
17232: 
17233: =item B<$forceid> If defined, allow $uid to be changed
17234: 
17235: =item B<$desiredhome> server to use as home server for student
17236: 
17237: =item B<$email> Student's permanent e-mail address
17238: 
17239: =item B<$type> Type of enrollment (auto or manual)
17240: 
17241: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
17242: 
17243: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
17244: 
17245: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
17246: 
17247: =item B<$context> role change context (shown in User Management Logs display in a course)
17248: 
17249: =item B<$inststatus> institutional status of user - : separated string of escaped status types
17250: 
17251: =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.
17252: 
17253: =back
17254: 
17255: =item *
17256: 
17257: modify_student_enrollment
17258: 
17259: Change a student's enrollment status in a class.  The environment variable
17260: 'role.request.course' must be defined for this function to proceed.
17261: 
17262: Inputs:
17263: 
17264: =over 4
17265: 
17266: =item $udom, student's domain
17267: 
17268: =item $uname, student's name
17269: 
17270: =item $uid, student's user id
17271: 
17272: =item $first, student's first name
17273: 
17274: =item $middle
17275: 
17276: =item $last
17277: 
17278: =item $gene
17279: 
17280: =item $usec
17281: 
17282: =item $end
17283: 
17284: =item $start
17285: 
17286: =item $type
17287: 
17288: =item $locktype
17289: 
17290: =item $cid
17291: 
17292: =item $selfenroll
17293: 
17294: =item $context
17295: 
17296: =item $credits, number of credits student will earn from this class
17297: 
17298: =item $instsec, institutional course section code for student
17299: 
17300: =back
17301: 
17302: 
17303: =item *
17304: 
17305: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
17306: custom role; give a custom role to a user for the level given by URL.  Specify
17307: name and domain of role author, and role name
17308: 
17309: =item *
17310: 
17311: revokerole($udom,$uname,$url,$role) : revoke a role for url
17312: 
17313: =item *
17314: 
17315: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
17316: 
17317: =back
17318: 
17319: =head2 Course Infomation
17320: 
17321: =over 4
17322: 
17323: =item *
17324: 
17325: coursedescription($courseid,$options) : returns a hash of information about the
17326: specified course id, including all environment settings for the
17327: course, the description of the course will be in the hash under the
17328: key 'description'
17329: 
17330: $options is an optional parameter that if supplied is a hash reference that controls
17331: what how this function works.  It has the following key/values:
17332: 
17333: =over 4
17334: 
17335: =item freshen_cache
17336: 
17337: If defined, and the environment cache for the course is valid, it is 
17338: returned in the returned hash.
17339: 
17340: =item one_time
17341: 
17342: If defined, the last cache time is set to _now_
17343: 
17344: =item user
17345: 
17346: If defined, the supplied username is used instead of the current user.
17347: 
17348: 
17349: =back
17350: 
17351: =item *
17352: 
17353: resdata($name,$domain,$type,@which) : request for current parameter
17354: setting for a specific $type, where $type is either 'course' or 'user',
17355: @what should be a list of parameters to ask about. This routine caches
17356: answers for 10 minutes.
17357: 
17358: =item *
17359: 
17360: get_courseresdata($courseid, $domain) : dump the entire course resource
17361: data base, returning a hash that is keyed by the resource name and has
17362: values that are the resource value.  I believe that the timestamps and
17363: versions are also returned.
17364: 
17365: =back
17366: 
17367: =head2 Course Modification
17368: 
17369: =over 4
17370: 
17371: =item *
17372: 
17373: writecoursepref($courseid,%prefs) : write preferences (environment
17374: database) for a course
17375: 
17376: =item *
17377: 
17378: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
17379: 
17380: =item *
17381: 
17382: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
17383: 
17384: =item *
17385: 
17386: is_course($courseid), is_course($cdom, $cnum)
17387: 
17388: Accepts either a combined $courseid (in the form of domain_courseid) or the
17389: two component version $cdom, $cnum. It checks if the specified course exists.
17390: 
17391: Returns:
17392:     undef if the course doesn't exist, otherwise
17393:     in scalar context the combined courseid.
17394:     in list context the two components of the course identifier, domain and 
17395:     courseid.    
17396: 
17397: =back
17398: 
17399: =head2 Bubblesheet Configuration
17400: 
17401: =over 4
17402: 
17403: =item *
17404: 
17405: get_scantron_config($which)
17406: 
17407: $which - the name of the configuration to parse from the file.
17408: 
17409: Parses and returns the bubblesheet configuration line selected as a
17410: hash of configuration file fields.
17411: 
17412: 
17413: Returns:
17414:     If the named configuration is not in the file, an empty
17415:     hash is returned.
17416: 
17417:     a hash with the fields
17418:       name         - internal name for the this configuration setup
17419:       description  - text to display to operator that describes this config
17420:       CODElocation - if 0 or the string 'none'
17421:                           - no CODE exists for this config
17422:                      if -1 || the string 'letter'
17423:                           - a CODE exists for this config and is
17424:                             a string of letters
17425:                      Unsupported value (but planned for future support)
17426:                           if a positive integer
17427:                                - The CODE exists as the first n items from
17428:                                  the question section of the form
17429:                           if the string 'number'
17430:                                - The CODE exists for this config and is
17431:                                  a string of numbers
17432:       CODEstart   - (only matter if a CODE exists) column in the line where
17433:                      the CODE starts
17434:       CODElength  - length of the CODE
17435:       IDstart     - column where the student/employee ID starts
17436:       IDlength    - length of the student/employee ID info
17437:       Qstart      - column where the information from the bubbled
17438:                     'questions' start
17439:       Qlength     - number of columns comprising a single bubble line from
17440:                     the sheet. (usually either 1 or 10)
17441:       Qon         - either a single character representing the character used
17442:                     to signal a bubble was chosen in the positional setup, or
17443:                     the string 'letter' if the letter of the chosen bubble is
17444:                     in the final, or 'number' if a number representing the
17445:                     chosen bubble is in the file (1->A 0->J)
17446:       Qoff        - the character used to represent that a bubble was
17447:                     left blank
17448:       PaperID     - if the scanning process generates a unique number for each
17449:                     sheet scanned the column that this ID number starts in
17450:       PaperIDlength - number of columns that comprise the unique ID number
17451:                       for the sheet of paper
17452:       FirstName   - column that the first name starts in
17453:       FirstNameLength - number of columns that the first name spans
17454:       LastName    - column that the last name starts in
17455:       LastNameLength - number of columns that the last name spans
17456:       BubblesPerRow - number of bubbles available in each row used to
17457:                       bubble an answer. (If not specified, 10 assumed).
17458: 
17459: 
17460: =item *
17461: 
17462: get_scantronformat_file($cdom)
17463: 
17464: $cdom - the course's domain (optional); if not supplied, uses
17465: domain for current $env{'request.course.id'}.
17466: 
17467: Returns an array containing lines from the scantron format file for
17468: the domain of the course.
17469: 
17470: If a url for a custom.tab file is listed in domain's configuration.db,
17471: lines are from this file.
17472: 
17473: Otherwise, if a default.tab has been published in RES space by the
17474: domainconfig user, lines are from this file.
17475: 
17476: Otherwise, fall back to getting lines from the legacy file on the
17477: local server:  /home/httpd/lonTabs/default_scantronformat.tab
17478: 
17479: =back
17480: 
17481: =head2 Resource Subroutines
17482: 
17483: =over 4
17484: 
17485: =item *
17486: 
17487: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
17488: 
17489: =item *
17490: 
17491: repcopy($filename) : subscribes to the requested file, and attempts to
17492: replicate from the owning library server, Might return
17493: 'unavailable', 'not_found', 'forbidden', 'ok', or
17494: 'bad_request', also attempts to grab the metadata for the
17495: resource. Expects the local filesystem pathname
17496: (/home/httpd/html/res/....)
17497: 
17498: =back
17499: 
17500: =head2 Resource Information
17501: 
17502: =over 4
17503: 
17504: =item *
17505: 
17506: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
17507: and returns the value of a variety of different possible values,
17508: $varname should be a request string, and the other parameters can be
17509: used to specify who and what one is asking about. Ordinarily, $cid 
17510: does not need to be specified, as it is retrived from 
17511: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
17512: within lonuserstate::loadmap() when initializing a course, before
17513: $env{'request.course.id'} has been set, so it needs to be provided
17514: in that one case.
17515: 
17516: Possible values for $varname are environment.lastname (or other item
17517: from the envirnment hash), user.name (or someother aspect about the
17518: user), resource.0.maxtries (or some other part and parameter of a
17519: resource)
17520: 
17521: =item *
17522: 
17523: directcondval($number) : get current value of a condition; reads from a state
17524: string
17525: 
17526: =item *
17527: 
17528: condval($condidx) : value of condition index based on state
17529: 
17530: =item *
17531: 
17532: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
17533: resource's metadata, $what should be either a specific key, or either
17534: 'keys' (to get a list of possible keys) or 'packages' to get a list of
17535: packages that this resource currently uses, the last 3 arguments are 
17536: only used internally for recursive metadata.
17537: 
17538: the toolsymb is only used where the uri is for an external tool (for which
17539: the uri as well as the symb are guaranteed to be unique).
17540: 
17541: this function automatically caches all requests except any made recursively
17542: to retrieve a list of metadata keys for an imported library file ($liburi is 
17543: defined).
17544: 
17545: =item *
17546: 
17547: metadata_query($query,$custom,$customshow) : make a metadata query against the
17548: network of library servers; returns file handle of where SQL and regex results
17549: will be stored for query
17550: 
17551: =item *
17552: 
17553: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
17554: return symbolic list entry (all arguments optional). 
17555: 
17556: Args: filename is the filename (including path) for the file for which a symb 
17557: is required; donotrecurse, if true will prevent calls to allowed() being made 
17558: to check access status if more than one resource was found in the bighash 
17559: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
17560: a randompick); ignorecachednull, if true will prevent a symb of '' being 
17561: returned if $env{$cache_str} is defined as ''; checkforblock if true will
17562: cause possible symbs to be checked to determine if they are subject to content
17563: blocking, if so they will not be included as possible symbs; possibles is a
17564: ref to a hash, which, as a side effect, will be populated with all possible 
17565: symbs (content blocking not tested).
17566:  
17567: returns the data handle
17568: 
17569: =item *
17570: 
17571: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
17572: and is a possible symb for the URL in $thisfn, and if is an encrypted
17573: resource that the user accessed using /enc/ returns a 1 on success, 0
17574: on failure, user must be in a course, as it assumes the existence of
17575: the course initial hash, and uses $env('request.course.id'}.  The third
17576: arg is an optional reference to a scalar.  If this arg is passed in the 
17577: call to symbverify, it will be set to 1 if the symb has been set to be 
17578: encrypted; otherwise it will be null.  
17579: 
17580: =item *
17581: 
17582: symbclean($symb) : removes versions numbers from a symb, returns the
17583: cleaned symb
17584: 
17585: =item *
17586: 
17587: is_on_map($uri) : checks if the $uri is somewhere on the current
17588: course map, user must be in a course for it to work.
17589: 
17590: =item *
17591: 
17592: numval($salt) : return random seed value (addend for rndseed)
17593: 
17594: =item *
17595: 
17596: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
17597: a random seed, all arguments are optional, if they aren't sent it uses the
17598: environment to derive them. Note: if symb isn't sent and it can't get one
17599: from &symbread it will use the current time as its return value
17600: 
17601: =item *
17602: 
17603: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
17604: unfakeable, receipt
17605: 
17606: =item *
17607: 
17608: receipt() : API to ireceipt working off of env values; given out to users
17609: 
17610: =item *
17611: 
17612: countacc($url) : count the number of accesses to a given URL
17613: 
17614: =item *
17615: 
17616: 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
17617: 
17618: =item *
17619: 
17620: 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)
17621: 
17622: =item *
17623: 
17624: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
17625: 
17626: =item *
17627: 
17628: devalidate($symb) : devalidate temporary spreadsheet calculations,
17629: forcing spreadsheet to reevaluate the resource scores next time.
17630: 
17631: =item * 
17632: 
17633: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
17634: when viewing in course context.
17635: 
17636:  input: six args -- filename (decluttered), course number, course domain,
17637:                     url, symb (if registered) and group (if this is a 
17638:                     group item -- e.g., bulletin board, group page etc.).
17639: 
17640:  output: array of five scalars --
17641:          $cfile -- url for file editing if editable on current server
17642:          $home -- homeserver of resource (i.e., for author if published,
17643:                                           or course if uploaded.).
17644:          $switchserver --  1 if server switch will be needed.
17645:          $forceedit -- 1 if icon/link should be to go to edit mode 
17646:          $forceview -- 1 if icon/link should be to go to view mode
17647: 
17648: =item *
17649: 
17650: is_course_upload($file,$cnum,$cdom)
17651: 
17652: Used in course context to determine if current file was uploaded to 
17653: the course (i.e., would be found in /userfiles/docs on the course's 
17654: homeserver.
17655: 
17656:   input: 3 args -- filename (decluttered), course number and course domain.
17657:   output: boolean -- 1 if file was uploaded.
17658: 
17659: =back
17660: 
17661: =head2 Storing/Retreiving Data
17662: 
17663: =over 4
17664: 
17665: =item *
17666: 
17667: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
17668: permanently for this url; hashref needs to be given and should be a \%hashname;
17669: the remaining args aren't required and if they aren't passed or are '' they will
17670: be derived from the env (with the exception of $laststore, which is an 
17671: optional arg used when a user's submission is stored in grading).
17672: $laststore is $version=$timestamp, where $version is the most recent version
17673: number retrieved for the corresponding $symb in the $namespace db file, and
17674: $timestamp is the timestamp for that transaction (UNIX time).
17675: $laststore is currently only passed when cstore() is called by 
17676: structuretags::finalize_storage().
17677: 
17678: =item *
17679: 
17680: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
17681: but uses critical subroutine
17682: 
17683: =item *
17684: 
17685: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
17686: all args are optional
17687: 
17688: =item *
17689: 
17690: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
17691: dumps the complete (or key matching regexp) namespace into a hash
17692: ($udom, $uname, $regexp, $range are optional) for a namespace that is
17693: normally &store()ed into
17694: 
17695: $range should be either an integer '100' (give me the first 100
17696:                                            matching records)
17697:               or be  two integers sperated by a - with no spaces
17698:                  '30-50' (give me the 30th through the 50th matching
17699:                           records)
17700: 
17701: 
17702: =item *
17703: 
17704: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
17705: replaces a &store() version of data with a replacement set of data
17706: for a particular resource in a namespace passed in the $storehash hash 
17707: reference. If $tolog is true, the transaction is logged in the courselog
17708: with an action=PUTSTORE.
17709: 
17710: =item *
17711: 
17712: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
17713: works very similar to store/cstore, but all data is stored in a
17714: temporary location and can be reset using tmpreset, $storehash should
17715: be a hash reference, returns nothing on success
17716: 
17717: =item *
17718: 
17719: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
17720: similar to restore, but all data is stored in a temporary location and
17721: can be reset using tmpreset. Returns a hash of values on success,
17722: error string otherwise.
17723: 
17724: =item *
17725: 
17726: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
17727: deltes all keys for $symb form the temporary storage hash.
17728: 
17729: =item *
17730: 
17731: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
17732: reference filled in from namesp ($udom and $uname are optional)
17733: 
17734: =item *
17735: 
17736: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
17737: namesp ($udom and $uname are optional)
17738: 
17739: =item *
17740: 
17741: dump($namespace,$udom,$uname,$regexp,$range) : 
17742: dumps the complete (or key matching regexp) namespace into a hash
17743: ($udom, $uname, $regexp, $range are optional)
17744: 
17745: $range should be either an integer '100' (give me the first 100
17746:                                            matching records)
17747:               or be  two integers sperated by a - with no spaces
17748:                  '30-50' (give me the 30th through the 50th matching
17749:                           records)
17750: =item *
17751: 
17752: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
17753: $store can be a scalar, an array reference, or if the amount to be 
17754: incremented is > 1, a hash reference.
17755: 
17756: ($udom and $uname are optional)
17757: 
17758: =item *
17759: 
17760: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
17761: ($udom and $uname are optional)
17762: 
17763: =item *
17764: 
17765: cput($namespace,$storehash,$udom,$uname) : critical put
17766: ($udom and $uname are optional)
17767: 
17768: =item *
17769: 
17770: newput($namespace,$storehash,$udom,$uname) :
17771: 
17772: Attempts to store the items in the $storehash, but only if they don't
17773: currently exist, if this succeeds you can be certain that you have 
17774: successfully created a new key value pair in the $namespace db.
17775: 
17776: 
17777: Args:
17778:  $namespace: name of database to store values to
17779:  $storehash: hashref to store to the db
17780:  $udom: (optional) domain of user containing the db
17781:  $uname: (optional) name of user caontaining the db
17782: 
17783: Returns:
17784:  'ok' -> succeeded in storing all keys of $storehash
17785:  'key_exists: <key>' -> failed to anything out of $storehash, as at
17786:                         least <key> already existed in the db (other
17787:                         requested keys may also already exist)
17788:  'error: <msg>' -> unable to tie the DB or other error occurred
17789:  'con_lost' -> unable to contact request server
17790:  'refused' -> action was not allowed by remote machine
17791: 
17792: 
17793: =item *
17794: 
17795: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
17796: reference filled in from namesp (encrypts the return communication)
17797: ($udom and $uname are optional)
17798: 
17799: =item *
17800: 
17801: log($udom,$name,$home,$message) : write to permanent log for user; use
17802: critical subroutine
17803: 
17804: =item *
17805: 
17806: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
17807: array reference filled in from namespace found in domain level on either
17808: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
17809: 
17810: =item *
17811: 
17812: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
17813: domain level either on specified domain server ($uhome) or primary domain 
17814: server ($udom and $uhome are optional)
17815: 
17816: =item * 
17817: 
17818: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
17819: for: authentication, language, quotas, timezone, date locale, and portal URL in
17820: the target domain.
17821: 
17822: May also include additional key => value pairs for the following groups:
17823: 
17824: =over
17825: 
17826: =item
17827: disk quotas (MB allocated by default to portfolios and authoring spaces).
17828: 
17829: =over
17830: 
17831: =item defaultquota, authorquota
17832: 
17833: =back
17834: 
17835: =item
17836: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
17837: portfolio for users).
17838: 
17839: =over
17840: 
17841: =item
17842: aboutme, blog, webdav, portfolio
17843: 
17844: =back
17845: 
17846: =item
17847: requestcourses: ability to request courses, and how requests are processed.
17848: 
17849: =over
17850: 
17851: =item
17852: official, unofficial, community, textbook, placement
17853: 
17854: =back
17855: 
17856: =item
17857: inststatus: types of institutional affiliation, and order in which they are displayed.
17858: 
17859: =over
17860: 
17861: =item
17862: inststatustypes, inststatusorder, inststatusguest
17863: 
17864: =back
17865: 
17866: =item
17867: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
17868: for course's uploaded content.
17869: 
17870: =over
17871: 
17872: =item
17873: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
17874: communityquota, textbookquota, placementquota
17875: 
17876: =back
17877: 
17878: =item
17879: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
17880: on your servers.
17881: 
17882: =over
17883: 
17884: =item 
17885: remotesessions, hostedsessions
17886: 
17887: =back
17888: 
17889: =back
17890: 
17891: In cases where a domain coordinator has never used the "Set Domain Configuration"
17892: utility to create a configuration.db file on a domain's primary library server 
17893: only the following domain defaults: auth_def, auth_arg_def, lang_def
17894: -- corresponding values are authentication type (internal, krb4, krb5,
17895: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
17896: will be available. Values are retrieved from cache (if current), unless the
17897: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
17898: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
17899: 
17900: Typical usage:
17901: 
17902: %domdefaults = &get_domain_defaults($target_domain);
17903: 
17904: =back
17905: 
17906: =head2 Network Status Functions
17907: 
17908: =over 4
17909: 
17910: =item *
17911: 
17912: dirlist() : return directory list based on URI (first arg).
17913: 
17914: Inputs: 1 required, 5 optional.
17915: 
17916: =over
17917: 
17918: =item 
17919: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
17920: 
17921: =item
17922: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
17923: 
17924: =item
17925: $username -  username of user/course to be listed. Extracted from $uri if absent. 
17926: 
17927: =item
17928: $getpropath - boolean: 1 if prepend path using &propath(). 
17929: 
17930: =item
17931: $getuserdir - boolean: 1 if prepend path for "userfiles".
17932: 
17933: =item 
17934: $alternateRoot - path to prepend in place of path from $uri.
17935: 
17936: =back
17937: 
17938: Returns: Array of up to two items.
17939: 
17940: =over
17941: 
17942: a reference to an array of files/subdirectories
17943: 
17944: =over
17945: 
17946: Each element in the array of files/subdirectories is a & separated list of
17947: item name and the result of running stat on the item.  If dirlist was requested
17948: for a file instead of a directory, the item name will be ''. For a directory 
17949: listing, if the item is a metadata file, the element will end &N&M 
17950: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
17951: default copyright set (1).  
17952: 
17953: =back
17954: 
17955: a scalar containing error condition (if encountered).
17956: 
17957: =over
17958: 
17959: =item 
17960: no_host (no homeserver identified for $username:$domain).
17961: 
17962: =item 
17963: no_such_host (server contacted for listing not identified as valid host).
17964: 
17965: =item 
17966: con_lost (connection to remote server failed).
17967: 
17968: =item 
17969: refused (invalid $username:$domain received on lond side).
17970: 
17971: =item 
17972: no_such_dir (directory at specified path on lond side does not exist). 
17973: 
17974: =item 
17975: empty (directory at specified path on lond side is empty).
17976: 
17977: =over
17978: 
17979: This is currently not encountered because the &ls3, &ls2, 
17980: &ls (_handler) routines on the lond side do not filter out
17981: . and .. from a directory listing. 
17982: 
17983: =back
17984: 
17985: =back
17986: 
17987: =back
17988: 
17989: =item *
17990: 
17991: spareserver() : find server with least workload from spare.tab
17992: 
17993: 
17994: =item *
17995: 
17996: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
17997: if there is no corresponding loncapa host.
17998: 
17999: =back
18000: 
18001: 
18002: =head2 Apache Request
18003: 
18004: =over 4
18005: 
18006: =item *
18007: 
18008: ssi($url,%hash) : server side include, does a complete request cycle on url to
18009: localhost, posts hash
18010: 
18011: =back
18012: 
18013: =head2 Data to String to Data
18014: 
18015: =over 4
18016: 
18017: =item *
18018: 
18019: hash2str(%hash) : convert a hash into a string complete with escaping and '='
18020: and '&' separators, supports elements that are arrayrefs and hashrefs
18021: 
18022: =item *
18023: 
18024: hashref2str($hashref) : convert a hashref into a string complete with
18025: escaping and '=' and '&' separators, supports elements that are
18026: arrayrefs and hashrefs
18027: 
18028: =item *
18029: 
18030: arrayref2str($arrayref) : convert an arrayref into a string complete
18031: with escaping and '&' separators, supports elements that are arrayrefs
18032: and hashrefs
18033: 
18034: =item *
18035: 
18036: str2hash($string) : convert string to hash using unescaping and
18037: splitting on '=' and '&', supports elements that are arrayrefs and
18038: hashrefs
18039: 
18040: =item *
18041: 
18042: str2array($string) : convert string to hash using unescaping and
18043: splitting on '&', supports elements that are arrayrefs and hashrefs
18044: 
18045: =back
18046: 
18047: =head2 Logging Routines
18048: 
18049: 
18050: These routines allow one to make log messages in the lonnet.log and
18051: lonnet.perm logfiles.
18052: 
18053: =over 4
18054: 
18055: =item *
18056: 
18057: logtouch() : make sure the logfile, lonnet.log, exists
18058: 
18059: =item *
18060: 
18061: logthis() : append message to the normal lonnet.log file, it gets
18062: preiodically rolled over and deleted.
18063: 
18064: =item *
18065: 
18066: logperm() : append a permanent message to lonnet.perm.log, this log
18067: file never gets deleted by any automated portion of the system, only
18068: messages of critical importance should go in here.
18069: 
18070: 
18071: =back
18072: 
18073: =head2 General File Helper Routines
18074: 
18075: =over 4
18076: 
18077: =item *
18078: 
18079: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
18080: (a) files in /uploaded
18081:   (i) If a local copy of the file exists - 
18082:       compares modification date of local copy with last-modified date for 
18083:       definitive version stored on home server for course. If local copy is 
18084:       stale, requests a new version from the home server and stores it. 
18085:       If the original has been removed from the home server, then local copy 
18086:       is unlinked.
18087:   (ii) If local copy does not exist -
18088:       requests the file from the home server and stores it. 
18089:   
18090:   If $caller is 'uploadrep':  
18091:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
18092:     for request for files originally uploaded via DOCS. 
18093:      - returns 'ok' if fresh local copy now available, -1 otherwise.
18094:   
18095:   Otherwise:
18096:      This indicates a call from the content generation phase of the request.
18097:      -  returns the entire contents of the file or -1.
18098:      
18099: (b) files in /res
18100:    - returns the entire contents of a file or -1; 
18101:    it properly subscribes to and replicates the file if neccessary.
18102: 
18103: 
18104: =item *
18105: 
18106: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
18107:                   reference
18108: 
18109: returns either a stat() list of data about the file or an empty list
18110: if the file doesn't exist or couldn't find out about it (connection
18111: problems or user unknown)
18112: 
18113: =item *
18114: 
18115: filelocation($dir,$file) : returns file system location of a file
18116: based on URI; meant to be "fairly clean" absolute reference, $dir is a
18117: directory that relative $file lookups are to looked in ($dir of /a/dir
18118: and a file of ../bob will become /a/bob)
18119: 
18120: =item *
18121: 
18122: hreflocation($dir,$file) : returns file system location or a URL; same as
18123: filelocation except for hrefs
18124: 
18125: =item *
18126: 
18127: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
18128: also removes beginning /home/httpd/html unless /priv/ follows it.
18129: 
18130: =back
18131: 
18132: =head2 Usererfile file routines (/uploaded*)
18133: 
18134: =over 4
18135: 
18136: =item *
18137: 
18138: userfileupload(): main rotine for putting a file in a user or course's
18139:                   filespace, arguments are,
18140: 
18141:  formname - required - this is the name of the element in $env where the
18142:            filename, and the contents of the file to create/modifed exist
18143:            the filename is in $env{'form.'.$formname.'.filename'} and the
18144:            contents of the file is located in $env{'form.'.$formname}
18145:  context - if coursedoc, store the file in the course of the active role
18146:              of the current user; 
18147:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
18148:            if 'canceloverwrite': delete file in tmp/overwrites directory
18149:  subdir - required - subdirectory to put the file in under ../userfiles/
18150:          if undefined, it will be placed in "unknown"
18151: 
18152:  (This routine calls clean_filename() to remove any dangerous
18153:  characters from the filename, and then calls finuserfileupload() to
18154:  complete the transaction)
18155: 
18156:  returns either the url of the uploaded file (/uploaded/....) if successful
18157:  and /adm/notfound.html if unsuccessful
18158: 
18159: =item *
18160: 
18161: clean_filename(): routine for cleaing a filename up for storage in
18162:                  userfile space, argument is:
18163: 
18164:  filename - proposed filename
18165: 
18166: returns: the new clean filename
18167: 
18168: =item *
18169: 
18170: finishuserfileupload(): routine that creates and sends the file to
18171: userspace, probably shouldn't be called directly
18172: 
18173:   docuname: username or courseid of destination for the file
18174:   docudom: domain of user/course of destination for the file
18175:   formname: same as for userfileupload()
18176:   fname: filename (including subdirectories) for the file
18177:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
18178:           if hashref, and context is scantron, will convert csv format to standard format
18179:   allfiles: reference to hash used to store objects found by parser
18180:   codebase: reference to hash used for codebases of java objects found by parser
18181:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
18182:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
18183:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
18184:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
18185:   context: if 'overwrite', will move the uploaded file from its temporary location to
18186:             userfiles to facilitate overwriting a previously uploaded file with same name.
18187:   mimetype: reference to scalar to accommodate mime type determined
18188:             from File::MMagic if $parser = parse.
18189: 
18190:  returns either the url of the uploaded file (/uploaded/....) if successful
18191:  and /adm/notfound.html if unsuccessful (or an error message if context 
18192:  was 'overwrite').
18193:  
18194: 
18195: =item *
18196: 
18197: renameuserfile(): renames an existing userfile to a new name
18198: 
18199:   Args:
18200:    docuname: username or courseid of destination for the file
18201:    docudom: domain of user/course of destination for the file
18202:    old: current file name (including any subdirs under userfiles)
18203:    new: desired file name (including any subdirs under userfiles)
18204: 
18205: =item *
18206: 
18207: mkdiruserfile(): creates a directory is a userfiles dir
18208: 
18209:   Args:
18210:    docuname: username or courseid of destination for the file
18211:    docudom: domain of user/course of destination for the file
18212:    dir: dir to create (including any subdirs under userfiles)
18213: 
18214: =item *
18215: 
18216: removeuserfile(): removes a file that exists in userfiles
18217: 
18218:   Args:
18219:    docuname: username or courseid of destination for the file
18220:    docudom: domain of user/course of destination for the file
18221:    fname: filname to delete (including any subdirs under userfiles)
18222: 
18223: =item *
18224: 
18225: removeuploadedurl(): convience function for removeuserfile()
18226: 
18227:   Args:
18228:    url:  a full /uploaded/... url to delete
18229: 
18230: =item * 
18231: 
18232: get_portfile_permissions():
18233:   Args:
18234:     domain: domain of user or course contain the portfolio files
18235:     user: name of user or num of course contain the portfolio files
18236:   Returns:
18237:     hashref of a dump of the proper file_permissions.db
18238:    
18239: 
18240: =item * 
18241: 
18242: get_access_controls():
18243: 
18244: Args:
18245:   current_permissions: the hash ref returned from get_portfile_permissions()
18246:   group: (optional) the group you want the files associated with
18247:   file: (optional) the file you want access info on
18248: 
18249: Returns:
18250:     a hash (keys are file names) of hashes containing
18251:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
18252:         values are XML containing access control settings (see below) 
18253: 
18254: Internal notes:
18255: 
18256:  access controls are stored in file_permissions.db as key=value pairs.
18257:     key -> path to file/file_name\0uniqueID:scope_end_start
18258:         where scope -> public,guest,course,group,domains or users.
18259:               end -> UNIX time for end of access (0 -> no end date)
18260:               start -> UNIX time for start of access
18261: 
18262:     value -> XML description of access control
18263:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
18264:             <start></start>
18265:             <end></end>
18266: 
18267:             <password></password>  for scope type = guest
18268: 
18269:             <domain></domain>     for scope type = course or group
18270:             <number></number>
18271:             <roles id="">
18272:              <role></role>
18273:              <access></access>
18274:              <section></section>
18275:              <group></group>
18276:             </roles>
18277: 
18278:             <dom></dom>         for scope type = domains
18279: 
18280:             <users>             for scope type = users
18281:              <user>
18282:               <uname></uname>
18283:               <udom></udom>
18284:              </user>
18285:             </users>
18286:            </scope> 
18287:               
18288:  Access data is also aggregated for each file in an additional key=value pair:
18289:  key -> path to file/file_name\0accesscontrol 
18290:  value -> reference to hash
18291:           hash contains key = value pairs
18292:           where key = uniqueID:scope_end_start
18293:                 value = UNIX time record was last updated
18294: 
18295:           Used to improve speed of look-ups of access controls for each file.  
18296:  
18297:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
18298: 
18299: =item *
18300: 
18301: modify_access_controls():
18302: 
18303: Modifies access controls for a portfolio file
18304: Args
18305: 1. file name
18306: 2. reference to hash of required changes,
18307: 3. domain
18308: 4. username
18309:   where domain,username are the domain of the portfolio owner 
18310:   (either a user or a course) 
18311: 
18312: Returns:
18313: 1. result of additions or updates ('ok' or 'error', with error message). 
18314: 2. result of deletions ('ok' or 'error', with error message).
18315: 3. reference to hash of any new or updated access controls.
18316: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
18317:    key = integer (inbound ID)
18318:    value = uniqueID
18319: 
18320: =item *
18321: 
18322: get_timebased_id():
18323: 
18324: Attempts to get a unique timestamp-based suffix for use with items added to a 
18325: course via the Course Editor (e.g., folders, composite pages, 
18326: group bulletin boards).
18327: 
18328: Args: (first three required; six others optional)
18329: 
18330: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
18331:    docssequence, or name of group
18332: 
18333: 2. keyid (alphanumeric): name of temporary locking key in hash,
18334:    e.g., num, boardids
18335: 
18336: 3. namespace: name of gdbm file used to store suffixes already assigned;  
18337:    file will be named nohist_namespace.db
18338: 
18339: 4. cdom: domain of course; default is current course domain from %env
18340: 
18341: 5. cnum: course number; default is current course number from %env
18342: 
18343: 6. idtype: set to concat if an additional digit is to be appended to the 
18344:    unix timestamp to form the suffix, if the plain timestamp is already
18345:    in use.  Default is to not do this, but simply increment the unix 
18346:    timestamp by 1 until a unique key is obtained.
18347: 
18348: 7. who: holder of locking key; defaults to user:domain for user.
18349: 
18350: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
18351:    retrying); default is 3.
18352: 
18353: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
18354: 
18355: Returns:
18356: 
18357: 1. suffix obtained (numeric)
18358: 
18359: 2. result of deleting locking key (ok if deleted, or lock never obtained)
18360: 
18361: 3. error: contains (localized) error message if an error occurred.
18362: 
18363: 
18364: =back
18365: 
18366: =head2 HTTP Helper Routines
18367: 
18368: =over 4
18369: 
18370: =item *
18371: 
18372: escape() : unpack non-word characters into CGI-compatible hex codes
18373: 
18374: =item *
18375: 
18376: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
18377: 
18378: =back
18379: 
18380: =head1 PRIVATE SUBROUTINES
18381: 
18382: =head2 Underlying communication routines (Shouldn't call)
18383: 
18384: =over 4
18385: 
18386: =item *
18387: 
18388: subreply() : tries to pass a message to lonc, returns con_lost if incapable
18389: 
18390: =item *
18391: 
18392: reply() : uses subreply to send a message to remote machine, logs all failures
18393: 
18394: =item *
18395: 
18396: critical() : passes a critical message to another server; if cannot
18397: get through then place message in connection buffer directory and
18398: returns con_delayed, if incapable of saving message, returns
18399: con_failed
18400: 
18401: =item *
18402: 
18403: reconlonc() : tries to reconnect lonc client processes.
18404: 
18405: =back
18406: 
18407: =head2 Resource Access Logging
18408: 
18409: =over 4
18410: 
18411: =item *
18412: 
18413: flushcourselogs() : flush (save) buffer logs and access logs
18414: 
18415: =item *
18416: 
18417: courselog($what) : save message for course in hash
18418: 
18419: =item *
18420: 
18421: courseacclog($what) : save message for course using &courselog().  Perform
18422: special processing for specific resource types (problems, exams, quizzes, etc).
18423: 
18424: =item *
18425: 
18426: goodbye() : flush course logs and log shutting down; it is called in srm.conf
18427: as a PerlChildExitHandler
18428: 
18429: =back
18430: 
18431: =head2 Other
18432: 
18433: =over 4
18434: 
18435: =item *
18436: 
18437: symblist($mapname,%newhash) : update symbolic storage links
18438: 
18439: =back
18440: 
18441: =cut
18442: 

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